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 DrillerServiceTest { private readonly AsbCloudDbContext context; private readonly CacheDb cacheDb; private DrillerDto drillerObj = new DrillerDto { Id = 0, Name = "Тестовый", Patronymic = "Тест", Surname = "Тестович" }; public DrillerServiceTest() { context = TestHelpter.MakeTestContext(); cacheDb = new CacheDb(); context.SaveChanges(); } ~DrillerServiceTest() { } [Fact] public async Task GetListAsync_count() { var service = new DrillerService(context); ///Добавляем элемент var id = await service.InsertAsync(drillerObj, CancellationToken.None); id = await service.InsertAsync(drillerObj, CancellationToken.None); id = await service.InsertAsync(drillerObj, CancellationToken.None); var newCount = (await service.GetAllAsync(CancellationToken.None)).Count(); Assert.Equal(3, newCount); } [Fact] public async Task InsertAsync_returns_id() { var service = new DrillerService(context); var id = await service.InsertAsync(drillerObj, CancellationToken.None); Assert.NotEqual(0, id); } [Fact] public async Task UpdateAsync_not_add_if_exists() { var service = new DrillerService(context); ///Добавляем элемент var id = await service.InsertAsync(drillerObj, CancellationToken.None); var oldCount = (await service.GetAllAsync(CancellationToken.None)).Count(); //Обновляем drillerObj.Id = id; drillerObj.Name = "Исправлено"; await service.UpdateAsync(id, drillerObj, CancellationToken.None); var newCount = (await service.GetAllAsync(CancellationToken.None)).Count(); Assert.Equal(newCount, oldCount); } [Fact] public async Task DeleteAsync_decrement_count() { var service = new DrillerService(context); ///Добавляем элемент var id = await service.InsertAsync(drillerObj, 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); } } }