pochemu testi padayut
This commit is contained in:
@@ -9,7 +9,7 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace MiniSoftContracts.Data_Models
|
||||
{
|
||||
public class OrderDataModel(string id, string clientId, string employeeId, DateTime orderDate, double totalPrice, bool isCancelled, List<SoftwareOrderDataModel> installedSoftware) : IValidation
|
||||
public class OrderDataModel(string id, string clientId, string employeeId, DateTime orderDate, double totalPrice, bool isCancelled, List<SoftwareOrderDataModel> softwareOrders) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace MiniSoftContracts.Data_Models
|
||||
|
||||
public bool IsCancelled { get; private set; } = isCancelled;
|
||||
|
||||
public List<SoftwareOrderDataModel> InstalledSoftware { get; private set; } = installedSoftware;
|
||||
public List<SoftwareOrderDataModel>? InstalledSoftware { get; private set; } = softwareOrders;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
|
||||
@@ -17,12 +17,12 @@ namespace MiniSoftDatabase.Implementations
|
||||
_dbContext = dbContext;
|
||||
var config = new MapperConfiguration(cfg =>
|
||||
{
|
||||
cfg.CreateMap<SoftwareOrder, SoftwareOrderDataModel>();
|
||||
cfg.CreateMap<SoftwareOrderDataModel, SoftwareOrder>();
|
||||
cfg.CreateMap<OrderDataModel, Order>()
|
||||
.ForMember(dest => dest.IsCancelled, opt => opt.MapFrom(src => false))
|
||||
.ForMember(dest => dest.SoftwareOrders, opt => opt.MapFrom(src => src.InstalledSoftware));
|
||||
cfg.CreateMap<Order, OrderDataModel>();
|
||||
cfg.CreateMap<SoftwareOrder, SoftwareOrderDataModel>();
|
||||
cfg.CreateMap<SoftwareOrderDataModel, SoftwareOrder>();
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
|
||||
@@ -29,16 +29,16 @@ namespace MiniSoftDatabase.Implementations
|
||||
.ForMember(x => x.IsActual, x => x.MapFrom(src => true))
|
||||
.ForMember(x => x.ChangeDate, x => x.MapFrom(src => DateTime.UtcNow));
|
||||
cfg.CreateMap<Post, PostDataModel>()
|
||||
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId));
|
||||
.ForMember(x => x.Id, x => x.MapFrom(src => src.PostId)); // проверить айди на совпадения
|
||||
});
|
||||
_mapper = new Mapper(config);
|
||||
}
|
||||
public List<PostDataModel> GetList(bool OnlyActual = true)
|
||||
public List<PostDataModel> GetList(bool onlyActual = true)
|
||||
{
|
||||
try
|
||||
{
|
||||
var query = _dbContext.Posts.AsQueryable();
|
||||
if (OnlyActual)
|
||||
if (onlyActual)
|
||||
{
|
||||
query = query.Where(x => x.IsActual);
|
||||
}
|
||||
|
||||
@@ -13,9 +13,9 @@ namespace MiniSoftDatabase.Models
|
||||
|
||||
public required string DeveloperName { get; set; }
|
||||
|
||||
public required string? PrevDeveloperName { get; set; }
|
||||
public string? PrevDeveloperName { get; set; }
|
||||
|
||||
public required string? PrevPrevDeveloperName { get; set; }
|
||||
public string? PrevPrevDeveloperName { get; set; }
|
||||
|
||||
[ForeignKey("DeveloperId")]
|
||||
public List<Software>? Softwares { get; set; }
|
||||
|
||||
@@ -11,7 +11,8 @@ namespace MiniSoftDatabase.Models
|
||||
{
|
||||
public required string Id { get; set; } = Guid.NewGuid().ToString();
|
||||
|
||||
public required string PostId { get; set; }
|
||||
// Основной Id для историчности
|
||||
public required string PostId { get; set; }
|
||||
|
||||
public required string PostName { get; set; }
|
||||
|
||||
|
||||
@@ -121,8 +121,6 @@ namespace MiniSoftTests.StorageContractsTests
|
||||
{
|
||||
var Developer = CreateModel(Guid.NewGuid().ToString(), "name new", "test", "prev");
|
||||
InsertDeveloperToDatabaseAndReturn(Developer.Id, DeveloperName: Developer.PrevDeveloperName!, prevDeveloperName: Developer.PrevPrevDeveloperName!);
|
||||
var developerBeforeUpdate = GetDeveloperFromDatabase(developer.Id);
|
||||
Assert.That(developerBeforeUpdate, Is.Not.Null);
|
||||
_developerStorageContract.UpdateElement(CreateModel(Developer.Id, "name new", "some name", "some name"));
|
||||
AssertElement(GetDeveloperFromDatabase(Developer.Id), Developer);
|
||||
}
|
||||
@@ -211,17 +209,5 @@ namespace MiniSoftTests.StorageContractsTests
|
||||
Assert.That(actual.PrevPrevDeveloperName, Is.EqualTo(expected.PrevPrevDeveloperName));
|
||||
});
|
||||
}
|
||||
|
||||
private static void AssertUpdatedElement(Developer? actual, Developer beforeUpdate, DeveloperDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.DeveloperName, Is.EqualTo(expected.DeveloperName));
|
||||
Assert.That(actual.PrevDeveloperName, Is.EqualTo(beforeUpdate.DeveloperName));
|
||||
Assert.That(actual.PrevPrevDeveloperName, Is.EqualTo(beforeUpdate.PrevDeveloperName));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,313 @@
|
||||
using MiniSoftContracts.Data_Models;
|
||||
using MiniSoftContracts.Enums;
|
||||
using MiniSoftContracts.Exceptions;
|
||||
using MiniSoftDatabase.Implementations;
|
||||
using MiniSoftDatabase.Models;
|
||||
using MiniSoftDatabase;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace MiniSoftTests.StorageContractsTests
|
||||
{
|
||||
internal class OrderStorageContractContractTest : BaseStorageContractTest
|
||||
{
|
||||
private OrderStorageContract _orderStorageContract;
|
||||
private Client _client;
|
||||
private Employee _employee;
|
||||
private Software _software;
|
||||
private Developer _developer;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_orderStorageContract = new OrderStorageContract(MiniSoftDbContext);
|
||||
_developer = InsertDeveloperToDatabaseAndReturn();
|
||||
_client = InsertClientToDatabaseAndReturn();
|
||||
_employee = InsertEmployeeToDatabaseAndReturn();
|
||||
_software = InsertSoftwareToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MiniSoftDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Orders\" CASCADE;");
|
||||
MiniSoftDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Clients\" CASCADE;");
|
||||
MiniSoftDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Employees\" CASCADE;");
|
||||
MiniSoftDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Softwares\" CASCADE;");
|
||||
MiniSoftDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Developers\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var Order = InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 5)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 10)]);
|
||||
var list = _orderStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == Order.Id), Order);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _orderStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByPeriod_Test()
|
||||
{
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, OrderDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, OrderDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, OrderDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, OrderDate: DateTime.UtcNow.AddDays(1).AddMinutes(3), Softwares: [(_software.Id, 1)]);
|
||||
var list = _orderStorageContract.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");
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(Employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
var list = _orderStorageContract.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 name", "+8-888-888-88-88");
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, Client.Id, Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
var list = _orderStorageContract.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_BySoftwareId_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn("Other name");
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 5)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1), (Software.Id, 4)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(Software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(Software.Id, 1), (_software.Id, 1)]);
|
||||
var list = _orderStorageContract.GetList(softwareId: _software.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
Assert.That(list.All(x => x.InstalledSoftware.Any(y => y.SoftwareId == _software.Id)));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByAllParameters_Test()
|
||||
{
|
||||
var Employee = InsertEmployeeToDatabaseAndReturn("Other Employee");
|
||||
var Client = InsertClientToDatabaseAndReturn("Other name", "+8-888-888-88-88");
|
||||
var Software = InsertSoftwareToDatabaseAndReturn("Other name");
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, OrderDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-3), Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(Employee.Id, Client.Id, OrderDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(Employee.Id, _client.Id, OrderDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(Employee.Id, _client.Id, OrderDate: DateTime.UtcNow.AddDays(-1).AddMinutes(3), Softwares: [(Software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, Client.Id, OrderDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), Softwares: [(_software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, Client.Id, OrderDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), Softwares: [(Software.Id, 1)]);
|
||||
InsertOrderToDatabaseAndReturn(Employee.Id, _client.Id, OrderDate: DateTime.UtcNow.AddDays(1).AddMinutes(-3), Softwares: [(_software.Id, 1)]);
|
||||
var list = _orderStorageContract.GetList(startDate: DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1), employeeId: _employee.Id, clientId: _client.Id, softwareId: Software.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var Order = InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
AssertElement(_orderStorageContract.GetElementById(Order.Id), Order);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)]);
|
||||
Assert.That(() => _orderStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasCanceled_Test()
|
||||
{
|
||||
var Order = InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)], isCancel: true);
|
||||
AssertElement(_orderStorageContract.GetElementById(Order.Id), Order);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var Order = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, 1, false, [_software.Id]);
|
||||
_orderStorageContract.AddElement(Order);
|
||||
AssertElement(GetOrderFromDatabaseById(Order.Id), Order);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var Order = CreateModel(Guid.NewGuid().ToString(), _employee.Id, _client.Id, 1, true, [_software.Id]);
|
||||
Assert.That(() => _orderStorageContract.AddElement(Order), Throws.Nothing);
|
||||
AssertElement(GetOrderFromDatabaseById(Order.Id), CreateModel(Order.Id, _employee.Id, _client.Id, 1, false, [_software.Id]));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_Test()
|
||||
{
|
||||
var Order = InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)], isCancel: false);
|
||||
_orderStorageContract.DeleteElement(Order.Id);
|
||||
var element = GetOrderFromDatabaseById(Order.Id);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsCancelled);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _orderStorageContract.DeleteElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_WhenRecordWasCanceled_Test()
|
||||
{
|
||||
var Order = InsertOrderToDatabaseAndReturn(_employee.Id, _client.Id, Softwares: [(_software.Id, 1)], isCancel: true);
|
||||
Assert.That(() => _orderStorageContract.DeleteElement(Order.Id), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
private Client InsertClientToDatabaseAndReturn(string name = "test", string phoneNumber = "+7-777-777-77-77")
|
||||
{
|
||||
var Client = new Client() { Id = Guid.NewGuid().ToString(), Name = name, PhoneNumber = phoneNumber };
|
||||
MiniSoftDbContext.Clients.Add(Client);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return Client;
|
||||
}
|
||||
|
||||
private Employee InsertEmployeeToDatabaseAndReturn(string name = "Фамилия Имя")
|
||||
{
|
||||
var Employee = new Employee() { Id = Guid.NewGuid().ToString(), FullName = name, PostId = Guid.NewGuid().ToString() };
|
||||
MiniSoftDbContext.Employees.Add(Employee);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return Employee;
|
||||
}
|
||||
|
||||
private Developer InsertDeveloperToDatabaseAndReturn()
|
||||
{
|
||||
var dev = new Developer() { Id = Guid.NewGuid().ToString(), DeveloperName = "name" };
|
||||
MiniSoftDbContext.Developers.Add(dev);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return dev;
|
||||
}
|
||||
|
||||
private Software InsertSoftwareToDatabaseAndReturn(string SoftwareName = "test", SoftwareType SoftwareType = SoftwareType.OperatingSystem, double price = 1, bool isDeleted = false)
|
||||
{
|
||||
var Software = new Software() { Id = Guid.NewGuid().ToString(), DeveloperId = _developer.Id, SoftwareName = SoftwareName, SoftwareType = SoftwareType, Price = price, IsDeleted = isDeleted };
|
||||
MiniSoftDbContext.Softwares.Add(Software);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return Software;
|
||||
}
|
||||
|
||||
private Order InsertOrderToDatabaseAndReturn(string EmployeeId, string ClientId, DateTime? OrderDate = null, double sum = 1, bool isCancel = false, List<(string, int)>? Softwares = null)
|
||||
{
|
||||
var Order = new Order() { Id = Guid.NewGuid().ToString(), ClientId = ClientId, EmployeeId = EmployeeId, OrderDate = OrderDate ?? DateTime.UtcNow, TotalPrice = sum, IsCancelled = isCancel, SoftwareOrders = [] };
|
||||
if (Softwares is not null)
|
||||
{
|
||||
foreach (var elem in Softwares)
|
||||
{
|
||||
Order.SoftwareOrders.Add(new SoftwareOrder { SoftwareId = elem.Item1, OrderId = Order.Id, Price = 1 });
|
||||
}
|
||||
}
|
||||
MiniSoftDbContext.Orders.Add(Order);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return Order;
|
||||
}
|
||||
|
||||
private static void AssertElement(OrderDataModel? actual, Order 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.IsCancelled, Is.EqualTo(expected.IsCancelled));
|
||||
});
|
||||
|
||||
if (expected.SoftwareOrders is not null)
|
||||
{
|
||||
Assert.That(actual.InstalledSoftware, Is.Not.Null);
|
||||
Assert.That(actual.InstalledSoftware, Has.Count.EqualTo(expected.SoftwareOrders.Count));
|
||||
for (int i = 0; i < actual.InstalledSoftware.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.InstalledSoftware[i].SoftwareId, Is.EqualTo(expected.SoftwareOrders[i].SoftwareId));
|
||||
Assert.That(actual.InstalledSoftware[i].Price, Is.EqualTo(expected.SoftwareOrders[i].Price));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.InstalledSoftware, Is.Null);
|
||||
}
|
||||
}
|
||||
|
||||
private static OrderDataModel CreateModel(string id, string EmployeeId, string ClientId, double sum, bool isCancel, List<string> SoftwareIds)
|
||||
{
|
||||
var Softwares = SoftwareIds.Select(x => new SoftwareOrderDataModel(id, x, 1)).ToList();
|
||||
return new(id, ClientId, EmployeeId, DateTime.UtcNow, sum, isCancel, Softwares);
|
||||
}
|
||||
|
||||
private Order? GetOrderFromDatabaseById(string id) => MiniSoftDbContext.Orders.Include(x => x.SoftwareOrders).FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Order? actual, OrderDataModel 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.IsCancelled, Is.EqualTo(expected.IsCancelled));
|
||||
});
|
||||
|
||||
if (expected.InstalledSoftware is not null)
|
||||
{
|
||||
Assert.That(actual.SoftwareOrders, Is.Not.Null);
|
||||
Assert.That(actual.SoftwareOrders, Has.Count.EqualTo(expected.InstalledSoftware.Count));
|
||||
for (int i = 0; i < actual.SoftwareOrders.Count; ++i)
|
||||
{
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.SoftwareOrders[i].SoftwareId, Is.EqualTo(expected.InstalledSoftware[i].SoftwareId));
|
||||
Assert.That(actual.SoftwareOrders[i].Price, Is.EqualTo(expected.InstalledSoftware[i].Price));
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.That(actual.SoftwareOrders, Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MiniSoftContracts.Data_Models;
|
||||
using MiniSoftContracts.Enums;
|
||||
using MiniSoftContracts.Exceptions;
|
||||
using MiniSoftDatabase.Implementations;
|
||||
using MiniSoftDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MiniSoftTests.StorageContractsTests
|
||||
{
|
||||
internal class PostStorageContractTest : BaseStorageContractTest
|
||||
{
|
||||
private PostStorageContract _postStorageContract;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_postStorageContract = new PostStorageContract(MiniSoftDbContext);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MiniSoftDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Posts\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
|
||||
var list = _postStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == post.PostId), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _postStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_OnlyActual_Test()
|
||||
{
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isActual: false);
|
||||
var list = _postStorageContract.GetList(onlyActual: true);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(!list.Any(x => !x.IsActual));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_IncludeNoActual_Test()
|
||||
{
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isActual: false);
|
||||
var list = _postStorageContract.GetList(onlyActual: false);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
Assert.That(list.Count(x => x.IsActual), Is.EqualTo(2));
|
||||
Assert.That(list.Count(x => !x.IsActual), Is.EqualTo(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetPostWithHistory_WhenHaveRecords_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
||||
var list = _postStorageContract.GetPostWithHistory(postId);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetPostWithHistory_WhenNoRecords_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(postId, "name 2", isActual: false);
|
||||
var list = _postStorageContract.GetPostWithHistory(Guid.NewGuid().ToString());
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_postStorageContract.GetElementById(post.PostId), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _postStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.PostId), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenTrySearchById_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.Id), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_postStorageContract.GetElementByName(post.PostName), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||
{
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _postStorageContract.GetElementByName("name"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||
Assert.That(() => _postStorageContract.GetElementById(post.PostName), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), isActual: true);
|
||||
_postStorageContract.AddElement(post);
|
||||
AssertElement(GetPostFromDatabaseByPostId(post.Id), post);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenActualIsFalse_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), isActual: false);
|
||||
Assert.That(() => _postStorageContract.AddElement(post), Throws.Nothing);
|
||||
AssertElement(GetPostFromDatabaseByPostId(post.Id), CreateModel(post.Id, isActual: true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), "name unique", isActual: true);
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), postName: post.PostName, isActual: true);
|
||||
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSamePostIdAndActualIsTrue_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), isActual: true);
|
||||
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
||||
Assert.That(() => _postStorageContract.AddElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
||||
_postStorageContract.UpdateElement(post);
|
||||
var posts = MiniSoftDbContext.Posts.Where(x => x.PostId == post.Id).OrderByDescending(x => x.ChangeDate);
|
||||
Assert.That(posts.Count(), Is.EqualTo(2));
|
||||
AssertElement(posts.First(), CreateModel(post.Id, isActual: true));
|
||||
AssertElement(posts.Last(), CreateModel(post.Id, isActual: false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenActualIsFalse_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), isActual: false);
|
||||
InsertPostToDatabaseAndReturn(post.Id, isActual: true);
|
||||
_postStorageContract.UpdateElement(post);
|
||||
AssertElement(GetPostFromDatabaseByPostId(post.Id), CreateModel(post.Id, isActual: true));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.UpdateElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), "New Name");
|
||||
InsertPostToDatabaseAndReturn(post.Id, postName: "name");
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), postName: post.PostName);
|
||||
Assert.That(() => _postStorageContract.UpdateElement(post), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertPostToDatabaseAndReturn(post.Id, isActual: false);
|
||||
Assert.That(() => _postStorageContract.UpdateElement(post), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
|
||||
_postStorageContract.DeleteElement(post.PostId);
|
||||
var element = GetPostFromDatabaseByPostId(post.PostId);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(!element!.IsActual);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.DeleteElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||
Assert.That(() => _postStorageContract.DeleteElement(post.PostId), Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: false);
|
||||
_postStorageContract.RestoreElement(post.PostId);
|
||||
var element = GetPostFromDatabaseByPostId(post.PostId);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsActual);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _postStorageContract.RestoreElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_ResElement_WhenRecordNotWasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), isActual: true);
|
||||
Assert.That(() => _postStorageContract.RestoreElement(post.PostId), Throws.Nothing);
|
||||
}
|
||||
|
||||
private Post InsertPostToDatabaseAndReturn(string id, string postName = "test", PostType postType = PostType.Consultant, double salary = 10, bool isActual = true, DateTime? changeDate = null)
|
||||
{
|
||||
var post = new Post() { Id = Guid.NewGuid().ToString(), PostId = id, PostName = postName, PostType = postType, Salary = salary, IsActual = isActual, ChangeDate = changeDate ?? DateTime.UtcNow };
|
||||
MiniSoftDbContext.Posts.Add(post);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return post;
|
||||
}
|
||||
|
||||
private static void AssertElement(PostDataModel? actual, Post expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.PostId));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
||||
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
|
||||
Assert.That(actual.Salary, Is.EqualTo(expected.Salary));
|
||||
Assert.That(actual.IsActual, Is.EqualTo(expected.IsActual));
|
||||
});
|
||||
}
|
||||
|
||||
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.Consultant, double salary = 10, bool isActual = false, DateTime? changeDate = null)
|
||||
=> new(postId, postName, postType, salary, isActual, changeDate ?? DateTime.UtcNow);
|
||||
|
||||
private Post? GetPostFromDatabaseByPostId(string id) => MiniSoftDbContext.Posts.Where(x => x.PostId == id).OrderByDescending(x => x.ChangeDate).FirstOrDefault();
|
||||
|
||||
private static void AssertElement(Post? actual, PostDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.PostId, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.PostName, Is.EqualTo(expected.PostName));
|
||||
Assert.That(actual.PostType, Is.EqualTo(expected.PostType));
|
||||
Assert.That(actual.Salary, Is.EqualTo(expected.Salary));
|
||||
Assert.That(actual.IsActual, Is.EqualTo(expected.IsActual));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,356 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MiniSoftContracts.Data_Models;
|
||||
using MiniSoftContracts.Enums;
|
||||
using MiniSoftContracts.Exceptions;
|
||||
using MiniSoftDatabase.Implementations;
|
||||
using MiniSoftDatabase.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MiniSoftTests.StorageContractsTests
|
||||
{
|
||||
internal class SoftwareStorageContractTest : BaseStorageContractTest
|
||||
{
|
||||
private SoftwareStorageContract _softwareStorageContract;
|
||||
private Developer _developer;
|
||||
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_softwareStorageContract = new SoftwareStorageContract(MiniSoftDbContext);
|
||||
_developer = InsertDeveloperToDatabaseAndReturn();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
MiniSoftDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Softwares\" CASCADE;");
|
||||
MiniSoftDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Developers\" CASCADE;");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 1");
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 2");
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 3");
|
||||
var list = _softwareStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == Software.Id), Software);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _softwareStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_OnlyActual_Test()
|
||||
{
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 1", isDeleted: true);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 2", isDeleted: false);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 3", isDeleted: false);
|
||||
var list = _softwareStorageContract.GetList(onlyActive: true);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(!list.Any(x => x.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_IncludeNoActual_Test()
|
||||
{
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 1", isDeleted: true);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 2", isDeleted: true);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 3", isDeleted: false);
|
||||
var list = _softwareStorageContract.GetList(onlyActive: false);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
Assert.That(list.Count(x => x.IsDeleted), Is.EqualTo(2));
|
||||
Assert.That(list.Count(x => !x.IsDeleted), Is.EqualTo(1));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByDeveloper_Test()
|
||||
{
|
||||
var manufacruer = InsertDeveloperToDatabaseAndReturn("name 2");
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 1", isDeleted: true);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 2", isDeleted: false);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), manufacruer.Id, "name 3", isDeleted: false);
|
||||
var list = _softwareStorageContract.GetList(devId: _developer.Id, onlyActive: false);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2)); //???
|
||||
Assert.That(list.All(x => x.DeveloperId == _developer.Id));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetList_ByDeveloperOnlyActual_Test()
|
||||
{
|
||||
var manufacruer = InsertDeveloperToDatabaseAndReturn("name 2");
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 1", isDeleted: true);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 2", isDeleted: false);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), manufacruer.Id, "name 3", isDeleted: false);
|
||||
var list = _softwareStorageContract.GetList(devId: _developer.Id, onlyActive: true);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(1));
|
||||
Assert.That(list.All(x => x.DeveloperId == _developer.Id && !x.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetHistoryBySoftwareId_WhenHaveRecords_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 1");
|
||||
InsertSoftwareHistoryToDatabaseAndReturn(Software.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertSoftwareHistoryToDatabaseAndReturn(Software.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertSoftwareHistoryToDatabaseAndReturn(Software.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _softwareStorageContract.GetHistoryById(Software.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetHistoryBySoftwareId_WhenNoRecords_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, "name 1");
|
||||
InsertSoftwareHistoryToDatabaseAndReturn(Software.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertSoftwareHistoryToDatabaseAndReturn(Software.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertSoftwareHistoryToDatabaseAndReturn(Software.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _softwareStorageContract.GetHistoryById(Guid.NewGuid().ToString());
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id);
|
||||
AssertElement(_softwareStorageContract.GetElementById(Software.Id), Software);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id);
|
||||
Assert.That(() => _softwareStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, isDeleted: true);
|
||||
Assert.That(() => _softwareStorageContract.GetElementById(Software.Id), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id);
|
||||
AssertElement(_softwareStorageContract.GetElementByName(Software.SoftwareName), Software);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||
{
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id);
|
||||
Assert.That(() => _softwareStorageContract.GetElementByName("name"), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, isDeleted: true);
|
||||
Assert.That(() => _softwareStorageContract.GetElementById(Software.SoftwareName), Is.Null);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id, isDeleted: false);
|
||||
_softwareStorageContract.AddElement(Software);
|
||||
AssertElement(GetSoftwareFromDatabaseById(Software.Id), Software);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id, isDeleted: true);
|
||||
Assert.That(() => _softwareStorageContract.AddElement(Software), Throws.Nothing);
|
||||
AssertElement(GetSoftwareFromDatabaseById(Software.Id), CreateModel(Software.Id, _developer.Id, isDeleted: false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id);
|
||||
InsertSoftwareToDatabaseAndReturn(Software.Id, _developer.Id, SoftwareName: "name unique");
|
||||
Assert.That(() => _softwareStorageContract.AddElement(Software), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id, "name unique", isDeleted: false);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, SoftwareName: Software.SoftwareName, isDeleted: false);
|
||||
Assert.That(() => _softwareStorageContract.AddElement(Software), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id, "name unique", isDeleted: false);
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, SoftwareName: Software.SoftwareName, isDeleted: true);
|
||||
Assert.That(() => _softwareStorageContract.AddElement(Software), Throws.Nothing);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdateElement_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id, isDeleted: false);
|
||||
InsertSoftwareToDatabaseAndReturn(Software.Id, _developer.Id, isDeleted: false);
|
||||
_softwareStorageContract.UpdateElement(Software);
|
||||
AssertElement(GetSoftwareFromDatabaseById(Software.Id), Software);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdateElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id, isDeleted: true);
|
||||
InsertSoftwareToDatabaseAndReturn(Software.Id, _developer.Id, isDeleted: false);
|
||||
_softwareStorageContract.UpdateElement(Software);
|
||||
AssertElement(GetSoftwareFromDatabaseById(Software.Id), CreateModel(Software.Id, _developer.Id, isDeleted: false));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdateElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _softwareStorageContract.UpdateElement(CreateModel(Guid.NewGuid().ToString(), _developer.Id)), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdateElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id, "name unique", isDeleted: false);
|
||||
InsertSoftwareToDatabaseAndReturn(Software.Id, _developer.Id, SoftwareName: "name");
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, SoftwareName: Software.SoftwareName);
|
||||
Assert.That(() => _softwareStorageContract.UpdateElement(Software), Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdateElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id, "name unique", isDeleted: false);
|
||||
InsertSoftwareToDatabaseAndReturn(Software.Id, _developer.Id, SoftwareName: "name");
|
||||
InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, SoftwareName: Software.SoftwareName, isDeleted: true);
|
||||
Assert.That(() => _softwareStorageContract.UpdateElement(Software), Throws.Nothing);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_UpdateElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var Software = CreateModel(Guid.NewGuid().ToString(), _developer.Id);
|
||||
InsertSoftwareToDatabaseAndReturn(Software.Id, _developer.Id, isDeleted: true);
|
||||
Assert.That(() => _softwareStorageContract.UpdateElement(Software), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, isDeleted: false);
|
||||
_softwareStorageContract.DeleteElement(Software.Id);
|
||||
var element = GetSoftwareFromDatabaseById(Software.Id);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsDeleted);
|
||||
});
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() => _softwareStorageContract.DeleteElement(Guid.NewGuid().ToString()), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Try_DeleteElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var Software = InsertSoftwareToDatabaseAndReturn(Guid.NewGuid().ToString(), _developer.Id, isDeleted: true);
|
||||
Assert.That(() => _softwareStorageContract.DeleteElement(Software.Id), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Developer InsertDeveloperToDatabaseAndReturn(string DeveloperName = "name")
|
||||
{
|
||||
var developer = new Developer() { Id = Guid.NewGuid().ToString(), DeveloperName = DeveloperName };
|
||||
MiniSoftDbContext.Developers.Add(developer);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return developer;
|
||||
}
|
||||
|
||||
private Software InsertSoftwareToDatabaseAndReturn(string id, string DeveloperId, string SoftwareName = "test", SoftwareType SoftwareType = SoftwareType.OperatingSystem, double price = 1, bool isDeleted = false)
|
||||
{
|
||||
var Software = new Software() { Id = id, DeveloperId = DeveloperId, SoftwareName = SoftwareName, SoftwareType = SoftwareType, Price = price, IsDeleted = isDeleted };
|
||||
MiniSoftDbContext.Softwares.Add(Software);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return Software;
|
||||
}
|
||||
|
||||
private SoftwareHistory InsertSoftwareHistoryToDatabaseAndReturn(string SoftwareId, double price, DateTime changeDate)
|
||||
{
|
||||
var SoftwareHistory = new SoftwareHistory() { Id = Guid.NewGuid().ToString(), SoftwareId = SoftwareId, OldPrice = price, ChangeDate = changeDate };
|
||||
MiniSoftDbContext.SoftwareHistories.Add(SoftwareHistory);
|
||||
MiniSoftDbContext.SaveChanges();
|
||||
return SoftwareHistory;
|
||||
}
|
||||
|
||||
private static void AssertElement(SoftwareDataModel? actual, Software expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.DeveloperId, Is.EqualTo(expected.DeveloperId));
|
||||
Assert.That(actual.SoftwareName, Is.EqualTo(expected.SoftwareName));
|
||||
Assert.That(actual.SoftwareType, Is.EqualTo(expected.SoftwareType));
|
||||
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
|
||||
private static SoftwareDataModel CreateModel(string id, string DeveloperId, string SoftwareName = "test", SoftwareType SoftwareType = SoftwareType.OperatingSystem, double price = 1, bool isDeleted = false)
|
||||
=> new(id, SoftwareName, SoftwareType, DeveloperId, price, isDeleted);
|
||||
|
||||
private Software? GetSoftwareFromDatabaseById(string id) => MiniSoftDbContext.Softwares.FirstOrDefault(x => x.Id == id);
|
||||
|
||||
private static void AssertElement(Software? actual, SoftwareDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.DeveloperId, Is.EqualTo(expected.DeveloperId));
|
||||
Assert.That(actual.SoftwareName, Is.EqualTo(expected.SoftwareName));
|
||||
Assert.That(actual.SoftwareType, Is.EqualTo(expected.SoftwareType));
|
||||
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user