Files
monorepo/LCU.API.Internal.LoanRate/LCU.API.Internal.LoanRateTests/ServiceTests/RateEvaluationServiceTest.cs
T
2026-04-07 14:21:04 -05:00

511 lines
19 KiB
C#

using LCU.Library.Internal.Exceptions;
using LCU.Library.Internal.LoanRateModels.Entities;
using LCU.API.Internal.LoanRateLib.Repositories;
using LCU.API.Internal.LoanRate.Services;
namespace LCU.API.Internal.LoanRateTests.ServiceTests;
public class RateEvaluationServiceTest
{
private readonly Mock<IUnitOfWork> _mockUnitOfWork;
private readonly Mock<IRateEvaluationRepository> _mockRateEvaluationRepository;
private readonly Mock<IRateService> _mockRateService;
private readonly IRateEvaluationService _service;
public RateEvaluationServiceTest()
{
_mockRateEvaluationRepository = new Mock<IRateEvaluationRepository>();
_mockUnitOfWork = new Mock<IUnitOfWork>();
_mockRateService = new Mock<IRateService>();
_service = new RateEvaluationService(_mockUnitOfWork.Object,
_mockRateEvaluationRepository.Object,
_mockRateService.Object);
}
[Fact]
public async void RateEvaluation_GetAsync_Success()
{
var rateEvaluations = new List<RateEvaluation>
{
new() { RateEvaluationId = 1 },
new() { RateEvaluationId = 2 },
new() { RateEvaluationId = 3 }
};
_mockRateEvaluationRepository.Setup(x => x.FindAsync())
.ReturnsAsync(rateEvaluations).Verifiable();
var result = (await _service.GetAsync()).ToList();
Assert.NotNull(result);
Assert.Equal(rateEvaluations.First().RateEvaluationId, result.First().RateEvaluationId);
Assert.Equal(rateEvaluations.Count, result.Count);
_mockRateEvaluationRepository.Verify();
}
[Fact]
public async void RateEvaluation_GetAsync_Success_NotFound()
{
List<RateEvaluation>? rateEvaluations = null;
_mockRateEvaluationRepository.Setup(x => x.FindAsync())
.ReturnsAsync(rateEvaluations).Verifiable();
var result = await _service.GetAsync();
Assert.NotNull(result);
Assert.Empty(result);
_mockRateEvaluationRepository.Verify();
}
[Fact]
public void RateEvaluation_GetAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.FindAsync())
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _service.GetAsync());
_mockRateEvaluationRepository.Verify(x => x.FindAsync(), Times.Once);
}
[Fact]
public async void RateEvaluation_GetByIdAsync_Success()
{
var rateEvaluationId = 1;
var rateEvaluation = new RateEvaluation
{
RateEvaluationId = rateEvaluationId
};
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluationId))
.ReturnsAsync(rateEvaluation).Verifiable();
var result = await _service.GetByIdAsync(rateEvaluationId);
Assert.NotNull(result);
Assert.Equal(rateEvaluation.RateEvaluationId, result.RateEvaluationId);
_mockRateEvaluationRepository.Verify();
}
[Fact]
public async void RateEvaluation_GetByIdAsync_Success_NotFound()
{
var rateEvaluationId = 1;
RateEvaluation? rateEvaluation = null;
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluationId))
.ReturnsAsync(rateEvaluation).Verifiable();
var result = await _service.GetByIdAsync(rateEvaluationId);
Assert.Null(result);
_mockRateEvaluationRepository.Verify();
}
[Fact]
public void RateEvaluation_GetByIdAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(It.IsAny<int>()))
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _service.GetByIdAsync(It.IsAny<int>()));
_mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
}
[Fact]
public async void RateEvaluation_GetRangeByIdAsync_Success()
{
var rateEvaluations = new List<RateEvaluation>
{
new() { RateEvaluationId = 1 },
new() { RateEvaluationId = 2 },
new() { RateEvaluationId = 3 }
};
var ids = rateEvaluations.Select(x => x.RateEvaluationId).ToList();
_mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(ids))
.ReturnsAsync(rateEvaluations).Verifiable();
var result = (await _service.GetRangeByIdAsync(ids)).ToList();
Assert.NotNull(result);
Assert.Equal(rateEvaluations.First().RateEvaluationId, result.First().RateEvaluationId);
Assert.Equal(rateEvaluations.Count, result.Count);
_mockRateEvaluationRepository.Verify();
}
[Fact]
public async void RateEvaluation_GetRangeByIdAsync_Success_NotFound()
{
List<RateEvaluation>? rateEvaluations = null;
var ids = new List<int>
{
1,2,3
};
_mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(ids))
.ReturnsAsync(rateEvaluations).Verifiable();
var result = await _service.GetRangeByIdAsync(ids);
Assert.NotNull(result);
Assert.Empty(result);
_mockRateEvaluationRepository.Verify();
}
[Fact]
public void RateEvaluation_GetRangeByIdAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(It.IsAny<List<int>>()))
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _service.GetRangeByIdAsync(It.IsAny<List<int>>()));
_mockRateEvaluationRepository.Verify(x => x.FindRangeByIdAsync(It.IsAny<List<int>>()), Times.Once);
}
[Fact]
public async void RateEvaluation_SaveAsync_Success()
{
var rateEvaluation = new RateEvaluation
{
RateId = 1,
};
var rateEvaluationReturned = new RateEvaluation
{
RateEvaluationId = 1,
RateId = 2
};
_mockRateEvaluationRepository.Setup(x => x.AddAsync(rateEvaluation))
.ReturnsAsync(rateEvaluationReturned).Verifiable();
var result = await _service.SaveAsync(rateEvaluation);
Assert.NotNull(result);
Assert.Equal(rateEvaluationReturned.RateEvaluationId, result.RateEvaluationId);
Assert.Equal(rateEvaluationReturned.RateId, result.RateId);
_mockRateEvaluationRepository.Verify(x => x.AddAsync(It.IsAny<RateEvaluation>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
_mockRateService.Verify(x => x.ClearCache(), Times.Once);
}
[Fact]
public void RateEvaluation_SaveAsync_Failure_ReturnedNull()
{
var rateEvaluation = new RateEvaluation
{
RateId = 1
};
RateEvaluation? rateEvaluationReturned = null;
_mockRateEvaluationRepository.Setup(x => x.AddAsync(rateEvaluation))
.ReturnsAsync(rateEvaluationReturned).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _service.SaveAsync(rateEvaluation));
_mockRateEvaluationRepository.Verify(x => x.AddAsync(It.IsAny<RateEvaluation>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public void RateEvaluation_SaveAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.AddAsync(It.IsAny<RateEvaluation>()))
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _service.SaveAsync(It.IsAny<RateEvaluation>()));
_mockRateEvaluationRepository.Verify(x => x.AddAsync(It.IsAny<RateEvaluation>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public async void RateEvaluation_SaveRangeAsync_Success()
{
var rateEvaluations = new List<RateEvaluation>
{
new() { RateId = 1 },
new() { RateId = 2 },
new() { RateId = 3 }
};
_mockRateEvaluationRepository.Setup(x => x.AddRangeAsync(rateEvaluations)).Verifiable();
await _service.SaveRangeAsync(rateEvaluations);
_mockRateEvaluationRepository.Verify(x =>
x.AddRangeAsync(It.IsAny<IEnumerable<RateEvaluation>>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
_mockRateService.Verify(x => x.ClearCache(), Times.Once);
}
[Fact]
public void RateEvaluation_SaveRangeAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.AddRangeAsync(It.IsAny<IEnumerable<RateEvaluation>>()))
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _service.SaveRangeAsync(It.IsAny<IEnumerable<RateEvaluation>>()));
_mockRateEvaluationRepository.Verify(x =>
x.AddRangeAsync(It.IsAny<IEnumerable<RateEvaluation>>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public async void RateEvaluation_UpdateAsync_Success()
{
var rateEvaluation = new RateEvaluation
{
RateEvaluationId = 1,
RateId = 1
};
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluation.RateEvaluationId))
.ReturnsAsync(rateEvaluation).Verifiable();
var result = await _service.UpdateAsync(rateEvaluation);
Assert.NotNull(result);
Assert.Equal(rateEvaluation.RateEvaluationId, result.RateEvaluationId);
Assert.Equal(rateEvaluation.RateId, result.RateId);
_mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once());
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
_mockRateService.Verify(x => x.ClearCache(), Times.Once);
}
[Fact]
public void RateEvaluation_UpdateAsync_Failure_ReturnedNull()
{
var rateEvaluation = new RateEvaluation
{
RateId = 1
};
RateEvaluation? rateEvaluationReturned = null;
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluation.RateEvaluationId))
.ReturnsAsync(rateEvaluationReturned).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _service.UpdateAsync(rateEvaluation));
_mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public void RateEvaluation_UpdateAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(It.IsAny<int>()))
.ThrowsAsync(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _service.UpdateAsync(It.IsAny<RateEvaluation>()));
_mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Never);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public async void RateEvaluation_UpdateRangeAsync_Success()
{
var rateEvaluations = new List<RateEvaluation>
{
new()
{
RateEvaluationId = 1,
RateId = 1
},
new()
{
RateEvaluationId = 2,
RateId = 2
},
new()
{
RateEvaluationId = 3,
RateId = 3
}
};
_mockRateEvaluationRepository.Setup(x => x.UpdateRange(rateEvaluations)).Verifiable();
await _service.UpdateRangeAsync(rateEvaluations);
_mockRateEvaluationRepository.Verify(x => x.UpdateRange(It.IsAny<List<RateEvaluation>>()), Times.Once());
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
_mockRateService.Verify(x => x.ClearCache(), Times.Once);
}
[Fact]
public void RateEvaluation_UpdateRangeAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.UpdateRange(It.IsAny<List<RateEvaluation>>()))
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _service.UpdateRangeAsync(It.IsAny<List<RateEvaluation>>()));
_mockRateEvaluationRepository.Verify(x => x.UpdateRange(It.IsAny<List<RateEvaluation>>()), Times.Never);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public async void RateEvaluation_DeleteAsync_Success()
{
var rateEvaluationId = 1;
var rateEvaluation = new RateEvaluation
{
RateEvaluationId = rateEvaluationId,
RateId = 1
};
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluationId))
.ReturnsAsync(rateEvaluation).Verifiable();
_mockRateEvaluationRepository.Setup(x => x.Remove(rateEvaluation)).Verifiable();
await _service.DeleteAsync(rateEvaluationId);
_mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once());
_mockRateEvaluationRepository.Verify(x => x.Remove(It.IsAny<RateEvaluation>()), Times.Once());
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
_mockRateService.Verify(x => x.ClearCache(), Times.Once);
}
[Fact]
public void RateEvaluation_DeleteAsync_Failure_ReturnedNull()
{
var rateEvaluationId = 1;
RateEvaluation? rateEvaluation = null;
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(rateEvaluationId))
.ReturnsAsync(rateEvaluation).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _service.DeleteAsync(rateEvaluationId));
_mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once());
_mockRateEvaluationRepository.Verify(x => x.Remove(It.IsAny<RateEvaluation>()), Times.Never);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public void RateEvaluation_DeleteAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.FindByIdAsync(It.IsAny<int>()))
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _service.DeleteAsync(It.IsAny<int>()));
_mockRateEvaluationRepository.Verify(x => x.FindByIdAsync(It.IsAny<int>()), Times.Once);
_mockRateEvaluationRepository.Verify(x => x.Remove(It.IsAny<RateEvaluation>()), Times.Never);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public async void RateEvaluation_DeleteRangeAsync_Success()
{
var rateEvaluationIds = new List<int>
{
1,
2,
3
};
var rateEvaluations = new List<RateEvaluation>
{
new()
{
RateEvaluationId = rateEvaluationIds[0],
RateId = 1
},
new()
{
RateEvaluationId = rateEvaluationIds[1],
RateId = 2
},
new()
{
RateEvaluationId = rateEvaluationIds[2],
RateId = 3
}
};
_mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(rateEvaluationIds)).ReturnsAsync(rateEvaluations).Verifiable();
_mockRateEvaluationRepository.Setup(x => x.RemoveRange(rateEvaluations)).Verifiable();
await _service.DeleteRangeAsync(rateEvaluationIds);
_mockRateEvaluationRepository.Verify(x => x.FindRangeByIdAsync(It.IsAny<List<int>>()), Times.Once());
_mockRateEvaluationRepository.Verify(x => x.RemoveRange(It.IsAny<List<RateEvaluation>>()), Times.Once());
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Once);
_mockRateService.Verify(x => x.ClearCache(), Times.Once);
}
[Fact]
public void RateEvaluation_DeleteRangeAsync_Failure_ReturnedNull()
{
var rateEvaluationIds = new List<int>
{
1,
2,
3
};
List<RateEvaluation>? rateEvaluations = null;
_mockRateEvaluationRepository.Setup(x => x.FindRangeByIdAsync(rateEvaluationIds))
.ReturnsAsync(rateEvaluations).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () => await _service.DeleteRangeAsync(rateEvaluationIds));
_mockRateEvaluationRepository.Verify(x => x.FindRangeByIdAsync(It.IsAny<List<int>>()), Times.Once());
_mockRateEvaluationRepository.Verify(x => x.RemoveRange(It.IsAny<List<RateEvaluation>>()), Times.Never);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public void RateEvaluation_DeleteRangeAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.RemoveRange(It.IsAny<List<RateEvaluation>>()))
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _service.DeleteRangeAsync(It.IsAny<List<int>>()));
_mockRateEvaluationRepository.Verify(x => x.FindRangeByIdAsync(It.IsAny<List<int>>()), Times.Once());
_mockRateEvaluationRepository.Verify(x => x.RemoveRange(It.IsAny<List<RateEvaluation>>()), Times.Never);
_mockUnitOfWork.Verify(x => x.CompleteAsync(), Times.Never);
_mockRateService.Verify(x => x.ClearCache(), Times.Never);
}
[Fact]
public async void RateEvaluation_CountByEvaluationDetailIdAsync_Success()
{
var evaluationDetailId = DateTime.Now.Millisecond;
var count = DateTime.Now.Minute;
_mockRateEvaluationRepository.Setup(x => x.CountByEvaluationDetailIdAsync(evaluationDetailId)).ReturnsAsync(count).Verifiable();
await _service.CountByEvaluationDetailIdAsync(evaluationDetailId);
_mockRateEvaluationRepository.Verify(x => x.CountByEvaluationDetailIdAsync(It.IsAny<int>()), Times.Once());
}
[Fact]
public void RateEvaluation_CountByEvaluationDetailIdAsync_Failure_Exception()
{
_mockRateEvaluationRepository.Setup(x => x.CountByEvaluationDetailIdAsync(It.IsAny<int>()))
.Throws(new Exception("Test")).Verifiable();
Assert.ThrowsAsync<ServiceException>(async () =>
await _service.CountByEvaluationDetailIdAsync(It.IsAny<int>()));
_mockRateEvaluationRepository.Verify(x => x.CountByEvaluationDetailIdAsync(It.IsAny<int>()), Times.Once());
}
}