forked from ddrilling/AsbCloudServer
100 lines
3.1 KiB
C#
100 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 Driller driller = new Driller
|
||
{
|
||
Id = 1,
|
||
Name = "Тестовый",
|
||
Patronymic = "Тест",
|
||
Surname = "Тестович"
|
||
};
|
||
private DrillerDto drillerObj = new DrillerDto
|
||
{
|
||
Id = 0,
|
||
Name = "Тестовый",
|
||
Patronymic = "Тест",
|
||
Surname = "Тестович"
|
||
};
|
||
private DrillerService service;
|
||
|
||
public DrillerServiceTest()
|
||
{
|
||
|
||
context = TestHelpter.MakeTestContext();
|
||
cacheDb = new CacheDb();
|
||
context.SaveChanges();
|
||
service = new DrillerService(context);
|
||
}
|
||
|
||
~DrillerServiceTest()
|
||
{
|
||
}
|
||
|
||
[Fact]
|
||
public async Task GetListAsync_count()
|
||
{
|
||
|
||
///Добавляем элемент
|
||
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 id = await service.InsertAsync(drillerObj, CancellationToken.None);
|
||
Assert.NotEqual(0, id);
|
||
}
|
||
|
||
[Fact]
|
||
public async Task UpdateAsync_not_add_if_exists()
|
||
{
|
||
///Добавляем элемент
|
||
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 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);
|
||
}
|
||
|
||
|
||
}
|
||
}
|