diff --git a/AsbCloudInfrastructure/Services/WellboreService.cs b/AsbCloudInfrastructure/Services/WellboreService.cs index 9d38683e..a0073128 100644 --- a/AsbCloudInfrastructure/Services/WellboreService.cs +++ b/AsbCloudInfrastructure/Services/WellboreService.cs @@ -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; } diff --git a/AsbCloudWebApi.Tests/ServicesTests/WellboreServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/WellboreServiceTest.cs index 77f94b84..985a7e6f 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/WellboreServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/WellboreServiceTest.cs @@ -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 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(); - wellService.GetAsync(Arg.Any(), Arg.Any()) - .Returns(Enumerable.Empty()); + wellService = Substitute.For(); - var wellOperationRepository = Substitute.For(); - wellOperationRepository.GetSectionsAsync(Arg.Any>(), Arg.Any()) - .Returns(Enumerable.Empty()); + wellOperationRepository = Substitute.For(); + + telemetryDataCache = Substitute.For>(); - var telemetryDataCache = Substitute.For>(); - telemetryDataCache.GetOrDefaultFirstLast(Arg.Any()); + 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(), Arg.Any()) + .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(), Arg.Any()) + .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(), Arg.Any()) + .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>(), Arg.Any()) + .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(), Arg.Any()) + .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()) + .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(), Arg.Any()) + .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>(), Arg.Any()) + .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()) + .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); } } }