forked from ddrilling/AsbCloudServer
169 lines
4.8 KiB
C#
169 lines
4.8 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AsbCloudApp.Data;
|
|
using AsbCloudApp.Data.WellSections;
|
|
using AsbCloudApp.Exceptions;
|
|
using AsbCloudApp.Services;
|
|
using AsbCloudInfrastructure.Services.WellSections;
|
|
using NSubstitute;
|
|
using Xunit;
|
|
|
|
namespace AsbCloudWebApi.Tests.UnitTests.Services.WellSections;
|
|
|
|
public class WellSectionPlanServiceTests
|
|
{
|
|
private const int idWellSectionPlan = 1;
|
|
private const int idWell = 3;
|
|
private const int idWellSectionType = 54;
|
|
|
|
private readonly IEnumerable<WellSectionPlanDto> fakePlanWellSections = new WellSectionPlanDto[]
|
|
{
|
|
new()
|
|
{
|
|
Id = idWellSectionPlan,
|
|
IdWell = idWell,
|
|
IdSectionType = idWellSectionType
|
|
}
|
|
};
|
|
|
|
private readonly IEnumerable<WellSectionTypeDto> fakeWellSectionTypes = new WellSectionTypeDto[]
|
|
{
|
|
new()
|
|
{
|
|
Id = idWellSectionType
|
|
}
|
|
};
|
|
|
|
private readonly IRepositoryWellRelated<WellSectionPlanDto> wellSectionPlanRepositoryMock =
|
|
Substitute.For<IRepositoryWellRelated<WellSectionPlanDto>>();
|
|
|
|
private readonly ICrudRepository<WellSectionTypeDto> wellSectionTypeRepositoryMock =
|
|
Substitute.For<ICrudRepository<WellSectionTypeDto>>();
|
|
|
|
private readonly WellSectionPlanService wellSectionPlanService;
|
|
|
|
public WellSectionPlanServiceTests()
|
|
{
|
|
wellSectionPlanService = new WellSectionPlanService(wellSectionPlanRepositoryMock, wellSectionTypeRepositoryMock);
|
|
|
|
wellSectionTypeRepositoryMock.GetAllAsync(Arg.Any<CancellationToken>())
|
|
.ReturnsForAnyArgs(fakeWellSectionTypes);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InsertAsync_InsertNewWellSectionTypeWithUniqueSectionTypeInWell_InvokesWellSectionPlanRepositoryInsertAsync()
|
|
{
|
|
//arrange
|
|
var insertedWellSection = new WellSectionPlanDto();
|
|
|
|
//act
|
|
await wellSectionPlanService.InsertAsync(insertedWellSection, CancellationToken.None);
|
|
|
|
//assert
|
|
await wellSectionPlanRepositoryMock.Received().InsertAsync(Arg.Any<WellSectionPlanDto>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task InsertAsync_InsertNewWellSectionTypeWithNotUniqueSectionTypeInWell_ReturnsDuplicateException()
|
|
{
|
|
//arrange
|
|
var insertedWellSection = new WellSectionPlanDto
|
|
{
|
|
Id = 2,
|
|
IdSectionType = idWellSectionType
|
|
};
|
|
|
|
wellSectionPlanRepositoryMock.GetByIdWellAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
|
|
.ReturnsForAnyArgs(fakePlanWellSections);
|
|
|
|
//act
|
|
Task Result() => wellSectionPlanService.InsertAsync(insertedWellSection, CancellationToken.None);
|
|
|
|
//assert
|
|
await Assert.ThrowsAsync<ArgumentInvalidException>(Result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_UpdateExistingWellSectionTypeWithUniqueSectionTypeInWell_InvokesWellSectionPlanRepositoryUpdateAsync()
|
|
{
|
|
//arrange
|
|
var updatedWellSection = new WellSectionPlanDto
|
|
{
|
|
Id = idWellSectionPlan,
|
|
IdSectionType = idWellSectionType
|
|
};
|
|
|
|
//act
|
|
await wellSectionPlanService.UpdateAsync(updatedWellSection, CancellationToken.None);
|
|
|
|
//assert
|
|
await wellSectionPlanRepositoryMock.Received().UpdateAsync(Arg.Any<WellSectionPlanDto>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_ReturnsLastUpdateDateNotNull()
|
|
{
|
|
//arrange
|
|
var updatedWellSection = new WellSectionPlanDto
|
|
{
|
|
Id = idWellSectionPlan,
|
|
IdSectionType = idWellSectionType
|
|
};
|
|
|
|
//act
|
|
await wellSectionPlanService.UpdateAsync(updatedWellSection, CancellationToken.None);
|
|
|
|
//assert
|
|
Assert.NotNull(updatedWellSection.LastUpdateDate);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task UpdateAsync_UpdateExistingWellSectionTypeWithNotUniqueSectionTypeInWell_ReturnsDuplicateException()
|
|
{
|
|
//arrange
|
|
var updatedWellSection = new WellSectionPlanDto
|
|
{
|
|
Id = 2,
|
|
IdSectionType = idWellSectionType
|
|
};
|
|
|
|
wellSectionPlanRepositoryMock.GetByIdWellAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
|
|
.ReturnsForAnyArgs(fakePlanWellSections);
|
|
|
|
//act
|
|
Task Result() => wellSectionPlanService.UpdateAsync(updatedWellSection, CancellationToken.None);
|
|
|
|
//assert
|
|
await Assert.ThrowsAsync<ArgumentInvalidException>(Result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetWellSectionTypesAsync_EmptyCollectionWellSectionPlan_ReturnsCollectionWellSectionType()
|
|
{
|
|
//arrange
|
|
wellSectionPlanRepositoryMock.GetByIdWellAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
|
|
.ReturnsForAnyArgs(Enumerable.Empty<WellSectionPlanDto>());
|
|
|
|
//act
|
|
var result = await wellSectionPlanService.GetWellSectionTypesAsync(idWell, CancellationToken.None);
|
|
|
|
//assert
|
|
Assert.Single(result);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetWellSectionTypesAsync_NotEmptyCollectionWellSectionPlan_ReturnsCollectionWellSectionType()
|
|
{
|
|
//arrange
|
|
wellSectionPlanRepositoryMock.GetByIdWellAsync(Arg.Any<int>(), Arg.Any<CancellationToken>())
|
|
.ReturnsForAnyArgs(fakePlanWellSections);
|
|
|
|
//act
|
|
var result = await wellSectionPlanService.GetWellSectionTypesAsync(idWell, CancellationToken.None);
|
|
|
|
//assert
|
|
Assert.Single(result);
|
|
}
|
|
} |