domBudg/back/Services.Tests/Domain/SpendingPlanServiceTests.cs

101 lines
4.1 KiB
C#
Raw Normal View History

using Contracts.DTO;
using Contracts.Repositories;
using Contracts.SearchModels;
using Contracts.ViewModels;
using Moq;
using Services.Domain;
using Services.Support.Exceptions;
namespace Services.Tests.Domain;
public class SpendingPlanServiceTests
{
[Fact]
public async Task Create_ReturnsSpendingPlanViewModel()
{
var spendingPlanRepoMock = new Mock<ISpendingPlanRepo>();
spendingPlanRepoMock.Setup(repo => repo.Create(It.IsAny<SpendingPlanDto>())).ReturnsAsync(new SpendingPlanDto());
var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object);
var result = await spendingPlanService.Create(new());
spendingPlanRepoMock.Verify(repo => repo.Create(It.IsAny<SpendingPlanDto>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<SpendingPlanViewModel>(result);
}
[Fact]
public async Task Update_WhenPlanExists_ThenUpdatePlan_ReturnsSpendingPlanViewModel()
{
var spendingPlanRepoMock = new Mock<ISpendingPlanRepo>();
spendingPlanRepoMock.Setup(repo => repo.Update(It.IsAny<SpendingPlanDto>()))
.ReturnsAsync(new SpendingPlanDto());
var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object);
var result = await spendingPlanService.Update(new());
spendingPlanRepoMock.Verify(repo => repo.Update(It.IsAny<SpendingPlanDto>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<SpendingPlanViewModel>(result);
}
[Fact]
public async Task Update_WhenPlanNotFound_ThenThrowsEntityNotFoundException()
{
var spendingPlanRepoMock = new Mock<ISpendingPlanRepo>();
spendingPlanRepoMock.Setup(repo => repo.Update(It.IsAny<SpendingPlanDto>()))
.ReturnsAsync((SpendingPlanDto)null);
var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object);
await Assert.ThrowsAsync<EntityNotFoundException>(() => spendingPlanService.Update(new()));
}
[Fact]
public async Task Delete_WhenPlanExists_ThenDeletePlan_ReturnsSpendingPlanViewModel()
{
var spendingPlanRepoMock = new Mock<ISpendingPlanRepo>();
spendingPlanRepoMock.Setup(repo => repo.Delete(It.IsAny<SpendingPlanSearch>()))
.ReturnsAsync(new SpendingPlanDto());
var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object);
var result = await spendingPlanService.Delete(new());
spendingPlanRepoMock.Verify(repo => repo.Delete(It.IsAny<SpendingPlanSearch>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<SpendingPlanViewModel>(result);
}
[Fact]
public async Task Delete_WhenPlanNotFound_ThenThrowsEntityNotFoundException()
{
var spendingPlanRepoMock = new Mock<ISpendingPlanRepo>();
spendingPlanRepoMock.Setup(repo => repo.Delete(It.IsAny<SpendingPlanSearch>()))
.ReturnsAsync((SpendingPlanDto)null);
var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object);
await Assert.ThrowsAsync<EntityNotFoundException>(() => spendingPlanService.Delete(new()));
}
[Fact]
public async Task GetList_ReturnsSpendingPlanViewModels()
{
var spendingPlanRepoMock = new Mock<ISpendingPlanRepo>();
spendingPlanRepoMock.Setup(repo => repo.GetList(It.IsAny<SpendingPlanSearch>()))
.ReturnsAsync(_getAllSpendingPlans());
var spendingPlanService = new SpendingPlanService(spendingPlanRepoMock.Object);
var result = await spendingPlanService.GetList(new());
spendingPlanRepoMock.Verify(repo => repo.GetList(It.IsAny<SpendingPlanSearch>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<List<SpendingPlanViewModel>>(result.ToList());
Assert.Equal(2, result.Count());
}
private IEnumerable<SpendingPlanDto> _getAllSpendingPlans() => new List<SpendingPlanDto>()
{
new() { Id = Guid.NewGuid(), StartAt = DateTime.Now, EndAt = DateTime.Now, Sum = 100 },
new() { Id = Guid.NewGuid(), StartAt = DateTime.Now, EndAt = DateTime.Now, Sum = 200 }
};
}