【ABP】项目示例(7)——数据播种

数据种子

在上一章节中,已经对仓储层和应用层进行了单元测试,在这一章节中,进行数据播种
大多数程序正常运行都需要依赖于初始数据,依赖于数据库的程序基本都是如此
例如需要有一个初始的管理员用户或者一个管理员角色,用来进行登录系统,像这种主要用于生产环境中的数据播种,往往都是涉及到程序版本更新,而需要添加或者更新数据种子,一般在数据迁移中进行数据播种
还有在单元测试中,一般也需要有初始数据,方便进行测试,像这种主要用于测试环境中的数据播种,一般在测试的基础模块中进行数据播种
在名称为General.Backend.Domain的类库中新建名称为DataSeed的文件夹,在名称为DataSeed的文件夹中新建名称为GeneralDataSeedContributor的数据播种参与类

public class GeneralDataSeedContributor : IDataSeedContributor, ITransientDependency
{
    private const string AdminAcount = "admin";
    private const string AdminPassword = "z1x2c3v.";
    private readonly UserService _userService;
    private readonly RoleService _roleService;
    private readonly IUserRepository _userRepository;
    private readonly IRoleRepository _roleRepository;

    public GeneralDataSeedContributor(
        UserService userService,
        RoleService roleService,
        IUserRepository userRepository,
        IRoleRepository roleRepository) 
    {
        _userService = userService;
        _roleService = roleService;
        _userRepository = userRepository;
        _roleRepository = roleRepository;
    }

    public async Task SeedAsync(DataSeedContext context)
    {
        await _userRepository.HardDeleteAsync(user => user.Account == AdminAcount, true);
        var user = await _userService.AddAsync(
                AdminAcount,
                AdminPassword,
                UserConsts.AdminName);
        await _userRepository.InsertAsync(user);

        await _roleRepository.HardDeleteAsync(role => role.Code == RoleConsts.AdminRoleCode, true);
        var role = await _roleService.AddAsync(
            RoleConsts.AdminRoleCode,
            RoleConsts.AdminRoleName,
            RoleConsts.AdminRoleName);
        await _roleRepository.InsertAsync(role);
    }
}

初始化一个管理员用户和一个管理员角色,用作系统初始化使用
在名称为GeneralTestBaseModule的基础测试模块类中进行数据播种

[DependsOn(
    typeof(AbpAutofacModule),
    typeof(AbpTestBaseModule),
    typeof(GeneralDomainModule)
    )]
public class GeneralTestBaseModule : AbpModule
{
    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        AsyncHelper.RunSync(async () =>
        {
            using var scope = context.ServiceProvider.CreateScope();
            await scope.ServiceProvider
                .GetRequiredService<IDataSeeder>()
                .SeedAsync();
        });
    }
}

这样在运行单元测试时,已经有了管理员用户和管理员角色初始数据,现在需要修改名称为UserRepositoryTests的用户单元测试类中的Should_Insert_Some_Valid_User方法,将数据条数判断为2更改为3,使单元测试通过

[Fact]
public async Task Should_Insert_Some_Valid_User()
{
    var user1 = new User(
        _guidGenerator.Create(),
        "hui",
        "1q2w3e4r%",
        "Hui",
        "00-12300",
        "深圳");
    var user2 = new User(
        _guidGenerator.Create(),
        "rain",
        "p0o9i8u7^",
        "Rain",
        "00-12400");

    await WithUnitOfWorkAsync(async () =>
    {
        await _userRepository.InsertAsync(user1);
        await _userRepository.InsertAsync(user2);
    });
    var result = await _userRepository.GetListAsync();

    result.Count.ShouldBe(3);
}

运行所有测试,测试用例全部运行通过,测试结果如下

在下一章节中,进行数据迁移

posted @ 2025-04-01 22:06  loveraindeme  阅读(53)  评论(0)    收藏  举报