DD.WellWorkover.Cloud/AsbCloudWebApi.Tests/ServicesTests/ScheduleServiceTest.cs
2022-05-25 20:58:29 +05:00

124 lines
4.1 KiB
C#

using AsbCloudApp.Data;
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;
using Xunit;
namespace AsbCloudWebApi.Tests.ServicesTests
{
public class ScheduleServiceTest
{
private readonly AsbCloudDbContext context;
private readonly CacheDb cacheDb;
private readonly ScheduleService service;
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
{
Id = 1,
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")
};
private ScheduleDto entity = new ScheduleDto
{
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")
};
private Well well = new Well {
Id=1,
Caption = "Test well 1",
IdCluster = 1
};
private Driller driller = new Driller
{
Id = 1,
Name = "Тестовый",
Patronymic = "Тест",
Surname = "Тестович"
};
public ScheduleServiceTest()
{
context = TestHelpter.MakeTestContext();
cacheDb = new CacheDb();
context.SaveChanges();
context.Deposits.Add(deposit);
context.Clusters.Add(clater);
context.Wells.Add(well);
context.Drillers.Add(driller);
service=new ScheduleService(context);
}
~ScheduleServiceTest()
{
}
[Fact]
public async Task GetListAsync_count()
{
var service = new ScheduleService(context);
///Добавляем элемент
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);
}
[Fact]
public async Task InsertAsync_returns_id()
{
var id = await service.InsertAsync(entity, 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();
//Обновляем
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();
Assert.Equal(newCount, oldCount);
}
[Fact]
public async Task GetDriller_by_workTime()
{
///Добавляем элемент
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);
}
}
}