From 45f0f50a347b63cab4572a98a706c9aeacc97d79 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Tue, 12 Mar 2024 15:00:14 +0500 Subject: [PATCH] =?UTF-8?q?Bug=20fix=20+=20=D0=B0=D0=B2=D1=82=D0=BE=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D1=8B=20(=D1=8E=D0=BD=D0=B8=D1=82-=D1=82?= =?UTF-8?q?=D0=B5=D1=81=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/WellCompositeOperationService.cs | 10 +- .../WellCompositeOperationServiceTest.cs | 299 ++++++++++++++++++ 2 files changed, 304 insertions(+), 5 deletions(-) create mode 100644 AsbCloudWebApi.Tests/Services/WellCompositeOperation/WellCompositeOperationServiceTest.cs diff --git a/AsbCloudInfrastructure/Services/WellCompositeOperationService.cs b/AsbCloudInfrastructure/Services/WellCompositeOperationService.cs index 0056b267..3a02b6b2 100644 --- a/AsbCloudInfrastructure/Services/WellCompositeOperationService.cs +++ b/AsbCloudInfrastructure/Services/WellCompositeOperationService.cs @@ -141,7 +141,7 @@ namespace AsbCloudInfrastructure.Services var categories = wellOperationCategoryRepository.Get(true); var categoriesDict = categories.ToDictionary(s => s.Id, s => s.Name); - var idsWellSectionTypes = WellSectionTypesWithCategories.Select(t => t.IdSectionType); + var idsWellSectionTypes = WellSectionTypesWithCategories.Select(t => t.IdSectionType).Distinct(); var usedCategories = WellSectionTypesWithCategories.Select(c => c.IdCategory).Distinct(); var wellOperationRequest = new WellsOperationRequest() @@ -179,10 +179,10 @@ namespace AsbCloudInfrastructure.Services WellSectionTypeCaption = g.First().WellSectionTypeCaption, }); - var composite = aggreagtedByWell - .OrderBy(o => o.DurationHours) - .ThenByDescending(o => o.DepthStart) - .First(); + var composite = aggreagtedByWell.GroupBy(g => g.DurationHours).Count() == 1 + ? aggreagtedByWell.OrderByDescending(o => o.DepthStart).First() + : aggreagtedByWell.OrderBy(o => o.DepthStart).First(); + composite.IdWell = 0; if (compositeDepth > composite.DepthStart) composite.DepthStart = compositeDepth; diff --git a/AsbCloudWebApi.Tests/Services/WellCompositeOperation/WellCompositeOperationServiceTest.cs b/AsbCloudWebApi.Tests/Services/WellCompositeOperation/WellCompositeOperationServiceTest.cs new file mode 100644 index 00000000..eece10e8 --- /dev/null +++ b/AsbCloudWebApi.Tests/Services/WellCompositeOperation/WellCompositeOperationServiceTest.cs @@ -0,0 +1,299 @@ +using AsbCloudApp.Requests; +using AsbCloudDb.Model; +using AsbCloudInfrastructure.Repository; +using AsbCloudInfrastructure.Services.ProcessMaps.Report; +using AsbCloudInfrastructure.Services; +using NSubstitute; +using ProtoBuf.Meta; +using SignalRSwaggerGen.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Xunit; +using AsbCloudApp.Repositories; +using AsbCloudApp.Data; +using AsbCloudApp.Services; + +namespace AsbCloudWebApi.Tests.Services.WellCompositeOperation +{ + public class WellCompositeOperationServiceTest + { + private WellCompositeOperationService service; + + private ICrudRepository wellSectionTypeRepository + = Substitute.For>(); + private IWellOperationCategoryRepository wellOperationCategoryRepository + = Substitute.For(); + private IWellOperationRepository wellOperationRepository + = Substitute.For(); + + + + private readonly static IEnumerable operationCategories = new List() + { + new(){Id = 5096, Name = "Шаблонирование перед спуском"}, + new(){Id = 5008, Name = "Шаблонировка во время бурения"}, + new(){Id = 5013, Name = "Подъем КНБК"}, + new(){Id = 5003, Name = "Бурение ротором"}, + new(){Id = 5036, Name = "Промывка"}, + new(){Id = 5012, Name = "Подъем инструмента"}, + new(){Id = 5083, Name = "Проработка принудительная"}, + }; + + private readonly static IEnumerable sectionTypes = new List() + { + new() {Id = 2, Caption = "Направление", Order = 0}, + new() {Id = 3, Caption = "Кондуктор", Order = 1}, + new() {Id = 31, Caption = "Техническая колонна", Order = 2} + }; + + private readonly static IEnumerable wellOperations1 = new List() + { + new WellOperationDataDto() + { + DepthStart = 50, + DurationHours = 1, + IdCategory = 5096, + IdWell = 55, + IdWellSectionType = 2, + OperationCategoryName = "Шаблонирование перед спуском", + WellSectionTypeCaption = "Направление" + }, + new WellOperationDataDto() + { + DepthStart = 84, + DurationHours = 1, + IdCategory = 5008, + IdWell = 64, + IdWellSectionType = 2, + OperationCategoryName = "Шаблонировка во время бурения", + WellSectionTypeCaption = "Направление" + } + }; + + private readonly static IEnumerable wellOperations2 = new List() + { + new WellOperationDataDto() + { + DepthStart = 10, + DurationHours = 1.5, + IdCategory = 5003, + IdWell = 55, + IdWellSectionType = 2, + OperationCategoryName = "Бурение ротором", + WellSectionTypeCaption = "Направление" + }, + new WellOperationDataDto() + { + DepthStart = 20, + DurationHours = 3.5, + IdCategory = 5003, + IdWell = 64, + IdWellSectionType = 2, + OperationCategoryName = "Бурение ротором", + WellSectionTypeCaption = "Направление" + } + }; + + private readonly static IEnumerable wellOperations3 = new List() + { + new WellOperationDataDto() + { + DepthStart = 1372, + DurationHours = 3, + IdCategory = 5036, + IdWell = 55, + IdWellSectionType = 3, + OperationCategoryName = "Промывка", + WellSectionTypeCaption = "Кондуктор" + }, + new WellOperationDataDto() + { + DepthStart = 1435, + DurationHours = 4, + IdCategory = 5036, + IdWell = 64, + IdWellSectionType = 3, + OperationCategoryName = "Промывка", + WellSectionTypeCaption = "Кондуктор" + } + }; + + private readonly static IEnumerable wellOperations4 = new List() + { + new WellOperationDataDto() + { + DepthStart = 1000, + DurationHours = 10, + IdCategory = 5012, + IdWell = 55, + IdWellSectionType = 31, + OperationCategoryName = "Подъем инструмента", + WellSectionTypeCaption = "Техническая колонна" + }, + new WellOperationDataDto() + { + DepthStart = 500, + DurationHours = 5, + IdCategory = 5083, + IdWell = 55, + IdWellSectionType = 31, + OperationCategoryName = "Проработка принудительная", + WellSectionTypeCaption = "Техническая колонна" + }, + new WellOperationDataDto() + { + DepthStart = 600, + DurationHours = 5, + IdCategory = 5083, + IdWell = 64, + IdWellSectionType = 31, + OperationCategoryName = "Проработка принудительная", + WellSectionTypeCaption = "Техническая колонна" + } + }; + + public WellCompositeOperationServiceTest() + { + + wellSectionTypeRepository.GetAllAsync(Arg.Any()) + .Returns(sectionTypes); + + wellOperationCategoryRepository.Get(Arg.Any()) + .Returns(operationCategories); + + service = new WellCompositeOperationService( + wellSectionTypeRepository, + wellOperationCategoryRepository, + wellOperationRepository); + } + + [Fact] + public async Task GetAsync_return_data() + { + // arrange + wellOperationRepository.GetAsync(Arg.Any(), Arg.Any()) + .Returns(wellOperations1); + + var idsWell = new List() { 55, 64 }; + + // act + var result = await service.GetAsync(idsWell, CancellationToken.None); + + // assert + var compositeWellOperation = result.SelectMany(o => o.Values.Where(o => o.IdWell == 0)).FirstOrDefault(); + Assert.NotNull(compositeWellOperation); + Assert.Equal(5013, compositeWellOperation.IdCategory); + Assert.Equal(1, compositeWellOperation.DurationHours); + Assert.Equal(84, compositeWellOperation.DepthStart); + + var currentWellOperations = result.SelectMany(o => o.Values.Where(o => o.IdWell != 0)); + var categories = currentWellOperations.Select(o => o.IdCategory).Distinct(); + Assert.NotNull(categories); + Assert.Single(categories); + Assert.Equal(5013, categories.First()); + + var categoryName = currentWellOperations.Select(o => o.OperationCategoryName).First(); + Assert.Equal("Подъем КНБК", categoryName); + } + + + [Fact] + public async Task GetAsync_return_data2() + { + // arrange + wellOperationRepository.GetAsync(Arg.Any(), Arg.Any()) + .Returns(wellOperations2); + + var idsWell = new List() { 55, 64 }; + + // act + var result = await service.GetAsync(idsWell, CancellationToken.None); + + // assert + var compositeWellOperation = result.SelectMany(o => o.Values.Where(o => o.IdWell == 0)).FirstOrDefault(); + Assert.NotNull(compositeWellOperation); + Assert.Equal(5003, compositeWellOperation.IdCategory); + Assert.Equal(1.5, compositeWellOperation.DurationHours); + Assert.Equal(10, compositeWellOperation.DepthStart); + } + + [Fact] + public async Task GetAsync_return_data3() + { + // arrange + wellOperationRepository.GetAsync(Arg.Any(), Arg.Any()) + .Returns(wellOperations3); + + var idsWell = new List() { 55, 64 }; + + // act + var result = await service.GetAsync(idsWell, CancellationToken.None); + + // assert + var compositeWellOperation = result.SelectMany(o => o.Values.Where(o => o.IdWell == 0)).FirstOrDefault(); + Assert.NotNull(compositeWellOperation); + Assert.Equal(5036, compositeWellOperation.IdCategory); + Assert.Equal(3, compositeWellOperation.DurationHours); + Assert.Equal(1372, compositeWellOperation.DepthStart); + } + + [Fact] + public async Task GetAsync_return_data4() + { + // arrange + wellOperationRepository.GetAsync(Arg.Any(), Arg.Any()) + .Returns(wellOperations4); + + var idsWell = new List() { 55, 64 }; + + // act + var result = await service.GetAsync(idsWell, CancellationToken.None); + + // assert + var currentWellOperations = result.SelectMany(o => o.Values.Where(o => o.IdWell != 0)); + var categories = currentWellOperations.Select(o => o.IdCategory).Distinct(); + Assert.NotNull(categories); + Assert.Single(categories); + Assert.Equal(5013, categories.First()); + + var categoryName = currentWellOperations.Select(o => o.OperationCategoryName).First(); + Assert.Equal("Подъем КНБК", categoryName); + + var compositeWellOperation = result.SelectMany(o => o.Values.Where(o => o.IdWell == 0)).FirstOrDefault(); + Assert.NotNull(compositeWellOperation); + Assert.Equal(5013, compositeWellOperation.IdCategory); + Assert.Equal(15, compositeWellOperation.DurationHours); + Assert.Equal(500, compositeWellOperation.DepthStart); + } + + [Fact] + public async Task GetAsync_return_data5() + { + // arrange + var wellOperations = new List(); + wellOperations.AddRange(wellOperations1); + wellOperations.AddRange(wellOperations2); + wellOperations.AddRange(wellOperations3); + wellOperations.AddRange(wellOperations4); + + wellOperationRepository.GetAsync(Arg.Any(), Arg.Any()) + .Returns(wellOperations); + + var idsWell = new List() { 55, 64 }; + + // act + var result = await service.GetAsync(idsWell, CancellationToken.None); + + // assert + Assert.Equal(4, result.Count()); + + var lastOperation = result.Last(); + var lastOperationComposite = lastOperation[0]; + Assert.Equal(1372, lastOperationComposite.DepthStart); + } + } +}