feat: add additional repository
This commit is contained in:
@@ -0,0 +1,13 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Faker;
|
||||||
|
|
||||||
|
class AddressFaker : Faker<Address>
|
||||||
|
{
|
||||||
|
public AddressFaker()
|
||||||
|
{
|
||||||
|
RuleFor(x => x.SdnId, f => f.Random.Int(1, int.MaxValue));
|
||||||
|
RuleFor(x => x.StreetAddress, f => f.Address.StreetAddress());
|
||||||
|
RuleFor(x => x.CityProvincePostal, f => $"{f.Address.City()}, {f.Address.StateAbbr()} {f.Address.ZipCode()}");
|
||||||
|
RuleFor(x => x.Country, f => f.Address.Country());
|
||||||
|
RuleFor(x => x.Remarks, f => f.Lorem.Sentence());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Faker;
|
||||||
|
|
||||||
|
class AliasFaker : Faker<Alias>
|
||||||
|
{
|
||||||
|
public AliasFaker()
|
||||||
|
{
|
||||||
|
RuleFor(x => x.SdnId, f => f.Random.Int(1, int.MaxValue));
|
||||||
|
RuleFor(x => x.Type, f => f.Lorem.Word());
|
||||||
|
RuleFor(x => x.Name, f => f.Person.FullName);
|
||||||
|
RuleFor(x => x.Remarks, f => f.Lorem.Sentence());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Faker;
|
||||||
|
|
||||||
|
class CommentFaker : Faker<Comment>
|
||||||
|
{
|
||||||
|
public CommentFaker()
|
||||||
|
{
|
||||||
|
RuleFor(x => x.SdnId, f => f.Random.Int(1, int.MaxValue));
|
||||||
|
RuleFor(x => x.Remarks, f => f.Lorem.Sentence());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Integration;
|
||||||
|
|
||||||
|
public class AddressRepositoryTests : RepositoryTest
|
||||||
|
{
|
||||||
|
private readonly AddressRepository _repository;
|
||||||
|
private readonly AddressFaker _faker = new();
|
||||||
|
|
||||||
|
public AddressRepositoryTests()
|
||||||
|
{
|
||||||
|
var loggerFactory = LoggerFactory.Create(builder => builder.ClearProviders());
|
||||||
|
_repository = new(_context, new Logger<AddressRepository>(loggerFactory));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WithNewEntity_ShouldAddEntityToDatabase()
|
||||||
|
{
|
||||||
|
var now = DateTimeOffset.UtcNow;
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(now);
|
||||||
|
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var result = await _context.Set<Address>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
result.Should().BeEquivalentTo(entity);
|
||||||
|
result!.CreatedAt.Should().Be(now.DateTime);
|
||||||
|
result.UpdatedAt.Should().Be(now.DateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WithExistingEntity_ShouldUpdateEntityInDatabase()
|
||||||
|
{
|
||||||
|
var createdTimeStamp = DateTimeOffset.UtcNow;
|
||||||
|
var updatedTimeStamp = createdTimeStamp.AddSeconds(2);
|
||||||
|
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(createdTimeStamp);
|
||||||
|
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
|
||||||
|
// Insert the entity
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Assert the entity was created
|
||||||
|
var createdEntity = await _context.Set<Address>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
createdEntity.Should().BeEquivalentTo(entity);
|
||||||
|
createdEntity!.CreatedAt.Should().Be(createdTimeStamp.DateTime);
|
||||||
|
createdEntity.UpdatedAt.Should().Be(createdTimeStamp.DateTime);
|
||||||
|
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(updatedTimeStamp);
|
||||||
|
|
||||||
|
// Update the entity
|
||||||
|
entity.StreetAddress = "Updated";
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Assert the entity was updated
|
||||||
|
var result = await _context.Set<Address>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
result.Should().BeEquivalentTo(entity);
|
||||||
|
result!.CreatedAt.Should().BeSameDateAs(createdTimeStamp.DateTime);
|
||||||
|
result.UpdatedAt.Should().BeSameDateAs(updatedTimeStamp.DateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Find_WithPredicate_ShouldReturnEntitiesMatchingPredicate()
|
||||||
|
{
|
||||||
|
var entities = _faker.Generate(3);
|
||||||
|
|
||||||
|
foreach (var entity in entities)
|
||||||
|
{
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var target = entities[0];
|
||||||
|
var result = await _repository.Find(x => x.Id == target.Id);
|
||||||
|
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
result.First().Should().BeEquivalentTo(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Integration;
|
||||||
|
|
||||||
|
public class AliasRepositoryTests : RepositoryTest
|
||||||
|
{
|
||||||
|
private readonly AliasRepository _repository;
|
||||||
|
private readonly AliasFaker _faker = new();
|
||||||
|
|
||||||
|
public AliasRepositoryTests()
|
||||||
|
{
|
||||||
|
var loggerFactory = LoggerFactory.Create(builder => builder.ClearProviders());
|
||||||
|
_repository = new(_context, new Logger<AliasRepository>(loggerFactory));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WithNewEntity_ShouldAddEntityToDatabase()
|
||||||
|
{
|
||||||
|
var now = DateTimeOffset.UtcNow;
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(now);
|
||||||
|
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var result = await _context.Set<Alias>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
result.Should().BeEquivalentTo(entity);
|
||||||
|
result!.CreatedAt.Should().Be(now.DateTime);
|
||||||
|
result.UpdatedAt.Should().Be(now.DateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WithExistingEntity_ShouldUpdateEntityInDatabase()
|
||||||
|
{
|
||||||
|
var createdTimeStamp = DateTimeOffset.UtcNow;
|
||||||
|
var updatedTimeStamp = createdTimeStamp.AddSeconds(2);
|
||||||
|
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(createdTimeStamp);
|
||||||
|
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
|
||||||
|
// Insert the entity
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Assert the entity was created
|
||||||
|
var createdEntity = await _context.Set<Alias>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
createdEntity.Should().BeEquivalentTo(entity);
|
||||||
|
createdEntity!.CreatedAt.Should().Be(createdTimeStamp.DateTime);
|
||||||
|
createdEntity.UpdatedAt.Should().Be(createdTimeStamp.DateTime);
|
||||||
|
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(updatedTimeStamp);
|
||||||
|
|
||||||
|
// Update the entity
|
||||||
|
entity.Remarks = "Updated";
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Assert the entity was updated
|
||||||
|
var result = await _context.Set<Alias>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
result.Should().BeEquivalentTo(entity);
|
||||||
|
result!.CreatedAt.Should().BeSameDateAs(createdTimeStamp.DateTime);
|
||||||
|
result.UpdatedAt.Should().BeSameDateAs(updatedTimeStamp.DateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Find_WithPredicate_ShouldReturnEntitiesMatchingPredicate()
|
||||||
|
{
|
||||||
|
var entities = _faker.Generate(3);
|
||||||
|
|
||||||
|
foreach (var entity in entities)
|
||||||
|
{
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var target = entities[0];
|
||||||
|
var result = await _repository.Find(x => x.Id == target.Id);
|
||||||
|
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
result.First().Should().BeEquivalentTo(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Integration;
|
||||||
|
|
||||||
|
public class CommentRepositoryTests : RepositoryTest
|
||||||
|
{
|
||||||
|
private readonly CommentRepository _repository;
|
||||||
|
private readonly CommentFaker _faker = new();
|
||||||
|
|
||||||
|
public CommentRepositoryTests()
|
||||||
|
{
|
||||||
|
var loggerFactory = LoggerFactory.Create(builder => builder.ClearProviders());
|
||||||
|
_repository = new(_context, new Logger<CommentRepository>(loggerFactory));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WithNewEntity_ShouldAddEntityToDatabase()
|
||||||
|
{
|
||||||
|
var now = DateTimeOffset.UtcNow;
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(now);
|
||||||
|
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var result = await _context.Set<Comment>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
result.Should().BeEquivalentTo(entity);
|
||||||
|
result!.CreatedAt.Should().Be(now.DateTime);
|
||||||
|
result.UpdatedAt.Should().Be(now.DateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WithExistingEntity_ShouldUpdateEntityInDatabase()
|
||||||
|
{
|
||||||
|
var createdTimeStamp = DateTimeOffset.UtcNow;
|
||||||
|
var updatedTimeStamp = createdTimeStamp.AddSeconds(2);
|
||||||
|
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(createdTimeStamp);
|
||||||
|
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
|
||||||
|
// Insert the entity
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Assert the entity was created
|
||||||
|
var createdEntity = await _context.Set<Comment>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
createdEntity.Should().BeEquivalentTo(entity);
|
||||||
|
createdEntity!.CreatedAt.Should().Be(createdTimeStamp.DateTime);
|
||||||
|
createdEntity.UpdatedAt.Should().Be(createdTimeStamp.DateTime);
|
||||||
|
|
||||||
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(updatedTimeStamp);
|
||||||
|
|
||||||
|
// Update the entity
|
||||||
|
entity.Remarks = "Updated";
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Assert the entity was updated
|
||||||
|
var result = await _context.Set<Comment>().FindAsync(entity.Id);
|
||||||
|
|
||||||
|
result.Should().BeEquivalentTo(entity);
|
||||||
|
result!.CreatedAt.Should().BeSameDateAs(createdTimeStamp.DateTime);
|
||||||
|
result.UpdatedAt.Should().BeSameDateAs(updatedTimeStamp.DateTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Find_WithPredicate_ShouldReturnEntitiesMatchingPredicate()
|
||||||
|
{
|
||||||
|
var entities = _faker.Generate(3);
|
||||||
|
|
||||||
|
foreach (var entity in entities)
|
||||||
|
{
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
var target = entities[0];
|
||||||
|
var result = await _repository.Find(x => x.Id == target.Id);
|
||||||
|
|
||||||
|
result.Should().HaveCount(1);
|
||||||
|
result.First().Should().BeEquivalentTo(target);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Integration;
|
||||||
|
|
||||||
|
public class RepositoryTest : IAsyncLifetime
|
||||||
|
{
|
||||||
|
private readonly AppDbContext _appDbContext;
|
||||||
|
protected readonly Mock<TimeProvider> _timeProvider = new();
|
||||||
|
protected readonly DbContext _context;
|
||||||
|
|
||||||
|
public RepositoryTest()
|
||||||
|
{
|
||||||
|
var options = new DbOptions { DatabaseName = $"{Guid.NewGuid()}.db" };
|
||||||
|
var context = new AppDbContext(options, _timeProvider.Object);
|
||||||
|
|
||||||
|
_appDbContext = context;
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InitializeAsync()
|
||||||
|
{
|
||||||
|
await _context.Database.MigrateAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task DisposeAsync()
|
||||||
|
{
|
||||||
|
await _appDbContext.DisposeAsync();
|
||||||
|
|
||||||
|
SqliteConnection.ClearAllPools();
|
||||||
|
|
||||||
|
File.Delete(_appDbContext.DatabasePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +1,14 @@
|
|||||||
namespace SanctionsSearch.Worker.Tests.Integration;
|
namespace SanctionsSearch.Worker.Tests.Integration;
|
||||||
|
|
||||||
public class SdnRepositoryTests : IAsyncLifetime
|
public class SdnRepositoryTests : RepositoryTest
|
||||||
{
|
{
|
||||||
private readonly AppDbContext _context;
|
private readonly SdnRepository _repository;
|
||||||
private readonly SdnRepository _sdnRepository;
|
private readonly SdnFaker _faker = new();
|
||||||
private readonly SdnFaker _sdnFaker = new();
|
|
||||||
private readonly Mock<TimeProvider> _timeProvider = new();
|
|
||||||
|
|
||||||
public SdnRepositoryTests()
|
public SdnRepositoryTests()
|
||||||
{
|
{
|
||||||
var loggerFactory = LoggerFactory.Create(builder => builder.ClearProviders());
|
var loggerFactory = LoggerFactory.Create(builder => builder.ClearProviders());
|
||||||
var options = new DbOptions { DatabaseName = $"{Guid.NewGuid()}.db" };
|
_repository = new SdnRepository(_context, new Logger<SdnRepository>(loggerFactory));
|
||||||
|
|
||||||
_context = new AppDbContext(options, _timeProvider.Object);
|
|
||||||
_sdnRepository = new SdnRepository(_context, new Logger<SdnRepository>(loggerFactory));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
@@ -22,14 +17,14 @@ public class SdnRepositoryTests : IAsyncLifetime
|
|||||||
var now = DateTimeOffset.UtcNow;
|
var now = DateTimeOffset.UtcNow;
|
||||||
_timeProvider.Setup(x => x.GetUtcNow()).Returns(now);
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(now);
|
||||||
|
|
||||||
var sdn = _sdnFaker.Generate();
|
var entity = _faker.Generate();
|
||||||
|
|
||||||
await _sdnRepository.Upsert(sdn);
|
await _repository.Upsert(entity);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
var result = await _context.Sdns.FindAsync(sdn.Id);
|
var result = await _context.Set<Sdn>().FindAsync(entity.Id);
|
||||||
|
|
||||||
result.Should().BeEquivalentTo(sdn);
|
result.Should().BeEquivalentTo(entity);
|
||||||
result!.CreatedAt.Should().Be(now.DateTime);
|
result!.CreatedAt.Should().Be(now.DateTime);
|
||||||
result.UpdatedAt.Should().Be(now.DateTime);
|
result.UpdatedAt.Should().Be(now.DateTime);
|
||||||
}
|
}
|
||||||
@@ -42,30 +37,30 @@ public class SdnRepositoryTests : IAsyncLifetime
|
|||||||
|
|
||||||
_timeProvider.Setup(x => x.GetUtcNow()).Returns(createdTimeStamp);
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(createdTimeStamp);
|
||||||
|
|
||||||
var sdn = _sdnFaker.Generate();
|
var entity = _faker.Generate();
|
||||||
|
|
||||||
// Insert the entity
|
// Insert the entity
|
||||||
await _sdnRepository.Upsert(sdn);
|
await _repository.Upsert(entity);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
// Assert the entity was created
|
// Assert the entity was created
|
||||||
var createdSdn = await _context.Sdns.FindAsync(sdn.Id);
|
var createdEntity = await _context.Set<Sdn>().FindAsync(entity.Id);
|
||||||
|
|
||||||
createdSdn.Should().BeEquivalentTo(sdn);
|
createdEntity.Should().BeEquivalentTo(entity);
|
||||||
createdSdn!.CreatedAt.Should().Be(createdTimeStamp.DateTime);
|
createdEntity!.CreatedAt.Should().Be(createdTimeStamp.DateTime);
|
||||||
createdSdn.UpdatedAt.Should().Be(createdTimeStamp.DateTime);
|
createdEntity.UpdatedAt.Should().Be(createdTimeStamp.DateTime);
|
||||||
|
|
||||||
_timeProvider.Setup(x => x.GetUtcNow()).Returns(updatedTimeStamp);
|
_timeProvider.Setup(x => x.GetUtcNow()).Returns(updatedTimeStamp);
|
||||||
|
|
||||||
// Update the entity
|
// Update the entity
|
||||||
sdn.Name = "Updated";
|
entity.Name = "Updated";
|
||||||
await _sdnRepository.Upsert(sdn);
|
await _repository.Upsert(entity);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
// Assert the entity was updated
|
// Assert the entity was updated
|
||||||
var result = await _context.Sdns.FindAsync(sdn.Id);
|
var result = await _context.Set<Sdn>().FindAsync(entity.Id);
|
||||||
|
|
||||||
result.Should().BeEquivalentTo(sdn);
|
result.Should().BeEquivalentTo(entity);
|
||||||
result!.CreatedAt.Should().BeSameDateAs(createdTimeStamp.DateTime);
|
result!.CreatedAt.Should().BeSameDateAs(createdTimeStamp.DateTime);
|
||||||
result.UpdatedAt.Should().BeSameDateAs(updatedTimeStamp.DateTime);
|
result.UpdatedAt.Should().BeSameDateAs(updatedTimeStamp.DateTime);
|
||||||
}
|
}
|
||||||
@@ -73,33 +68,19 @@ public class SdnRepositoryTests : IAsyncLifetime
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task Find_WithPredicate_ShouldReturnEntitiesMatchingPredicate()
|
public async Task Find_WithPredicate_ShouldReturnEntitiesMatchingPredicate()
|
||||||
{
|
{
|
||||||
var sdns = _sdnFaker.Generate(3);
|
var entities = _faker.Generate(3);
|
||||||
|
|
||||||
foreach (var sdn in sdns)
|
foreach (var entity in entities)
|
||||||
{
|
{
|
||||||
await _sdnRepository.Upsert(sdn);
|
await _repository.Upsert(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
var target = sdns[0];
|
var target = entities[0];
|
||||||
var result = await _sdnRepository.Find(x => x.Id == target.Id);
|
var result = await _repository.Find(x => x.Id == target.Id);
|
||||||
|
|
||||||
result.Should().HaveCount(1);
|
result.Should().HaveCount(1);
|
||||||
result.First().Should().BeEquivalentTo(target);
|
result.First().Should().BeEquivalentTo(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task InitializeAsync()
|
|
||||||
{
|
|
||||||
await _context.Database.MigrateAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task DisposeAsync()
|
|
||||||
{
|
|
||||||
await _context.DisposeAsync();
|
|
||||||
|
|
||||||
SqliteConnection.ClearAllPools();
|
|
||||||
|
|
||||||
File.Delete(_context.DatabasePath);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Unit;
|
||||||
|
|
||||||
|
public class AddressRepositoryTests
|
||||||
|
{
|
||||||
|
private readonly Mock<DbContext> _context = new();
|
||||||
|
private readonly Mock<ILogger<AddressRepository>> _logger = new();
|
||||||
|
private readonly AddressFaker _faker = new();
|
||||||
|
private readonly AddressRepository _repository;
|
||||||
|
|
||||||
|
public AddressRepositoryTests()
|
||||||
|
{
|
||||||
|
_repository = new AddressRepository(_context.Object, _logger.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WhenExceptionIsThrown_ItShouldLogError()
|
||||||
|
{
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
var mockSet = new Mock<DbSet<Address>>();
|
||||||
|
|
||||||
|
mockSet
|
||||||
|
.Setup(x => x.FindAsync(entity.Id))
|
||||||
|
.Throws<Exception>();
|
||||||
|
|
||||||
|
_context
|
||||||
|
.Setup(x => x.Set<Address>())
|
||||||
|
.Returns(mockSet.Object);
|
||||||
|
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.Log(
|
||||||
|
LogLevel.Error,
|
||||||
|
It.IsAny<EventId>(),
|
||||||
|
It.IsAny<It.IsAnyType>(),
|
||||||
|
It.IsAny<Exception>(),
|
||||||
|
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Find_WhenExceptionIsThrown_ItShouldReturnEmptyListAndLogError()
|
||||||
|
{
|
||||||
|
_context
|
||||||
|
.Setup(x => x.Set<Address>())
|
||||||
|
.Throws<Exception>();
|
||||||
|
|
||||||
|
var result = await _repository.Find(x => x.Id == 1);
|
||||||
|
|
||||||
|
result.Should().BeEmpty();
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.Log(
|
||||||
|
LogLevel.Error,
|
||||||
|
It.IsAny<EventId>(),
|
||||||
|
It.IsAny<It.IsAnyType>(),
|
||||||
|
It.IsAny<Exception>(),
|
||||||
|
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Unit;
|
||||||
|
|
||||||
|
public class AliasRepositoryTests
|
||||||
|
{
|
||||||
|
private readonly Mock<DbContext> _context = new();
|
||||||
|
private readonly Mock<ILogger<AliasRepository>> _logger = new();
|
||||||
|
private readonly AliasFaker _faker = new();
|
||||||
|
private readonly AliasRepository _repository;
|
||||||
|
|
||||||
|
public AliasRepositoryTests()
|
||||||
|
{
|
||||||
|
_repository = new AliasRepository(_context.Object, _logger.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WhenExceptionIsThrown_ItShouldLogError()
|
||||||
|
{
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
var mockSet = new Mock<DbSet<Alias>>();
|
||||||
|
|
||||||
|
mockSet
|
||||||
|
.Setup(x => x.FindAsync(entity.Id))
|
||||||
|
.Throws<Exception>();
|
||||||
|
|
||||||
|
_context
|
||||||
|
.Setup(x => x.Set<Alias>())
|
||||||
|
.Returns(mockSet.Object);
|
||||||
|
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.Log(
|
||||||
|
LogLevel.Error,
|
||||||
|
It.IsAny<EventId>(),
|
||||||
|
It.IsAny<It.IsAnyType>(),
|
||||||
|
It.IsAny<Exception>(),
|
||||||
|
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Find_WhenExceptionIsThrown_ItShouldReturnEmptyListAndLogError()
|
||||||
|
{
|
||||||
|
_context
|
||||||
|
.Setup(x => x.Set<Alias>())
|
||||||
|
.Throws<Exception>();
|
||||||
|
|
||||||
|
var result = await _repository.Find(x => x.Id == 1);
|
||||||
|
|
||||||
|
result.Should().BeEmpty();
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.Log(
|
||||||
|
LogLevel.Error,
|
||||||
|
It.IsAny<EventId>(),
|
||||||
|
It.IsAny<It.IsAnyType>(),
|
||||||
|
It.IsAny<Exception>(),
|
||||||
|
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Tests.Unit;
|
||||||
|
|
||||||
|
public class CommentRepositoryTests
|
||||||
|
{
|
||||||
|
private readonly Mock<DbContext> _context = new();
|
||||||
|
private readonly Mock<ILogger<CommentRepository>> _logger = new();
|
||||||
|
private readonly CommentFaker _faker = new();
|
||||||
|
private readonly CommentRepository _repository;
|
||||||
|
|
||||||
|
public CommentRepositoryTests()
|
||||||
|
{
|
||||||
|
_repository = new CommentRepository(_context.Object, _logger.Object);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Upsert_WhenExceptionIsThrown_ItShouldLogError()
|
||||||
|
{
|
||||||
|
var entity = _faker.Generate();
|
||||||
|
var mockSet = new Mock<DbSet<Comment>>();
|
||||||
|
|
||||||
|
mockSet
|
||||||
|
.Setup(x => x.FindAsync(entity.Id))
|
||||||
|
.Throws<Exception>();
|
||||||
|
|
||||||
|
_context
|
||||||
|
.Setup(x => x.Set<Comment>())
|
||||||
|
.Returns(mockSet.Object);
|
||||||
|
|
||||||
|
await _repository.Upsert(entity);
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.Log(
|
||||||
|
LogLevel.Error,
|
||||||
|
It.IsAny<EventId>(),
|
||||||
|
It.IsAny<It.IsAnyType>(),
|
||||||
|
It.IsAny<Exception>(),
|
||||||
|
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task Find_WhenExceptionIsThrown_ItShouldReturnEmptyListAndLogError()
|
||||||
|
{
|
||||||
|
_context
|
||||||
|
.Setup(x => x.Set<Comment>())
|
||||||
|
.Throws<Exception>();
|
||||||
|
|
||||||
|
var result = await _repository.Find(x => x.Id == 1);
|
||||||
|
|
||||||
|
result.Should().BeEmpty();
|
||||||
|
|
||||||
|
_logger.Verify(
|
||||||
|
x => x.Log(
|
||||||
|
LogLevel.Error,
|
||||||
|
It.IsAny<EventId>(),
|
||||||
|
It.IsAny<It.IsAnyType>(),
|
||||||
|
It.IsAny<Exception>(),
|
||||||
|
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,29 +4,29 @@ public class SdnRepositoryTests
|
|||||||
{
|
{
|
||||||
private readonly Mock<DbContext> _context = new();
|
private readonly Mock<DbContext> _context = new();
|
||||||
private readonly Mock<ILogger<SdnRepository>> _logger = new();
|
private readonly Mock<ILogger<SdnRepository>> _logger = new();
|
||||||
private readonly SdnFaker _sdnFaker = new();
|
private readonly SdnFaker _faker = new();
|
||||||
private readonly SdnRepository _sdnRepository;
|
private readonly SdnRepository _repository;
|
||||||
|
|
||||||
public SdnRepositoryTests()
|
public SdnRepositoryTests()
|
||||||
{
|
{
|
||||||
_sdnRepository = new SdnRepository(_context.Object, _logger.Object);
|
_repository = new SdnRepository(_context.Object, _logger.Object);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task Upsert_WhenExceptionIsThrown_ItShouldLogError()
|
public async Task Upsert_WhenExceptionIsThrown_ItShouldLogError()
|
||||||
{
|
{
|
||||||
var sdn = _sdnFaker.Generate();
|
var entity = _faker.Generate();
|
||||||
var mockSet = new Mock<DbSet<Sdn>>();
|
var mockSet = new Mock<DbSet<Sdn>>();
|
||||||
|
|
||||||
mockSet
|
mockSet
|
||||||
.Setup(x => x.FindAsync(sdn.Id))
|
.Setup(x => x.FindAsync(entity.Id))
|
||||||
.Throws<Exception>();
|
.Throws<Exception>();
|
||||||
|
|
||||||
_context
|
_context
|
||||||
.Setup(x => x.Set<Sdn>())
|
.Setup(x => x.Set<Sdn>())
|
||||||
.Returns(mockSet.Object);
|
.Returns(mockSet.Object);
|
||||||
|
|
||||||
await _sdnRepository.Upsert(sdn);
|
await _repository.Upsert(entity);
|
||||||
|
|
||||||
_logger.Verify(
|
_logger.Verify(
|
||||||
x => x.Log(
|
x => x.Log(
|
||||||
@@ -46,7 +46,7 @@ public class SdnRepositoryTests
|
|||||||
.Setup(x => x.Set<Sdn>())
|
.Setup(x => x.Set<Sdn>())
|
||||||
.Throws<Exception>();
|
.Throws<Exception>();
|
||||||
|
|
||||||
var result = await _sdnRepository.Find(x => x.Id == 1);
|
var result = await _repository.Find(x => x.Id == 1);
|
||||||
|
|
||||||
result.Should().BeEmpty();
|
result.Should().BeEmpty();
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Interfaces;
|
||||||
|
|
||||||
|
interface IAddressRepository : IRepository<Address>
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Interfaces;
|
||||||
|
|
||||||
|
interface IAliasRepository : IRepository<Alias>
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Interfaces;
|
||||||
|
|
||||||
|
interface ICommentRepository : IRepository<Comment>
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -3,5 +3,8 @@ namespace SanctionsSearch.Worker.Interfaces;
|
|||||||
interface IUnitOfWork
|
interface IUnitOfWork
|
||||||
{
|
{
|
||||||
ISdnRepository Sdns { get; }
|
ISdnRepository Sdns { get; }
|
||||||
|
IAddressRepository Addresses { get; }
|
||||||
|
IAliasRepository Aliases { get; }
|
||||||
|
ICommentRepository Comments { get; }
|
||||||
Task SaveChangesAsync();
|
Task SaveChangesAsync();
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Persistence;
|
||||||
|
|
||||||
|
class AddressRepository(
|
||||||
|
DbContext dbContext,
|
||||||
|
ILogger<AddressRepository> logger
|
||||||
|
) : EfRepository<Address>(dbContext, logger), IAddressRepository
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Persistence;
|
||||||
|
|
||||||
|
class AliasRepository(
|
||||||
|
DbContext dbContext,
|
||||||
|
ILogger<AliasRepository> logger
|
||||||
|
) : EfRepository<Alias>(dbContext, logger), IAliasRepository
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace SanctionsSearch.Worker.Persistence;
|
||||||
|
|
||||||
|
class CommentRepository(
|
||||||
|
DbContext dbContext,
|
||||||
|
ILogger<CommentRepository> logger
|
||||||
|
) : EfRepository<Comment>(dbContext, logger), ICommentRepository
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -4,6 +4,9 @@ class EfUnitOfWork(AppDbContext context, ILoggerFactory loggerFactory) : IUnitOf
|
|||||||
{
|
{
|
||||||
private readonly AppDbContext _context = context;
|
private readonly AppDbContext _context = context;
|
||||||
public ISdnRepository Sdns { get; } = new SdnRepository(context, loggerFactory.CreateLogger<SdnRepository>());
|
public ISdnRepository Sdns { get; } = new SdnRepository(context, loggerFactory.CreateLogger<SdnRepository>());
|
||||||
|
public IAddressRepository Addresses { get; } = new AddressRepository(context, loggerFactory.CreateLogger<AddressRepository>());
|
||||||
|
public IAliasRepository Aliases { get; } = new AliasRepository(context, loggerFactory.CreateLogger<AliasRepository>());
|
||||||
|
public ICommentRepository Comments { get; } = new CommentRepository(context, loggerFactory.CreateLogger<CommentRepository>());
|
||||||
|
|
||||||
public async Task SaveChangesAsync()
|
public async Task SaveChangesAsync()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -63,7 +63,15 @@ class Program
|
|||||||
builder.Services.AddScoped(rs => rs.GetRequiredService<IOptionsSnapshot<DbOptions>>().Value);
|
builder.Services.AddScoped(rs => rs.GetRequiredService<IOptionsSnapshot<DbOptions>>().Value);
|
||||||
|
|
||||||
builder.Services.AddSingleton(TimeProvider.System);
|
builder.Services.AddSingleton(TimeProvider.System);
|
||||||
|
builder.Services.AddScoped<IOfacFileService, OfacFileService>();
|
||||||
|
|
||||||
|
builder.Services.AddScoped<ISdnRepository, SdnRepository>();
|
||||||
|
builder.Services.AddScoped<IAddressRepository, AddressRepository>();
|
||||||
|
builder.Services.AddScoped<IAliasRepository, AliasRepository>();
|
||||||
|
builder.Services.AddScoped<ICommentRepository, CommentRepository>();
|
||||||
|
builder.Services.AddScoped<IUnitOfWork, EfUnitOfWork>();
|
||||||
builder.Services.AddDbContext<AppDbContext>();
|
builder.Services.AddDbContext<AppDbContext>();
|
||||||
|
|
||||||
builder.Services.AddHostedService<Worker>();
|
builder.Services.AddHostedService<Worker>();
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
|
|||||||
Reference in New Issue
Block a user