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(); private readonly IUnitOfWork _unitOfWork = Substitute.For(); private readonly ItemControleService _sut; private static readonly Guid Obra = Guid.NewGuid(); public ItemControleServiceTests() { _sut = new ItemControleService(_repository, _unitOfWork); _repository.ObraExistsAsync(Obra, Arg.Any()).Returns(true); } [Fact] public async Task CreateAsync_DeveLancarNotFound_QuandoObraNaoExiste() { var act = () => _sut.CreateAsync(new CreateItemControleRequest("IC-1", "Desc", Guid.NewGuid())); await act.Should().ThrowAsync(); await _repository.DidNotReceive().AddAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task CreateAsync_DeveLancarConflict_QuandoCodigoJaExisteNaObra() { _repository.CodigoExistsAsync("IC-1", Obra, null, Arg.Any()).Returns(true); var act = () => _sut.CreateAsync(new CreateItemControleRequest("IC-1", "Desc", Obra)); await act.Should().ThrowAsync(); await _repository.DidNotReceive().AddAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task CreateAsync_DevePersistirEmTransacao_QuandoValido() { _repository.CodigoExistsAsync(Arg.Any(), Obra, null, Arg.Any()).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(), Arg.Any()); } [Fact] public async Task DeleteAsync_DeveLancarConflict_QuandoTemItensProgramados() { var id = Guid.NewGuid(); _repository.GetByIdAsync(id, Arg.Any()) .Returns(new ItemControle { Id = id, Codigo = "IC-1", Descricao = "Desc", ObraId = Obra }); _repository.ContarItensProgramadosAsync(id, Arg.Any()).Returns(5); var act = () => _sut.DeleteAsync(id); await act.Should().ThrowAsync(); await _repository.DidNotReceive().DeleteAsync(Arg.Any(), Arg.Any()); } [Fact] public async Task DeleteAsync_DeveExcluir_QuandoSemItensProgramados() { var id = Guid.NewGuid(); _repository.GetByIdAsync(id, Arg.Any()) .Returns(new ItemControle { Id = id, Codigo = "IC-1", Descricao = "Desc", ObraId = Obra }); _repository.ContarItensProgramadosAsync(id, Arg.Any()).Returns(0); await _sut.DeleteAsync(id); await _repository.Received(1).DeleteAsync(id, Arg.Any()); _unitOfWork.Received(1).Commit(); } }