Тесты

This commit is contained in:
2025-02-27 09:12:20 +04:00
parent c12efe7f92
commit 087a21da11
6 changed files with 935 additions and 3 deletions

View File

@@ -3,11 +3,11 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MagicCarpetContracts.Infrastructure;
namespace MagicCarpetTests.Infrastructure;
internal class ConfigurationDatabaseTest
internal class ConfigurationDatabaseTest : IConfigurationDatabase
{
public string ConnectionString =>
"Host=local;Port=5432;Database=MagicCarpetTest;Username=postgres;Password=postgres;";
public string ConnectionString => "Host=local;Port=5432;Database=MagicCarpetTest;Username=postgres;Password=postgres;";
}

View File

@@ -0,0 +1,218 @@
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.StoragesContracts;
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]
internal class ClientStorageContractTests : BaseStorageContractTest
{
private ClientStorageContarct _clientStorageContract;
[SetUp]
public void SetUp()
{
_clientStorageContract = new ClientStorageContarct(MagicCarpetDbContext);
}
[TearDown]
public void TearDown()
{
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
}
[Test]
public void Try_GetList_WhenHaveRecords_Test()
{
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+5-555-555-55-55");
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+6-666-666-66-66");
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: "+7-777-777-77-77");
var list = _clientStorageContract.GetList();
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(3));
AssertElement(list.First(x => x.Id == client.Id), client);
}
[Test]
public void Try_GetList_WhenNoRecords_Test()
{
var list = _clientStorageContract.GetList();
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.Empty);
}
[Test]
public void Try_GetElementById_WhenHaveRecord_Test()
{
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
AssertElement(_clientStorageContract.GetElementById(client.Id), client);
}
[Test]
public void Try_GetElementById_WhenNoRecord_Test()
{
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
Assert.That(() => _clientStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
}
[Test]
public void Try_GetElementByFIO_WhenHaveRecord_Test()
{
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
AssertElement(_clientStorageContract.GetElementByFIO(client.FIO), client);
}
[Test]
public void Try_GetElementByFIO_WhenNoRecord_Test()
{
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
Assert.That(() => _clientStorageContract.GetElementByFIO("New Fio"), Is.Null);
}
[Test]
public void Try_GetElementByPhoneNumber_WhenHaveRecord_Test()
{
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
AssertElement(_clientStorageContract.GetElementByPhoneNumber(client.PhoneNumber), client);
}
[Test]
public void Try_GetElementByPhoneNumber_WhenNoRecord_Test()
{
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
Assert.That(() => _clientStorageContract.GetElementByPhoneNumber("+8-888-888-88-88"), Is.Null);
}
[Test]
public void Try_AddElement_Test()
{
var client = CreateModel(Guid.NewGuid().ToString());
_clientStorageContract.AddElement(client);
AssertElement(GetClientFromDatabase(client.Id), client);
}
[Test]
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
{
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
InsertClientToDatabaseAndReturn(client.Id);
Assert.That(() => _clientStorageContract.AddElement(client), Throws.TypeOf<ElementExistsException>());
}
[Test]
public void Try_AddElement_WhenHaveRecordWithSamePhoneNumber_Test()
{
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: client.PhoneNumber);
Assert.That(() => _clientStorageContract.AddElement(client), Throws.TypeOf<ElementExistsException>());
}
[Test]
public void Try_UpdElement_Test()
{
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
InsertClientToDatabaseAndReturn(client.Id);
_clientStorageContract.UpdElement(client);
AssertElement(GetClientFromDatabase(client.Id), client);
}
[Test]
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
{
Assert.That(() => _clientStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
public void Try_UpdElement_WhenHaveRecordWithSamePhoneNumber_Test()
{
var client = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555-555-55-55", 500);
InsertClientToDatabaseAndReturn(client.Id, phoneNumber: "+7-777-777-77-77");
InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString(), phoneNumber: client.PhoneNumber);
Assert.That(() => _clientStorageContract.UpdElement(client), Throws.TypeOf<ElementExistsException>());
}
[Test]
public void Try_DelElement_Test()
{
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
_clientStorageContract.DelElement(client.Id);
var element = GetClientFromDatabase(client.Id);
Assert.That(element, Is.Null);
}
[Test]
public void Try_DelElement_WhenHaveSalesByThisBuyer_Test()
{
var client = InsertClientToDatabaseAndReturn(Guid.NewGuid().ToString());
var employeeId = Guid.NewGuid().ToString();
MagicCarpetDbContext.Employees.Add(new Employee() { Id = employeeId, FIO = "test", PostId = Guid.NewGuid().ToString() });
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
MagicCarpetDbContext.Sales.Add(new Sale() { Id = Guid.NewGuid().ToString(), EmployeeId = employeeId, ClientId = client.Id, Sum = 10, DiscountType = DiscountType.None, Discount = 0 });
MagicCarpetDbContext.SaveChanges();
var salesBeforeDelete = MagicCarpetDbContext.Sales.Where(x => x.ClientId == client.Id).ToArray();
_clientStorageContract.DelElement(client.Id);
var element = GetClientFromDatabase(client.Id);
var salesAfterDelete = MagicCarpetDbContext.Sales.Where(x => x.ClientId == client.Id).ToArray();
Assert.Multiple(() =>
{
Assert.That(element, Is.Null);
Assert.That(salesBeforeDelete, Has.Length.EqualTo(2));
Assert.That(salesAfterDelete, Is.Empty);
Assert.That(MagicCarpetDbContext.Sales.Count(), Is.EqualTo(2));
});
}
[Test]
public void Try_DelElement_WhenNoRecordWithThisId_Test()
{
Assert.That(() => _clientStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
}
private Client InsertClientToDatabaseAndReturn(string id, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
{
var client = new Client() { Id = id, FIO = fio, PhoneNumber = phoneNumber, DiscountSize = discountSize };
MagicCarpetDbContext.Clients.Add(client);
MagicCarpetDbContext.SaveChanges();
return client;
}
private static void AssertElement(ClientDataModel? actual, Client expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
Assert.That(actual.PhoneNumber, Is.EqualTo(expected.PhoneNumber));
Assert.That(actual.DiscountSize, Is.EqualTo(expected.DiscountSize));
});
}
private static ClientDataModel CreateModel(string id, string fio = "test", string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
=> new(id, fio, phoneNumber, discountSize);
private Client? GetClientFromDatabase(string id) => MagicCarpetDbContext.Clients.FirstOrDefault(x => x.Id == id);
private static void AssertElement(Client? actual, ClientDataModel expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.FIO, Is.EqualTo(expected.FIO));
Assert.That(actual.PhoneNumber, Is.EqualTo(expected.PhoneNumber));
Assert.That(actual.DiscountSize, Is.EqualTo(expected.DiscountSize));
});
}
}

