From 7eedf62419917738c6358323def886eac321b0a9 Mon Sep 17 00:00:00 2001 From: Frolov-Nikita Date: Sat, 20 Jan 2024 15:38:37 +0500 Subject: [PATCH] fix integration test --- .../Repositories/IChangeLogRepository.cs | 6 +- .../IProcessMapPlanBaseRepository.cs | 8 +- .../ProcessMapPlanBaseRepository.cs | 18 +- .../Clients/IProcessMapPlanDrillingClient.cs | 12 +- .../ProcessMapPlanDrillingControllerTest.cs | 430 +++++++++++++++++- .../ProcessMapPlanBaseController.cs | 44 +- 6 files changed, 466 insertions(+), 52 deletions(-) diff --git a/AsbCloudApp/Repositories/IChangeLogRepository.cs b/AsbCloudApp/Repositories/IChangeLogRepository.cs index d748f5f5..9a5ab8c9 100644 --- a/AsbCloudApp/Repositories/IChangeLogRepository.cs +++ b/AsbCloudApp/Repositories/IChangeLogRepository.cs @@ -18,7 +18,7 @@ public interface IChangeLogRepository /// /// /// - Task InsertRangeAsync(int idUser, IEnumerable dtos, CancellationToken token); + Task InsertRange(int idUser, IEnumerable dtos, CancellationToken token); /// /// Редактирование записей @@ -27,7 +27,7 @@ public interface IChangeLogRepository /// /// /// - Task UpdateRangeAsync(int idUser, IEnumerable dtos, CancellationToken token); + Task UpdateRange(int idUser, IEnumerable dtos, CancellationToken token); /// /// Удаление записей @@ -36,6 +36,6 @@ public interface IChangeLogRepository /// /// /// - Task DeleteRangeAsync(int idUser, IEnumerable ids, CancellationToken token); + Task DeleteRange(int idUser, IEnumerable ids, CancellationToken token); } diff --git a/AsbCloudApp/Repositories/IProcessMapPlanBaseRepository.cs b/AsbCloudApp/Repositories/IProcessMapPlanBaseRepository.cs index 631131b9..cc8f2367 100644 --- a/AsbCloudApp/Repositories/IProcessMapPlanBaseRepository.cs +++ b/AsbCloudApp/Repositories/IProcessMapPlanBaseRepository.cs @@ -22,7 +22,7 @@ public interface IProcessMapPlanBaseRepository: IChangeLogRepository /// /// /// - Task ClearAndInsertRangeAsync(int idUser, int idWell, IEnumerable dtos, CancellationToken token); + Task ClearAndInsertRange(int idUser, int idWell, IEnumerable dtos, CancellationToken token); /// /// Получение дат изменений записей @@ -30,7 +30,7 @@ public interface IProcessMapPlanBaseRepository: IChangeLogRepository /// /// /// - Task> GetDatesChangeAsync(int idWell, CancellationToken token); + Task> GetDatesChange(int idWell, CancellationToken token); /// /// Получение журнала изменений @@ -39,7 +39,7 @@ public interface IProcessMapPlanBaseRepository: IChangeLogRepository /// /// /// - Task> GetChangeLogAsync(int idWell, DateOnly? date, CancellationToken token); + Task> GetChangeLog(int idWell, DateOnly? date, CancellationToken token); /// /// Получение записей по параметрам @@ -47,5 +47,5 @@ public interface IProcessMapPlanBaseRepository: IChangeLogRepository /// /// /// - Task> GetAsync(ProcessMapPlanBaseRequest request, CancellationToken token); + Task> Get(ProcessMapPlanBaseRequest request, CancellationToken token); } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Repository/ProcessMapPlanBaseRepository.cs b/AsbCloudInfrastructure/Repository/ProcessMapPlanBaseRepository.cs index 54e304b5..e6dcda12 100644 --- a/AsbCloudInfrastructure/Repository/ProcessMapPlanBaseRepository.cs +++ b/AsbCloudInfrastructure/Repository/ProcessMapPlanBaseRepository.cs @@ -28,7 +28,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe this.wellService = wellService; } - public async Task InsertRangeAsync(int idUser, IEnumerable dtos, CancellationToken token) + public async Task InsertRange(int idUser, IEnumerable dtos, CancellationToken token) { var result = 0; if (dtos.Any()) @@ -53,7 +53,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe return result; } - public async Task ClearAndInsertRangeAsync(int idUser, int idWell, IEnumerable dtos, CancellationToken token) + public async Task ClearAndInsertRange(int idUser, int idWell, IEnumerable dtos, CancellationToken token) { if (dtos.Any(d => d.IdWell != idWell)) throw new ArgumentInvalidException(nameof(dtos), $"Все записи должны относиться к скважине idWell = {idWell}"); @@ -74,7 +74,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe entity.IdEditor = idUser; } result += await context.SaveChangesAsync(token); - result += await InsertRangeAsync(idUser, dtos, token); + result += await InsertRange(idUser, dtos, token); await transaction.CommitAsync(token); } catch @@ -86,7 +86,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe return result; } - public async Task DeleteRangeAsync(int idUser, IEnumerable ids, CancellationToken token) + public async Task DeleteRange(int idUser, IEnumerable ids, CancellationToken token) { var dbSet = context.Set(); var entitiesToMarkDeleted = dbSet @@ -103,7 +103,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe return result; } - public async Task> GetAsync(ProcessMapPlanBaseRequest request, CancellationToken token) + public async Task> Get(ProcessMapPlanBaseRequest request, CancellationToken token) { var timezone = wellService.GetTimezone(request.IdWell); var offset = TimeSpan.FromHours(timezone.Hours); @@ -118,7 +118,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe if (request.UpdateFrom.HasValue) { var from = request.UpdateFrom.Value.ToUniversalTime(); - query = query.Where(e => e.Creation > from || e.Obsolete > from); + query = query.Where(e => e.Creation >= from || e.Obsolete >= from); } if (request.Moment.HasValue) @@ -134,7 +134,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe return dtos; } - public async Task> GetChangeLogAsync(int idWell, DateOnly? date, CancellationToken token) + public async Task> GetChangeLog(int idWell, DateOnly? date, CancellationToken token) { var query = context .Set() @@ -160,7 +160,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe return dtos; } - public async Task> GetDatesChangeAsync(int idWell, CancellationToken token) + public async Task> GetDatesChange(int idWell, CancellationToken token) { var wellEntitiesQuery = context .Set() @@ -191,7 +191,7 @@ public class ProcessMapPlanBaseRepository : IProcessMapPlanBaseRe return datesOnly; } - public async Task UpdateRangeAsync(int idUser, IEnumerable dtos, CancellationToken token) + public async Task UpdateRange(int idUser, IEnumerable dtos, CancellationToken token) { if (dtos.Any(d => d.Id == 0)) throw new ArgumentInvalidException(nameof(dtos), "Отредактированные значения должны иметь id больше 0"); diff --git a/AsbCloudWebApi.IntegrationTests/Clients/IProcessMapPlanDrillingClient.cs b/AsbCloudWebApi.IntegrationTests/Clients/IProcessMapPlanDrillingClient.cs index f1eb677e..45dfd59a 100644 --- a/AsbCloudWebApi.IntegrationTests/Clients/IProcessMapPlanDrillingClient.cs +++ b/AsbCloudWebApi.IntegrationTests/Clients/IProcessMapPlanDrillingClient.cs @@ -10,22 +10,22 @@ public interface IProcessMapPlanDrillingClient private const string BaseRoute = "/api/well/{idWell}/ProcessMapPlanDrilling"; [Post(BaseRoute)] - Task> InsertRangeAsync(int idWell, IEnumerable dtos); + Task> InsertRange(int idWell, [Body] IEnumerable dtos); [Post($"{BaseRoute}/replace")] - Task> ClearAndInsertRangeAsync(int idWell, IEnumerable dtos); + Task> ClearAndInsertRange(int idWell, [Body] IEnumerable dtos); [Delete(BaseRoute)] - Task> DeleteRangeAsync(int idWell, IEnumerable ids); + Task> DeleteRange(int idWell, [Body] IEnumerable ids); [Get(BaseRoute)] - Task>> GetAsync(int idWell, [FromQuery] ProcessMapPlanBaseRequest request); + Task>> Get(int idWell, ProcessMapPlanBaseRequest request); [Get($"{BaseRoute}/changeLog")] - Task>> GetChangeLogAsync(int idWell, [FromQuery] DateOnly? date); + Task>> GetChangeLog(int idWell, DateOnly? date); [Get($"{BaseRoute}/dates")] - Task>> GetDatesChangeAsync(int idWell); + Task>> GetDatesChange(int idWell); [Put(BaseRoute)] Task> UpdateRangeAsync(int idWell, IEnumerable dtos); diff --git a/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlanDrillingControllerTest.cs b/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlanDrillingControllerTest.cs index 4994b2c6..d3916869 100644 --- a/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlanDrillingControllerTest.cs +++ b/AsbCloudWebApi.IntegrationTests/Controllers/ProcessMapPlanDrillingControllerTest.cs @@ -1,4 +1,6 @@ using AsbCloudApp.Data.ProcessMapPlan; +using AsbCloudApp.Requests; +using AsbCloudDb.Model.ProcessMapPlan; using AsbCloudDb.Model.ProcessMaps; using AsbCloudWebApi.IntegrationTests.Clients; using Mapster; @@ -11,8 +13,7 @@ namespace AsbCloudWebApi.IntegrationTests.Controllers; public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest { private IProcessMapPlanDrillingClient client; - - readonly ProcessMapPlanDrillingDto dto = new (){ + private readonly ProcessMapPlanDrillingDto dto = new (){ Id = 0, IdAuthor = 0, IdEditor = null, @@ -42,6 +43,37 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest UsageSpin = 14, Comment = "это тестовая запись", }; + private readonly ProcessMapPlanDrilling entity = new () + { + Id = 0, + IdAuthor = 1, + IdEditor = null, + Creation = DateTimeOffset.UtcNow, + Obsolete = null, + IdState = AsbCloudDb.Model.ChangeLogAbstract.IdStateActual, + IdPrevious = null, + + IdWell = 1, + IdWellSectionType = 1, + DepthStart = 0.5, + DepthEnd = 1.5, + + IdMode = 1, + AxialLoadPlan = 2.718281, + AxialLoadLimitMax = 3.1415926, + DeltaPressurePlan = 4, + DeltaPressureLimitMax = 5, + TopDriveTorquePlan = 6, + TopDriveTorqueLimitMax = 7, + TopDriveSpeedPlan = 8, + TopDriveSpeedLimitMax = 9, + FlowPlan = 10, + FlowLimitMax = 11, + RopPlan = 12, + UsageSaub = 13, + UsageSpin = 14, + Comment = "это тестовая запись", + }; public ProcessMapPlanDrillingControllerTest(WebAppFactoryFixture factory) : base(factory) { @@ -49,29 +81,411 @@ public class ProcessMapPlanDrillingControllerTest: BaseIntegrationTest } [Fact] - public async Task InsertRangeAsync_ReturnsSuccess_WhenNewItemIsValid() + public async Task InsertRange_returns_success() { //arrange - var expected = dto; + var dbset = dbContext.Set(); + var expected = dto.Adapt(); //act - var response = await client.InsertRangeAsync(dto.IdWell, new[] { expected }); - + var response = await client.InsertRange(dto.IdWell, new[] { expected }); + //assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(1, response.Content); - var entity = await dbContext.Set() + var entity = dbContext.Set() .Where(p => p.AxialLoadPlan == dto.AxialLoadPlan) .Where(p => p.AxialLoadLimitMax == dto.AxialLoadLimitMax) .Where(p => p.Comment == dto.Comment) .Where(p => p.IdWell == dto.IdWell) - .FirstOrDefaultAsync(); + .FirstOrDefault(); Assert.NotNull(entity); var actual = entity.Adapt(); + Assert.Equal(ProcessMapPlanBase.IdStateActual, actual.IdState); + var excludeProps = new[] { + nameof(ProcessMapPlanDrillingDto.Id), + nameof(ProcessMapPlanDrillingDto.IdState), + nameof(ProcessMapPlanDrillingDto.IdAuthor), + nameof(ProcessMapPlanDrillingDto.Creation), + }; + MatchHelper.Match(expected, actual, excludeProps); + } + + [Fact] + public async Task ClearAndInsertRange_returns_success() + { + // arrange + var dbset = dbContext.Set(); + var startTime = DateTimeOffset.UtcNow; + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + var entry = dbset.Add(entity); + dbContext.SaveChanges(); + entry.State = EntityState.Detached; + + // act + var result = await client.ClearAndInsertRange(entity.IdWell, new ProcessMapPlanDrillingDto[] { dto }); + + // assert + var doneTime = DateTimeOffset.UtcNow; + Assert.Equal(HttpStatusCode.OK, result.StatusCode); + Assert.Equal(2, result.Content); + + var count = dbset.Count(); + Assert.Equal(2, count); + + var oldEntity = dbset.First(p => p.Id == entry.Entity.Id); + Assert.Equal(ProcessMapPlanBase.IdClearedOnImport, oldEntity.IdState); + Assert.Equal(1, oldEntity.IdEditor); + Assert.NotNull(oldEntity.Obsolete); + Assert.InRange(oldEntity.Obsolete.Value, startTime, doneTime); + + var newEntity = dbset.First(p => p.Id != entry.Entity.Id); + Assert.Equal(ProcessMapPlanBase.IdStateActual, newEntity.IdState); + Assert.Equal(1, newEntity.IdAuthor); + Assert.Null(newEntity.IdEditor); + Assert.Null(newEntity.Obsolete); + Assert.Null(newEntity.IdPrevious); + Assert.InRange(newEntity.Creation, startTime, doneTime); + } + + [Fact] + public async Task UpdateRange_returns_success() + { + // arrange + var dbset = dbContext.Set(); + var startTime = DateTimeOffset.UtcNow; + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + var entry = dbset.Add(entity); + dbContext.SaveChanges(); + entry.State = EntityState.Detached; + + var dtoCopy = dto.Adapt(); + dtoCopy.Id = entry.Entity.Id; + dtoCopy.Comment = "nebuchadnezzar"; + dtoCopy.DeltaPressureLimitMax ++; + dtoCopy.DeltaPressurePlan ++; + dtoCopy.FlowPlan ++; + dtoCopy.FlowLimitMax ++; + dtoCopy.RopPlan ++; + dtoCopy.AxialLoadPlan ++; + dtoCopy.AxialLoadLimitMax ++; + dtoCopy.DepthStart ++; + dtoCopy.DepthEnd ++; + dtoCopy.TopDriveSpeedPlan ++; + dtoCopy.TopDriveSpeedLimitMax ++; + dtoCopy.TopDriveTorquePlan ++; + dtoCopy.TopDriveTorqueLimitMax ++; + + // act + var result = await client.UpdateRangeAsync(entity.IdWell, new ProcessMapPlanDrillingDto[] { dtoCopy }); + + // assert + var doneTime = DateTimeOffset.UtcNow; + Assert.Equal(HttpStatusCode.OK, result.StatusCode); + Assert.Equal(2, result.Content); + + var count = dbset.Count(); + Assert.Equal(2, count); + + var oldEntity = dbset.First(p => p.Id == entry.Entity.Id); + Assert.Equal(ProcessMapPlanBase.IdStateReplaced, oldEntity.IdState); + Assert.Equal(1, oldEntity.IdEditor); + Assert.NotNull(oldEntity.Obsolete); + Assert.InRange(oldEntity.Obsolete.Value, startTime, doneTime); + + var newEntity = dbset.First(p => p.Id != entry.Entity.Id); + Assert.Equal(ProcessMapPlanBase.IdStateActual, newEntity.IdState); + Assert.Equal(1, newEntity.IdAuthor); + Assert.Null(newEntity.IdEditor); + Assert.Null(newEntity.Obsolete); + Assert.Equal(oldEntity.Id, newEntity.IdPrevious); + Assert.InRange(newEntity.Creation, startTime, doneTime); + + var expected = dtoCopy.Adapt(); + var excludeProps = new[] { + nameof(ProcessMapPlanDrillingDto.Id), + nameof(ProcessMapPlanDrillingDto.IdAuthor), + nameof(ProcessMapPlanDrillingDto.Creation), + }; + MatchHelper.Match(expected, newEntity!, excludeProps); + } + + [Fact] + public async Task DeleteRange_returns_success() + { + //arrange + var dbset = dbContext.Set(); + var startTime = DateTimeOffset.UtcNow; + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + var entry = dbset.Add(entity); + dbContext.SaveChanges(); + entry.State = EntityState.Detached; + + //act + var response = await client.DeleteRange(dto.IdWell, new[] { entry.Entity.Id }); + + //assert + var doneTime = DateTimeOffset.UtcNow; + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(1, response.Content); + + var actual = dbContext.Set() + .Where(p => p.Id == entry.Entity.Id) + .FirstOrDefault(); + + Assert.NotNull(actual); + Assert.Equal(ProcessMapPlanBase.IdStateDeleted, actual.IdState); + Assert.Equal(1, actual.IdEditor); + Assert.NotNull(actual.Obsolete); + Assert.InRange(actual.Obsolete.Value, startTime, doneTime); + } + + [Fact] + public async Task GetDatesChange_returns_success() + { + //arrange + var dbset = dbContext.Set(); + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + var entity2 = entity.Adapt(); + entity2.Creation = entity.Creation.AddDays(1); + dbset.Add(entity); + dbset.Add(entity2); + dbContext.SaveChanges(); + var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours; + var offset = TimeSpan.FromHours(timezoneHours); + var dates = new[] { entity.Creation, entity2.Creation } + .Select(d => d.ToOffset(offset)) + .Select(d => new DateOnly(d.Year, d.Month, d.Day)); + + //act + var response = await client.GetDatesChange(dto.IdWell); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Equal(2, response.Content.Count()); + Assert.All(response.Content, d => dates.Contains(d)); + } + + [Fact] + public async Task Get_all_returns_success() + { + //arrange + var dbset = dbContext.Set(); + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + + dbset.Add(entity); + + var entityDeleted = entity.Adapt(); + entityDeleted.Creation = entity.Creation.AddDays(-1); + entityDeleted.Obsolete = entity.Creation; + entityDeleted.IdState = ProcessMapPlanBase.IdStateDeleted; + entityDeleted.IdEditor = 1; + dbset.Add(entityDeleted); + + dbContext.SaveChanges(); + + var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours; + var offset = TimeSpan.FromHours(timezoneHours); + + //act + var request = new ProcessMapPlanBaseRequest + { + IdWell = dto.IdWell, + }; + var response = await client.Get(dto.IdWell, request); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Equal(2, response.Content.Count()); + } + + [Fact] + public async Task Get_actual_returns_success() + { + //arrange + var dbset = dbContext.Set(); + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + + dbset.Add(entity); + + var entityDeleted = entity.Adapt(); + entityDeleted.Creation = entity.Creation.AddDays(-1); + entityDeleted.Obsolete = entity.Creation; + entityDeleted.IdState = ProcessMapPlanBase.IdStateDeleted; + entityDeleted.IdEditor = 1; + entityDeleted.Comment = "nothing"; + dbset.Add(entityDeleted); + + dbContext.SaveChanges(); + + var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours; + var offset = TimeSpan.FromHours(timezoneHours); + + //act + var request = new ProcessMapPlanBaseRequest { + IdWell = dto.IdWell, + Moment = new DateTimeOffset(3000, 1, 1, 0, 0, 0, 0, TimeSpan.Zero) + }; + var response = await client.Get(dto.IdWell, request); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Single(response.Content); + + var actual = response.Content.First()!; + + var expected = entity.Adapt()!; + var excludeProps = new[] { + nameof(ProcessMapPlanDrillingDto.Id), + nameof(ProcessMapPlanDrillingDto.IdAuthor), + nameof(ProcessMapPlanDrillingDto.Creation), + }; + MatchHelper.Match(expected, actual, excludeProps); + } + + [Fact] + public async Task Get_at_moment_returns_success() + { + //arrange + var dbset = dbContext.Set(); + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + + var entityDeleted = entity.Adapt(); + entityDeleted.Creation = entity.Creation.AddDays(-1); + entityDeleted.Obsolete = entity.Creation; + entityDeleted.IdState = ProcessMapPlanBase.IdStateDeleted; + entityDeleted.IdEditor = 1; + entityDeleted.Comment = "nothing"; + dbset.Add(entityDeleted); + + var entityDeleted2 = entity.Adapt(); + entityDeleted2.Obsolete = entity.Creation.AddSeconds(1); + entityDeleted2.IdState = ProcessMapPlanBase.IdStateDeleted; + entityDeleted2.IdEditor = 1; + entityDeleted2.Comment = "nothing"; + dbset.Add(entityDeleted2); + + dbContext.SaveChanges(); + + var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours; + var offset = TimeSpan.FromHours(timezoneHours); + + //act + var request = new ProcessMapPlanBaseRequest + { + IdWell = dto.IdWell, + Moment = entityDeleted.Creation + }; + var response = await client.Get(dto.IdWell, request); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Equal(2, response.Content.Count()); + } + + [Fact] + public async Task Get_section_returns_success() + { + //arrange + var dbset = dbContext.Set(); + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + + dbset.Add(entity); + + var entity2 = entity.Adapt(); + entity2.IdWellSectionType = 2; + entity2.Comment = "IdWellSectionType = 2"; + dbset.Add(entity2); + + dbContext.SaveChanges(); + + var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours; + var offset = TimeSpan.FromHours(timezoneHours); + + //act + var request = new ProcessMapPlanBaseRequest + { + IdWell = dto.IdWell, + IdWellSectionType = 2, + }; + var response = await client.Get(dto.IdWell, request); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Single(response.Content); + + var actual = response.Content.First()!; + + var expected = entity2.Adapt()!; + var excludeProps = new[] { + nameof(ProcessMapPlanDrillingDto.Id), + nameof(ProcessMapPlanDrillingDto.IdAuthor), + nameof(ProcessMapPlanDrillingDto.Creation), + }; + MatchHelper.Match(expected, actual, excludeProps); + } + + [Fact] + public async Task Get_updated_returns_success() + { + //arrange + var dbset = dbContext.Set(); + dbset.RemoveRange(dbset); + dbContext.SaveChanges(); + + dbset.Add(entity); + + var entity2 = entity.Adapt(); + entity2.Creation = entity.Creation.AddHours(1); + entity2.Comment = "IdWellSectionType = 2"; + dbset.Add(entity2); + + var entity3 = entity.Adapt(); + entity3.Obsolete = entity.Creation.AddHours(1); + entity3.Comment = "IdWellSectionType = 3"; + dbset.Add(entity3); + + dbContext.SaveChanges(); + + var timezoneHours = Data.Defaults.Wells[0].Timezone.Hours; + var offset = TimeSpan.FromHours(timezoneHours); + var updateFrom = entity.Creation.ToOffset(offset).AddHours(0.5); + + //act + var request = new ProcessMapPlanBaseRequest + { + IdWell = dto.IdWell, + UpdateFrom = updateFrom, + }; + var response = await client.Get(dto.IdWell, request); + + //assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.NotNull(response.Content); + Assert.Equal(2, response.Content.Count()); + + var actual = response.Content + .Where(p=>p.Comment == entity2.Comment) + .First(); + + var expected = entity2.Adapt(); var excludeProps = new[] { nameof(ProcessMapPlanDrillingDto.Id), nameof(ProcessMapPlanDrillingDto.IdAuthor), diff --git a/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs b/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs index d01d5ee7..20c5ec44 100644 --- a/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs +++ b/AsbCloudWebApi/Controllers/ProcessMapPlan/ProcessMapPlanBaseController.cs @@ -42,13 +42,13 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [HttpPost] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] - public async Task InsertRangeAsync([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) + public async Task InsertRange([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) { if (idWell == 0 || dtos.Any(d => d.IdWell != idWell)) return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell"); - var idUser = await AssertUserHasAccessToWellAsync(idWell, token); - var result = await repository.InsertRangeAsync(idUser, dtos, token); + var idUser = await AssertUserHasAccessToWell(idWell, token); + var result = await repository.InsertRange(idUser, dtos, token); return Ok(result); } @@ -62,13 +62,13 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [HttpPost("replace")] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] - public async Task ClearAndInsertRangeAsync([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) + public async Task ClearAndInsertRange([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) { if (idWell == 0 || dtos.Any(d => d.IdWell != idWell)) return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell"); - var idUser = await AssertUserHasAccessToWellAsync(idWell, token); - var result = await repository.ClearAndInsertRangeAsync(idUser, idWell, dtos, token); + var idUser = await AssertUserHasAccessToWell(idWell, token); + var result = await repository.ClearAndInsertRange(idUser, idWell, dtos, token); return Ok(result); } @@ -82,10 +82,10 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [HttpDelete] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] - public async Task DeleteRangeAsync([FromRoute]int idWell, IEnumerable ids, CancellationToken token) + public async Task DeleteRange([FromRoute]int idWell, IEnumerable ids, CancellationToken token) { - var idUser = await AssertUserHasAccessToWellAsync(idWell, token); - var result = await repository.DeleteRangeAsync(idUser, ids, token); + var idUser = await AssertUserHasAccessToWell(idWell, token); + var result = await repository.DeleteRange(idUser, ids, token); return Ok(result); } @@ -99,11 +99,11 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] - public async Task>> GetAsync([FromRoute] int idWell, [FromQuery]ProcessMapPlanBaseRequest request, CancellationToken token) + public async Task>> Get([FromRoute] int idWell, [FromQuery]ProcessMapPlanBaseRequest request, CancellationToken token) { request.IdWell = idWell; - await AssertUserHasAccessToWellAsync(request.IdWell, token); - var result = await repository.GetAsync(request, token); + await AssertUserHasAccessToWell(request.IdWell, token); + var result = await repository.Get(request, token); return Ok(result); } @@ -117,10 +117,10 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [HttpGet("changeLog")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] - public async Task>> GetChangeLogAsync([FromRoute] int idWell, [FromQuery] DateOnly? date, CancellationToken token) + public async Task>> GetChangeLog([FromRoute] int idWell, [FromQuery] DateOnly? date, CancellationToken token) { - await AssertUserHasAccessToWellAsync(idWell, token); - var result = await repository.GetChangeLogAsync(idWell, date, token); + await AssertUserHasAccessToWell(idWell, token); + var result = await repository.GetChangeLog(idWell, date, token); return Ok(result); } @@ -133,10 +133,10 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [HttpGet("dates")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] - public async Task>> GetDatesChangeAsync([FromRoute] int idWell, CancellationToken token) + public async Task>> GetDatesChange([FromRoute] int idWell, CancellationToken token) { - await AssertUserHasAccessToWellAsync(idWell, token); - var result = await repository.GetDatesChangeAsync(idWell, token); + await AssertUserHasAccessToWell(idWell, token); + var result = await repository.GetDatesChange(idWell, token); return Ok(result); } @@ -150,7 +150,7 @@ public abstract class ProcessMapPlanBaseController : ControllerBase [HttpPut] [ProducesResponseType(typeof(int), StatusCodes.Status200OK)] [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] - public async Task UpdateRangeAsync([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) + public async Task UpdateRange([FromRoute] int idWell, IEnumerable dtos, CancellationToken token) { var first = dtos.FirstOrDefault(); if(first is null) @@ -159,9 +159,9 @@ public abstract class ProcessMapPlanBaseController : ControllerBase if (idWell == 0 || dtos.Any(d => d.IdWell != idWell)) return this.ValidationBadRequest(nameof(dtos), "all dtos should contain same idWell"); - var idUser = await AssertUserHasAccessToWellAsync(idWell, token); + var idUser = await AssertUserHasAccessToWell(idWell, token); - var result = await repository.UpdateRangeAsync(idUser, dtos, token); + var result = await repository.UpdateRange(idUser, dtos, token); return Ok(result); } @@ -172,7 +172,7 @@ public abstract class ProcessMapPlanBaseController : ControllerBase /// /// /// - private async Task AssertUserHasAccessToWellAsync(int idWell, CancellationToken token) + private async Task AssertUserHasAccessToWell(int idWell, CancellationToken token) { var idUser = GetUserId(); var idCompany = User.GetCompanyId();