scm-api / tests /SCM.UnitTests /Controle /ItemControleServiceTests.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
3.31 kB
using FluentAssertions;
using NSubstitute;
using SCM.Application.Common;
using SCM.Application.Controle;
using SCM.Application.Controle.Dtos;
using SCM.Domain.Controle;
using SCM.Shared.Exceptions;
using Xunit;
namespace SCM.UnitTests.Controle;
public class ItemControleServiceTests
{
private readonly IItemControleRepository _repository = Substitute.For<IItemControleRepository>();
private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();
private readonly ItemControleService _sut;
private static readonly Guid Obra = Guid.NewGuid();
public ItemControleServiceTests()
{
_sut = new ItemControleService(_repository, _unitOfWork);
_repository.ObraExistsAsync(Obra, Arg.Any<CancellationToken>()).Returns(true);
}
[Fact]
public async Task CreateAsync_DeveLancarNotFound_QuandoObraNaoExiste()
{
var act = () => _sut.CreateAsync(new CreateItemControleRequest("IC-1", "Desc", Guid.NewGuid()));
await act.Should().ThrowAsync<NotFoundException>();
await _repository.DidNotReceive().AddAsync(Arg.Any<ItemControle>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task CreateAsync_DeveLancarConflict_QuandoCodigoJaExisteNaObra()
{
_repository.CodigoExistsAsync("IC-1", Obra, null, Arg.Any<CancellationToken>()).Returns(true);
var act = () => _sut.CreateAsync(new CreateItemControleRequest("IC-1", "Desc", Obra));
await act.Should().ThrowAsync<ConflictException>();
await _repository.DidNotReceive().AddAsync(Arg.Any<ItemControle>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task CreateAsync_DevePersistirEmTransacao_QuandoValido()
{
_repository.CodigoExistsAsync(Arg.Any<string>(), Obra, null, Arg.Any<CancellationToken>()).Returns(false);
var response = await _sut.CreateAsync(new CreateItemControleRequest("IC-1", "Tubulação", Obra));
response.Codigo.Should().Be("IC-1");
_unitOfWork.Received(1).Commit();
await _repository.Received(1).AddAsync(Arg.Any<ItemControle>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteAsync_DeveLancarConflict_QuandoTemItensProgramados()
{
var id = Guid.NewGuid();
_repository.GetByIdAsync(id, Arg.Any<CancellationToken>())
.Returns(new ItemControle { Id = id, Codigo = "IC-1", Descricao = "Desc", ObraId = Obra });
_repository.ContarItensProgramadosAsync(id, Arg.Any<CancellationToken>()).Returns(5);
var act = () => _sut.DeleteAsync(id);
await act.Should().ThrowAsync<ConflictException>();
await _repository.DidNotReceive().DeleteAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteAsync_DeveExcluir_QuandoSemItensProgramados()
{
var id = Guid.NewGuid();
_repository.GetByIdAsync(id, Arg.Any<CancellationToken>())
.Returns(new ItemControle { Id = id, Codigo = "IC-1", Descricao = "Desc", ObraId = Obra });
_repository.ContarItensProgramadosAsync(id, Arg.Any<CancellationToken>()).Returns(0);
await _sut.DeleteAsync(id);
await _repository.Received(1).DeleteAsync(id, Arg.Any<CancellationToken>());
_unitOfWork.Received(1).Commit();
}
}