From a9dc67f83fd123f11f08461641796939719a1688 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Tue, 27 Feb 2024 13:44:14 +0500 Subject: [PATCH] ScheduleDto Add IValidatableObject --- AsbCloudApp/Data/ScheduleDto.cs | 10 +- .../Controllers/ScheduleControllerTest.cs | 117 ++++++++++++++---- .../Data/Defaults.cs | 12 +- 3 files changed, 112 insertions(+), 27 deletions(-) diff --git a/AsbCloudApp/Data/ScheduleDto.cs b/AsbCloudApp/Data/ScheduleDto.cs index ea6d54b0..84cbe515 100644 --- a/AsbCloudApp/Data/ScheduleDto.cs +++ b/AsbCloudApp/Data/ScheduleDto.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace AsbCloudApp.Data @@ -6,7 +7,7 @@ namespace AsbCloudApp.Data /// /// Описание данных графика работ /// - public class ScheduleDto : IId, IWellRelated + public class ScheduleDto : IId, IWellRelated, IValidatableObject { /// [Required] @@ -50,5 +51,12 @@ namespace AsbCloudApp.Data /// Бурильщик /// public DrillerDto? Driller { get; set; } + + /// + public IEnumerable Validate(ValidationContext validationContext) + { + if(DrillStart >= DrillEnd) + yield return new ValidationResult($"DrillStart > DrillEnd"); + } } } diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/ScheduleControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/ScheduleControllerTest.cs index ebc52258..a58e20f1 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/ScheduleControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/ScheduleControllerTest.cs @@ -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 ValidDtos { get; } public abstract IEnumerable InvalidDtos { get; } + public abstract IEnumerable ForbiddenDtos { get; } protected List ExcludeProps { get; set; } = new() { "Id" }; - protected ICrudWellRelatedClient client; + protected ICrudWellRelatedClient 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, 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(); return dto; } + + protected virtual TEntity Convert(TDto dto) + { + var entity = dto.Adapt(); + return entity; + } } public class ScheduleControllerTest : CrudWellRelatedClient { - static DrillerDto driller = new (){ - Id = 2 , - Name = "Name" , - Patronymic = "Patronymic", - Surname = "Surname", - }; + static Driller driller = Data.Defaults.Drillers.First(); + static DrillerDto drillerDto = driller.Adapt(); + static Well well = Data.Defaults.Wells.First(); public override IEnumerable 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 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 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(); - dbContext.Set().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; + } } } diff --git a/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs b/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs index 45219449..4142705c 100644 --- a/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs +++ b/AsbCloudWebApi.IntegrationTests/Data/Defaults.cs @@ -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" } };