DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs
2022-05-26 13:34:50 +05:00

146 lines
5.3 KiB
C#

using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services;
using Moq;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace AsbCloudWebApi.Tests.ServicesTests
{
public class ScheduleServiceTest
{
private readonly AsbCloudDbContext context;
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 Schedule scd = new Schedule
{
Id = 1,
IdWell = 1,
IdDriller = 1,
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 MakeScheduleDto(ScheduleDto? dto = null)
{
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,
Timezone = new SimpleTimezone { Hours = 5 }
};
private Driller driller = new Driller
{
Id = 1,
Name = "Тестовый",
Patronymic = "Тест",
Surname = "Тестович"
};
public ScheduleServiceTest()
{
context = TestHelpter.MakeTestContext();
context.SaveChanges();
context.Deposits.Add(deposit);
context.Clusters.Add(clater);
context.Wells.Add(well);
context.Drillers.Add(driller);
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()
{
}
[Fact]
public async Task GetListAsync_count()
{
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();
Assert.Equal(3, newCount);
}
[Fact]
public async Task InsertAsync_returns_id()
{
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 scheduleService.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_shift1()
{
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);
}
}
}