From 9d59dd3b30f3afe7a3c1d679eade06c8b27521a9 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Thu, 26 May 2022 13:34:50 +0500 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D1=87=D0=B8=D0=BD=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ServicesTests/ScheduleServiceTest.cs | 106 +++++++++++------- 1 file changed, 64 insertions(+), 42 deletions(-) diff --git a/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs b/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs index 6d019a20..19da37c9 100644 --- a/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs +++ b/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs @@ -2,11 +2,8 @@ using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services; -using AsbCloudInfrastructure.Services.Cache; -using AsbCloudInfrastructure.Services.SAUB; using Moq; using System; -using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -17,34 +14,38 @@ namespace AsbCloudWebApi.Tests.ServicesTests public class ScheduleServiceTest { private readonly AsbCloudDbContext context; - private readonly CacheDb cacheDb; - private readonly ScheduleService service; - + private readonly ScheduleService scheduleService; private Deposit deposit = new Deposit { Id = 1, Caption = "Депозит 1" }; private Cluster clater = new Cluster { Id = 1, Caption = "Кластер 1", IdDeposit = 1, Timezone = new SimpleTimezone() }; - private ScheduleItem scd = new ScheduleItem + private Schedule scd = new Schedule { Id = 1, IdWell = 1, IdDriller = 1, - ShiftStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"), - ShiftEnd = DateTimeOffset.Parse("2022-05-16T18:00:00.286Z"), + ShiftStart = new TimeOnly(10, 00), + ShiftEnd = new TimeOnly(18, 00), DrillStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"), DrillEnd = DateTimeOffset.Parse("2022-05-16T18:00:00.286Z") }; - private ScheduleDto entity = new ScheduleDto + + private ScheduleDto MakeScheduleDto(ScheduleDto? dto = null) { - IdWell=1, - IdDriller=1, - ShiftStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"), - ShiftEnd = DateTimeOffset.Parse("2022-05-16T18:00:00.286Z"), - DrillStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"), - DrillEnd = DateTimeOffset.Parse("2022-05-16T18:00:00.286Z") - }; + return new ScheduleDto + { + IdWell = dto?.IdWell ?? 1, + IdDriller = dto?.IdDriller ?? 1, + ShiftStart = dto?.ShiftStart ?? new TimeOnly(10, 00), + ShiftEnd = dto?.ShiftEnd ?? new TimeOnly(18, 00), + DrillStart = dto?.DrillStart ?? DateTime.Parse("2022-05-16T10:00:00.286Z"), + DrillEnd = dto?.DrillEnd ?? DateTime.Parse("2022-05-16T18:00:00.286Z") + }; + } + private Well well = new Well { Id=1, Caption = "Test well 1", - IdCluster = 1 + IdCluster = 1, + Timezone = new SimpleTimezone { Hours = 5 } }; private Driller driller = new Driller { @@ -57,7 +58,6 @@ namespace AsbCloudWebApi.Tests.ServicesTests public ScheduleServiceTest() { context = TestHelpter.MakeTestContext(); - cacheDb = new CacheDb(); context.SaveChanges(); context.Deposits.Add(deposit); @@ -65,7 +65,13 @@ namespace AsbCloudWebApi.Tests.ServicesTests context.Wells.Add(well); context.Drillers.Add(driller); - service=new ScheduleService(context); + context.SaveChanges(); + + var timezone = new SimpleTimezoneDto { Hours = 5 }; + var wellServiceMock = new Mock(); + wellServiceMock.Setup(s => s.GetTimezone(It.IsAny())).Returns(timezone); + scheduleService = new ScheduleService(context, wellServiceMock.Object); + AsbCloudInfrastructure.DependencyInjection.MapsterSetup(); } ~ScheduleServiceTest() @@ -75,49 +81,65 @@ namespace AsbCloudWebApi.Tests.ServicesTests [Fact] public async Task GetListAsync_count() { - var service = new ScheduleService(context); + var id = await scheduleService.InsertAsync(MakeScheduleDto(), CancellationToken.None); + id = await scheduleService.InsertAsync(MakeScheduleDto(), CancellationToken.None); + id = await scheduleService.InsertAsync(MakeScheduleDto(), CancellationToken.None); - ///Добавляем элемент - var id = await service.InsertAsync(entity, CancellationToken.None); - id = await service.InsertAsync(entity, CancellationToken.None); - id = await service.InsertAsync(entity, CancellationToken.None); - - var newCount = (await service.GetAllAsync(CancellationToken.None)).Count(); + var newCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count(); Assert.Equal(3, newCount); } [Fact] public async Task InsertAsync_returns_id() { - var id = await service.InsertAsync(entity, CancellationToken.None); + var id = await scheduleService.InsertAsync(MakeScheduleDto(), CancellationToken.None); Assert.NotEqual(0, id); } [Fact] public async Task UpdateAsync_not_add_if_exists() { - ///Добавляем элемент context.Schedule.Add(scd); - var oldCount = (await service.GetAllAsync(CancellationToken.None)).Count(); + var oldCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count(); - //Обновляем - entity.Id = 1; - entity.DrillEnd = entity.DrillEnd.AddHours(-3); - await service.UpdateAsync(entity.Id, entity, CancellationToken.None); - var newCount = (await service.GetAllAsync(CancellationToken.None)).Count(); + var dto = MakeScheduleDto(); + dto.Id = 1; + dto.DrillEnd = dto.DrillEnd.AddHours(-3); + await scheduleService.UpdateAsync(dto.Id, dto, CancellationToken.None); + var newCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count(); Assert.Equal(newCount, oldCount); } [Fact] - public async Task GetDriller_by_workTime() + public async Task GetDriller_by_workTime_shift1() { - ///Добавляем элемент - var id = await service.InsertAsync(entity, CancellationToken.None); - - //Обновляем - var res = await service.GetSchedule(well.Id, entity.DrillEnd.AddHours(-2), CancellationToken.None); - Assert.Equal(scd.IdDriller, res.Id); + var dto = MakeScheduleDto(); + dto.ShiftStart = new TimeOnly(8, 00); + dto.ShiftEnd = new TimeOnly(20, 00); + var id = await scheduleService.InsertAsync(dto, CancellationToken.None); + var drillerWorkTime = new DateTime( + dto.DrillStart.Year, + dto.DrillStart.Month, + dto.DrillStart.Day, + 16, 1, 0, DateTimeKind.Unspecified); + var res = await scheduleService.GetDrillerAsync(well.Id, drillerWorkTime, CancellationToken.None); + Assert.Equal(id, res?.Id); } + [Fact] + public async Task GetDriller_by_workTime_shift2() + { + var dto = MakeScheduleDto(); + dto.ShiftStart = new TimeOnly(20, 00); + dto.ShiftEnd = new TimeOnly(8, 00); + var id = await scheduleService.InsertAsync(dto, CancellationToken.None); + var drillerWorkTime = new DateTime( + dto.DrillStart.Year, + dto.DrillStart.Month, + dto.DrillStart.Day, + 20, 1, 0, DateTimeKind.Unspecified); + var res = await scheduleService.GetDrillerAsync(well.Id, drillerWorkTime, CancellationToken.None); + Assert.Equal(id, res?.Id); + } } }