using BuildFlow.Application.Roles.Handlers; using BuildFlow.Application.Roles.Queries; using BuildFlow.Domain.Entities; using BuildFlow.Infrastructure.Persistence; using FluentAssertions; using Microsoft.EntityFrameworkCore; using Xunit; namespace BuildFlow.Rag.Tests.Roles; /// /// 角色列表查询处理器单元测试。覆盖正常查询和空表场景。 /// public class GetRoleListQueryHandlerTests { private static async Task<(RagDbContext db, GetRoleListQueryHandler handler)> SetupAsync() { var options = new DbContextOptionsBuilder() .UseInMemoryDatabase($"rolelist-{Guid.NewGuid()}") .Options; var db = new RagDbContext(options); db.Roles.AddRange( new Role { Id = Guid.NewGuid(), Name = "SuperAdmin", CreatedBy = "s", UpdatedBy = "s", OperatorIP = "0" }, new Role { Id = Guid.NewGuid(), Name = "Admin", CreatedBy = "s", UpdatedBy = "s", OperatorIP = "0" }, new Role { Id = Guid.NewGuid(), Name = "User", CreatedBy = "s", UpdatedBy = "s", OperatorIP = "0" } ); await db.SaveChangesAsync(); var handler = new GetRoleListQueryHandler(db); return (db, handler); } [Fact] public async Task GetRoleList_ReturnsAllRoles() { // Arrange var (db, handler) = await SetupAsync(); // Act var result = await handler.Handle(new GetRoleListQuery(), CancellationToken.None); // Assert result.Should().HaveCount(3); result.Select(r => r.Name).Should().Contain(new[] { "SuperAdmin", "Admin", "User" }); } [Fact] public async Task GetRoleList_OnEmptyDatabase_ReturnsEmptyList() { // Arrange var options = new DbContextOptionsBuilder() .UseInMemoryDatabase($"rolelist-empty-{Guid.NewGuid()}") .Options; var db = new RagDbContext(options); var handler = new GetRoleListQueryHandler(db); // Act var result = await handler.Handle(new GetRoleListQuery(), CancellationToken.None); // Assert result.Should().BeEmpty(); } }