Add abstract class CrudWellRelatedClient

This commit is contained in:
ngfrolov 2024-02-27 09:32:08 +05:00
parent 9362c2c1bd
commit e82720b421
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
8 changed files with 196 additions and 7 deletions

View File

@ -43,5 +43,4 @@ namespace AsbCloudInfrastructure.Repository
return dtos;
}
}
}

View File

@ -0,0 +1,29 @@
using AsbCloudApp.Data;
using Refit;
namespace AsbCloudWebApi.IntegrationTests.Clients;
public interface ICrudWellRelatedClient<TDto>
where TDto : IId, IWellRelated
{
[Post("/")]
Task<IApiResponse<int>> InsertAsync([Body] TDto dto);
[Post("/range")]
Task<IApiResponse<int>> InsertRangeAsync([Body] IEnumerable<TDto> dtos);
[Get("/")]
Task<IApiResponse<IEnumerable<TDto>>> GetAllAsync();
[Get("/well/{idWell}")]
Task<IApiResponse<IEnumerable<TDto>>> GetByIdWellAsync(int idWell);
[Get("/{id}")]
Task<IApiResponse<TDto>> GetOrDefaultAsync(int id);
[Put("/")]
Task<IApiResponse<int>> UpdateAsync([Body] TDto dto);
[Delete("/{id}")]
Task<IApiResponse<int>> DeleteAsync(int id);
}

View File

@ -26,7 +26,7 @@ public class AdminDepositControllerTest : BaseIntegrationTest
public AdminDepositControllerTest(WebAppFactoryFixture factory)
: base(factory)
{
client = factory.GetAuthorizedHttpClient<IAdminDepositClient>();
client = factory.GetAuthorizedHttpClient<IAdminDepositClient>(string.Empty);
dbContext.CleanupDbSet<Deposit>();
}

View File

@ -82,7 +82,7 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest
public ProcessMapPlanDrillingControllerTest(WebAppFactoryFixture factory) : base(factory)
{
dbContext.CleanupDbSet<ProcessMapPlanDrilling>();
client = factory.GetAuthorizedHttpClient<IProcessMapPlanDrillingClient>();
client = factory.GetAuthorizedHttpClient<IProcessMapPlanDrillingClient>(string.Empty);
}
[Fact]

View File

@ -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<TDto, TEntity> : BaseIntegrationTest
where TDto : AsbCloudApp.Data.IId, AsbCloudApp.Data.IWellRelated
where TEntity : class, AsbCloudDb.Model.IId, AsbCloudDb.Model.IWellRelated
{
public abstract IEnumerable<TDto> ValidDtos { get; }
public abstract IEnumerable<TDto> InvalidDtos { get; }
protected List<string> ExcludeProps { get; set; } = new() { "Id" };
protected ICrudWellRelatedClient<TDto> client;
public CrudWellRelatedClient(WebAppFactoryFixture factory, string uriSuffix)
: base(factory)
{
client = factory.GetAuthorizedHttpClient<ICrudWellRelatedClient<TDto>>(uriSuffix);
}
protected async Task<DbSet<TEntity>> GetCleanDbSet()
{
var dbset = dbContext.Set<TEntity>();
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<TDto>();
return dto;
}
}
public class ScheduleControllerTest : CrudWellRelatedClient<ScheduleDto, Schedule>
{
static DrillerDto driller = new (){
Id = 2 ,
Name = "Name" ,
Patronymic = "Patronymic",
Surname = "Surname",
};
static Well well = Data.Defaults.Wells.First();
public override IEnumerable<ScheduleDto> 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<ScheduleDto> 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<Driller>();
dbContext.Set<Driller>().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;
}
}
}

View File

@ -66,7 +66,7 @@ public class SlipsStatControllerTest : BaseIntegrationTest
wellOperations.Add(factWellOperation);
dbContext.SaveChanges();
client = factory.GetAuthorizedHttpClient<ISlipsTimeClient>();
client = factory.GetAuthorizedHttpClient<ISlipsTimeClient>(string.Empty);
}
[Fact]

View File

@ -44,7 +44,7 @@ public class WellOperationControllerTest : BaseIntegrationTest
public WellOperationControllerTest(WebAppFactoryFixture factory)
: base(factory)
{
client = factory.GetAuthorizedHttpClient<IWellOperationClient>();
client = factory.GetAuthorizedHttpClient<IWellOperationClient>(string.Empty);
}
/// <summary>

View File

@ -83,10 +83,12 @@ public class WebAppFactoryFixture : WebApplicationFactory<Startup>,
return httpClient;
}
public T GetAuthorizedHttpClient<T>()
public T GetAuthorizedHttpClient<T>(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<T>(httpClient, refitSettings);
}
}