View File

@@ -0,0 +1,244 @@
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));
});
}
}

View File

@@ -0,0 +1,153 @@
using MagicCarpetContracts.DataModels;
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]
internal class SalaryStorageContractTests : BaseStorageContractTest
{
private SalaryStorageContract _salaryStorageContract;
private Employee _employee;
[SetUp]
public void SetUp()
{
_salaryStorageContract = new SalaryStorageContract(MagicCarpetDbContext);
_employee = InsertEmployeeToDatabaseAndReturn();
}
[TearDown]
public void TearDown()
{
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Salaries\" CASCADE;");
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
}
[Test]
public void Try_GetList_WhenHaveRecords_Test()
{
var salary = InsertSalaryToDatabaseAndReturn(_employee.Id, employeeSalary: 100);
InsertSalaryToDatabaseAndReturn(_employee.Id);
InsertSalaryToDatabaseAndReturn(_employee.Id);
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(3));
AssertElement(list.First(), salary);
}
[Test]
public void Try_GetList_WhenNoRecords_Test()
{
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-10), DateTime.UtcNow.AddDays(10));
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.Empty);
}
[Test]
public void Try_GetList_OnlyInDatePeriod_Test()
{
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-5));
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(5));
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1));
Assert.That(list, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(list, Has.Count.EqualTo(2));
});
}
[Test]
public void Try_GetList_ByEmployee_Test()
{
var employee = InsertEmployeeToDatabaseAndReturn("name 2");
InsertSalaryToDatabaseAndReturn(_employee.Id);
InsertSalaryToDatabaseAndReturn(_employee.Id);
InsertSalaryToDatabaseAndReturn(employee.Id);
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _employee.Id);
Assert.That(list, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(list, Has.Count.EqualTo(2));
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
});
}
[Test]
public void Try_GetList_ByEmployeeOnlyInDatePeriod_Test()
{
var employee = InsertEmployeeToDatabaseAndReturn("name 2");
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
InsertSalaryToDatabaseAndReturn(employee.Id, salaryDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
InsertSalaryToDatabaseAndReturn(employee.Id, salaryDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
InsertSalaryToDatabaseAndReturn(_employee.Id, salaryDate: DateTime.UtcNow.AddDays(-2));
var list = _salaryStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _employee.Id);
Assert.That(list, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(list, Has.Count.EqualTo(2));
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
});
}
[Test]
public void Try_AddElement_Test()
{
var salary = CreateModel(_employee.Id);
_salaryStorageContract.AddElement(salary);
AssertElement(GetSalaryFromDatabaseByEmployeeId(_employee.Id), salary);
}
private Employee InsertEmployeeToDatabaseAndReturn(string employeeFIO = "fio")
{
var employee = new Employee() { Id = Guid.NewGuid().ToString(), PostId = Guid.NewGuid().ToString(), FIO = employeeFIO, IsDeleted = false };
MagicCarpetDbContext.Employees.Add(employee);
MagicCarpetDbContext.SaveChanges();
return employee;
}
private Salary InsertSalaryToDatabaseAndReturn(string employeeId, double employeeSalary = 1, DateTime? salaryDate = null)
{
var salary = new Salary() { EmployeeId = employeeId, EmployeeSalary = employeeSalary, SalaryDate = salaryDate ?? DateTime.UtcNow };
MagicCarpetDbContext.Salaries.Add(salary);
MagicCarpetDbContext.SaveChanges();
return salary;
}
private static void AssertElement(SalaryDataModel? actual, Salary expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
Assert.That(actual.Salary, Is.EqualTo(expected.EmployeeSalary));
});
}
private static SalaryDataModel CreateModel(string employeeId, double employeeSalary = 1, DateTime? salaryDate = null)
=> new(employeeId, salaryDate ?? DateTime.UtcNow, employeeSalary);
private Salary? GetSalaryFromDatabaseByEmployeeId(string id) => MagicCarpetDbContext.Salaries.FirstOrDefault(x => x.EmployeeId == id);
private static void AssertElement(Salary? actual, SalaryDataModel expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
Assert.That(actual.EmployeeSalary, Is.EqualTo(expected.Salary));
});
}
}

