Files
election-system/tests/ElectionSystem.Api.Tests/RegionScopeServiceTests.cs
T
2026-09-08 08:23:02 +08:00

38 lines
1.4 KiB
C#

using ElectionSystem.Api.Infrastructure.Persistence;
using ElectionSystem.Api.Modules.Organization;
using ElectionSystem.Api.Modules.Organization.Domain;
using Microsoft.EntityFrameworkCore;
namespace ElectionSystem.Api.Tests;
public sealed class RegionScopeServiceTests
{
[Fact]
public async Task Nationwide_scope_contains_every_active_administrative_region()
{
var options = new DbContextOptionsBuilder<ElectionDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
await using var context = new ElectionDbContext(options);
var province = Region("11", "省", AdministrativeRegionLevel.Province);
var disabled = Region("12", "停用省", AdministrativeRegionLevel.Province);
disabled.IsActive = false;
context.AddRange(province, disabled);
await context.SaveChangesAsync();
var scope = new RegionScopeService(context);
Assert.True(await scope.IsInRegionSubtreeAsync(province.Id, RegionScopeService.NationwideRegionId, CancellationToken.None));
Assert.False(await scope.IsInRegionSubtreeAsync(disabled.Id, RegionScopeService.NationwideRegionId, CancellationToken.None));
}
private static AdministrativeRegion Region(string code, string name, AdministrativeRegionLevel level) => new()
{
Code = code,
Name = name,
Level = level,
Source = "test",
SourceVersion = "1"
};
}