scm-api / tests /SCM.UnitTests /Qualidade /AgenteServiceTests.cs
rafaeldavila's picture
Deploy SCM API (.NET 8) - Docker Space
de8b914 verified
Raw History Blame Contribute Delete
2.82 kB
using FluentAssertions;
using NSubstitute;
using SCM.Application.Common;
using SCM.Application.Qualidade;
using SCM.Application.Qualidade.Dtos;
using SCM.Domain.Qualidade;
using SCM.Shared.Exceptions;
using Xunit;
namespace SCM.UnitTests.Qualidade;
public class AgenteServiceTests
{
private readonly IAgenteRepository _repository = Substitute.For<IAgenteRepository>();
private readonly IUnitOfWork _unitOfWork = Substitute.For<IUnitOfWork>();
private readonly AgenteService _sut;
public AgenteServiceTests() => _sut = new AgenteService(_repository, _unitOfWork);
[Fact]
public async Task CreateAsync_DeveLancarConflict_QuandoCodigoJaExiste()
{
_repository.CodigoExistsAsync("AG-1", null, Arg.Any<CancellationToken>()).Returns(true);
var act = () => _sut.CreateAsync(new CreateAgenteRequest("AG-1", "Nome", AgenteTipo.CQ, null, null));
await act.Should().ThrowAsync<ConflictException>();
await _repository.DidNotReceive().AddAsync(Arg.Any<Agente>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task CreateAsync_DevePersistirEmTransacao_QuandoValido()
{
_repository.CodigoExistsAsync(Arg.Any<string>(), null, Arg.Any<CancellationToken>()).Returns(false);
var response = await _sut.CreateAsync(new CreateAgenteRequest("AG-1", "Nome", AgenteTipo.Producao, null, null));
response.Codigo.Should().Be("AG-1");
response.Tipo.Should().Be("Producao");
_unitOfWork.Received(1).BeginTransaction();
_unitOfWork.Received(1).Commit();
await _repository.Received(1).AddAsync(Arg.Any<Agente>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task DeleteAsync_DeveLancarConflict_QuandoTemProgramacao()
{
var id = Guid.NewGuid();
_repository.GetByIdAsync(id, Arg.Any<CancellationToken>())
.Returns(new Agente { Id = id, Codigo = "AG-1", Nome = "Nome", Tipo = AgenteTipo.CQ });
_repository.ContarProgramacoesAsync(id, Arg.Any<CancellationToken>()).Returns(4);
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_QuandoSemProgramacao()
{
var id = Guid.NewGuid();
_repository.GetByIdAsync(id, Arg.Any<CancellationToken>())
.Returns(new Agente { Id = id, Codigo = "AG-1", Nome = "Nome", Tipo = AgenteTipo.CQ });
_repository.ContarProgramacoesAsync(id, Arg.Any<CancellationToken>()).Returns(0);
await _sut.DeleteAsync(id);
await _repository.Received(1).DeleteAsync(id, Arg.Any<CancellationToken>());
_unitOfWork.Received(1).Commit();
}
}