diff --git a/AsbCloudInfrastructure/Repository/CrudWellRelatedRepositoryBase.cs b/AsbCloudInfrastructure/Repository/CrudWellRelatedRepositoryBase.cs index d788f3d6..d6b267a8 100644 --- a/AsbCloudInfrastructure/Repository/CrudWellRelatedRepositoryBase.cs +++ b/AsbCloudInfrastructure/Repository/CrudWellRelatedRepositoryBase.cs @@ -43,5 +43,4 @@ namespace AsbCloudInfrastructure.Repository return dtos; } } - } diff --git a/AsbCloudWebApi.IntegrationTests/Clients/ICrudWellRelatedClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/ICrudWellRelatedClient.cs new file mode 100644 index 00000000..7f99ba5f --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Clients/ICrudWellRelatedClient.cs @@ -0,0 +1,29 @@ +using AsbCloudApp.Data; +using Refit; + +namespace AsbCloudWebApi.IntegrationTests.Clients; + +public interface ICrudWellRelatedClient + where TDto : IId, IWellRelated +{ + [Post("/")] + Task> InsertAsync([Body] TDto dto); + + [Post("/range")] + Task> InsertRangeAsync([Body] IEnumerable dtos); + + [Get("/")] + Task>> GetAllAsync(); + + [Get("/well/{idWell}")] + Task>> GetByIdWellAsync(int idWell); + + [Get("/{id}")] + Task> GetOrDefaultAsync(int id); + + [Put("/")] + Task> UpdateAsync([Body] TDto dto); + + [Delete("/{id}")] + Task> DeleteAsync(int id); +} diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTest.cs index 1d2a2c3c..331c7b7d 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/AdminDepositControllerTest.cs @@ -26,7 +26,7 @@ public class AdminDepositControllerTest : BaseIntegrationTest public AdminDepositControllerTest(WebAppFactoryFixture factory) : base(factory) { - client = factory.GetAuthorizedHttpClient(); + client = factory.GetAuthorizedHttpClient(string.Empty); dbContext.CleanupDbSet(); } diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs index f06e2133..f04ba9ca 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlan/ProcessMapPlanDrillingControllerTest.cs @@ -82,7 +82,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest public ProcessMapPlanDrillingControllerTest(WebAppFactoryFixture factory) : base(factory) { dbContext.CleanupDbSet(); - client = factory.GetAuthorizedHttpClient(); + client = factory.GetAuthorizedHttpClient(string.Empty); } [Fact] diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/ScheduleControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/ScheduleControllerTest.cs new file mode 100644 index 00000000..ebc52258 --- /dev/null +++ b/AsbCloudWebApi.IntegrationTests/Controllers/ScheduleControllerTest.cs @@ -0,0 +1,159 @@ +using AsbCloudApp.Data; +using AsbCloudDb.Model; +using AsbCloudInfrastructure; +using AsbCloudWebApi.IntegrationTests.Clients; +using Mapster; +using Microsoft.EntityFrameworkCore; +using System.Net; +using Xunit; + +namespace AsbCloudWebApi.IntegrationTests.Controllers +{ + public abstract class CrudWellRelatedClient : BaseIntegrationTest + where TDto : AsbCloudApp.Data.IId, AsbCloudApp.Data.IWellRelated + where TEntity : class, AsbCloudDb.Model.IId, AsbCloudDb.Model.IWellRelated + { + public abstract IEnumerable ValidDtos { get; } + public abstract IEnumerable InvalidDtos { get; } + protected List ExcludeProps { get; set; } = new() { "Id" }; + + protected ICrudWellRelatedClient client; + + public CrudWellRelatedClient(WebAppFactoryFixture factory, string uriSuffix) + : base(factory) + { + client = factory.GetAuthorizedHttpClient>(uriSuffix); + } + + protected async Task> GetCleanDbSet() + { + var dbset = dbContext.Set(); + dbset.RemoveRange(dbset); + await dbContext.SaveChangesAsync(CancellationToken.None); + return dbset; + } + + [Fact] + public async Task Insert_returns_success() + { + foreach (var validDto in ValidDtos) + await _Insert_returns_success(validDto); + } + + private async Task _Insert_returns_success(TDto validDto) + { + var dbset = await GetCleanDbSet(); + + //act + var response = await client.InsertAsync(validDto); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.True(response.Content > 0); + + var entity = dbset.First(); + var fromDbDto = Convert(entity); + MatchHelper.Match(validDto, fromDbDto, ExcludeProps); + } + + [Fact] + public async Task Insert_fails() + { + foreach (var inValidDto in InvalidDtos) + await _Insert_fails(inValidDto); + } + + private async Task _Insert_fails(TDto invalidDto) + { + var dbset = await GetCleanDbSet(); + //act + var response = await client.InsertAsync(invalidDto); + + //assert + Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); + } + + protected virtual TDto Convert(TEntity entity) + { + var dto = entity.Adapt(); + return dto; + } + } + + public class ScheduleControllerTest : CrudWellRelatedClient + { + static DrillerDto driller = new (){ + Id = 2 , + Name = "Name" , + Patronymic = "Patronymic", + Surname = "Surname", + }; + static Well well = Data.Defaults.Wells.First(); + + public override IEnumerable ValidDtos { get; } = new ScheduleDto[] + { + new() { + Id = 1, + IdWell = well.Id, + Driller = driller, + IdDriller = driller.Id, + DrillStart = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Unspecified), + DrillEnd = new DateTime(2024, 1, 2, 0, 0, 0, DateTimeKind.Unspecified), + ShiftStart = new TimeDto(8, 0, 0), + ShiftEnd = new TimeDto(20, 0, 0), + }, + new() { + Id = 1, + IdWell = well.Id, + Driller = driller, + IdDriller = driller.Id, + DrillStart = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Unspecified), + DrillEnd = new DateTime(2024, 1, 2, 0, 0, 0, DateTimeKind.Unspecified), + ShiftStart = new TimeDto(20, 0, 0), + ShiftEnd = new TimeDto(8, 0, 0), + } + }; + + public override IEnumerable InvalidDtos { get; } = new ScheduleDto[] + { + new() { + Id = 1, + IdWell = well.Id, + Driller = driller, + IdDriller = driller.Id, + DrillStart = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Unspecified), + DrillEnd = new DateTime(2022, 1, 2, 0, 0, 0, DateTimeKind.Unspecified), + ShiftStart = new TimeDto(8, 0, 0), + ShiftEnd = new TimeDto(20, 0, 0), + }, + new() { + Id = 1, + IdWell = well.Id, + Driller = driller, + IdDriller = int.MaxValue - 1, + DrillStart = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Unspecified), + DrillEnd = new DateTime(2024, 1, 2, 0, 0, 0, DateTimeKind.Unspecified), + ShiftStart = new TimeDto(8, 0, 0), + ShiftEnd = new TimeDto(20, 0, 0), + } + }; + + public ScheduleControllerTest(WebAppFactoryFixture factory) + : base(factory, "api/schedule") + { + var dbDriller = driller.Adapt(); + dbContext.Set().Add(dbDriller); + dbContext.SaveChanges(); + + ExcludeProps.Add(nameof(ScheduleDto.Driller)); + } + + protected override ScheduleDto Convert(Schedule entity) + { + var dto = base.Convert(entity); + dto.DrillStart = entity.DrillStart.ToRemoteDateTime(well.Timezone.Hours); + dto.DrillEnd = entity.DrillEnd.ToRemoteDateTime(well.Timezone.Hours); + return dto; + } + } +} diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/SlipsStatControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/SlipsStatControllerTest.cs index 908d5b95..57eacadf 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/SlipsStatControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/SlipsStatControllerTest.cs @@ -66,7 +66,7 @@ public class SlipsStatControllerTest : BaseIntegrationTest wellOperations.Add(factWellOperation); dbContext.SaveChanges(); - client = factory.GetAuthorizedHttpClient(); + client = factory.GetAuthorizedHttpClient(string.Empty); } [Fact] diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs index a47d5f09..459e6b52 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/WellOperationControllerTest.cs @@ -44,7 +44,7 @@ public class WellOperationControllerTest : BaseIntegrationTest public WellOperationControllerTest(WebAppFactoryFixture factory) : base(factory) { - client = factory.GetAuthorizedHttpClient(); + client = factory.GetAuthorizedHttpClient(string.Empty); } /// diff --git a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs index aa8bd27e..44bd4b67 100644 --- a/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs +++ b/AsbCloudWebApi.IntegrationTests/WebAppFactoryFixture.cs @@ -83,10 +83,12 @@ public class WebAppFactoryFixture : WebApplicationFactory, return httpClient; } - public T GetAuthorizedHttpClient() + public T GetAuthorizedHttpClient(string uriSuffix) { var httpClient = GetAuthorizedHttpClient(); + if (!string.IsNullOrEmpty(uriSuffix)) + if(httpClient.BaseAddress is not null) + httpClient.BaseAddress = new Uri(httpClient.BaseAddress, uriSuffix); return RestService.For(httpClient, refitSettings); } - } \ No newline at end of file