починил тесты.

This commit is contained in:
ngfrolov 2022-05-26 13:34:50 +05:00
parent eabec7e7ee
commit 9d59dd3b30

View File

@ -2,11 +2,8 @@
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services; using AsbCloudInfrastructure.Services;
using AsbCloudInfrastructure.Services.Cache;
using AsbCloudInfrastructure.Services.SAUB;
using Moq; using Moq;
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -17,34 +14,38 @@ namespace AsbCloudWebApi.Tests.ServicesTests
public class ScheduleServiceTest public class ScheduleServiceTest
{ {
private readonly AsbCloudDbContext context; private readonly AsbCloudDbContext context;
private readonly CacheDb cacheDb; private readonly ScheduleService scheduleService;
private readonly ScheduleService service;
private Deposit deposit = new Deposit { Id = 1, Caption = "Депозит 1" }; private Deposit deposit = new Deposit { Id = 1, Caption = "Депозит 1" };
private Cluster clater = new Cluster { Id = 1, Caption = "Кластер 1", IdDeposit = 1, Timezone = new SimpleTimezone() }; 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, Id = 1,
IdWell = 1, IdWell = 1,
IdDriller = 1, IdDriller = 1,
ShiftStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"), ShiftStart = new TimeOnly(10, 00),
ShiftEnd = DateTimeOffset.Parse("2022-05-16T18:00:00.286Z"), ShiftEnd = new TimeOnly(18, 00),
DrillStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"), DrillStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"),
DrillEnd = DateTimeOffset.Parse("2022-05-16T18: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, return new ScheduleDto
IdDriller=1, {
ShiftStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"), IdWell = dto?.IdWell ?? 1,
ShiftEnd = DateTimeOffset.Parse("2022-05-16T18:00:00.286Z"), IdDriller = dto?.IdDriller ?? 1,
DrillStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"), ShiftStart = dto?.ShiftStart ?? new TimeOnly(10, 00),
DrillEnd = DateTimeOffset.Parse("2022-05-16T18:00:00.286Z") 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 { private Well well = new Well {
Id=1, Id=1,
Caption = "Test well 1", Caption = "Test well 1",
IdCluster = 1 IdCluster = 1,
Timezone = new SimpleTimezone { Hours = 5 }
}; };
private Driller driller = new Driller private Driller driller = new Driller
{ {
@ -57,7 +58,6 @@ namespace AsbCloudWebApi.Tests.ServicesTests
public ScheduleServiceTest() public ScheduleServiceTest()
{ {
context = TestHelpter.MakeTestContext(); context = TestHelpter.MakeTestContext();
cacheDb = new CacheDb();
context.SaveChanges(); context.SaveChanges();
context.Deposits.Add(deposit); context.Deposits.Add(deposit);
@ -65,7 +65,13 @@ namespace AsbCloudWebApi.Tests.ServicesTests
context.Wells.Add(well); context.Wells.Add(well);
context.Drillers.Add(driller); context.Drillers.Add(driller);
service=new ScheduleService(context); context.SaveChanges();
var timezone = new SimpleTimezoneDto { Hours = 5 };
var wellServiceMock = new Mock<IWellService>();
wellServiceMock.Setup(s => s.GetTimezone(It.IsAny<int>())).Returns(timezone);
scheduleService = new ScheduleService(context, wellServiceMock.Object);
AsbCloudInfrastructure.DependencyInjection.MapsterSetup();
} }
~ScheduleServiceTest() ~ScheduleServiceTest()
@ -75,49 +81,65 @@ namespace AsbCloudWebApi.Tests.ServicesTests
[Fact] [Fact]
public async Task GetListAsync_count() 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 newCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count();
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();
Assert.Equal(3, newCount); Assert.Equal(3, newCount);
} }
[Fact] [Fact]
public async Task InsertAsync_returns_id() 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); Assert.NotEqual(0, id);
} }
[Fact] [Fact]
public async Task UpdateAsync_not_add_if_exists() public async Task UpdateAsync_not_add_if_exists()
{ {
///Добавляем элемент
context.Schedule.Add(scd); context.Schedule.Add(scd);
var oldCount = (await service.GetAllAsync(CancellationToken.None)).Count(); var oldCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count();
//Обновляем var dto = MakeScheduleDto();
entity.Id = 1; dto.Id = 1;
entity.DrillEnd = entity.DrillEnd.AddHours(-3); dto.DrillEnd = dto.DrillEnd.AddHours(-3);
await service.UpdateAsync(entity.Id, entity, CancellationToken.None); await scheduleService.UpdateAsync(dto.Id, dto, CancellationToken.None);
var newCount = (await service.GetAllAsync(CancellationToken.None)).Count(); var newCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count();
Assert.Equal(newCount, oldCount); Assert.Equal(newCount, oldCount);
} }
[Fact] [Fact]
public async Task GetDriller_by_workTime() public async Task GetDriller_by_workTime_shift1()
{ {
///Добавляем элемент var dto = MakeScheduleDto();
var id = await service.InsertAsync(entity, CancellationToken.None); dto.ShiftStart = new TimeOnly(8, 00);
dto.ShiftEnd = new TimeOnly(20, 00);
//Обновляем var id = await scheduleService.InsertAsync(dto, CancellationToken.None);
var res = await service.GetSchedule(well.Id, entity.DrillEnd.AddHours(-2), CancellationToken.None); var drillerWorkTime = new DateTime(
Assert.Equal(scd.IdDriller, res.Id); 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);
}
} }
} }