feat: work on implementing UoW pattern
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
using Bogus;
|
||||
|
||||
namespace SanctionsSearch.Worker.Tests.Fakes;
|
||||
|
||||
class SdnFaker : Faker<Sdn>
|
||||
{
|
||||
public SdnFaker()
|
||||
{
|
||||
RuleFor(x => x.Id, f => f.Random.Int(1, int.MaxValue));
|
||||
RuleFor(x => x.Name, f => f.Person.FullName);
|
||||
RuleFor(x => x.Type, f => f.Lorem.Word());
|
||||
RuleFor(x => x.Program, f => f.Lorem.Word());
|
||||
RuleFor(x => x.Title, f => f.Lorem.Word());
|
||||
RuleFor(x => x.CallSign, f => f.Lorem.Word());
|
||||
RuleFor(x => x.VesselType, f => f.Lorem.Word());
|
||||
RuleFor(x => x.Tonnage, f => f.Lorem.Word());
|
||||
RuleFor(x => x.GrossRegisteredTonnage, f => f.Lorem.Word());
|
||||
RuleFor(x => x.VesselFlag, f => f.Lorem.Word());
|
||||
RuleFor(x => x.VesselOwner, f => f.Lorem.Word());
|
||||
RuleFor(x => x.Remarks, f => f.Lorem.Word());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
using SanctionsSearch.Worker.Tests.Fakes;
|
||||
|
||||
namespace SanctionsSearch.Worker.Tests.Integration;
|
||||
|
||||
public class EfRepositoryTests : IAsyncLifetime
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
private readonly EfRepository<Sdn> _sdnRepository;
|
||||
private readonly SdnFaker _sdnFaker = new();
|
||||
|
||||
public EfRepositoryTests()
|
||||
{
|
||||
var loggerFactory = LoggerFactory.Create(builder => builder.ClearProviders());
|
||||
|
||||
_context = new AppDbContext(new DbOptions { DatabaseName = $"{Guid.NewGuid()}.db" });
|
||||
_sdnRepository = new EfRepository<Sdn>(_context, loggerFactory.CreateLogger<EfRepository<Sdn>>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Upsert_WithNewEntity_ShouldAddEntityToDatabase()
|
||||
{
|
||||
var sdn = _sdnFaker.Generate();
|
||||
|
||||
await _sdnRepository.Upsert(sdn);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
var result = await _context.Sdns.FindAsync(sdn.Id);
|
||||
|
||||
result.Should().BeEquivalentTo(sdn);
|
||||
result!.CreatedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
|
||||
result.UpdatedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Upsert_WithExistingEntity_ShouldUpdateEntityInDatabase()
|
||||
{
|
||||
var sdn = _sdnFaker.Generate();
|
||||
|
||||
await _sdnRepository.Upsert(sdn);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
var createdSdn = await _context.Sdns.FindAsync(sdn.Id);
|
||||
|
||||
createdSdn.Should().BeEquivalentTo(sdn);
|
||||
createdSdn!.CreatedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
|
||||
createdSdn.UpdatedAt.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromSeconds(1));
|
||||
|
||||
var updatedSdn = _sdnFaker.Generate();
|
||||
updatedSdn.Id = sdn.Id;
|
||||
|
||||
await _sdnRepository.Upsert(updatedSdn);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
var result = await _context.Sdns.FindAsync(sdn.Id);
|
||||
|
||||
result.Should().BeEquivalentTo(updatedSdn);
|
||||
result!.CreatedAt.Should().BeSameDateAs(createdSdn.CreatedAt);
|
||||
result.UpdatedAt.Should().BeAfter(createdSdn.UpdatedAt);
|
||||
}
|
||||
|
||||
public async Task InitializeAsync()
|
||||
{
|
||||
await _context.Database.MigrateAsync();
|
||||
}
|
||||
|
||||
public async Task DisposeAsync()
|
||||
{
|
||||
await _context.DisposeAsync();
|
||||
|
||||
SqliteConnection.ClearAllPools();
|
||||
|
||||
File.Delete(_context.DatabasePath);
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Bogus" Version="35.6.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.12.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
|
||||
<PackageReference Include="Moq" Version="4.20.70" />
|
||||
|
||||
@@ -8,4 +8,6 @@ global using Moq;
|
||||
global using RichardSzalay.MockHttp;
|
||||
|
||||
global using SanctionsSearch.Worker.Services;
|
||||
global using SanctionsSearch.Worker.Options;
|
||||
global using SanctionsSearch.Worker.Options;
|
||||
global using SanctionsSearch.Worker.Models;
|
||||
global using SanctionsSearch.Worker.Persistence;
|
||||
Reference in New Issue
Block a user