From d0eeddaca4386284a475c407a5bddf4f55ce9123 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Mon, 13 May 2024 16:48:47 +0500 Subject: [PATCH] =?UTF-8?q?Fix=20DrillTestController.PostDataAsync(..).=20?= =?UTF-8?q?=D0=A2=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=B2=D1=82=D0=BE=D1=80=D1=8F=D1=8E=D1=89=D0=B8?= =?UTF-8?q?=D0=B5=D1=81=D1=8F=20=D0=B4=D1=80=D0=B8=D0=BB=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudDb/EFExtensions.cs | 2 +- .../Repository/DrillTestRepository.cs | 6 +- .../Clients/IDrillTestControllerClient.cs | 29 +++++++ .../Controllers/DrillControllerTest.cs | 3 - .../Controllers/DrillTestControllerTest.cs | 77 +++++++++++++++++++ 5 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 AsbCloudWebApi.IntegrationTests/Clients/IDrillTestControllerClient.cs create mode 100644 AsbCloudWebApi.IntegrationTests/Controllers/DrillTestControllerTest.cs diff --git a/AsbCloudDb/EFExtensions.cs b/AsbCloudDb/EFExtensions.cs index 9da4c626..4b8361e7 100644 --- a/AsbCloudDb/EFExtensions.cs +++ b/AsbCloudDb/EFExtensions.cs @@ -227,7 +227,7 @@ namespace AsbCloudDb DateTime vDate => $"'{FormatDateValue(vDate)}'", DateTimeOffset vDate => $"'{FormatDateValue(vDate.UtcDateTime)}'", IFormattable vFormattable => FormatFormattableValue(vFormattable), - _ => System.Text.Json.JsonSerializer.Serialize(v), + _ => $"'{EscapeCurlyBraces(JsonSerializer.Serialize(v))}'", }; private static string EscapeCurlyBraces(string vStr) diff --git a/AsbCloudInfrastructure/Repository/DrillTestRepository.cs b/AsbCloudInfrastructure/Repository/DrillTestRepository.cs index 961dd67b..96ba0ece 100644 --- a/AsbCloudInfrastructure/Repository/DrillTestRepository.cs +++ b/AsbCloudInfrastructure/Repository/DrillTestRepository.cs @@ -2,6 +2,7 @@ using AsbCloudApp.Exceptions; using AsbCloudApp.Repositories; using AsbCloudApp.Requests; +using AsbCloudDb; using AsbCloudDb.Model; using Mapster; using Microsoft.EntityFrameworkCore; @@ -68,11 +69,12 @@ namespace AsbCloudInfrastructure.Repository var entities = dtos.Select(dto => { var entity = dto.Adapt(); + entity.TimeStampStart = dto.TimeStampStart.ToUniversalTime(); entity.IdTelemetry = idTelemetry; return entity; }); - db.DrillTests.AddRange(entities); - var result = await db.SaveChangesAsync(token); + + var result = await db.Database.ExecInsertOrUpdateAsync(db.Set(), entities, token); return result; } diff --git a/AsbCloudWebApi.IntegrationTests/Clients/IDrillTestControllerClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/IDrillTestControllerClient.cs new file mode 100644 index 00000000..ba622557 --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Clients/IDrillTestControllerClient.cs @@ -0,0 +1,29 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.DrillTestReport; +using AsbCloudApp.Data.SAUB; +using AsbCloudApp.Requests; +using Microsoft.AspNetCore.Mvc; +using Refit; + +namespace AsbCloudWebApi.IntegrationTests.Clients; + +public interface IDrillTestControllerClient +{ + [Post("/api/telemetry/{uid}/DrillTest")] + Task PostDataAsync( + string uid, + IEnumerable dtos, + CancellationToken token); + + [Get("/api/well/{idWell}/DrillTest")] + Task> GenerateReportAsync( + int idWell, + int id, + CancellationToken cancellationToken); + + [HttpGet("/api/well/{idWell}/DrillTest/all")] + Task>> GetListAsync( + int idWell, + FileReportRequest request, + CancellationToken cancellationToken); +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs index 638630ca..ed15f095 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/DrillControllerTest.cs @@ -1,9 +1,6 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Requests; using AsbCloudDb.Model; using AsbCloudWebApi.IntegrationTests.Clients; using AsbCloudWebApi.IntegrationTests.Data; -using System; using Xunit; namespace AsbCloudWebApi.IntegrationTests.Controllers; diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/DrillTestControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/DrillTestControllerTest.cs new file mode 100644 index 00000000..5b1da11b --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Controllers/DrillTestControllerTest.cs @@ -0,0 +1,77 @@ +using AsbCloudApp.Data.SAUB; +using AsbCloudDb.Model; +using AsbCloudWebApi.IntegrationTests.Clients; +using Xunit; + +namespace AsbCloudWebApi.IntegrationTests.Controllers; + +public class DrillTestControllerTest : BaseIntegrationTest +{ + private readonly IDrillTestControllerClient client; + static readonly string uid = DateTime.UtcNow.ToString("yyyyMMdd_HHmmssfff"); + private static readonly SimpleTimezone timezone = new() { TimezoneId = "a", Hours = 5 }; + private static readonly Telemetry telemetry = new Telemetry() { Id = 1, RemoteUid = uid, TimeZone = timezone, Info = new() }; + private readonly IEnumerable drillTests = [new DrillTestBaseDto { + DepthStart = 12, + Id = 1, + Params = [ new DrillTestParamsDto() { + DepthDrillStep = 1, + DepthSpeed = 2, + Speed = 3, + Step = 4, + TimeDrillStep = 5, + Workload = 6, + }, new DrillTestParamsDto() { + DepthDrillStep = 7, + DepthSpeed = 8, + Speed = 9, + Step = 10, + TimeDrillStep = 11, + Workload = 12, + }], + TimeStampStart = DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(5)) + }]; + + public DrillTestControllerTest(WebAppFactoryFixture factory) + : base(factory) + { + client = factory.GetAuthorizedHttpClient(string.Empty); + } + + [Fact] + public async Task PostDataAsync() + { + // arrange + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.SaveChanges(); + + // act + var response = await client.PostDataAsync(uid, drillTests, CancellationToken.None); + + // assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var count = dbContext.Set().Count(); + Assert.Equal(1, count); + } + + [Fact] + public async Task PostDataAsync_twice_should_be_ok() + { + // arrange + dbContext.CleanupDbSet(); + dbContext.CleanupDbSet(); + dbContext.Set().Add(telemetry); + dbContext.SaveChanges(); + + // act + _ = await client.PostDataAsync(uid, drillTests, CancellationToken.None); + var response = await client.PostDataAsync(uid, drillTests, CancellationToken.None); + + // assert + Assert.Equal(System.Net.HttpStatusCode.OK, response.StatusCode); + var count = dbContext.Set().Count(); + Assert.Equal(1, count); + } +} \ No newline at end of file