forked from ddrilling/AsbCloudServer
Добавлены тесты WellboreServiceTest.
This commit is contained in:
parent
4a72504ee8
commit
18b2647889
@ -58,7 +58,8 @@ public class WellboreService : IWellboreService
|
||||
Id = 1,
|
||||
Name = string.Format(WellboreNameFormat, 1),
|
||||
Well = well,
|
||||
};
|
||||
};
|
||||
//if(well.)
|
||||
|
||||
if(well.IdTelemetry is not null)
|
||||
{
|
||||
@ -121,9 +122,6 @@ public class WellboreService : IWellboreService
|
||||
var dataCache = telemetryDataCache.GetOrDefaultFirstLast(well.IdTelemetry.Value);
|
||||
if (dataCache is not null)
|
||||
{
|
||||
wellbore.DateStart = dataCache.Value.First.DateTime;
|
||||
wellbore.DepthStart = dataCache.Value.First.WellDepth!.Value;
|
||||
|
||||
wellbore.DateEnd = dataCache.Value.Last.DateTime;
|
||||
wellbore.DepthEnd = dataCache.Value.Last.WellDepth!.Value;
|
||||
}
|
||||
|
@ -3,14 +3,11 @@ using AsbCloudApp.Data.SAUB;
|
||||
using AsbCloudApp.Repositories;
|
||||
using AsbCloudApp.Requests;
|
||||
using AsbCloudApp.Services;
|
||||
using AsbCloudInfrastructure.Repository;
|
||||
using AsbCloudInfrastructure.Services.SAUB;
|
||||
using AsbCloudInfrastructure.Services;
|
||||
using NSubstitute;
|
||||
using Org.BouncyCastle.Asn1.Ocsp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
@ -19,25 +16,205 @@ namespace AsbCloudWebApi.Tests.ServicesTests
|
||||
{
|
||||
public class WellboreServiceTest
|
||||
{
|
||||
private IWellService wellService;
|
||||
private IWellOperationRepository wellOperationRepository;
|
||||
private ITelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache;
|
||||
private WellboreService wellboreService;
|
||||
|
||||
private WellDto well1 = new WellDto
|
||||
{
|
||||
Id = 1,
|
||||
IdState = 1,
|
||||
IdTelemetry = 1,
|
||||
LastTelemetryDate = DateTime.Now,
|
||||
Caption = "well 1"
|
||||
};
|
||||
|
||||
private WellDto well2 = new WellDto
|
||||
{
|
||||
Id = 2,
|
||||
IdState = 1,
|
||||
IdTelemetry = 100,
|
||||
LastTelemetryDate = DateTime.Now,
|
||||
Caption = "well 2"
|
||||
};
|
||||
|
||||
public WellboreServiceTest()
|
||||
{
|
||||
var wellService = Substitute.For<IWellService>();
|
||||
wellService.GetAsync(Arg.Any<WellRequest>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Enumerable.Empty<WellDto>());
|
||||
wellService = Substitute.For<IWellService>();
|
||||
|
||||
var wellOperationRepository = Substitute.For<IWellOperationRepository>();
|
||||
wellOperationRepository.GetSectionsAsync(Arg.Any<IEnumerable<int>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Enumerable.Empty<SectionByOperationsDto>());
|
||||
wellOperationRepository = Substitute.For<IWellOperationRepository>();
|
||||
|
||||
telemetryDataCache = Substitute.For<ITelemetryDataCache<TelemetryDataSaubDto>>();
|
||||
|
||||
var telemetryDataCache = Substitute.For<TelemetryDataCache<TelemetryDataSaubDto>>();
|
||||
telemetryDataCache.GetOrDefaultFirstLast(Arg.Any<int>());
|
||||
wellboreService = new WellboreService(wellService, wellOperationRepository, telemetryDataCache);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetWellboresAsync()
|
||||
public async Task GetWellboresAsync_returns_empty_collection()
|
||||
{
|
||||
var result = await wellboreService.GetWellboresAsync(new[] { 1 }, CancellationToken.None);
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.False(result.Any());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetWellboresAsync_returns_one_bore_by_well_only()
|
||||
{
|
||||
wellService.GetAsync(Arg.Any<WellRequest>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new WellDto[] { well1 });
|
||||
|
||||
var result = await wellboreService.GetWellboresAsync(new[] { 1 }, CancellationToken.None);
|
||||
|
||||
Assert.Single(result);
|
||||
var wellbore0 = result.ElementAt(0);
|
||||
Assert.Equal(well1.Caption, wellbore0.Well.Caption);
|
||||
Assert.Equal(well1.Id, wellbore0.Well.Id);
|
||||
|
||||
Assert.Equal("Ствол 1", wellbore0.Name);
|
||||
Assert.Equal(1, wellbore0.Id);
|
||||
|
||||
Assert.Equal(default, wellbore0.DateStart);
|
||||
Assert.Equal(default, wellbore0.DateEnd);
|
||||
Assert.Equal(default, wellbore0.DepthStart);
|
||||
Assert.Equal(default, wellbore0.DepthEnd);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetWellboresAsync_returns_two_bore_by_two_wells_only()
|
||||
{
|
||||
wellService.GetAsync(Arg.Any<WellRequest>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new WellDto[] { well1, well2 });
|
||||
|
||||
var result = await wellboreService.GetWellboresAsync(new[] { 1 }, CancellationToken.None);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetWellboresAsync_returns_two_bore_by_well_with_sections()
|
||||
{
|
||||
wellService.GetAsync(Arg.Any<WellRequest>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new WellDto[] { well1 });
|
||||
|
||||
var section0 = new SectionByOperationsDto()
|
||||
{ IdWell = well1.Id, IdWellSectionType = 0, IdType = 1, DateStart = new DateTime(2023, 01, 01), DateEnd = new DateTime(2023, 01, 02), DepthStart = 000, DepthEnd = 100 };
|
||||
var section1 = new SectionByOperationsDto()
|
||||
{ IdWell = well1.Id, IdWellSectionType = 0, IdType = 1, DateStart = new DateTime(2023, 01, 02), DateEnd = new DateTime(2023, 01, 03), DepthStart = 100, DepthEnd = 300 };
|
||||
var section2 = new SectionByOperationsDto()
|
||||
{ IdWell = well1.Id, IdWellSectionType = 0, IdType = 1, DateStart = new DateTime(2023, 01, 03), DateEnd = new DateTime(2023, 01, 04), DepthStart = 200, DepthEnd = 210 };
|
||||
var section3 = new SectionByOperationsDto()
|
||||
{ IdWell = int.MaxValue, IdWellSectionType = 0, IdType = 1, DateStart = new DateTime(2023, 01, 03), DateEnd = new DateTime(2023, 01, 04), DepthStart = 200, DepthEnd = 220 };
|
||||
|
||||
wellOperationRepository.GetSectionsAsync(Arg.Any<IEnumerable<int>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new SectionByOperationsDto[]{section0, section1, section2, section3 });
|
||||
|
||||
var result = await wellboreService.GetWellboresAsync(new[] { 1 }, CancellationToken.None);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
var wellbore0 = result.ElementAt(0);
|
||||
Assert.Equal(well1.Caption, wellbore0.Well.Caption);
|
||||
Assert.Equal(well1.Id, wellbore0.Well.Id);
|
||||
|
||||
Assert.Equal("Ствол 1", wellbore0.Name);
|
||||
Assert.Equal(1, wellbore0.Id);
|
||||
|
||||
Assert.Equal(section0.DateStart, wellbore0.DateStart);
|
||||
Assert.Equal(section0.DepthStart, wellbore0.DepthStart);
|
||||
Assert.Equal(section1.DateEnd, wellbore0.DateEnd);
|
||||
Assert.Equal(section1.DepthEnd, wellbore0.DepthEnd);
|
||||
|
||||
var wellbore1 = result.ElementAt(1);
|
||||
Assert.Equal(well1.Caption, wellbore1.Well.Caption);
|
||||
Assert.Equal(well1.Id, wellbore1.Well.Id);
|
||||
|
||||
Assert.Equal("Ствол 2", wellbore1.Name);
|
||||
Assert.Equal(2, wellbore1.Id);
|
||||
|
||||
Assert.Equal(section2.DateStart, wellbore1.DateStart);
|
||||
Assert.Equal(section2.DepthStart, wellbore1.DepthStart);
|
||||
Assert.Equal(section2.DateEnd, wellbore1.DateEnd);
|
||||
Assert.Equal(section2.DepthEnd, wellbore1.DepthEnd);
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task GetWellboresAsync_returns_one_bore_by_well_with_telemetry()
|
||||
{
|
||||
wellService.GetAsync(Arg.Any<WellRequest>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new WellDto[] { well1 });
|
||||
|
||||
var firstCacheItem = new TelemetryDataSaubDto { DateTime = new DateTime(2000, 01, 01), WellDepth = 0, };
|
||||
var lastCacheItem = new TelemetryDataSaubDto { DateTime = new DateTime(2023, 01, 05), WellDepth = 321, };
|
||||
|
||||
telemetryDataCache.GetOrDefaultFirstLast(Arg.Any<int>())
|
||||
.Returns((firstCacheItem, lastCacheItem));
|
||||
|
||||
var result = await wellboreService.GetWellboresAsync(new[] { 1 }, CancellationToken.None);
|
||||
|
||||
Assert.Single(result);
|
||||
var wellbore0 = result.ElementAt(0);
|
||||
Assert.Equal(well1.Caption, wellbore0.Well.Caption);
|
||||
Assert.Equal(well1.Id, wellbore0.Well.Id);
|
||||
|
||||
Assert.Equal("Ствол 1", wellbore0.Name);
|
||||
Assert.Equal(1, wellbore0.Id);
|
||||
|
||||
Assert.Equal(firstCacheItem.DateTime, wellbore0.DateStart);
|
||||
Assert.Equal(firstCacheItem.WellDepth!.Value, wellbore0.DepthStart);
|
||||
Assert.Equal(lastCacheItem.DateTime, wellbore0.DateEnd);
|
||||
Assert.Equal(lastCacheItem.WellDepth!.Value, wellbore0.DepthEnd);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetWellboresAsync_returns_two_bore_by_well_with_all()
|
||||
{
|
||||
wellService.GetAsync(Arg.Any<WellRequest>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new WellDto[] { well1 });
|
||||
|
||||
var section0 = new SectionByOperationsDto()
|
||||
{ IdWell = well1.Id, IdWellSectionType = 0, IdType = 1, DateStart = new DateTime(2023, 01, 01), DateEnd = new DateTime(2023, 01, 02), DepthStart = 000, DepthEnd = 100 };
|
||||
var section1 = new SectionByOperationsDto()
|
||||
{ IdWell = well1.Id, IdWellSectionType = 0, IdType = 1, DateStart = new DateTime(2023, 01, 02), DateEnd = new DateTime(2023, 01, 03), DepthStart = 100, DepthEnd = 300 };
|
||||
var section2 = new SectionByOperationsDto()
|
||||
{ IdWell = well1.Id, IdWellSectionType = 0, IdType = 1, DateStart = new DateTime(2023, 01, 03), DateEnd = new DateTime(2023, 01, 04), DepthStart = 200, DepthEnd = 210 };
|
||||
|
||||
wellOperationRepository.GetSectionsAsync(Arg.Any<IEnumerable<int>>(), Arg.Any<CancellationToken>())
|
||||
.Returns(new SectionByOperationsDto[] { section0, section1, section2});
|
||||
|
||||
var firstCacheItem = new TelemetryDataSaubDto { DateTime = new DateTime(2000, 01, 01), WellDepth = 0, };
|
||||
var lastCacheItem = new TelemetryDataSaubDto { DateTime = new DateTime(2023, 01, 05), WellDepth = 321, };
|
||||
|
||||
telemetryDataCache.GetOrDefaultFirstLast(Arg.Any<int>())
|
||||
.Returns((firstCacheItem, lastCacheItem));
|
||||
|
||||
var result = await wellboreService.GetWellboresAsync(new[] { 1 }, CancellationToken.None);
|
||||
|
||||
Assert.Equal(2, result.Count());
|
||||
var wellbore0 = result.ElementAt(0);
|
||||
Assert.Equal(well1.Caption, wellbore0.Well.Caption);
|
||||
Assert.Equal(well1.Id, wellbore0.Well.Id);
|
||||
|
||||
Assert.Equal("Ствол 1", wellbore0.Name);
|
||||
Assert.Equal(1, wellbore0.Id);
|
||||
|
||||
Assert.Equal(section0.DateStart, wellbore0.DateStart);
|
||||
Assert.Equal(section0.DepthStart, wellbore0.DepthStart);
|
||||
Assert.Equal(section1.DateEnd, wellbore0.DateEnd);
|
||||
Assert.Equal(section1.DepthEnd, wellbore0.DepthEnd);
|
||||
|
||||
var wellbore1 = result.ElementAt(1);
|
||||
Assert.Equal(well1.Caption, wellbore1.Well.Caption);
|
||||
Assert.Equal(well1.Id, wellbore1.Well.Id);
|
||||
|
||||
Assert.Equal("Ствол 2", wellbore1.Name);
|
||||
Assert.Equal(2, wellbore1.Id);
|
||||
|
||||
Assert.Equal(section2.DateStart, wellbore1.DateStart);
|
||||
Assert.Equal(section2.DepthStart, wellbore1.DepthStart);
|
||||
Assert.Equal(lastCacheItem.DateTime, wellbore1.DateEnd);
|
||||
Assert.Equal(lastCacheItem.WellDepth!.Value, wellbore1.DepthEnd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user