2022-05-22 21:18:43 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
|
|
|
|
using AsbCloudInfrastructure.Services;
|
|
|
|
|
using AsbCloudInfrastructure.Services.Cache;
|
|
|
|
|
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 ScheduleDto entity = new ScheduleDto
|
|
|
|
|
{
|
|
|
|
|
ShiftStart = DateTime.Parse("2022-05-16T10:00:00"),
|
|
|
|
|
ShiftEnd = DateTime.Parse("2022-05-16T18:00:00"),
|
|
|
|
|
DrillStart = DateTime.Parse("2022-05-16T10:00:00"),
|
|
|
|
|
DrillEnd = DateTime.Parse("2022-05-16T18:00:00")
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public ScheduleServiceTest()
|
|
|
|
|
{
|
|
|
|
|
context = TestHelpter.MakeTestContext();
|
|
|
|
|
cacheDb = new CacheDb();
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ScheduleServiceTest()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task GetListAsync_count()
|
|
|
|
|
{
|
2022-05-25 20:19:08 +05:00
|
|
|
|
var service = new ScheduleService(context);
|
2022-05-22 21:18:43 +05:00
|
|
|
|
|
|
|
|
|
///Добавляем элемент
|
|
|
|
|
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()
|
|
|
|
|
{
|
2022-05-25 20:19:08 +05:00
|
|
|
|
var service = new ScheduleService(context);
|
2022-05-22 21:18:43 +05:00
|
|
|
|
var id = await service.InsertAsync(entity, CancellationToken.None);
|
|
|
|
|
Assert.NotEqual(0, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task UpdateAsync_not_add_if_exists()
|
|
|
|
|
{
|
2022-05-25 20:19:08 +05:00
|
|
|
|
var service = new ScheduleService(context);
|
2022-05-22 21:18:43 +05:00
|
|
|
|
|
|
|
|
|
///Добавляем элемент
|
|
|
|
|
var id = await service.InsertAsync(entity, CancellationToken.None);
|
|
|
|
|
var oldCount = (await service.GetAllAsync(CancellationToken.None)).Count();
|
|
|
|
|
|
|
|
|
|
//Обновляем
|
|
|
|
|
entity.Id = id;
|
|
|
|
|
entity.DrillEnd = entity.DrillEnd.AddHours(-3);
|
|
|
|
|
await service.UpdateAsync(id, entity, CancellationToken.None);
|
|
|
|
|
var newCount = (await service.GetAllAsync(CancellationToken.None)).Count();
|
|
|
|
|
Assert.Equal(newCount, oldCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task DeleteAsync_decrement_count()
|
|
|
|
|
{
|
2022-05-25 20:19:08 +05:00
|
|
|
|
var service = new ScheduleService(context);
|
2022-05-22 21:18:43 +05:00
|
|
|
|
|
|
|
|
|
///Добавляем элемент
|
|
|
|
|
var id = await service.InsertAsync(entity, CancellationToken.None);
|
|
|
|
|
var oldCount = (await service.GetAllAsync(CancellationToken.None)).Count();
|
|
|
|
|
|
|
|
|
|
//Удаляем его
|
|
|
|
|
await service.DeleteAsync(id, CancellationToken.None);
|
|
|
|
|
var newCount = (await service.GetAllAsync(CancellationToken.None)).Count();
|
|
|
|
|
Assert.Equal(newCount, oldCount-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|