feat: complete end to end functionality
This commit is contained in:
@@ -1,63 +0,0 @@
|
||||
namespace SanctionsSearch.Worker.Tests.Unit;
|
||||
|
||||
public class AddressRepositoryTests
|
||||
{
|
||||
private readonly Mock<DbContext> _contextMock = new();
|
||||
private readonly Mock<ILogger<AddressRepository>> _loggerMock = new();
|
||||
private readonly AddressFaker _faker = new();
|
||||
private readonly AddressRepository _repository;
|
||||
|
||||
public AddressRepositoryTests()
|
||||
{
|
||||
_repository = new AddressRepository(_contextMock.Object, _loggerMock.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>();
|
||||
|
||||
_contextMock
|
||||
.Setup(x => x.Set<Address>())
|
||||
.Returns(mockSet.Object);
|
||||
|
||||
await _repository.Upsert(entity);
|
||||
|
||||
_loggerMock.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()
|
||||
{
|
||||
_contextMock
|
||||
.Setup(x => x.Set<Address>())
|
||||
.Throws<Exception>();
|
||||
|
||||
var result = await _repository.Find(x => x.Id == 1);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
|
||||
_loggerMock.Verify(
|
||||
x => x.Log(
|
||||
LogLevel.Error,
|
||||
It.IsAny<EventId>(),
|
||||
It.IsAny<It.IsAnyType>(),
|
||||
It.IsAny<Exception>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
namespace SanctionsSearch.Worker.Tests.Unit;
|
||||
|
||||
public class AliasRepositoryTests
|
||||
{
|
||||
private readonly Mock<DbContext> _contextMock = new();
|
||||
private readonly Mock<ILogger<AliasRepository>> _loggerMock = new();
|
||||
private readonly AliasFaker _faker = new();
|
||||
private readonly AliasRepository _repository;
|
||||
|
||||
public AliasRepositoryTests()
|
||||
{
|
||||
_repository = new AliasRepository(_contextMock.Object, _loggerMock.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>();
|
||||
|
||||
_contextMock
|
||||
.Setup(x => x.Set<Alias>())
|
||||
.Returns(mockSet.Object);
|
||||
|
||||
await _repository.Upsert(entity);
|
||||
|
||||
_loggerMock.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()
|
||||
{
|
||||
_contextMock
|
||||
.Setup(x => x.Set<Alias>())
|
||||
.Throws<Exception>();
|
||||
|
||||
var result = await _repository.Find(x => x.Id == 1);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
|
||||
_loggerMock.Verify(
|
||||
x => x.Log(
|
||||
LogLevel.Error,
|
||||
It.IsAny<EventId>(),
|
||||
It.IsAny<It.IsAnyType>(),
|
||||
It.IsAny<Exception>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
namespace SanctionsSearch.Worker.Tests.Unit;
|
||||
|
||||
public class CommentRepositoryTests
|
||||
{
|
||||
private readonly Mock<DbContext> _contextMock = new();
|
||||
private readonly Mock<ILogger<CommentRepository>> _loggerMock = new();
|
||||
private readonly CommentFaker _faker = new();
|
||||
private readonly CommentRepository _repository;
|
||||
|
||||
public CommentRepositoryTests()
|
||||
{
|
||||
_repository = new CommentRepository(_contextMock.Object, _loggerMock.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>();
|
||||
|
||||
_contextMock
|
||||
.Setup(x => x.Set<Comment>())
|
||||
.Returns(mockSet.Object);
|
||||
|
||||
await _repository.Upsert(entity);
|
||||
|
||||
_loggerMock.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()
|
||||
{
|
||||
_contextMock
|
||||
.Setup(x => x.Set<Comment>())
|
||||
.Throws<Exception>();
|
||||
|
||||
var result = await _repository.Find(x => x.Id == 1);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
|
||||
_loggerMock.Verify(
|
||||
x => x.Log(
|
||||
LogLevel.Error,
|
||||
It.IsAny<EventId>(),
|
||||
It.IsAny<It.IsAnyType>(),
|
||||
It.IsAny<Exception>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
namespace SanctionsSearch.Worker.Tests.Unit;
|
||||
|
||||
public class EfUnitOfWorkTests
|
||||
{
|
||||
private readonly Mock<DbContext> _contextMock = new();
|
||||
private readonly Mock<ILogger<EfUnitOfWork>> _loggerMock = new();
|
||||
private readonly Mock<ILoggerFactory> _loggerFactoryMock = new();
|
||||
private readonly EfUnitOfWork _unitOfWork;
|
||||
|
||||
public EfUnitOfWorkTests()
|
||||
{
|
||||
_unitOfWork = new(_contextMock.Object, _loggerMock.Object, _loggerFactoryMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Sdns_WhenCalled_ShouldReturnSdnRepository()
|
||||
{
|
||||
_unitOfWork.Sdns.Should().BeOfType<SdnRepository>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Addresses_WhenCalled_ShouldReturnAddressRepository()
|
||||
{
|
||||
_unitOfWork.Addresses.Should().BeOfType<AddressRepository>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Aliases_WhenCalled_ShouldReturnAliasRepository()
|
||||
{
|
||||
_unitOfWork.Aliases.Should().BeOfType<AliasRepository>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Comments_WhenCalled_ShouldReturnCommentRepository()
|
||||
{
|
||||
_unitOfWork.Comments.Should().BeOfType<CommentRepository>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveChangesAsync_WhenADatabaseUpdateExceptionIsThrown_ItShouldBeCaughtAndLogged()
|
||||
{
|
||||
_contextMock
|
||||
.Setup(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ThrowsAsync(new DbUpdateException());
|
||||
|
||||
var action = _unitOfWork.SaveChangesAsync;
|
||||
|
||||
action.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SaveChangesAsync_WhenExceptionIsThrown_ItShouldNotBeCaught()
|
||||
{
|
||||
_contextMock
|
||||
.Setup(x => x.SaveChangesAsync(It.IsAny<CancellationToken>()))
|
||||
.ThrowsAsync(new Exception());
|
||||
|
||||
var action = _unitOfWork.SaveChangesAsync;
|
||||
|
||||
action.Should().ThrowAsync<Exception>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DisposeAsync_WhenExceptionIsThrown_ItShouldBeCaught()
|
||||
{
|
||||
_contextMock
|
||||
.Setup(x => x.DisposeAsync())
|
||||
.Throws(new Exception());
|
||||
|
||||
var action = async () => await _unitOfWork.DisposeAsync();
|
||||
|
||||
action.Should().NotThrowAsync();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Dispose_WhenExceptionIsThrown_ItShouldBeCaught()
|
||||
{
|
||||
_contextMock
|
||||
.Setup(x => x.Dispose())
|
||||
.Throws(new Exception());
|
||||
|
||||
var action = _unitOfWork.Dispose;
|
||||
|
||||
action.Should().NotThrow();
|
||||
}
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
namespace SanctionsSearch.Worker.Tests.Unit;
|
||||
|
||||
public class SdnRepositoryTests
|
||||
{
|
||||
private readonly Mock<DbContext> _contextMock = new();
|
||||
private readonly Mock<ILogger<SdnRepository>> _loggerMock = new();
|
||||
private readonly SdnFaker _faker = new();
|
||||
private readonly SdnRepository _repository;
|
||||
|
||||
public SdnRepositoryTests()
|
||||
{
|
||||
_repository = new SdnRepository(_contextMock.Object, _loggerMock.Object);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Upsert_WhenExceptionIsThrown_ItShouldLogError()
|
||||
{
|
||||
var entity = _faker.Generate();
|
||||
var mockSet = new Mock<DbSet<Sdn>>();
|
||||
|
||||
mockSet
|
||||
.Setup(x => x.FindAsync(entity.Id))
|
||||
.Throws<Exception>();
|
||||
|
||||
_contextMock
|
||||
.Setup(x => x.Set<Sdn>())
|
||||
.Returns(mockSet.Object);
|
||||
|
||||
await _repository.Upsert(entity);
|
||||
|
||||
_loggerMock.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()
|
||||
{
|
||||
_contextMock
|
||||
.Setup(x => x.Set<Sdn>())
|
||||
.Throws<Exception>();
|
||||
|
||||
var result = await _repository.Find(x => x.Id == 1);
|
||||
|
||||
result.Should().BeEmpty();
|
||||
|
||||
_loggerMock.Verify(
|
||||
x => x.Log(
|
||||
LogLevel.Error,
|
||||
It.IsAny<EventId>(),
|
||||
It.IsAny<It.IsAnyType>(),
|
||||
It.IsAny<Exception>(),
|
||||
It.IsAny<Func<It.IsAnyType, Exception?, string>>()
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user