arrange tests
This commit is contained in:
parent
8743325067
commit
db8c41368d
@ -16,6 +16,9 @@ public static class DependencyInjection
|
|||||||
options.Invoke(cfg);
|
options.Invoke(cfg);
|
||||||
cfg.RegisterAllServices(services);
|
cfg.RegisterAllServices(services);
|
||||||
|
|
||||||
|
// register db repository
|
||||||
|
services.AddSingleton<IRepository, DbRepository>();
|
||||||
|
|
||||||
// register db repository factory
|
// register db repository factory
|
||||||
services.AddSingleton<IRepositoryFactory, DbRepositoryFactory>();
|
services.AddSingleton<IRepositoryFactory, DbRepositoryFactory>();
|
||||||
|
|
||||||
|
|||||||
@ -26,7 +26,6 @@
|
|||||||
<ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Application\DigitalData.Core.Application.csproj" />
|
||||||
<ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Client\DigitalData.Core.Client.csproj" />
|
||||||
<ProjectReference Include="..\DigitalData.Core.DTO\DigitalData.Core.DTO.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.Infrastructure\DigitalData.Core.Infrastructure.csproj" />
|
||||||
<ProjectReference Include="..\DigitalData.Core.Security\DigitalData.Core.Security.csproj" />
|
<ProjectReference Include="..\DigitalData.Core.Security\DigitalData.Core.Security.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@ -5,15 +5,14 @@ using Microsoft.EntityFrameworkCore;
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using DigitalData.Core.Infrastructure.AutoMapper;
|
|
||||||
using DigitalData.Core.Abstraction.Application.Repository;
|
using DigitalData.Core.Abstraction.Application.Repository;
|
||||||
using Bogus.Platform;
|
using DigitalData.Core.Infrastructure;
|
||||||
|
|
||||||
public class DbRepositoryTests
|
public class DbRepositoryTests
|
||||||
{
|
{
|
||||||
private IHost _host;
|
private IHost _host;
|
||||||
|
|
||||||
private IRepository<User> _userRepo;
|
private IRepository Repo;
|
||||||
|
|
||||||
[SetUp]
|
[SetUp]
|
||||||
public void Setup()
|
public void Setup()
|
||||||
@ -22,13 +21,16 @@ public class DbRepositoryTests
|
|||||||
|
|
||||||
builder.Services.AddDbContext<MockDbContext>(opt => opt.UseInMemoryDatabase("MockDB"));
|
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());
|
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
||||||
|
|
||||||
_host = builder.Build();
|
_host = builder.Build();
|
||||||
|
|
||||||
_userRepo = _host.Services.GetRequiredService<IRepository<User>>();
|
Repo = _host.Services.GetRequiredService<IRepository>();
|
||||||
}
|
}
|
||||||
|
|
||||||
[TearDown]
|
[TearDown]
|
||||||
@ -47,9 +49,9 @@ public class DbRepositoryTests
|
|||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
if (multiple)
|
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
|
else
|
||||||
Assert.DoesNotThrowAsync(async () => await _userRepo.CreateAsync(faker.Generate()));
|
Assert.DoesNotThrowAsync(async () => await Repo.CreateAsync(faker.Generate()));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase(true, TestName = "WhenDtoUsed")]
|
[TestCase(true, TestName = "WhenDtoUsed")]
|
||||||
@ -58,10 +60,10 @@ public class DbRepositoryTests
|
|||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
var createdUser = useDto
|
var createdUser = useDto
|
||||||
? await _userRepo.CreateAsync(Fake.UserCreateDto)
|
? await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto)
|
||||||
: await _userRepo.CreateAsync(Fake.User);
|
: 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
|
||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
@ -75,12 +77,12 @@ public class DbRepositoryTests
|
|||||||
public async Task ReadAsync_ShouldReturnUpdated()
|
public async Task ReadAsync_ShouldReturnUpdated()
|
||||||
{
|
{
|
||||||
// Arrange
|
// 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" };
|
var userUpdateDto = new UserUpdateDto() { Age = 10, Email = "Bar", FirstName = "Foo" };
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _userRepo.UpdateAsync(userUpdateDto, u => u.Id == createdUser!.Id);
|
await Repo.UpdateAsync<User, UserUpdateDto>(userUpdateDto, u => u.Id == createdUser!.Id);
|
||||||
var upToDateUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser!.Id);
|
var upToDateUser = await Repo.Get<User>().Where(u => u.Id == createdUser!.Id).FirstOrDefaultAsync();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
@ -96,12 +98,12 @@ public class DbRepositoryTests
|
|||||||
public async Task ReadAsync_ShouldNotReturnDeleted()
|
public async Task ReadAsync_ShouldNotReturnDeleted()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var createdUser = await _userRepo.CreateAsync(Fake.UserCreateDto);
|
var createdUser = await Repo.CreateAsync<User, UserCreateDto>(Fake.UserCreateDto);
|
||||||
var readUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
|
var readUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
await _userRepo.DeleteAsync(u => u.Id == createdUser.Id);
|
await Repo.DeleteAsync<User>(u => u.Id == createdUser.Id);
|
||||||
var deletedUser = await _userRepo.ReadFirstOrDefaultAsync(u => u.Id == createdUser.Id);
|
var deletedUser = await Repo.Get<User>().Where(u => u.Id == createdUser.Id).FirstOrDefaultAsync();
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.Multiple(() =>
|
Assert.Multiple(() =>
|
||||||
|
|||||||
@ -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; }
|
public required int Id { get; init; }
|
||||||
|
|
||||||
|
|||||||
@ -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>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user