From c5a9f67ea004bbb91fd23723d99767bca5c91d82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Mon, 8 Apr 2024 07:27:17 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A2=D0=B5=D1=81=D1=82=D1=8B=20=D0=B0=D0=BF?= =?UTF-8?q?=D0=B8=20=D0=B0=D0=B2=D1=82=D0=BE=D0=BE=D0=BF=D1=80=D0=B5=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D1=91=D0=BD=D0=BD=D1=8B=D1=85=20=D0=BE=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Clients/IDetectedOperationClient.cs | 23 +++ .../DetectedOperationControllerTests.cs | 182 ++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 AsbCloudWebApi.IntegrationTests/Clients/IDetectedOperationClient.cs create mode 100644 AsbCloudWebApi.IntegrationTests/Controllers/DetectedOperationControllerTests.cs diff --git a/AsbCloudWebApi.IntegrationTests/Clients/IDetectedOperationClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/IDetectedOperationClient.cs new file mode 100644 index 00000000..ecf37d37 --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Clients/IDetectedOperationClient.cs @@ -0,0 +1,23 @@ +using AsbCloudApp.Data; +using AsbCloudApp.Data.DetectedOperation; +using AsbCloudApp.Requests; +using Refit; + +namespace AsbCloudWebApi.IntegrationTests.Clients; + +public interface IDetectedOperationClient +{ + private const string BaseRoute = "/api/well/{idWell}/DetectedOperation"; + + [Post(BaseRoute)] + Task> InsertRangeAsync(int idWell, IEnumerable dtos); + + [Put(BaseRoute)] + Task> UpdateRangeAsync(int idWell, IEnumerable dtos); + + [Delete(BaseRoute)] + Task> DeleteRangeAsync(int idWell, [Body] IEnumerable ids); + + [Get(BaseRoute)] + Task>> GetAsync(int idWell, [Query] DetectedOperationRequest request); +} \ No newline at end of file diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/DetectedOperationControllerTests.cs b/AsbCloudWebApi.IntegrationTests/Controllers/DetectedOperationControllerTests.cs new file mode 100644 index 00000000..5f518884 --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Controllers/DetectedOperationControllerTests.cs @@ -0,0 +1,182 @@ +using System.Net; +using AsbCloudApp.Data.DetectedOperation; +using AsbCloudApp.Data.WellOperation; +using AsbCloudApp.Requests; +using AsbCloudDb.Model; +using AsbCloudWebApi.IntegrationTests.Clients; +using AsbCloudWebApi.IntegrationTests.Data; +using Mapster; +using Microsoft.EntityFrameworkCore; +using Xunit; + +namespace AsbCloudWebApi.IntegrationTests.Controllers; + +public class DetectedOperationControllerTests : BaseIntegrationTest +{ + private readonly IDetectedOperationClient client; + + private readonly DetectedOperationDto dto = new() + { + IdCategory = WellOperationCategory.IdRotor, + DateStart = new DateTimeOffset(new DateTime(2023, 5, 12, 1,0,0, DateTimeKind.Utc)), + DateEnd = new DateTimeOffset(new DateTime(2023, 5, 12, 1,0,0, DateTimeKind.Utc)), + DepthStart = 0, + DepthEnd = 80, + OperationCategory = new WellOperationCategoryDto + { + Id = WellOperationCategory.IdRotor, + IdParent = WellOperationCategory.IdDrilling, + Name = "Бурение ротором" + }, + EnabledSubsystems = new EnabledSubsystems + { + IsAutoRotor = true + }, + Value = 400, + }; + + public DetectedOperationControllerTests(WebAppFactoryFixture factory) + : base(factory) + { + client = factory.GetAuthorizedHttpClient(string.Empty); + + dbContext.CleanupDbSet(); + } + + [Fact] + public async Task InsertRangeAsync_returns_success() + { + //arrange + var well = dbContext.Wells.First(); + dto.IdTelemetry = well.IdTelemetry!.Value; + + //act + var response = await client.InsertRangeAsync(well.Id, new[] { dto }); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Equal(1, response.Content); + } + + [Fact] + public async Task UpdateRangeAsync_returns_success() + { + //arrange + var well = dbContext.Wells.First(); + dto.IdTelemetry = well.IdTelemetry!.Value; + + var entity = dto.Adapt(); + dbContext.DetectedOperations.Add(entity); + await dbContext.SaveChangesAsync(); + dto.Id = entity.Id; + + //act + var response = await client.UpdateRangeAsync(well.Id, new[] { dto }); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Equal(1, response.Content); + } + + [Fact] + public async Task UpdateRangeAsync_returns_bad_request_when_id_is_invalid() + { + //arrange + var well = dbContext.Wells.First(); + + //act + var response = await client.UpdateRangeAsync(well.Id, new[] { dto }); + + //assert + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + } + + + [Fact] + public async Task DeleteRangeAsync_returns_success() + { + //arrange + var well = dbContext.Wells.First(); + dto.IdTelemetry = well.IdTelemetry!.Value; + + var entity = dto.Adapt(); + dbContext.DetectedOperations.Add(entity); + await dbContext.SaveChangesAsync(); + + var ids = await dbContext.DetectedOperations.Select(d => d.Id).ToArrayAsync(); + + //act + var response = await client.DeleteRangeAsync(well.Id, ids); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Equal(1, response.Content); + } + + [Fact] + public async Task GetAsync_returns_first_page() + { + //arrange + const int pageSize = 10; + const int pageIndex = 0; + + var request = new DetectedOperationRequest + { + Skip = pageIndex, + Take = pageSize, + IdsCategories = new[] { dto.IdCategory } + }; + + var well = dbContext.Wells.First(); + dto.IdTelemetry = well.IdTelemetry!.Value; + + var entity = dto.Adapt(); + dbContext.DetectedOperations.Add(entity); + await dbContext.SaveChangesAsync(); + + //act + var response = await client.GetAsync(well.Id, request); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + + var totalExpected = response.Content.Count - pageSize * pageIndex; + + Assert.Equal(totalExpected, response.Content.Items.Count()); + } + + [Fact] + public async Task GetAsync_returns_first_operation() + { + //arrange + var well = dbContext.Wells.First(); + dto.IdTelemetry = well.IdTelemetry!.Value; + + var entity = dto.Adapt(); + dbContext.DetectedOperations.Add(entity); + await dbContext.SaveChangesAsync(); + + var request = new DetectedOperationRequest + { + IdsCategories = new[] { dto.IdCategory } + }; + + //act + var response = await client.GetAsync(well.Id, request); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + + var firstOperation = response.Content.Items.ElementAt(0); + + Assert.Equal(well.IdTelemetry, firstOperation.IdTelemetry); + Assert.Equal(dto.EnabledSubsystems, firstOperation.EnabledSubsystems); + Assert.Equal(dto.DateStart.ToOffset(TimeSpan.FromHours(Defaults.Timezone.Hours)), firstOperation.DateStart); + Assert.Equal(dto.DateEnd.ToOffset(TimeSpan.FromHours(Defaults.Timezone.Hours)), firstOperation.DateEnd); + } +} \ No newline at end of file