管理端后端

This commit is contained in:
biss committed 2026-08-29 17:26:59 +08:00
1 parent eebc5518c5
commit 4ef6f6c90d
24 files changed
+1745 -10

No files matched your search

@@ -1,6 +1,10 @@
using ElectionSystem.Api.Infrastructure.Auditing;
using ElectionSystem.Api.Infrastructure.Persistence;
using ElectionSystem.Api.Modules.Identity.Domain;
using ElectionSystem.Api.Modules.Organization;
using ElectionSystem.Api.Modules.Organization.Domain;
using ElectionSystem.Api.Modules.Voters.Domain;
using System.Text;
using Microsoft.EntityFrameworkCore;
namespace ElectionSystem.Api.Tests;
@@ -24,4 +28,49 @@ public sealed class PersistenceModelTests
var properties = typeof(AuditEntry).GetProperties().Select(property => property.Name);
Assert.DoesNotContain(properties, property => property.Contains("Vote", StringComparison.OrdinalIgnoreCase));
}
[Fact]
public async Task Provincial_administrator_scope_includes_all_descendant_regions()
{
var options = new DbContextOptionsBuilder<ElectionDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
await using var context = new ElectionDbContext(options);
var province = new AdministrativeRegion { Code = "11", Name = "省", Level = AdministrativeRegionLevel.Province, Source = "test", SourceVersion = "test" };
var city = new AdministrativeRegion { Code = "1101", Name = "市", Level = AdministrativeRegionLevel.City, ParentId = province.Id, Source = "test", SourceVersion = "test" };
var village = new AdministrativeRegion { Code = "110101001001", Name = "村", Level = AdministrativeRegionLevel.Village, ParentId = city.Id, Source = "test", SourceVersion = "test" };
var admin = new ApplicationUser { UserName = "province-admin", DisplayName = "Province administrator" };
context.AddRange(province, city, village, admin);
context.UserRegionalRoleAssignments.Add(new UserRegionalRoleAssignment { UserId = admin.Id, RegionId = province.Id, Role = RegionalRole.ProvinceAdministrator });
await context.SaveChangesAsync();
var scope = await new RegionScopeService(context).GetManagedRegionIdsAsync(admin.Id, CancellationToken.None);
Assert.Equal(3, scope.Count);
Assert.Contains(village.Id, scope);
}
[Fact]
public void Voter_number_and_region_status_indexes_are_defined()
{
var options = new DbContextOptionsBuilder<ElectionDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
using var context = new ElectionDbContext(options);
var entity = context.Model.FindEntityType(typeof(VoterProfile))!;
Assert.Contains(entity.GetIndexes(), x => x.IsUnique && x.Properties.Single().Name == nameof(VoterProfile.VoterNumber));
Assert.Contains(entity.GetIndexes(), x => x.Properties.Select(p => p.Name).SequenceEqual(new[] { nameof(VoterProfile.RegionId), nameof(VoterProfile.Status), nameof(VoterProfile.DisplayName) }));
}
[Fact]
public async Task AreaCity_import_preserves_parent_chain_and_four_levels()
{
const string csv = "id,pid,deep,name\n11,0,0,北京市\n1101,11,1,市辖区\n110101,1101,2,东城区\n110101001,110101,3,东华门街道\n";
var options = new DbContextOptionsBuilder<ElectionDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString()).Options;
await using var context = new ElectionDbContext(options);
await using var stream = new MemoryStream(Encoding.UTF8.GetBytes(csv));
var result = await new AreaCityCatalogImportService(context).ImportAsync(stream, "test-2026", CancellationToken.None);
Assert.Equal(4, result.CreatedCount);
var township = await context.AdministrativeRegions.SingleAsync(x => x.Code == "110101001");
var district = await context.AdministrativeRegions.SingleAsync(x => x.Code == "110101");
Assert.Equal(district.Id, township.ParentId);
Assert.Equal(AdministrativeRegionLevel.Township, township.Level);
}
}