DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs

147 lines
5.3 KiB
C#
Raw Normal View History

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;
2022-05-26 13:34:50 +05:00
private readonly ScheduleService scheduleService;
2022-05-25 20:58:29 +05:00
private Deposit deposit = new Deposit { Id = 1, Caption = "Депозит 1" };
private Cluster clater = new Cluster { Id = 1, Caption = "Кластер 1", IdDeposit = 1, Timezone = new SimpleTimezone() };
2022-05-26 13:34:50 +05:00
private Schedule scd = new Schedule
2022-05-25 20:58:29 +05:00
{
Id = 1,
IdWell = 1,
IdDriller = 1,
2022-05-26 13:34:50 +05:00
ShiftStart = new TimeOnly(10, 00),
ShiftEnd = new TimeOnly(18, 00),
2022-05-25 20:58:29 +05:00
DrillStart = DateTimeOffset.Parse("2022-05-16T10:00:00.286Z"),
DrillEnd = DateTimeOffset.Parse("2022-05-16T18:00:00.286Z")
};
2022-05-26 13:34:50 +05:00
private ScheduleDto MakeScheduleDto(ScheduleDto? dto = null)
{
2022-05-26 13:34:50 +05:00
return new ScheduleDto
{
IdWell = dto?.IdWell ?? 1,
IdDriller = dto?.IdDriller ?? 1,
2022-06-10 11:58:49 +05:00
//ShiftStart = dto?.ShiftStart ?? new TimeOnly(10, 00),
//ShiftEnd = dto?.ShiftEnd ?? new TimeOnly(18, 00),
2022-05-26 13:34:50 +05:00
DrillStart = dto?.DrillStart ?? DateTime.Parse("2022-05-16T10:00:00.286Z"),
DrillEnd = dto?.DrillEnd ?? DateTime.Parse("2022-05-16T18:00:00.286Z")
};
}
2022-06-15 14:57:37 +05:00
private Well well = new Well
{
Id = 1,
Caption = "Test well 1",
2022-05-26 13:34:50 +05:00
IdCluster = 1,
Timezone = new SimpleTimezone { Hours = 5 }
2022-05-25 20:58:29 +05:00
};
private Driller driller = new Driller
{
Id = 1,
Name = "Тестовый",
Patronymic = "Тест",
Surname = "Тестович"
};
2022-06-15 14:57:37 +05:00
public ScheduleServiceTest()
{
context = TestHelpter.MakeTestContext();
context.SaveChanges();
2022-05-25 20:58:29 +05:00
context.Deposits.Add(deposit);
context.Clusters.Add(clater);
context.Wells.Add(well);
context.Drillers.Add(driller);
2022-05-26 13:34:50 +05:00
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()
{
2022-05-26 13:34:50 +05:00
var id = await scheduleService.InsertAsync(MakeScheduleDto(), CancellationToken.None);
id = await scheduleService.InsertAsync(MakeScheduleDto(), CancellationToken.None);
id = await scheduleService.InsertAsync(MakeScheduleDto(), CancellationToken.None);
2022-05-26 13:34:50 +05:00
var newCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count();
Assert.Equal(3, newCount);
}
[Fact]
public async Task InsertAsync_returns_id()
2022-05-25 20:58:29 +05:00
{
2022-05-26 13:34:50 +05:00
var id = await scheduleService.InsertAsync(MakeScheduleDto(), CancellationToken.None);
Assert.NotEqual(0, id);
}
[Fact]
public async Task UpdateAsync_not_add_if_exists()
{
2022-05-25 20:58:29 +05:00
context.Schedule.Add(scd);
2022-05-26 13:34:50 +05:00
var oldCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count();
2022-05-26 13:34:50 +05:00
var dto = MakeScheduleDto();
dto.Id = 1;
dto.DrillEnd = dto.DrillEnd.AddHours(-3);
2022-06-09 13:36:14 +05:00
await scheduleService.UpdateAsync(dto, CancellationToken.None);
2022-05-26 13:34:50 +05:00
var newCount = (await scheduleService.GetAllAsync(CancellationToken.None)).Count();
Assert.Equal(newCount, oldCount);
}
[Fact]
2022-05-26 13:34:50 +05:00
public async Task GetDriller_by_workTime_shift1()
{
2022-05-26 13:34:50 +05:00
var dto = MakeScheduleDto();
2022-06-10 12:01:46 +05:00
dto.ShiftStart = new TimeOnly(8, 00);
dto.ShiftEnd = new TimeOnly(20, 00);
2022-05-26 13:34:50 +05:00
var id = await scheduleService.InsertAsync(dto, CancellationToken.None);
2022-06-15 14:57:37 +05:00
var drillerWorkTime = new DateTime(
2022-05-26 13:34:50 +05:00
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);
}
2022-05-26 13:34:50 +05:00
[Fact]
public async Task GetDriller_by_workTime_shift2()
{
var dto = MakeScheduleDto();
2022-06-10 12:01:46 +05:00
dto.ShiftStart = new TimeOnly(20, 00);
dto.ShiftEnd = new TimeOnly(8, 00);
2022-05-26 13:34:50 +05: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);
}
}
}