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

119 lines
5.0 KiB
C#

using Contracts.DTO;
using Contracts.Repositories;
using Contracts.SearchModels;
using Contracts.Services;
using Contracts.ViewModels;
using Moq;
using Services.Support.Exceptions;
namespace Services.Tests.Domain;
public class SpendingGroupServiceTests
{
[Fact]
public async Task Create_ReturnsSpendingGroupViewModel()
{
var spendingGroupRepoMock = new Mock<ISpendingGroupRepo>();
spendingGroupRepoMock.Setup(repo => repo.Create(It.IsAny<SpendingGroupDto>())).ReturnsAsync(new SpendingGroupDto());
var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object);
var result = await spendingGroupService.Create(new());
spendingGroupRepoMock.Verify(repo => repo.Create(It.IsAny<SpendingGroupDto>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<SpendingGroupViewModel>(result);
}
[Fact]
public async Task GetList_ReturnsSpendingGroupViewModels()
{
var spendingGroupRepoMock = new Mock<ISpendingGroupRepo>();
spendingGroupRepoMock.Setup(repo => repo.GetList(It.IsAny<SpendingGroupSearch>())).ReturnsAsync(_getAllSpendingGroups());
var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object);
var result = await spendingGroupService.GetList();
spendingGroupRepoMock.Verify(repo => repo.GetList(It.IsAny<SpendingGroupSearch>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<List<SpendingGroupViewModel>>(result.ToList());
}
[Fact]
public async Task Delete_ReturnsSpendingGroupViewModel()
{
var spendingGroupRepoMock = new Mock<ISpendingGroupRepo>();
spendingGroupRepoMock.Setup(repo => repo.Delete(It.IsAny<SpendingGroupSearch>())).ReturnsAsync(new SpendingGroupDto());
var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object);
var result = await spendingGroupService.Delete(new());
spendingGroupRepoMock.Verify(repo => repo.Delete(It.IsAny<SpendingGroupSearch>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<SpendingGroupViewModel>(result);
}
[Fact]
public void Delete_WhenSpendingGroupNotFound_ThrowsEntityNotFoundException()
{
var spendingGroupRepoMock = new Mock<ISpendingGroupRepo>();
spendingGroupRepoMock.Setup(repo => repo.Delete(It.IsAny<SpendingGroupSearch>())).ReturnsAsync((SpendingGroupDto)null);
var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object);
Assert.ThrowsAsync<EntityNotFoundException>(() => spendingGroupService.Delete(new()));
}
[Fact]
public async Task Update_ReturnsSpendingGroupViewModel()
{
var spendingGroupRepoMock = new Mock<ISpendingGroupRepo>();
spendingGroupRepoMock.Setup(repo => repo.Update(It.IsAny<SpendingGroupDto>())).ReturnsAsync(new SpendingGroupDto());
var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object);
var result = await spendingGroupService.Update(new());
spendingGroupRepoMock.Verify(repo => repo.Update(It.IsAny<SpendingGroupDto>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<SpendingGroupViewModel>(result);
}
[Fact]
public void Update_WhenSpendingGroupNotFound_ThrowsEntityNotFoundException()
{
var spendingGroupRepoMock = new Mock<ISpendingGroupRepo>();
spendingGroupRepoMock.Setup(repo => repo.Update(It.IsAny<SpendingGroupDto>())).ReturnsAsync((SpendingGroupDto)null);
var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object);
Assert.ThrowsAsync<EntityNotFoundException>(() => spendingGroupService.Update(new()));
}
[Fact]
public async Task GetDetails_ReturnsSpendingGroupViewModel()
{
var spendingGroupRepoMock = new Mock<ISpendingGroupRepo>();
spendingGroupRepoMock.Setup(repo => repo.Get(It.IsAny<SpendingGroupSearch>())).ReturnsAsync(new SpendingGroupDto());
var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object);
var result = await spendingGroupService.GetDetails(new());
spendingGroupRepoMock.Verify(repo => repo.Get(It.IsAny<SpendingGroupSearch>()), Times.Once);
Assert.NotNull(result);
Assert.IsType<SpendingGroupViewModel>(result);
}
[Fact]
public void GetDetails_WhenSpendingGroupNotFound_ThrowsEntityNotFoundException()
{
var spendingGroupRepoMock = new Mock<ISpendingGroupRepo>();
spendingGroupRepoMock.Setup(repo => repo.Get(It.IsAny<SpendingGroupSearch>())).ReturnsAsync((SpendingGroupDto)null);
var spendingGroupService = new SpendingGroupService(spendingGroupRepoMock.Object);
Assert.ThrowsAsync<EntityNotFoundException>(() => spendingGroupService.GetDetails(new()));
}
private IEnumerable<SpendingGroupDto> _getAllSpendingGroups() => new List<SpendingGroupDto>()
{
new() { Id = Guid.NewGuid(), Name = "Group 1" },
new() { Id = Guid.NewGuid(), Name = "Group 2" }
};
}