arrange tests

This commit is contained in:
tekh 2025-09-11 18:53:39 +02:00
parent 8743325067
commit db8c41368d
7 changed files with 38 additions and 26 deletions

View File

@ -16,6 +16,9 @@ public static class DependencyInjection
options.Invoke(cfg);
cfg.RegisterAllServices(services);
// register db repository
services.AddSingleton<IRepository, DbRepository>();
// register db repository factory
services.AddSingleton<IRepositoryFactory, DbRepositoryFactory>();

View File

@ -26,7 +26,6 @@
<ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" />
<ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.csproj" />
<ProjectReference Include="..\DigitalData.Core.DTO\DigitalData.Core.DTO.csproj" />
<ProjectReference Include="..\DigitalData.Core.Infrastructure.AutoMapper\DigitalData.Core.Infrastructure.AutoMapper.csproj" />
<ProjectReference Include="..\DigitalData.Core.Infrastructure\DigitalData.Core.Infrastructure.csproj" />
<ProjectReference Include="..\DigitalData.Core.Security\DigitalData.Core.Security.csproj" />
</ItemGroup>

View File

@ -5,15 +5,14 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Reflection;
using DigitalData.Core.Infrastructure.AutoMapper;
using DigitalData.Core.Abstraction.Application.Repository;
using Bogus.Platform;
using DigitalData.Core.Infrastructure;
public class DbRepositoryTests
{
private IHost _host;
private IRepository<User> _userRepo;
private IRepository Repo;
[SetUp]
public void Setup()
@ -22,13 +21,16 @@ public class DbRepositoryTests
builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB"));
builder.Services.AddDbRepository<MockDbContext>(typeof(User).GetAssembly());
builder.Services.AddDbRepository(opt =>
{
opt.RegisterFromAssembly<MockDbContext>(typeof(User).Assembly);
});
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
_host = builder.Build();
_userRepo = _host.Services.GetRequiredService<IRepository<User>>();
Repo = _host.Services.GetRequiredService<IRepository>();
}
[TearDown]
@ -47,9 +49,9 @@ public class DbRepositoryTests
// Act & Assert
if (multiple)
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate(Random.Shared.Next(1, 10))));
else
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate()));
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate()));
}
[TestCase(true, TestName = "WhenDtoUsed")]
@ -58,10 +60,10 @@ public class DbRepositoryTests
{
// Act
var createdUser = useDto
? await _userRepo.CreateAsync(Fake.UserCreateDto)
: await _userRepo.CreateAsync(Fake.User);
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
: await Repo.CreateAsync(Fake.User);
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Assert
Assert.Multiple(() =>
@ -75,12 +77,12 @@ public class DbRepositoryTests
public async Task ReadAsync_ShouldReturnUpdated()
{
// Arrange
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto);
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
var userUpdateDto = new UserUpdateDto() { Age = 10, Email = "Bar", FirstName = "Foo" };
// Act
await _userRepo.UpdateAsync(userUpdateDto, u => u.Id == createdUser!.Id);
var upToDateUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser!.Id);
await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, u => u.Id == createdUser!.Id);
var upToDateUser = await Repo.Get<User>().Where(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
// Assert
Assert.Multiple(() =>
@ -96,12 +98,12 @@ public class DbRepositoryTests
public async Task ReadAsync_ShouldNotReturnDeleted()
{
// Arrange
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto);
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Act
await _userRepo.DeleteAsync(u => u.Id == createdUser.Id);
var deletedUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
await Repo.DeleteAsync<User>(u => u.Id == createdUser.Id);
var deletedUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
// Assert
Assert.Multiple(() =>

View File

@ -1,6 +1,8 @@
namespace DigitalData.Core.Tests.Mock;
using DigitalData.Core.Abstraction.Application.Repository;
public class User : UserBase
namespace DigitalData.Core.Tests.Mock;
public class User : UserBase, IEntity
{
public required int Id { get; init; }

View File

@ -1,5 +1,7 @@
namespace DigitalData.Core.Tests.Mock;
using DigitalData.Core.Abstraction.Application.Repository;
public class UserCreateDto : UserBase
namespace DigitalData.Core.Tests.Mock;
public class UserCreateDto : UserBase, IDto<User>
{
}

View File

@ -1,5 +1,7 @@
namespace DigitalData.Core.Tests.Mock;
using DigitalData.Core.Abstraction.Application.Repository;
public class UserReadDto : UserBase
namespace DigitalData.Core.Tests.Mock;
public class UserReadDto : UserBase, IDto<User>
{
}

View File

@ -1,5 +1,7 @@
namespace DigitalData.Core.Tests.Mock;
using DigitalData.Core.Abstraction.Application.Repository;
public class UserUpdateDto : UserBase
namespace DigitalData.Core.Tests.Mock;
public class UserUpdateDto : UserBase, IDto<User>
{
}