forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
245 lines
11 KiB
C#
245 lines
11 KiB
C#
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetDatabase.Implementations;
|
|
using MagicCarpetDatabase.Models;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MagicCarpetTests.StoragesContracts;
|
|
|
|
[TestFixture]
|
|
class EmployeeStorageContractTests : BaseStorageContractTest
|
|
{
|
|
private EmployeeStorageContract _employeeStorageContract;
|
|
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_employeeStorageContract = new EmployeeStorageContract(MagicCarpetDbContext);
|
|
}
|
|
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenHaveRecords_Test()
|
|
{
|
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1");
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2");
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
|
var list = _employeeStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(3));
|
|
AssertElement(list.First(), employee);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_WhenNoRecords_Test()
|
|
{
|
|
var list = _employeeStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.Empty);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_ByPostId_Test()
|
|
{
|
|
var postId = Guid.NewGuid().ToString();
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId);
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId);
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
|
var list = _employeeStorageContract.GetList(email: postId);
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(2));
|
|
Assert.That(list.All(x => x.PostId == postId));
|
|
}
|
|
[Test]
|
|
public void Try_GetList_ByEmailId_Test()
|
|
{
|
|
var email = Guid.NewGuid().ToString();
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", email);
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", email);
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
|
var list = _employeeStorageContract.GetList(email: email);
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(2));
|
|
Assert.That(list.All(x => x.Email == email));
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_ByBirthDate_Test()
|
|
{
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", birthDate: DateTime.UtcNow.AddYears(-25));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", birthDate: DateTime.UtcNow.AddYears(-21));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", birthDate: DateTime.UtcNow.AddYears(-20));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", birthDate: DateTime.UtcNow.AddYears(-19));
|
|
var list = _employeeStorageContract.GetList(fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1));
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(2));
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_ByEmploymentDate_Test()
|
|
{
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", employmentDate: DateTime.UtcNow.AddDays(-2));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", employmentDate: DateTime.UtcNow.AddDays(-1));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", employmentDate: DateTime.UtcNow.AddDays(1));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", employmentDate: DateTime.UtcNow.AddDays(2));
|
|
var list = _employeeStorageContract.GetList(fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(2));
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetList_ByAllParameters_Test()
|
|
{
|
|
var postId = Guid.NewGuid().ToString();
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1", postId, "email@mail.ru", birthDate: DateTime.UtcNow.AddYears(-25), employmentDate: DateTime.UtcNow.AddDays(-2));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2", postId, "email@mail.ru", birthDate: DateTime.UtcNow.AddYears(-22), employmentDate: DateTime.UtcNow.AddDays(-1));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3", postId, "email@mail.ru", birthDate: DateTime.UtcNow.AddYears(-21), employmentDate: DateTime.UtcNow.AddDays(-1));
|
|
InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4", "email@mail.ru", birthDate: DateTime.UtcNow.AddYears(-20), employmentDate: DateTime.UtcNow.AddDays(1));
|
|
var list = _employeeStorageContract.GetList(postId: postId, email: "email@mail.ru", fromBirthDate: DateTime.UtcNow.AddYears(-21).AddMinutes(-1), toBirthDate: DateTime.UtcNow.AddYears(-20).AddMinutes(1), fromEmploymentDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-1), toEmploymentDate: DateTime.UtcNow.AddDays(1).AddMinutes(1));
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(1));
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
|
{
|
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
|
AssertElement(_employeeStorageContract.GetElementById(employee.Id), employee);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementById_WhenNoRecord_Test()
|
|
{
|
|
Assert.That(() => _employeeStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
|
{
|
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
|
AssertElement(_employeeStorageContract.GetElementByFIO(employee.FIO), employee);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
|
{
|
|
Assert.That(() => _employeeStorageContract.GetElementByFIO("New Fio"), Is.Null);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_Test()
|
|
{
|
|
var employee = CreateModel(Guid.NewGuid().ToString());
|
|
_employeeStorageContract.AddElement(employee);
|
|
AssertElement(GetEmployeeFromDatabase(employee.Id), employee);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
|
{
|
|
var employee = CreateModel(Guid.NewGuid().ToString());
|
|
InsertEmployeeToDatabaseAndReturn(employee.Id);
|
|
Assert.That(() => _employeeStorageContract.AddElement(employee), Throws.TypeOf<ElementExistsException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_Test()
|
|
{
|
|
var employee = CreateModel(Guid.NewGuid().ToString(), "New Fio");
|
|
InsertEmployeeToDatabaseAndReturn(employee.Id);
|
|
_employeeStorageContract.UpdElement(employee);
|
|
AssertElement(GetEmployeeFromDatabase(employee.Id), employee);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
Assert.That(() => _employeeStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_UpdElement_WhenNoRecordWasDeleted_Test()
|
|
{
|
|
var employee = CreateModel(Guid.NewGuid().ToString());
|
|
InsertEmployeeToDatabaseAndReturn(employee.Id, isDeleted: true);
|
|
Assert.That(() => _employeeStorageContract.UpdElement(employee), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DelElement_Test()
|
|
{
|
|
var employee = InsertEmployeeToDatabaseAndReturn(Guid.NewGuid().ToString());
|
|
_employeeStorageContract.DelElement(employee.Id);
|
|
var element = GetEmployeeFromDatabase(employee.Id);
|
|
Assert.That(element, Is.Not.Null);
|
|
Assert.That(element.IsDeleted);
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
|
{
|
|
Assert.That(() => _employeeStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
[Test]
|
|
public void Try_DelElement_WhenNoRecordWasDeleted_Test()
|
|
{
|
|
var employee = CreateModel(Guid.NewGuid().ToString());
|
|
InsertEmployeeToDatabaseAndReturn(employee.Id, isDeleted: true);
|
|
Assert.That(() => _employeeStorageContract.DelElement(employee.Id), Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
|
|
private Employee InsertEmployeeToDatabaseAndReturn(string id, string fio = "test", string? email = null,string ? postId = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false)
|
|
{
|
|
var employee = new Employee() { Id = id, FIO = fio, PostId = postId ?? Guid.NewGuid().ToString(), Email = email, BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-20), EmploymentDate = employmentDate ?? DateTime.UtcNow, IsDeleted = isDeleted };
|
|
MagicCarpetDbContext.Employees.Add(employee);
|
|
MagicCarpetDbContext.SaveChanges();
|
|
return employee;
|
|
}
|
|
|
|
private static void AssertElement(EmployeeDataModel? actual, Employee expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
|
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
|
Assert.That(actual.Email, Is.EqualTo(expected.Email));
|
|
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
|
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
|
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
|
});
|
|
}
|
|
|
|
private static EmployeeDataModel CreateModel(string id, string fio = "fio", string? postId = null, string? email = null, DateTime? birthDate = null, DateTime? employmentDate = null, bool isDeleted = false) =>
|
|
new(id, fio, postId ?? Guid.NewGuid().ToString(), email, birthDate ?? DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted);
|
|
|
|
private Employee? GetEmployeeFromDatabase(string id) => MagicCarpetDbContext.Employees.FirstOrDefault(x => x.Id == id);
|
|
|
|
private static void AssertElement(Employee? actual, EmployeeDataModel expected)
|
|
{
|
|
Assert.That(actual, Is.Not.Null);
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
|
Assert.That(actual.PostId, Is.EqualTo(expected.PostId));
|
|
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
|
|
Assert.That(actual.Email, Is.EqualTo(expected.Email));
|
|
Assert.That(actual.BirthDate, Is.EqualTo(expected.BirthDate));
|
|
Assert.That(actual.EmploymentDate, Is.EqualTo(expected.EmploymentDate));
|
|
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
|
});
|
|
}
|
|
}
|