362 lines
14 KiB
C#
362 lines
14 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using PuferFishContracts.DataModels;
|
|
using PuferFishContracts.Enums;
|
|
using PuferFishContracts.Exceptions;
|
|
using PuferFishDataBase.Implementations;
|
|
using PuferFishDataBase.Models;
|
|
|
|
namespace PuferFishTests.StoragesContracts;
|
|
|
|
[TestFixture]
|
|
internal class SaleStorageContractTests : BaseStorageContractTest
|
|
{
|
|
private SaleStorageContract _saletStorageContract;
|
|
private Buyer _buyer;
|
|
private Worker _worker;
|
|
private Product _product;
|
|
[SetUp]
|
|
public void SetUp()
|
|
{
|
|
_saletStorageContract = new SaleStorageContract(PuferFishDbContext);
|
|
_buyer = InsertBuyerToDatabaseAndReturn();
|
|
_worker = InsertWorkerToDatabaseAndReturn();
|
|
_product = InsertProductToDatabaseAndReturn();
|
|
}
|
|
[TearDown]
|
|
public void TearDown()
|
|
{
|
|
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Sales\"CASCADE; ");
|
|
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Buyers\"CASCADE; ");
|
|
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\"CASCADE; ");
|
|
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Products\"CASCADE; ");
|
|
}
|
|
[Test]
|
|
public void Try_GetList_WhenHaveRecords_Test()
|
|
{
|
|
var sale = InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id,
|
|
products: [(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, products:
|
|
[(_product.Id, 5, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, null, products:
|
|
[(_product.Id, 10, 1.2)]);
|
|
var list = _saletStorageContract.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 = _saletStorageContract.GetList();
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Is.Empty);
|
|
}
|
|
[Test]
|
|
public void Try_GetList_ByPeriod_Test()
|
|
{
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, saleDate:
|
|
DateTime.UtcNow.AddDays(-1).AddMinutes(-3), products: [(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, saleDate:
|
|
DateTime.UtcNow.AddDays(-1).AddMinutes(3), products: [(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, null, saleDate:
|
|
DateTime.UtcNow.AddDays(1).AddMinutes(-3), products: [(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, null, saleDate:
|
|
DateTime.UtcNow.AddDays(1).AddMinutes(3), products: [(_product.Id, 1, 1.2)]);
|
|
var list = _saletStorageContract.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_ByWorkerId_Test()
|
|
{
|
|
var worker = InsertWorkerToDatabaseAndReturn("Other worker");
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, products:
|
|
[(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, products:
|
|
[(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(worker.Id, null, products:
|
|
[(_product.Id, 1, 1.2)]);
|
|
var list = _saletStorageContract.GetList(workerId: _worker.Id);
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(2));
|
|
Assert.That(list.All(x => x.WorkerId == _worker.Id));
|
|
}
|
|
[Test]
|
|
public void Try_GetList_ByBuyerId_Test()
|
|
{
|
|
var buyer = InsertBuyerToDatabaseAndReturn("Other fio", "+8-888-888-88 - 88");
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, products:
|
|
[(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, products:
|
|
[(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, buyer.Id, products:
|
|
[(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, null, products:
|
|
[(_product.Id, 1, 1.2)]);
|
|
var list = _saletStorageContract.GetList(buyerId: _buyer.Id);
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(2));
|
|
Assert.That(list.All(x => x.BuyerId == _buyer.Id));
|
|
}
|
|
[Test]
|
|
public void Try_GetList_ByProductId_Test()
|
|
{
|
|
var product = InsertProductToDatabaseAndReturn("Other name");
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, products:
|
|
[(_product.Id, 5, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, products:
|
|
[(_product.Id, 1, 1.2), (product.Id, 4, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, null, products:
|
|
[(product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, null, products:
|
|
[(product.Id, 1, 1.2), (_product.Id, 1, 1.2)]);
|
|
var list = _saletStorageContract.GetList(productId: _product.Id);
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(3));
|
|
Assert.That(list.All(x => x.Products!.Any(y => y.ProductId ==
|
|
_product.Id)));
|
|
}
|
|
[Test]
|
|
public void Try_GetList_ByAllParameters_Test()
|
|
{
|
|
var worker = InsertWorkerToDatabaseAndReturn("Other worker");
|
|
var buyer = InsertBuyerToDatabaseAndReturn("Other fio", "+8-888-888-88 - 88");
|
|
var product = InsertProductToDatabaseAndReturn("Other name");
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, saleDate:
|
|
DateTime.UtcNow.AddDays(-1).AddMinutes(-3), products: [(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(worker.Id, null, saleDate:
|
|
DateTime.UtcNow.AddDays(-1).AddMinutes(3), products: [(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(worker.Id, _buyer.Id, saleDate:
|
|
DateTime.UtcNow.AddDays(-1).AddMinutes(3), products: [(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(worker.Id, _buyer.Id, saleDate:
|
|
DateTime.UtcNow.AddDays(-1).AddMinutes(3), products: [(product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, buyer.Id, saleDate:
|
|
DateTime.UtcNow.AddDays(1).AddMinutes(-3), products: [(_product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, saleDate:
|
|
DateTime.UtcNow.AddDays(1).AddMinutes(-3), products: [(product.Id, 1, 1.2)]);
|
|
InsertSaleToDatabaseAndReturn(worker.Id, null, saleDate:
|
|
DateTime.UtcNow.AddDays(1).AddMinutes(-3), products: [(_product.Id, 1, 1.2)]);
|
|
var list = _saletStorageContract.GetList(startDate:
|
|
DateTime.UtcNow.AddDays(-1), endDate: DateTime.UtcNow.AddDays(1), workerId:
|
|
_worker.Id, buyerId: _buyer.Id, productId: product.Id);
|
|
Assert.That(list, Is.Not.Null);
|
|
Assert.That(list, Has.Count.EqualTo(1));
|
|
}
|
|
[Test]
|
|
public void Try_GetElementById_WhenHaveRecord_Test()
|
|
{
|
|
var sale = InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id,
|
|
products: [(_product.Id, 1, 1.2)]);
|
|
AssertElement(_saletStorageContract.GetElementById(sale.Id), sale);
|
|
}
|
|
[Test]
|
|
public void Try_GetElementById_WhenNoRecord_Test()
|
|
{
|
|
InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id, products:
|
|
[(_product.Id, 1, 1.2)]);
|
|
Assert.That(() =>
|
|
_saletStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
|
}
|
|
[Test]
|
|
public void Try_GetElementById_WhenRecordHasCanceled_Test()
|
|
{
|
|
var sale = InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id,
|
|
products: [(_product.Id, 1, 1.2)], isCancel: true);
|
|
AssertElement(_saletStorageContract.GetElementById(sale.Id), sale);
|
|
}
|
|
[Test]
|
|
public void Try_AddElement_Test()
|
|
{
|
|
var sale = CreateModel(Guid.NewGuid().ToString(), _worker.Id,
|
|
_buyer.Id, false, [_product.Id]);
|
|
_saletStorageContract.AddElement(sale);
|
|
AssertElement(GetSaleFromDatabaseById(sale.Id), sale);
|
|
}
|
|
[Test]
|
|
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
|
{
|
|
var sale = CreateModel(Guid.NewGuid().ToString(), _worker.Id,
|
|
_buyer.Id, true, [_product.Id]);
|
|
Assert.That(() => _saletStorageContract.AddElement(sale),
|
|
Throws.Nothing);
|
|
AssertElement(GetSaleFromDatabaseById(sale.Id), CreateModel(sale.Id,
|
|
_worker.Id, _buyer.Id, false, [_product.Id]));
|
|
}
|
|
[Test]
|
|
public void Try_DelElement_Test()
|
|
{
|
|
var sale = InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id,
|
|
products: [(_product.Id, 1, 1.2)], isCancel: false);
|
|
_saletStorageContract.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(() =>
|
|
_saletStorageContract.DelElement(Guid.NewGuid().ToString()),
|
|
Throws.TypeOf<ElementNotFoundException>());
|
|
}
|
|
[Test]
|
|
public void Try_DelElement_WhenRecordWasCanceled_Test()
|
|
{
|
|
var sale = InsertSaleToDatabaseAndReturn(_worker.Id, _buyer.Id,
|
|
products: [(_product.Id, 1, 1.2)], isCancel: true);
|
|
Assert.That(() => _saletStorageContract.DelElement(sale.Id),
|
|
Throws.TypeOf<ElementDeletedException>());
|
|
}
|
|
private Buyer InsertBuyerToDatabaseAndReturn(string fio = "test", string phoneNumber = "+7-777-777-77-77")
|
|
{
|
|
var buyer = new Buyer()
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
FIO = fio,
|
|
PhoneNumber = phoneNumber,
|
|
Points = 10
|
|
};
|
|
PuferFishDbContext.Buyers.Add(buyer);
|
|
PuferFishDbContext.SaveChanges();
|
|
return buyer;
|
|
}
|
|
private Worker InsertWorkerToDatabaseAndReturn(string fio = "test")
|
|
{
|
|
var worker = new Worker()
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
FIO =
|
|
fio,
|
|
PostId = Guid.NewGuid().ToString()
|
|
};
|
|
PuferFishDbContext.Workers.Add(worker);
|
|
PuferFishDbContext.SaveChanges();
|
|
return worker;
|
|
}
|
|
private Product InsertProductToDatabaseAndReturn(string productName =
|
|
"test", ProductType productType = ProductType.Onigiri, double price = 1, bool
|
|
isDeleted = false)
|
|
{
|
|
var product = new Product()
|
|
{
|
|
Id = Guid.NewGuid().ToString(),
|
|
ProductName = productName,
|
|
ProductType = productType,
|
|
Price = price,
|
|
IsDeleted = isDeleted
|
|
};
|
|
PuferFishDbContext.Products.Add(product);
|
|
PuferFishDbContext.SaveChanges();
|
|
return product;
|
|
}
|
|
private Sale InsertSaleToDatabaseAndReturn(string workerId, string?
|
|
buyerId, DateTime? saleDate = null, double sum = 1, double points = 0, bool isCancel = false, List<(string,
|
|
int, double)>? products = null)
|
|
{
|
|
var sale = new Sale()
|
|
{
|
|
WorkerId = workerId,
|
|
BuyerId = buyerId,
|
|
SaleDate = saleDate ?? DateTime.UtcNow,
|
|
Sum = sum,
|
|
Points = points,
|
|
IsCancel = isCancel,
|
|
SaleProducts = []
|
|
};
|
|
if (products is not null)
|
|
{
|
|
foreach (var elem in products)
|
|
{
|
|
sale.SaleProducts.Add(new SaleProduct
|
|
{
|
|
ProductId = elem.Item1,
|
|
SaleId = sale.Id,
|
|
Count = elem.Item2,
|
|
Price = elem.Item3
|
|
});
|
|
}
|
|
}
|
|
PuferFishDbContext.Sales.Add(sale);
|
|
PuferFishDbContext.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.WorkerId, Is.EqualTo(expected.WorkerId));
|
|
Assert.That(actual.BuyerId, Is.EqualTo(expected.BuyerId));
|
|
Assert.That(actual.Points, Is.EqualTo(expected.Points));
|
|
Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel));
|
|
});
|
|
if (expected.SaleProducts is not null)
|
|
{
|
|
Assert.That(actual.Products, Is.Not.Null);
|
|
Assert.That(actual.Products, Has.Count.EqualTo(expected.SaleProducts.Count));
|
|
for (int i = 0; i < actual.Products.Count; ++i)
|
|
{
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.Products[i].ProductId,
|
|
Is.EqualTo(expected.SaleProducts[i].ProductId));
|
|
Assert.That(actual.Products[i].Count,
|
|
Is.EqualTo(expected.SaleProducts[i].Count));
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Assert.That(actual.Products, Is.Null);
|
|
}
|
|
}
|
|
private static SaleDataModel CreateModel(string id, string workerId, string? buyerId, bool isCancel, List<string> productIds)
|
|
{
|
|
var products = productIds.Select(x => new SaleProductDataModel(id, x, 1, 1)).ToList();
|
|
return new(id, workerId, buyerId, isCancel, products);
|
|
}
|
|
private Sale? GetSaleFromDatabaseById(string id) =>
|
|
PuferFishDbContext.Sales.Include(x => x.SaleProducts).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.WorkerId, Is.EqualTo(expected.WorkerId));
|
|
Assert.That(actual.BuyerId, Is.EqualTo(expected.BuyerId));
|
|
Assert.That(actual.IsCancel, Is.EqualTo(expected.IsCancel));
|
|
});
|
|
if (expected.Products is not null)
|
|
{
|
|
Assert.That(actual.SaleProducts, Is.Not.Null);
|
|
Assert.That(actual.SaleProducts,
|
|
Has.Count.EqualTo(expected.Products.Count));
|
|
for (int i = 0; i < actual.SaleProducts.Count; ++i)
|
|
{
|
|
Assert.Multiple(() =>
|
|
{
|
|
Assert.That(actual.SaleProducts[i].ProductId,
|
|
Is.EqualTo(expected.Products[i].ProductId));
|
|
Assert.That(actual.SaleProducts[i].Count,
|
|
Is.EqualTo(expected.Products[i].Count));
|
|
});
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Assert.That(actual.SaleProducts, Is.Null);
|
|
}
|
|
}
|
|
}
|