View File

@@ -0,0 +1,306 @@
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Enums;
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;
using static NUnit.Framework.Internal.OSPlatform;
namespace MagicCarpetTests.StoragesContracts;
[TestFixture]
internal class SaleStorageContractTests : BaseStorageContractTest
{
private SaleStorageContract _saleStorageContract;
private Client _client;
private Employee _employee;
private Tour _tour;
[SetUp]
public void SetUp()
{
_saleStorageContract = new SaleStorageContract(MagicCarpetDbContext);;
_client = InsertClientToDatabaseAndReturn();
_employee = InsertEmployeeToDatabaseAndReturn();
_tour = InsertTourToDatabaseAndReturn();
}
[TearDown]
public void TearDown()
{
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE;");
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
MagicCarpetDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Tours\" CASCADE;");
}
[Test]
public void Try_GetList_WhenHaveRecords_Test()
{
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 5)]);
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(_tour.Id, 10)]);
var list = _saleStorageContract.GetList();
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(3));
AssertElement(list.First(x => x.Id == sale.Id), sale);
}
[Test]
public void Try_GetList_WhenNoRecords_Test()
{
var list = _saleStorageContract.GetList();
Assert.That(list, Is.Not.Null);
Assert.That(list, Is.Empty);
}
[Test]
public void Try_GetList_ByPeriod_Test()
{
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(3), tours: [(_tour.Id, 1)]);
var list = _saleStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1));
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(2));
}
[Test]
public void Try_GetList_ByEmployeeId_Test()
{
var employee = InsertEmployeeToDatabaseAndReturn("Other employee");
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(employee.Id, null, tours: [(_tour.Id, 1)]);
var list = _saleStorageContract.GetList(employeeId: _employee.Id);
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(2));
Assert.That(list.All(x => x.EmployeeId == _employee.Id));
}
[Test]
public void Try_GetList_ByClientId_Test()
{
var client = InsertClientToDatabaseAndReturn("Other fio", "+8-888-888-88-88");
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, client.Id, tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(_tour.Id, 1)]);
var list = _saleStorageContract.GetList(clientId: _client.Id);
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(2));
Assert.That(list.All(x => x.ClientId == _client.Id));
}
[Test]
public void Try_GetList_ByTourId_Test()
{
var tour = InsertTourToDatabaseAndReturn("Other name");
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 5)]);
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1), (tour.Id, 4)]);
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, null, tours: [(tour.Id, 1), (_tour.Id, 1)]);
var list = _saleStorageContract.GetList(tourId: _tour.Id);
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(3));
Assert.That(list.All(x => x.Tours.Any(y => y.TourId == _tour.Id)));
}
[Test]
public void Try_GetList_ByAllParameters_Test()
{
var employee = InsertEmployeeToDatabaseAndReturn("Other employee");
var client = InsertClientToDatabaseAndReturn("Other fio", "+8-888-888-88-88");
var tour = InsertTourToDatabaseAndReturn("Other name");
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(employee.Id, null, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), tours: [(tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, client.Id, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(tour.Id, 1)]);
InsertSaleToDatabaseAndReturn(employee.Id, null, saleDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), tours: [(_tour.Id, 1)]);
var list = _saleStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1), employeeId: _employee.Id, clientId: _client.Id, tourId: tour.Id);
Assert.That(list, Is.Not.Null);
Assert.That(list, Has.Count.EqualTo(1));
}
[Test]
public void Try_GetElementById_WhenHaveRecord_Test()
{
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
AssertElement(_saleStorageContract.GetElementById(sale.Id), sale);
}
[Test]
public void Try_GetElementById_WhenNoRecord_Test()
{
InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)]);
Assert.That(() => _saleStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
}
[Test]
public void Try_GetElementById_WhenRecordHasCanceled_Test()
{
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: true);
AssertElement(_saleStorageContract.GetElementById(sale.Id), sale);
}
[Test]
public void Try_AddElement_Test()
{
var sale = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, false, [_tour.Id]);
_saleStorageContract.AddElement(sale);
AssertElement(GetSaleFromDatabaseById(sale.Id), sale);
}
[Test]
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
{
var sale = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, true, [_tour.Id]);
Assert.That(() => _saleStorageContract.AddElement(sale), Throws.Nothing);
AssertElement(GetSaleFromDatabaseById(sale.Id), CreateModel(sale.Id, _employee.Id, _client.Id, 1, DiscountType.RegularCustomer, 1, false, [_tour.Id]));
}
[Test]
public void Try_DelElement_Test()
{
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: false);
_saleStorageContract.DelElement(sale.Id);
var element = GetSaleFromDatabaseById(sale.Id);
Assert.Multiple(() =>
{
Assert.That(element, Is.Not.Null);
Assert.That(element!.IsCancel);
});
}
[Test]
public void Try_DelElement_WhenNoRecordWithThisId_Test()
{
Assert.That(() => _saleStorageContract.DelElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
}
[Test]
public void Try_DelElement_WhenRecordWasCanceled_Test()
{
var sale = InsertSaleToDatabaseAndReturn(_employee.Id, _client.Id, tours: [(_tour.Id, 1)], isCancel: true);
Assert.That(() => _saleStorageContract.DelElement(sale.Id), Throws.TypeOf<ElementDeletedException>());
}
private Client InsertClientToDatabaseAndReturn(string fio = "test", string phoneNumber = "+7-777-777-77-77")
{
var client = new Client() { Id = Guid.NewGuid().ToString(), FIO = fio, PhoneNumber = phoneNumber, DiscountSize = 10 };
MagicCarpetDbContext.Clients.Add(client);
MagicCarpetDbContext.SaveChanges();
return client;
}
private Employee InsertEmployeeToDatabaseAndReturn(string fio = "test")
{
var employee = new Employee() { Id = Guid.NewGuid().ToString(), FIO = fio, PostId = Guid.NewGuid().ToString() };
MagicCarpetDbContext.Employees.Add(employee);
MagicCarpetDbContext.SaveChanges();
return employee;
}
private Tour InsertTourToDatabaseAndReturn(string tourName = "test", TourType tourType = TourType.Beach, double price = 1, bool isDeleted = false)
{
var tour = new Tour() { Id = Guid.NewGuid().ToString(), TourName = tourName, Type = tourType, Price = price };
MagicCarpetDbContext.Tours.Add(tour);
MagicCarpetDbContext.SaveChanges();
return tour;
}
private Sale InsertSaleToDatabaseAndReturn(string employeeId, string? clientId, DateTime? saleDate = null, double sum = 1, DiscountType discountType = DiscountType.OnSale, double discount = 0, bool isCancel = false, List<(string, int)>? tours = null)
{
var sale = new Sale() { EmployeeId = employeeId, ClientId = clientId, SaleDate = saleDate ?? DateTime.UtcNow, Sum = sum, DiscountType = discountType, Discount = discount, IsCancel = isCancel, SaleTours = [] };
if (tours is not null)
{
foreach (var elem in tours)
{
sale.SaleTours.Add(new SaleTour { TourId = elem.Item1, SaleId = sale.Id, Count = elem.Item2 });
}
}
MagicCarpetDbContext.Sales.Add(sale);
MagicCarpetDbContext.SaveChanges();
return sale;
}
private static void AssertElement(SaleDataModel? actual, Sale expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
Assert.That(actual.ClientId, Is.EqualTo(expected.ClientId));
Assert.That(actual.DiscountType, Is.EqualTo(expected.DiscountType));
Assert.That(actual.Discount, Is.EqualTo(expected.Discount));
Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel));
});
if (expected.SaleTours is not null)
{
Assert.That(actual.Tours, Is.Not.Null);
Assert.That(actual.Tours, Has.Count.EqualTo(expected.SaleTours.Count));
for (int i = 0; i < actual.Tours.Count; ++i)
{
Assert.Multiple(() =>
{
Assert.That(actual.Tours[i].TourId, Is.EqualTo(expected.SaleTours[i].TourId));
Assert.That(actual.Tours[i].Count, Is.EqualTo(expected.SaleTours[i].Count));
});
}
}
else
{
Assert.That(actual.Tours, Is.Null);
}
}
private static SaleDataModel CreateModel(string id, string employeeId, string? clientId, double sum, DiscountType discountType, double discount, bool isCancel, List<string> tourIds)
{
var tours = tourIds.Select(x => new SaleTourDataModel(id, x, 1)).ToList();
return new(id, employeeId, clientId, sum, discountType, discount, isCancel, tours);
}
private Sale? GetSaleFromDatabaseById(string id) => MagicCarpetDbContext.Sales.Include(x => x.SaleTours).FirstOrDefault(x => x.Id == id);
private static void AssertElement(Sale? actual, SaleDataModel expected)
{
Assert.That(actual, Is.Not.Null);
Assert.Multiple(() =>
{
Assert.That(actual.Id, Is.EqualTo(expected.Id));
Assert.That(actual.EmployeeId, Is.EqualTo(expected.EmployeeId));
Assert.That(actual.ClientId, Is.EqualTo(expected.ClientId));
Assert.That(actual.DiscountType, Is.EqualTo(expected.DiscountType));
Assert.That(actual.Discount, Is.EqualTo(expected.Discount));
Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel));
});
if (expected.Tours is not null)
{
Assert.That(actual.SaleTours, Is.Not.Null);
Assert.That(actual.SaleTours, Has.Count.EqualTo(expected.Tours.Count));
for (int i = 0; i < actual.SaleTours.Count; ++i)
{
Assert.Multiple(() =>
{
Assert.That(actual.SaleTours[i].TourId, Is.EqualTo(expected.Tours[i].TourId));
Assert.That(actual.SaleTours[i].Count, Is.EqualTo(expected.Tours[i].Count));
});
}
}
else
{
Assert.That(actual.SaleTours, Is.Null);
}
}
}

View File

@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MagicCarpetTests.StoragesContracts;
class TourStorageContractTests
{
}