forked from ddrilling/AsbCloudServer
96 lines
3.1 KiB
C#
96 lines
3.1 KiB
C#
|
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, cacheDb);
|
|||
|
|
|||
|
///Добавляем элемент
|
|||
|
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(newCount, 3);
|
|||
|
}
|
|||
|
|
|||
|
[Fact]
|
|||
|
public async Task InsertAsync_returns_id()
|
|||
|
{
|
|||
|
var service = new DrillerService(context, cacheDb);
|
|||
|
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, cacheDb);
|
|||
|
|
|||
|
///Добавляем элемент
|
|||
|
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, cacheDb);
|
|||
|
|
|||
|
///Добавляем элемент
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|