forked from ddrilling/AsbCloudServer
ScheduleDto Add IValidatableObject
This commit is contained in:
parent
e82720b421
commit
a9dc67f83f
@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace AsbCloudApp.Data
|
||||
@ -6,7 +7,7 @@ namespace AsbCloudApp.Data
|
||||
/// <summary>
|
||||
/// Описание данных графика работ
|
||||
/// </summary>
|
||||
public class ScheduleDto : IId, IWellRelated
|
||||
public class ScheduleDto : IId, IWellRelated, IValidatableObject
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
[Required]
|
||||
@ -50,5 +51,12 @@ namespace AsbCloudApp.Data
|
||||
/// Бурильщик
|
||||
/// </summary>
|
||||
public DrillerDto? Driller { get; set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
||||
{
|
||||
if(DrillStart >= DrillEnd)
|
||||
yield return new ValidationResult($"DrillStart > DrillEnd");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ using AsbCloudInfrastructure;
|
||||
using AsbCloudWebApi.IntegrationTests.Clients;
|
||||
using Mapster;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System.Net;
|
||||
using Xunit;
|
||||
|
||||
@ -15,9 +16,10 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
{
|
||||
public abstract IEnumerable<TDto> ValidDtos { get; }
|
||||
public abstract IEnumerable<TDto> InvalidDtos { get; }
|
||||
public abstract IEnumerable<TDto> ForbiddenDtos { get; }
|
||||
protected List<string> ExcludeProps { get; set; } = new() { "Id" };
|
||||
|
||||
protected ICrudWellRelatedClient<TDto> client;
|
||||
protected ICrudWellRelatedClient<TDto> client;
|
||||
|
||||
public CrudWellRelatedClient(WebAppFactoryFixture factory, string uriSuffix)
|
||||
: base(factory)
|
||||
@ -34,13 +36,13 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Insert_returns_success()
|
||||
public async Task Insert_returns_success_for_validDtos()
|
||||
{
|
||||
foreach (var validDto in ValidDtos)
|
||||
await _Insert_returns_success(validDto);
|
||||
await Insert_returns_success(validDto);
|
||||
}
|
||||
|
||||
private async Task _Insert_returns_success(TDto validDto)
|
||||
private async Task Insert_returns_success(TDto validDto)
|
||||
{
|
||||
var dbset = await GetCleanDbSet();
|
||||
|
||||
@ -57,15 +59,16 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Insert_fails()
|
||||
public async Task Insert_returns_badRequest_for_invalidDtos()
|
||||
{
|
||||
foreach (var inValidDto in InvalidDtos)
|
||||
await _Insert_fails(inValidDto);
|
||||
await Insert_returns_badRequest(inValidDto);
|
||||
}
|
||||
|
||||
private async Task _Insert_fails(TDto invalidDto)
|
||||
private async Task Insert_returns_badRequest(TDto invalidDto)
|
||||
{
|
||||
var dbset = await GetCleanDbSet();
|
||||
await GetCleanDbSet();
|
||||
|
||||
//act
|
||||
var response = await client.InsertAsync(invalidDto);
|
||||
|
||||
@ -73,21 +76,73 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Insert_returns_forbidden_for_forbiddenDtos()
|
||||
{
|
||||
foreach (var forbiddenDto in ForbiddenDtos)
|
||||
await Insert_returns_forbidden(forbiddenDto);
|
||||
}
|
||||
|
||||
private async Task Insert_returns_forbidden(TDto forbiddenDto)
|
||||
{
|
||||
await GetCleanDbSet();
|
||||
|
||||
//act
|
||||
var response = await client.InsertAsync(forbiddenDto);
|
||||
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync_returns_data()
|
||||
{
|
||||
//arrange
|
||||
var dbset = await GetCleanDbSet();
|
||||
var entries = new List<(EntityEntry<TEntity>, TDto)>();
|
||||
|
||||
foreach (var dto in ValidDtos)
|
||||
{
|
||||
var entity = Convert(dto);
|
||||
entity.Id = 0;
|
||||
var entry = dbset.Add(entity);
|
||||
entries.Add((entry, dto));
|
||||
}
|
||||
dbContext.SaveChanges();
|
||||
|
||||
//act
|
||||
var response = await client.GetAllAsync();
|
||||
|
||||
//assert
|
||||
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
|
||||
Assert.NotNull(response.Content);
|
||||
Assert.Equal(entries.Count, response.Content.Count());
|
||||
|
||||
foreach (var item in response.Content)
|
||||
{
|
||||
var entry = entries.First(e => e.Item1.Entity.Id == item.Id);
|
||||
MatchHelper.Match(entry.Item2, item, ExcludeProps);
|
||||
}
|
||||
}
|
||||
|
||||
protected virtual TDto Convert(TEntity entity)
|
||||
{
|
||||
var dto = entity.Adapt<TDto>();
|
||||
return dto;
|
||||
}
|
||||
|
||||
protected virtual TEntity Convert(TDto dto)
|
||||
{
|
||||
var entity = dto.Adapt<TEntity>();
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public class ScheduleControllerTest : CrudWellRelatedClient<ScheduleDto, Schedule>
|
||||
{
|
||||
static DrillerDto driller = new (){
|
||||
Id = 2 ,
|
||||
Name = "Name" ,
|
||||
Patronymic = "Patronymic",
|
||||
Surname = "Surname",
|
||||
};
|
||||
static Driller driller = Data.Defaults.Drillers.First();
|
||||
static DrillerDto drillerDto = driller.Adapt<DrillerDto>();
|
||||
|
||||
static Well well = Data.Defaults.Wells.First();
|
||||
|
||||
public override IEnumerable<ScheduleDto> ValidDtos { get; } = new ScheduleDto[]
|
||||
@ -95,7 +150,7 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
new() {
|
||||
Id = 1,
|
||||
IdWell = well.Id,
|
||||
Driller = driller,
|
||||
Driller = drillerDto,
|
||||
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),
|
||||
@ -105,7 +160,7 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
new() {
|
||||
Id = 1,
|
||||
IdWell = well.Id,
|
||||
Driller = driller,
|
||||
Driller = drillerDto,
|
||||
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),
|
||||
@ -117,9 +172,8 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
public override IEnumerable<ScheduleDto> InvalidDtos { get; } = new ScheduleDto[]
|
||||
{
|
||||
new() {
|
||||
Id = 1,
|
||||
IdWell = well.Id,
|
||||
Driller = driller,
|
||||
Driller = drillerDto,
|
||||
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),
|
||||
@ -127,9 +181,8 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
ShiftEnd = new TimeDto(20, 0, 0),
|
||||
},
|
||||
new() {
|
||||
Id = 1,
|
||||
IdWell = well.Id,
|
||||
Driller = driller,
|
||||
Driller = drillerDto,
|
||||
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),
|
||||
@ -138,13 +191,21 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
}
|
||||
};
|
||||
|
||||
public override IEnumerable<ScheduleDto> ForbiddenDtos { get; } = new ScheduleDto[] {
|
||||
new() {
|
||||
IdWell = int.MaxValue - 1,
|
||||
Driller = drillerDto,
|
||||
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),
|
||||
}
|
||||
};
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
@ -155,5 +216,13 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers
|
||||
dto.DrillEnd = entity.DrillEnd.ToRemoteDateTime(well.Timezone.Hours);
|
||||
return dto;
|
||||
}
|
||||
|
||||
protected override Schedule Convert(ScheduleDto dto)
|
||||
{
|
||||
var entity = base.Convert(dto);
|
||||
entity.DrillStart = dto.DrillStart.FromTimeZoneOffsetHours(well.Timezone.Hours);
|
||||
entity.DrillEnd = dto.DrillEnd.FromTimeZoneOffsetHours(well.Timezone.Hours);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,8 +10,16 @@ namespace AsbCloudWebApi.IntegrationTests.Data
|
||||
new()
|
||||
{
|
||||
Id = 1,
|
||||
Name = "test",
|
||||
Surname = "test"
|
||||
Name = "test1",
|
||||
Surname = "test1",
|
||||
Patronymic = "test1"
|
||||
},
|
||||
new()
|
||||
{
|
||||
Id = 2,
|
||||
Name = "test2",
|
||||
Surname = "test2",
|
||||
Patronymic = "test2"
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user