Files
h-school/tests/HSchool.AppHost.Tests/SchoolSeedTests.cs
T
Leonid PershinandCursor cc18429108 Complete phase 39 school owners with owner-scoped API, menu, and tests.
Drop golden save load tests in favor of incompatible-save behavior; dress legacy people files on reload; fix AppHost isolation for multi-user socket and save cleanup.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-20 09:44:08 +03:00

98 lines
3.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Net.Http.Json;
namespace HSchool.AppHost.Tests;
/// <summary>
/// The people seed is a school field of its own, not the school id. These tests share the
/// AppHost, so each one starts by clearing the list.
/// </summary>
[Collection(AppHostCollection.Name)]
public class SchoolSeedTests(AppHostFixture fixture)
{
private static readonly DateTime Start = new(2012, 4, 3, 6, 0, 0, DateTimeKind.Utc);
[Fact]
public async Task SameSeed_PopulatesTheSamePeople_DifferentSeedsDoNot()
{
using var client = fixture.App.CreateHttpClient("server");
await SchoolApiTests.ResetAsync(client);
var first = await SchoolApiTests.CreateAsync(client, "Сид одинаковый А", Start, seed: 42);
var second = await SchoolApiTests.CreateAsync(client, "Сид одинаковый Б", Start, seed: 42);
var firstYearFive = await YearFiveAsync(client, first.Id);
var secondYearFive = await YearFiveAsync(client, second.Id);
Assert.Equal(firstYearFive, secondYearFive);
using (var delete = await client.DeleteAsync($"/api/schools/{first.Id}", TestContext.Current.CancellationToken))
{
delete.EnsureSuccessStatusCode();
}
var other = await SchoolApiTests.CreateAsync(client, "Сид другой", Start, seed: 7);
var otherYearFive = await YearFiveAsync(client, other.Id);
Assert.NotEqual(secondYearFive, otherYearFive);
}
[Fact]
public async Task UnseededSchool_KeepsItsPeopleAfterReload()
{
using var client = fixture.App.CreateHttpClient("server");
await SchoolApiTests.ResetAsync(client);
var created = await SchoolApiTests.CreateAsync(client, "Сид брошен", Start);
var before = await YearFiveAsync(client, created.Id);
using var reload = await client.PostAsync("/api/dev/reload-schools", content: null, TestContext.Current.CancellationToken);
reload.EnsureSuccessStatusCode();
var restored = (await SchoolApiTests.GetSchoolsAsync(client)).Schools.Single(school => school.Id == created.Id);
Assert.Equal(created.Seed, restored.Seed);
Assert.Equal(before, await YearFiveAsync(client, created.Id));
}
[Fact]
public async Task PassedSeed_IsVisibleInTheSchoolList()
{
using var client = fixture.App.CreateHttpClient("server");
await SchoolApiTests.ResetAsync(client);
var created = await SchoolApiTests.CreateAsync(client, "Сид в списке", Start, seed: 20260819);
Assert.Equal(20260819, created.Seed);
var listed = (await SchoolApiTests.GetSchoolsAsync(client)).Schools.Single(school => school.Id == created.Id);
Assert.Equal(20260819, listed.Seed);
}
[Fact]
public async Task TwoUnseededSchools_ArePopulatedDifferently()
{
using var client = fixture.App.CreateHttpClient("server");
await SchoolApiTests.ResetAsync(client);
var first = await SchoolApiTests.CreateAsync(client, "Без сида А", Start);
var second = await SchoolApiTests.CreateAsync(client, "Без сида Б", Start);
Assert.NotEqual(first.Seed, second.Seed);
Assert.NotEqual(await YearFiveAsync(client, first.Id), await YearFiveAsync(client, second.Id));
}
private static async Task<(string Id, string FullName)[]> YearFiveAsync(HttpClient client, int schoolId)
{
var page = await client.GetFromJsonAsync<PeoplePage>(
$"/api/schools/{schoolId}/people?role=student&year=5&sort=surname&pageSize=100",
TestContext.Current.CancellationToken);
Assert.NotNull(page);
Assert.NotEmpty(page.People);
return [.. page.People.Select(person => (person.Id, person.FullName))];
}
private sealed record PeoplePage(IReadOnlyList<PersonRow> People);
private sealed record PersonRow(string Id, string FullName);
}