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() { 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 service = new ScheduleService(context); var id = await service.InsertAsync(entity, CancellationToken.None); Assert.NotEqual(0, id); } [Fact] public async Task UpdateAsync_not_add_if_exists() { var service = new ScheduleService(context); ///Добавляем элемент 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() { var service = new ScheduleService(context); ///Добавляем элемент 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); } } }