From 011a479a4b1ad278d99de9d9f4942b74e900b817 Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Thu, 25 Jan 2024 10:35:16 +0500 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B2=D1=81=D1=82=D0=B0=D0=B2=D0=BA=D0=B8=20/?= =?UTF-8?q?=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20+=20=D0=B8=D0=BD=D1=82=D0=B5=D0=B3=D1=80=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D0=BE=D0=BD=D0=BD=D1=8B=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D1=8B?= =?UTF-8?q?=20(=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Repositories/IWellOperationRepository.cs | 5 +- .../Repository/WellOperationRepository.cs | 67 +++++----- .../Clients/IWellOperationClient.cs | 14 +++ .../WellOperationControllerTest.cs | 115 ++++++++++++++++++ .../Controllers/WellOperationController.cs | 13 +- 5 files changed, 173 insertions(+), 41 deletions(-) create mode 100644 AsbCloudWebApi.IntegrationTests/Clients/IWellOperationClient.cs create mode 100644 AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs diff --git a/AsbCloudApp/Repositories/IWellOperationRepository.cs b/AsbCloudApp/Repositories/IWellOperationRepository.cs index d6302726..43a7a568 100644 --- a/AsbCloudApp/Repositories/IWellOperationRepository.cs +++ b/AsbCloudApp/Repositories/IWellOperationRepository.cs @@ -2,6 +2,7 @@ using AsbCloudApp.Requests; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Threading; using System.Threading.Tasks; @@ -120,7 +121,7 @@ namespace AsbCloudApp.Repositories /// /// /// - bool Validate(IEnumerable wellOperations); + IEnumerable Validate(IEnumerable wellOperations); /// /// Валидация данных (проверка с базой) @@ -128,6 +129,6 @@ namespace AsbCloudApp.Repositories /// /// /// - Task ValidateWithDbAsync(IEnumerable wellOperations, CancellationToken cancellationToken); + IEnumerable ValidateWithDbAsync(IEnumerable wellOperations, CancellationToken cancellationToken); } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs index 4f77a485..8a389e74 100644 --- a/AsbCloudInfrastructure/Repository/WellOperationRepository.cs +++ b/AsbCloudInfrastructure/Repository/WellOperationRepository.cs @@ -9,6 +9,7 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Caching.Memory; using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -333,12 +334,12 @@ public class WellOperationRepository : IWellOperationRepository return dtos; } - public async Task ValidateWithDbAsync(IEnumerable wellOperationDtos, CancellationToken token) + public IEnumerable ValidateWithDbAsync(IEnumerable wellOperationDtos, CancellationToken token) { var firstOperation = wellOperationDtos .FirstOrDefault(); if (firstOperation is null) - return false; + return Enumerable.Empty(); var request = new WellOperationRequest() { @@ -346,53 +347,51 @@ public class WellOperationRepository : IWellOperationRepository OperationType = firstOperation.IdType, }; - var entities = await BuildQuery(request) + var entities = BuildQuery(request) .AsNoTracking() - .ToArrayAsync(token) - .ConfigureAwait(false); + .ToArray(); - if (!entities.Any()) - return Validate(wellOperationDtos); + var wellOperationsUnion = entities.Union(wellOperationDtos).OrderBy(o => o.DateStart); - var wellOperationsUnion = entities.Union(wellOperationDtos).OrderBy(o => o.DateStart).ToArray(); + return Validate(wellOperationsUnion); + } - for(var i = 1; i < wellOperationsUnion.Count(); i++) + public IEnumerable Validate(IEnumerable wellOperationDtos) + { + var firstOperation = wellOperationDtos + .FirstOrDefault(); + if (firstOperation is null) + return Enumerable.Empty(); + + var validationResults = new List(); + + var wellOperations = wellOperationDtos.ToArray(); + for (var i = wellOperations.Count() - 1; i >= 1; i--) { - var prevOperation = wellOperationsUnion[i - 1]; - var currentOperation = wellOperationsUnion[i]; + var prevOperation = wellOperations[i - 1]; + var currentOperation = wellOperations[i]; var prevOperationDateStart = prevOperation.DateStart.ToUniversalTime(); var currentOperationDateStart = currentOperation.DateStart.ToUniversalTime(); var prevOperationDateEnd = prevOperation.DateStart.AddHours(prevOperation.DurationHours).ToUniversalTime(); - var currrentOperationDateEnd = currentOperation.DateStart.AddHours(currentOperation.DurationHours).ToUniversalTime(); - if (currentOperation.Id == 0 && ((prevOperationDateStart.AddDays(Gap) < currentOperationDateStart) || (prevOperationDateEnd >= currrentOperationDateEnd))) + if (prevOperationDateStart.AddDays(Gap) < currentOperationDateStart) { - return false; + validationResults.Add(new ValidationResult( + $"Разница дат между операциями не должна превышать 90 дней", + new[] { nameof(wellOperations) })); + } + + if (prevOperationDateEnd > currentOperationDateStart) + { + validationResults.Add(new ValidationResult( + $"Предыдущая операция не завершена", + new[] { nameof(wellOperations) })); } } - return true; - } - public bool Validate(IEnumerable wellOperationDtos) - { - var firstOperation = wellOperationDtos - .FirstOrDefault(); - if (firstOperation is null) - return false; - - var maxOperationDateStart = firstOperation.DateStart.ToUniversalTime(); - - foreach (var dto in wellOperationDtos) - { - var currentOperationDateStart = dto.DateStart.ToUniversalTime(); - if (maxOperationDateStart.AddDays(Gap) < currentOperationDateStart) - return false; - - maxOperationDateStart = currentOperationDateStart; - } - return true; + return validationResults; } /// diff --git a/AsbCloudWebApi.IntegrationTests/Clients/IWellOperationClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/IWellOperationClient.cs new file mode 100644 index 00000000..6192d306 --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Clients/IWellOperationClient.cs @@ -0,0 +1,14 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.ProcessMapPlan; +using AsbCloudApp.Requests; +using Refit; + +namespace AsbCloudWebApi.IntegrationTests.Clients; + +public interface IWellOperationClient +{ + private const string BaseRoute = "/api/wellOperation"; + + [Post(BaseRoute)] + Task> InsertRange(int idWell, [Body] IEnumerable dtos); +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs new file mode 100644 index 00000000..17130b5e --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs @@ -0,0 +1,115 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.ProcessMapPlan; +using AsbCloudApp.Requests; +using AsbCloudDb.Model; +using AsbCloudDb.Model.ProcessMaps; +using AsbCloudWebApi.IntegrationTests.Clients; +using System.Net; +using Xunit; + +namespace AsbCloudWebApi.IntegrationTests.Controllers; + + +public class WellOperationControllerTest : BaseIntegrationTest +{ + private readonly int idWell = 4; + + private readonly WellOperationDto[] dtos = new WellOperationDto[] + { + new WellOperationDto() + { + + }, + new WellOperationDto() + { + + } + }; + + private IWellOperationClient wellOperationClient; + + public WellOperationControllerTest(WebAppFactoryFixture factory) + : base(factory) + { + wellOperationClient = factory.GetAuthorizedHttpClient(); + var rep = factory.Get + } + + /// + /// Успешное добавление операции с предварительной очисткой + /// + /// + [Fact] + public async Task InsertRangeWithDeleteBefore_returns_success() + { + ////arrange + //dbContext.WellOperations.Add(wellOperation); + //dbContext.SaveChanges(); + + //var request = new OperationStatRequest + //{ + // DateStartUTC = schedule.DrillStart.DateTime, + // DateEndUTC = schedule.DrillEnd.DateTime, + // DurationMinutesMin = 0, + // DurationMinutesMax = 5 + //}; + + //var dtoExpected = new SlipsStatDto + //{ + // DrillerName = $"{Data.Defaults.Drillers[0].Surname} {Data.Defaults.Drillers[0].Name} {Data.Defaults.Drillers[0].Patronymic}", + // WellCount = 1, + // SectionCaption = "Пилотный ствол", + // SlipsCount = 1, + // SlipsTimeInMinutes = (detectedOperation.DateEnd - detectedOperation.DateStart).TotalMinutes, + // SectionDepth = factWellOperation.DepthEnd - factWellOperation.DepthStart, + //}; + + ////act + //var response = await slipsTimeClient.GetAll(request); + + ////assert + //Assert.NotNull(response.Content); + //Assert.Single(response.Content); + + //var dtoActual = response.Content.First(); + //MatchHelper.Match(dtoExpected, dtoActual); + } + + /// + /// Успешное добавление операции без очистки + /// + /// + [Fact] + public async Task InsertRange_returns_success() { + //arrange + var dbset = dbContext.Set(); + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + + operationRepository.Validate(dtos); + + //act + var response = await wellOperationClient.InsertRange(idWell, dtos); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + } + + /// + /// Неуспешное добавление операции с предварительной очисткой + /// + /// + [Fact] + public async Task InsertRangeWithDeleteBefore_returns_error() + { + } + + /// + /// Неуспешное добавление операции без очистки + /// + /// + [Fact] + public async Task InsertRange_returns_error() + { + } +} \ No newline at end of file diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index fa479f12..5c0039cc 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -237,7 +237,8 @@ namespace AsbCloudWebApi.Controllers wellOperation.IdUser = User.GetUserId(); wellOperation.IdType = idType; - if (!await operationRepository.ValidateWithDbAsync(new[] { wellOperation }, cancellationToken)) + var validationResult = operationRepository.ValidateWithDbAsync(new[] { wellOperation }, cancellationToken); + if(validationResult.Any()) return this.ValidationBadRequest(nameof(wellOperation), "The date difference between the operations is more than 3 months"); var result = await operationRepository.InsertRangeAsync(new[] { wellOperation }, cancellationToken); @@ -291,7 +292,8 @@ namespace AsbCloudWebApi.Controllers } - if (!await Validate(wellOperations, deleteBeforeInsert, cancellationToken)) + var validationResult = Validate(wellOperations, deleteBeforeInsert, cancellationToken); + if (validationResult.Any()) return this.ValidationBadRequest(nameof(wellOperations), "The date difference between the operations is more than 3 months"); var result = await operationRepository.InsertRangeAsync(wellOperations, cancellationToken); @@ -307,12 +309,12 @@ namespace AsbCloudWebApi.Controllers /// /// /// - private async Task Validate(IEnumerable wellOperations, bool deleteBeforeInsert, CancellationToken cancellationToken) + private IEnumerable Validate(IEnumerable wellOperations, bool deleteBeforeInsert, CancellationToken cancellationToken) { if (deleteBeforeInsert) return operationRepository.Validate(wellOperations); else - return await operationRepository.ValidateWithDbAsync(wellOperations, cancellationToken); + return operationRepository.ValidateWithDbAsync(wellOperations, cancellationToken); } /// @@ -340,7 +342,8 @@ namespace AsbCloudWebApi.Controllers value.LastUpdateDate = DateTimeOffset.UtcNow; value.IdUser = User.GetUserId(); - if (!await operationRepository.ValidateWithDbAsync(new[] { value }, token)) + var validationResult = operationRepository.ValidateWithDbAsync(new[] { value }, token); + if (validationResult.Any()) return this.ValidationBadRequest(nameof(value), "The date difference between the operations is more than 3 months"); var result = await operationRepository.UpdateAsync(value, token)