В процессе
This commit is contained in:
parent
643a5b7280
commit
f8f8211f24
@ -11,13 +11,11 @@ using System.Xml;
|
||||
|
||||
namespace PuferFishContracts.DataModels
|
||||
{
|
||||
public class PostDataModel(string id, string postName, PostType postType, bool isActual, DateTime changeDate) : IValidation
|
||||
public class PostDataModel(string postId, string postName, PostType postType) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string Id { get; private set; } = postId;
|
||||
public string PostName { get; private set; } = postName;
|
||||
public PostType PostType { get; private set; } = postType;
|
||||
public bool IsActual { get; private set; } = isActual;
|
||||
public DateTime ChangeDate { get; private set; } = changeDate;
|
||||
public void Validate()
|
||||
{
|
||||
if (Id.IsEmpty())
|
||||
|
@ -7,19 +7,32 @@ using PuferFishContracts.Infrastructure;
|
||||
using PuferFishContracts.Extensions;
|
||||
using PuferFishContracts.Exceptions;
|
||||
using System.Xml;
|
||||
using PuferFishContracts.StoragesContracts;
|
||||
|
||||
namespace PuferFishContracts.DataModels;
|
||||
|
||||
public class SaleDataModel(string id, string workerId, string? buyerId, double sum, double points, bool isCancel, List<SaleProductDataModel> products) : IValidation
|
||||
public class SaleDataModel : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string WorkerId { get; private set; } = workerId;
|
||||
public string? BuyerId { get; private set; } = buyerId;
|
||||
public string Id { get; private set; }
|
||||
public string WorkerId { get; private set; }
|
||||
public string? BuyerId { get; private set; }
|
||||
public DateTime SaleDate { get; private set; } = DateTime.UtcNow;
|
||||
public double Sum { get; private set; } = sum;
|
||||
public double Points { get; private set; } = points;
|
||||
public bool IsCancel { get; private set; } = isCancel;
|
||||
public List<SaleProductDataModel> Products { get; private set; } = products;
|
||||
public double Sum { get; private set; }
|
||||
public double Points { get; private set; }
|
||||
public bool IsCancel { get; private set; }
|
||||
public List<SaleProductDataModel>? Products { get; private set; }
|
||||
|
||||
public SaleDataModel(string id, string workerId, string? buyerId, bool isCancel, List<SaleProductDataModel> saleProducts)
|
||||
{
|
||||
Id = id;
|
||||
WorkerId = workerId;
|
||||
BuyerId = buyerId;
|
||||
IsCancel = isCancel;
|
||||
Products = saleProducts;
|
||||
var percent = 0.5;
|
||||
Sum = Products?.Sum(x => x.Price * x.Count) ?? 0;
|
||||
Points = Sum * percent;
|
||||
}
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
|
@ -9,11 +9,12 @@ using PuferFishContracts.Infrastructure;
|
||||
|
||||
namespace PuferFishContracts.DataModels;
|
||||
|
||||
public class SaleProductDataModel(string saleId, string productId, int count) : IValidation
|
||||
public class SaleProductDataModel(string saleId, string productId, int count, int price) : IValidation
|
||||
{
|
||||
public string SaleId { get; private set; } = saleId;
|
||||
public string ProductId { get; private set; } = productId;
|
||||
public int Count { get; private set; } = count;
|
||||
public double Price { get; private set; } = price;
|
||||
public void Validate()
|
||||
{
|
||||
if (SaleId.IsEmpty())
|
||||
|
@ -16,4 +16,14 @@
|
||||
<ProjectReference Include="..\PuferFishContracts\PuferFishContracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="PuferFish" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="PuferFishTests" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -35,9 +35,9 @@ namespace PuferFishTests.BusinessLogicsContractsTests
|
||||
//Arrange
|
||||
var listOriginal = new List<PostDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(),"name 1", PostType.chef, true, DateTime.UtcNow),
|
||||
new(Guid.NewGuid().ToString(), "name 2", PostType.chef, false, DateTime.UtcNow),
|
||||
new(Guid.NewGuid().ToString(), "name 3", PostType.chef, true, DateTime.UtcNow),};
|
||||
new(Guid.NewGuid().ToString(), "name 1", PostType.chef),
|
||||
new(Guid.NewGuid().ToString(), "name 2", PostType.chef),
|
||||
new(Guid.NewGuid().ToString(), "name 3", PostType.chef),};
|
||||
_postStorageContract.Setup(x =>
|
||||
x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
||||
//Act
|
||||
@ -105,8 +105,8 @@ namespace PuferFishTests.BusinessLogicsContractsTests
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<PostDataModel>()
|
||||
{
|
||||
new(postId, "name 1", PostType.chef, true, DateTime.UtcNow),
|
||||
new(postId, "name 2", PostType.chef, false, DateTime.UtcNow)
|
||||
new(postId, "name 1", PostType.chef),
|
||||
new(postId, "name 2", PostType.chef)
|
||||
};
|
||||
_postStorageContract.Setup(x =>
|
||||
x.GetPostWithHistory(It.IsAny<string>())).Returns(listOriginal);
|
||||
@ -182,7 +182,7 @@ Throws.TypeOf<StorageException>());
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new PostDataModel(id, "name", PostType.chef, true, DateTime.UtcNow);
|
||||
var record = new PostDataModel(id, "name", PostType.chef);
|
||||
_postStorageContract.Setup(x =>
|
||||
x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
@ -198,7 +198,7 @@ Throws.TypeOf<StorageException>());
|
||||
{
|
||||
//Arrange
|
||||
var postName = "name";
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.chef, true, DateTime.UtcNow);
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), postName, PostType.chef);
|
||||
_postStorageContract.Setup(x =>
|
||||
x.GetElementByName(postName)).Returns(record);
|
||||
//Act
|
||||
@ -272,12 +272,12 @@ Throws.TypeOf<ElementNotFoundException>());
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.chef, true, DateTime.UtcNow.AddDays(-1));
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.chef);
|
||||
_postStorageContract.Setup(x =>
|
||||
x.AddElement(It.IsAny<PostDataModel>()))
|
||||
.Callback((PostDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType && x.ChangeDate == record.ChangeDate;
|
||||
flag = x.Id == record.Id && x.PostName == record.PostName && x.PostType == record.PostType;
|
||||
});
|
||||
//Act
|
||||
_postBusinessLogicContract.InsertPost(record);
|
||||
@ -295,7 +295,7 @@ Throws.TypeOf<ElementNotFoundException>());
|
||||
ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.chef, true, DateTime.UtcNow)),
|
||||
_postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.chef)),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
_postStorageContract.Verify(x =>
|
||||
x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
@ -314,7 +314,7 @@ Throws.TypeOf<ElementNotFoundException>());
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.InsertPost(new
|
||||
PostDataModel("id", "name", PostType.chef, true, DateTime.UtcNow)),
|
||||
PostDataModel("id", "name", PostType.chef)),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x =>
|
||||
x.AddElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
@ -328,7 +328,7 @@ Throws.TypeOf<ElementNotFoundException>());
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.chef, true, DateTime.UtcNow)),
|
||||
_postBusinessLogicContract.InsertPost(new(Guid.NewGuid().ToString(), "name", PostType.chef)),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x =>
|
||||
x.AddElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
@ -338,13 +338,13 @@ Throws.TypeOf<ElementNotFoundException>());
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.chef, true, DateTime.UtcNow.AddDays(-1));
|
||||
var record = new PostDataModel(Guid.NewGuid().ToString(), "name", PostType.chef);
|
||||
_postStorageContract.Setup(x =>
|
||||
x.UpdElement(It.IsAny<PostDataModel>()))
|
||||
.Callback((PostDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.PostName ==
|
||||
record.PostName && x.PostType == record.PostType && x.ChangeDate == record.ChangeDate;
|
||||
record.PostName && x.PostType == record.PostType;
|
||||
});
|
||||
//Act
|
||||
_postBusinessLogicContract.UpdatePost(record);
|
||||
@ -362,7 +362,7 @@ Throws.TypeOf<ElementNotFoundException>());
|
||||
ElementNotFoundException(""));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.chef, true, DateTime.UtcNow)),
|
||||
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.chef)),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
_postStorageContract.Verify(x =>
|
||||
x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
@ -376,7 +376,7 @@ Throws.TypeOf<ElementNotFoundException>());
|
||||
ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.chef, true, DateTime.UtcNow)),
|
||||
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.chef)),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
_postStorageContract.Verify(x =>
|
||||
x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
@ -395,7 +395,7 @@ Throws.TypeOf<ElementNotFoundException>());
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _postBusinessLogicContract.UpdatePost(new
|
||||
PostDataModel("id", "name", PostType.chef, true, DateTime.UtcNow)),
|
||||
PostDataModel("id", "name", PostType.chef)),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
_postStorageContract.Verify(x =>
|
||||
x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
@ -409,7 +409,7 @@ x.UpdElement(It.IsAny<PostDataModel>()), Times.Never);
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.chef, true, DateTime.UtcNow)),
|
||||
_postBusinessLogicContract.UpdatePost(new(Guid.NewGuid().ToString(), "name", PostType.chef)),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_postStorageContract.Verify(x =>
|
||||
x.UpdElement(It.IsAny<PostDataModel>()), Times.Once);
|
||||
|
@ -34,15 +34,14 @@ internal class ProductBusinessLogicContractTests
|
||||
{
|
||||
//Arrange
|
||||
var listOriginal = new List<ProductDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "name 1", ProductType.Onigiri, 10, false),
|
||||
new(Guid.NewGuid().ToString(), "name 2", ProductType.Onigiri, 10, true),
|
||||
new(Guid.NewGuid().ToString(), "name 3", ProductType.Onigiri, 10, false),
|
||||
};
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(), It.IsAny<string>())).Returns(listOriginal);
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), "name 1", ProductType.Onigiri, 10, false),
|
||||
new(Guid.NewGuid().ToString(), "name 2", ProductType.Onigiri, 10, true),
|
||||
new(Guid.NewGuid().ToString(), "name 3", ProductType.Onigiri, 10, false),
|
||||
};
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns(listOriginal);
|
||||
//Act
|
||||
var listOnlyActive =
|
||||
_productBusinessLogicContract.GetAllProducts(true);
|
||||
var listOnlyActive = _productBusinessLogicContract.GetAllProducts(true);
|
||||
var list = _productBusinessLogicContract.GetAllProducts(false);
|
||||
//Assert
|
||||
Assert.Multiple(() =>
|
||||
@ -52,22 +51,19 @@ new(Guid.NewGuid().ToString(), "name 3", ProductType.Onigiri, 10, false),
|
||||
Assert.That(listOnlyActive, Is.EquivalentTo(listOriginal));
|
||||
Assert.That(list, Is.EquivalentTo(listOriginal));
|
||||
});
|
||||
_productStorageContract.Verify(x => x.GetList(true, null),
|
||||
Times.Once);
|
||||
_productStorageContract.Verify(x => x.GetList(false, null),
|
||||
Times.Once);
|
||||
_productStorageContract.Verify(x => x.GetList(true), Times.Once);
|
||||
_productStorageContract.Verify(x => x.GetList(false), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllProducts_ReturnEmptyList_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>())).Returns([]);
|
||||
//Act
|
||||
var listOnlyActive =
|
||||
_productBusinessLogicContract.GetAllProducts(true);
|
||||
{//Arrange
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Returns([]);
|
||||
|
||||
// Act
|
||||
var listOnlyActive = _productBusinessLogicContract.GetAllProducts(true);
|
||||
var list = _productBusinessLogicContract.GetAllProducts(false);
|
||||
//Assert
|
||||
|
||||
// Assert
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(listOnlyActive, Is.Not.Null);
|
||||
@ -75,8 +71,8 @@ new(Guid.NewGuid().ToString(), "name 3", ProductType.Onigiri, 10, false),
|
||||
Assert.That(listOnlyActive, Has.Count.EqualTo(0));
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
});
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
null), Times.Exactly(2));
|
||||
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Exactly(2));
|
||||
}
|
||||
[Test]
|
||||
public void GetAllProducts_ReturnNull_ThrowException_Test()
|
||||
@ -85,22 +81,18 @@ new(Guid.NewGuid().ToString(), "name 3", ProductType.Onigiri, 10, false),
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()),
|
||||
Throws.TypeOf<NullListException>());
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetAllProducts_StorageThrowError_ThrowException_Test()
|
||||
{
|
||||
//Arrange
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>())).Throws(new StorageException(new
|
||||
_productStorageContract.Setup(x => x.GetList(It.IsAny<bool>())).Throws(new StorageException(new
|
||||
InvalidOperationException()));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()),
|
||||
Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>(),
|
||||
It.IsAny<string>()), Times.Once);
|
||||
Assert.That(() => _productBusinessLogicContract.GetAllProducts(It.IsAny<bool>()), Throws.TypeOf<StorageException>());
|
||||
_productStorageContract.Verify(x => x.GetList(It.IsAny<bool>()), Times.Once);
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
@ -213,8 +205,7 @@ GetProductHistoryByProduct_ProductIdIsNotGuid_ThrowException_Test()
|
||||
//Arrange
|
||||
var name = "name";
|
||||
var record = new ProductDataModel(Guid.NewGuid().ToString(), name, ProductType.Onigiri, 10, false);
|
||||
_productStorageContract.Setup(x =>
|
||||
x.GetElementByName(name)).Returns(record);
|
||||
_productStorageContract.Setup(x => x.GetElementByName(name)).Returns(record);
|
||||
//Act
|
||||
var element = _productBusinessLogicContract.GetProductByData(name);
|
||||
//Assert
|
||||
|
@ -35,11 +35,10 @@ internal class SaleBusinessLogicContractTests
|
||||
var date = DateTime.UtcNow;
|
||||
var listOriginal = new List<SaleDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false, []),
|
||||
};
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false, [new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false, []),
|
||||
};
|
||||
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns(listOriginal);
|
||||
@ -120,12 +119,12 @@ internal class SaleBusinessLogicContractTests
|
||||
var date = DateTime.UtcNow;
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<SaleDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 5)]), new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false, []),
|
||||
};
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false,[]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false,[]),
|
||||
};
|
||||
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns(listOriginal);
|
||||
@ -240,12 +239,12 @@ GetAllSalesByWorkerByPeriod_WorkerIdIsNotGuid_ThrowException_Test()
|
||||
var date = DateTime.UtcNow;
|
||||
var buyerId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<SaleDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false, []),
|
||||
};
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false, []),
|
||||
};
|
||||
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
It.IsAny<string>())).Returns(listOriginal);
|
||||
@ -358,10 +357,10 @@ It.IsAny<string>()), Times.Never);
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
var listOriginal = new List<SaleDataModel>()
|
||||
{
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, 10, 0, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 5)]),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false, []),
|
||||
new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), null, false,[]),
|
||||
};
|
||||
_saleStorageContract.Setup(x => x.GetList(It.IsAny<DateTime?>(),
|
||||
It.IsAny<DateTime?>(), It.IsAny<string>(), It.IsAny<string>(),
|
||||
@ -475,10 +474,9 @@ Throws.TypeOf<ArgumentNullException>());
|
||||
{
|
||||
//Arrange
|
||||
var id = Guid.NewGuid().ToString();
|
||||
var record = new SaleDataModel(id, Guid.NewGuid().ToString(), null,
|
||||
10, 0, false,
|
||||
var record = new SaleDataModel(id, Guid.NewGuid().ToString(), null, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 5)]);
|
||||
Guid.NewGuid().ToString(), 5, 5)]);
|
||||
_saleStorageContract.Setup(x =>
|
||||
x.GetElementById(id)).Returns(record);
|
||||
//Act
|
||||
@ -539,8 +537,8 @@ Guid.NewGuid().ToString(), 5)]);
|
||||
{
|
||||
//Arrange
|
||||
var flag = false;
|
||||
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 10, false, [new SaleProductDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 5)]);
|
||||
var record = new SaleDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, [new SaleProductDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 5, 5)]);
|
||||
_saleStorageContract.Setup(x =>
|
||||
x.AddElement(It.IsAny<SaleDataModel>()))
|
||||
.Callback((SaleDataModel x) =>
|
||||
@ -565,8 +563,8 @@ Guid.NewGuid().ToString(), 5)]);
|
||||
ElementExistsException("Data", "Data"));
|
||||
//Act&Assert
|
||||
Assert.That(() =>
|
||||
_saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 10, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 5)])), Throws.TypeOf<ElementExistsException>());
|
||||
_saleStorageContract.Verify(x =>
|
||||
x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
}
|
||||
@ -584,9 +582,8 @@ Guid.NewGuid().ToString(), 5)]);
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _saleBusinessLogicContract.InsertSale(new
|
||||
SaleDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 10, 10, false, [])), Throws.TypeOf<ValidationException>());
|
||||
_saleStorageContract.Verify(x =>
|
||||
x.AddElement(It.IsAny<SaleDataModel>()), Times.Never);
|
||||
SaleDataModel("id", Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), false, [])), Throws.TypeOf<ValidationException>());
|
||||
_saleStorageContract.Verify(x => x.AddElement(It.IsAny<SaleDataModel>()), Times.Never);
|
||||
}
|
||||
[Test]
|
||||
public void InsertSale_StorageThrowError_ThrowException_Test()
|
||||
@ -599,8 +596,8 @@ x.AddElement(It.IsAny<SaleDataModel>()), Times.Never);
|
||||
Assert.That(() =>
|
||||
_saleBusinessLogicContract.InsertSale(new(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 10, 10, false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5)])), Throws.TypeOf<StorageException>());
|
||||
Guid.NewGuid().ToString(), false,
|
||||
[new SaleProductDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 5, 5)])), Throws.TypeOf<StorageException>());
|
||||
_saleStorageContract.Verify(x =>
|
||||
x.AddElement(It.IsAny<SaleDataModel>()), Times.Once);
|
||||
}
|
||||
|
@ -15,31 +15,28 @@ internal class PostDataModelTests
|
||||
[Test]
|
||||
public void IdIsNullOrEmptyTest()
|
||||
{
|
||||
var post = CreateDataModel(null, "name", PostType.chef,
|
||||
true, DateTime.UtcNow);
|
||||
var post = CreateDataModel(null, "name", PostType.chef);
|
||||
Assert.That(() => post.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
post = CreateDataModel(string.Empty, "name", PostType.chef,
|
||||
true, DateTime.UtcNow);
|
||||
post = CreateDataModel(string.Empty, "name", PostType.chef);
|
||||
Assert.That(() => post.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
[Test]
|
||||
public void IdIsNotGuidTest()
|
||||
{
|
||||
var post = CreateDataModel("id", "name", PostType.chef,
|
||||
true, DateTime.UtcNow);
|
||||
var post = CreateDataModel("id", "name", PostType.chef);
|
||||
Assert.That(() => post.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
[Test]
|
||||
public void PostNameIsEmptyTest()
|
||||
{
|
||||
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.chef, true, DateTime.UtcNow);
|
||||
var manufacturer = CreateDataModel(Guid.NewGuid().ToString(), null, PostType.chef);
|
||||
Assert.That(() => manufacturer.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
manufacturer = CreateDataModel(Guid.NewGuid().ToString(),
|
||||
string.Empty, PostType.chef, true, DateTime.UtcNow);
|
||||
string.Empty, PostType.chef);
|
||||
Assert.That(() => manufacturer.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
@ -47,7 +44,7 @@ internal class PostDataModelTests
|
||||
public void PostTypeIsNoneTest()
|
||||
{
|
||||
var post = CreateDataModel(Guid.NewGuid().ToString(), "name",
|
||||
PostType.None, true, DateTime.UtcNow);
|
||||
PostType.None);
|
||||
Assert.That(() => post.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
@ -60,17 +57,14 @@ internal class PostDataModelTests
|
||||
var postType = PostType.chef;
|
||||
var isActual = false;
|
||||
var changeDate = DateTime.UtcNow.AddDays(-1);
|
||||
var post = CreateDataModel(postId, postName, postType,
|
||||
isActual, changeDate);
|
||||
var post = CreateDataModel(postId, postName, postType);
|
||||
Assert.That(() => post.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(post.Id, Is.EqualTo(postId));
|
||||
Assert.That(post.PostName, Is.EqualTo(postName));
|
||||
Assert.That(post.PostType, Is.EqualTo(postType));
|
||||
Assert.That(post.IsActual, Is.EqualTo(isActual));
|
||||
Assert.That(post.ChangeDate, Is.EqualTo(changeDate));
|
||||
});
|
||||
}
|
||||
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType, bool isActual, DateTime changeDate) => new(id, postName, postType, isActual, changeDate);
|
||||
private static PostDataModel CreateDataModel(string? id, string? postName, PostType postType) => new(id, postName, postType);
|
||||
}
|
@ -70,7 +70,7 @@ Throws.TypeOf<ValidationException>());
|
||||
public void SumIsLessOrZeroTest()
|
||||
{
|
||||
var sale = CreateDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, 10,
|
||||
Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 0, 0,
|
||||
false, CreateSubDataModel());
|
||||
Assert.That(() => sale.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
@ -101,7 +101,7 @@ Throws.TypeOf<ValidationException>());
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
var buyerId = Guid.NewGuid().ToString();
|
||||
var sum = 10;
|
||||
var points = 1;
|
||||
var points = 10;
|
||||
var isCancel = true;
|
||||
var products = CreateSubDataModel();
|
||||
var sale = CreateDataModel(saleId, workerId, buyerId, sum, points, isCancel, products);
|
||||
@ -119,7 +119,7 @@ Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
private static SaleDataModel CreateDataModel(string? id, string? workerId,
|
||||
string? buyerId, double sum, double points, bool
|
||||
isCancel, List<SaleProductDataModel>? products) => new(id, workerId, buyerId, sum, points, isCancel, products);
|
||||
isCancel, List<SaleProductDataModel>? products) => new(id, workerId, buyerId,isCancel, products);
|
||||
private static List<SaleProductDataModel> CreateSubDataModel()
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1)];
|
||||
=> [new(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), 1, 1)];
|
||||
}
|
@ -15,11 +15,11 @@ internal class SaleProductDataModelTests
|
||||
public void SaleIdIsNullOrEmptyTest()
|
||||
{
|
||||
var saleProduct = CreateDataModel(null, Guid.NewGuid().ToString(),
|
||||
10);
|
||||
10, 5);
|
||||
Assert.That(() => saleProduct.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
saleProduct = CreateDataModel(string.Empty,
|
||||
Guid.NewGuid().ToString(), 10);
|
||||
Guid.NewGuid().ToString(), 10, 5);
|
||||
Assert.That(() => saleProduct.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
@ -27,7 +27,7 @@ internal class SaleProductDataModelTests
|
||||
public void SaleIdIsNotGuidTest()
|
||||
{
|
||||
var saleProduct = CreateDataModel("saleId",
|
||||
Guid.NewGuid().ToString(), 10);
|
||||
Guid.NewGuid().ToString(), 10, 5);
|
||||
Assert.That(() => saleProduct.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
@ -35,11 +35,11 @@ internal class SaleProductDataModelTests
|
||||
public void ProductIdIsNullOrEmptyTest()
|
||||
{
|
||||
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(), null,
|
||||
10);
|
||||
10, 5);
|
||||
Assert.That(() => saleProduct.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
saleProduct = CreateDataModel(string.Empty,
|
||||
Guid.NewGuid().ToString(), 10);
|
||||
Guid.NewGuid().ToString(), 10, 5);
|
||||
Assert.That(() => saleProduct.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
@ -47,7 +47,7 @@ internal class SaleProductDataModelTests
|
||||
public void ProductIdIsNotGuidTest()
|
||||
{
|
||||
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(),
|
||||
"productId", 10);
|
||||
"productId", 10, 5);
|
||||
Assert.That(() => saleProduct.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
@ -55,10 +55,10 @@ internal class SaleProductDataModelTests
|
||||
public void CountIsLessOrZeroTest()
|
||||
{
|
||||
var saleProduct = CreateDataModel(Guid.NewGuid().ToString(),
|
||||
Guid.NewGuid().ToString(), 0);
|
||||
Guid.NewGuid().ToString(), 0, -5);
|
||||
Assert.That(() => saleProduct.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10);
|
||||
saleProduct = CreateDataModel(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), -10, 0);
|
||||
Assert.That(() => saleProduct.Validate(),
|
||||
Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
@ -68,7 +68,8 @@ internal class SaleProductDataModelTests
|
||||
var saleId = Guid.NewGuid().ToString();
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
var count = 10;
|
||||
var saleProduct = CreateDataModel(saleId, productId, count);
|
||||
var price = 5;
|
||||
var saleProduct = CreateDataModel(saleId, productId, count, price);
|
||||
Assert.That(() => saleProduct.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
@ -77,5 +78,5 @@ internal class SaleProductDataModelTests
|
||||
Assert.That(saleProduct.Count, Is.EqualTo(count));
|
||||
});
|
||||
}
|
||||
private static SaleProductDataModel CreateDataModel(string? saleId, string? productId, int count) => new(saleId, productId, count);
|
||||
private static SaleProductDataModel CreateDataModel(string? saleId, string? productId, int count, int price) => new(saleId, productId, count, price);
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using PuferFishContracts.Infrastructure;
|
||||
|
||||
namespace PuferFishTests.Infrastructure
|
||||
{
|
||||
internal class ConfigurationDatabaseTest : IConfigurationDatabase
|
||||
{
|
||||
public string ConnectionString => @"Host=127.0.0.1;Port=5432;Database=PuferFishTests; Username=postgres;Password=postgres;";
|
||||
}
|
||||
}
|
@ -20,6 +20,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\PuferFishBusinessLogic\PuferFishBusinessLogic.csproj" />
|
||||
<ProjectReference Include="..\PuferFishContracts\PuferFishContracts.csproj" />
|
||||
<ProjectReference Include="..\PuferFishDataBase\PuferFishDataBase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using PuferFishDataBase;
|
||||
using PuferFishTests.Infrastructure;
|
||||
|
||||
namespace PuferFishTests.StoragesContracts
|
||||
{
|
||||
internal abstract class BaseStorageContractTest
|
||||
{
|
||||
protected PuferFishDbContext PuferFishDbContext { get; private set; }
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetUp()
|
||||
{
|
||||
PuferFishDbContext = new PuferFishDbContext(new ConfigurationDatabaseTest());
|
||||
PuferFishDbContext.Database.EnsureDeleted();
|
||||
PuferFishDbContext.Database.EnsureCreated();
|
||||
}
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
PuferFishDbContext.Database.EnsureDeleted();
|
||||
PuferFishDbContext.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,249 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PuferFishContracts.DataModels;
|
||||
using PuferFishContracts.Exceptions;
|
||||
using PuferFishDataBase.Implementations;
|
||||
using PuferFishDataBase.Models;
|
||||
|
||||
namespace PuferFishTests.StoragesContracts;
|
||||
|
||||
[TestFixture]
|
||||
internal class BuyerStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private BuyerStorageContract _buyerStorageContract;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_buyerStorageContract = new BuyerStorageContract(PuferFishDbContext);
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Sales\" CASCADE; ");
|
||||
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\" CASCADE; ");
|
||||
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Buyers\" CASCADE; ");
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var buyer = InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
phoneNumber: "+5-555-555-55-55");
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
phoneNumber: "+6-666-666-66-66");
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
phoneNumber: "+7-777-777-77-77");
|
||||
var list = _buyerStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == buyer.Id), buyer);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _buyerStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var buyer =
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_buyerStorageContract.GetElementById(buyer.Id), buyer);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() =>
|
||||
_buyerStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
||||
{
|
||||
var buyer =
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_buyerStorageContract.GetElementByFIO(buyer.FIO),
|
||||
buyer);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
||||
{
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _buyerStorageContract.GetElementByFIO("New Fio"),
|
||||
Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByPhoneNumber_WhenHaveRecord_Test()
|
||||
{
|
||||
var buyer =
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_buyerStorageContract.GetElementByPhoneNumber(buyer.PhoneNumber), buyer);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByPhoneNumber_WhenNoRecord_Test()
|
||||
{
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _buyerStorageContract.GetElementByPhoneNumber("+8-888 - 888 - 88 - 88"), Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var buyer = CreateModel(Guid.NewGuid().ToString());
|
||||
_buyerStorageContract.AddElement(buyer);
|
||||
AssertElement(GetBuyerFromDatabase(buyer.Id), buyer);
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var buyer = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555 - 555 - 55 - 55", 500);
|
||||
InsertBuyerToDatabaseAndReturn(buyer.Id);
|
||||
Assert.That(() => _buyerStorageContract.AddElement(buyer),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSamePhoneNumber_Test()
|
||||
{
|
||||
var buyer = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555 - 555 - 55 - 55", 500);
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
phoneNumber: buyer.PhoneNumber);
|
||||
Assert.That(() => _buyerStorageContract.AddElement(buyer),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var buyer = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555 - 555 - 55 - 55", 500);
|
||||
InsertBuyerToDatabaseAndReturn(buyer.Id);
|
||||
_buyerStorageContract.UpdElement(buyer);
|
||||
AssertElement(GetBuyerFromDatabase(buyer.Id), buyer);
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_buyerStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSamePhoneNumber_Test()
|
||||
{
|
||||
var buyer = CreateModel(Guid.NewGuid().ToString(), "New Fio", "+5-555 - 555 - 55 - 55", 500);
|
||||
InsertBuyerToDatabaseAndReturn(buyer.Id, phoneNumber: "+7-777-777-77-77");
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
phoneNumber: buyer.PhoneNumber);
|
||||
Assert.That(() => _buyerStorageContract.UpdElement(buyer),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var buyer =
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
_buyerStorageContract.DelElement(buyer.Id);
|
||||
var element = GetBuyerFromDatabase(buyer.Id);
|
||||
Assert.That(element, Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_WhenHaveSalesByThisBuyer_Test()
|
||||
{
|
||||
var buyer =
|
||||
InsertBuyerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
var workerId = Guid.NewGuid().ToString();
|
||||
PuferFishDbContext.Workers.Add(new Worker()
|
||||
{
|
||||
Id = workerId,
|
||||
FIO =
|
||||
"test",
|
||||
PostId = Guid.NewGuid().ToString()
|
||||
});
|
||||
PuferFishDbContext.Sales.Add(new Sale()
|
||||
{
|
||||
Id =
|
||||
Guid.NewGuid().ToString(),
|
||||
WorkerId = workerId,
|
||||
BuyerId = buyer.Id,
|
||||
Sum = 10,
|
||||
Points = 0
|
||||
});
|
||||
PuferFishDbContext.Sales.Add(new Sale()
|
||||
{
|
||||
Id =
|
||||
Guid.NewGuid().ToString(),
|
||||
WorkerId = workerId,
|
||||
BuyerId = buyer.Id,
|
||||
Sum = 10,
|
||||
Points = 0
|
||||
});
|
||||
PuferFishDbContext.SaveChanges();
|
||||
var salesBeforeDelete = PuferFishDbContext.Sales.Where(x =>
|
||||
x.BuyerId == buyer.Id).ToArray();
|
||||
_buyerStorageContract.DelElement(buyer.Id);
|
||||
var element = GetBuyerFromDatabase(buyer.Id);
|
||||
var salesAfterDelete = PuferFishDbContext.Sales.Where(x => x.BuyerId
|
||||
== buyer.Id).ToArray();
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Null);
|
||||
Assert.That(salesBeforeDelete, Has.Length.EqualTo(2));
|
||||
Assert.That(salesAfterDelete, Is.Empty);
|
||||
Assert.That(PuferFishDbContext.Sales.Count(), Is.EqualTo(2));
|
||||
});
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_buyerStorageContract.DelElement(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
private Buyer InsertBuyerToDatabaseAndReturn(string id, string fio =
|
||||
"test", string phoneNumber = "+7-777-777-77-77", double points = 10)
|
||||
{
|
||||
var buyer = new Buyer()
|
||||
{
|
||||
Id = id,
|
||||
FIO = fio,
|
||||
PhoneNumber = phoneNumber,
|
||||
Points = points
|
||||
};
|
||||
PuferFishDbContext.Buyers.Add(buyer);
|
||||
PuferFishDbContext.SaveChanges();
|
||||
return buyer;
|
||||
}
|
||||
private static void AssertElement(BuyerDataModel? actual, Buyer 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.Points,
|
||||
Is.EqualTo(expected.Points));
|
||||
});
|
||||
}
|
||||
private static BuyerDataModel CreateModel(string id, string fio = "test",
|
||||
string phoneNumber = "+7-777-777-77-77", double discountSize = 10)
|
||||
=> new(id, fio, phoneNumber, discountSize);
|
||||
private Buyer? GetBuyerFromDatabase(string id) =>
|
||||
PuferFishDbContext.Buyers.FirstOrDefault(x => x.Id == id);
|
||||
private static void AssertElement(Buyer? actual, BuyerDataModel 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.Points,
|
||||
Is.EqualTo(expected.Points));
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,156 @@
|
||||
|
||||
/*
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PuferFishContracts.DataModels;
|
||||
using PuferFishDataBase.Implementations;
|
||||
using PuferFishDataBase.Models;
|
||||
|
||||
namespace PuferFishTests.StoragesContracts;
|
||||
|
||||
[TestFixture]
|
||||
internal class PointsStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private PointsStorageContract _pointsStorageContract;
|
||||
private Buyer _buyer;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_pointsStorageContract = new PointsStorageContract(PuferFishDbContext);
|
||||
_buyer = InsertBuyerToDatabaseAndReturn();
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Pointss\"CASCADE; ");
|
||||
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Buyers\"CASCADE; ");
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var salary = InsertPointsToDatabaseAndReturn(_buyer.Id, buyerPoints: 100);
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id);
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id);
|
||||
var list = _pointsStorageContract.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 = _pointsStorageContract.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()
|
||||
{
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(-1).AddMinutes(-5));
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(1).AddMinutes(5));
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(-2));
|
||||
var list = _pointsStorageContract.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_ByBuyer_Test()
|
||||
{
|
||||
var worker = InsertBuyerToDatabaseAndReturn("name 2");
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id);
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id);
|
||||
InsertPointsToDatabaseAndReturn(worker.Id);
|
||||
var list = _pointsStorageContract.GetList(DateTime.UtcNow.AddDays(-1), DateTime.UtcNow.AddDays(1), _buyer.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.BuyerId == _buyer.Id));
|
||||
});
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_ByWorkerOnlyInDatePeriod_Test()
|
||||
{
|
||||
var buyer = InsertBuyerToDatabaseAndReturn("name 2");
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertPointsToDatabaseAndReturn(buyer.Id, pointsDate: DateTime.UtcNow.AddDays(-1).AddMinutes(5));
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertPointsToDatabaseAndReturn(buyer.Id, pointsDate: DateTime.UtcNow.AddDays(1).AddMinutes(-5));
|
||||
InsertPointsToDatabaseAndReturn(_buyer.Id, pointsDate: DateTime.UtcNow.AddDays(-2));
|
||||
var list = _pointsStorageContract.GetList(DateTime.UtcNow.AddDays(- 1), DateTime.UtcNow.AddDays(1), _buyer.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(list, Has.Count.EqualTo(2));
|
||||
Assert.That(list.All(x => x.BuyerId == _buyer.Id));
|
||||
});
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var buyer = CreateModel(_buyer.Id);
|
||||
_pointsStorageContract.AddElement(buyer);
|
||||
AssertElement(GetPointsFromDatabaseByBuyerId(_buyer.Id), buyer);
|
||||
}
|
||||
private Buyer InsertBuyerToDatabaseAndReturn(string buyerFIO = "fio")
|
||||
{
|
||||
var buyer = new Buyer()
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
FIO = buyerFIO,
|
||||
|
||||
IsDeleted = false
|
||||
};
|
||||
PuferFishDbContext.Buyers.Add(buyer);
|
||||
PuferFishDbContext.SaveChanges();
|
||||
return buyer;
|
||||
}
|
||||
private Points InsertPointsToDatabaseAndReturn(string buyerId, double buyerPoints = 1, DateTime? pointsDate = null)
|
||||
{
|
||||
var points = new Points()
|
||||
{
|
||||
BuyerId = buyerId,
|
||||
BuyerPoints = buyerPoints,
|
||||
PointsDate = pointsDate ?? DateTime.UtcNow
|
||||
};
|
||||
PuferFishDbContext.Pointss.Add(points);
|
||||
PuferFishDbContext.SaveChanges();
|
||||
return points;
|
||||
}
|
||||
private static void AssertElement(PointsDataModel? actual, Points expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.BuyerId, Is.EqualTo(expected.BuyerId));
|
||||
Assert.That(actual.Points, Is.EqualTo(expected.BuyerPoints));
|
||||
});
|
||||
}
|
||||
private static PointsDataModel CreateModel(string buyerId, double buyerPoints = 1, DateTime? pointsDate = null)
|
||||
=> new(buyerId, pointsDate ?? DateTime.UtcNow, buyerPoints);
|
||||
private Points? GetPointsFromDatabaseByBuyerId(string id) =>
|
||||
PuferFishDbContext.Pointss.FirstOrDefault(x => x.BuyerId == id);
|
||||
private static void AssertElement(Points? actual, PointsDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.BuyerId, Is.EqualTo(expected.BuyerId));
|
||||
Assert.That(actual.BuyerPoints, Is.EqualTo(expected.Points));
|
||||
});
|
||||
}
|
||||
}
|
||||
*/
|
@ -0,0 +1,280 @@
|
||||
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 PostStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private PostStorageContract _postStorageContract;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_postStorageContract = new PostStorageContract(PuferFishDbContext);
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
PuferFishDbContext.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_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());
|
||||
_postStorageContract.AddElement(post);
|
||||
AssertElement(GetPostFromDatabaseByPostId(post.Id), post);
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString(), "name unique");
|
||||
InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(), postName:
|
||||
post.PostName, isActual: true);
|
||||
Assert.That(() => _postStorageContract.AddElement(post),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSamePostId_Test()
|
||||
{
|
||||
var post = CreateModel(Guid.NewGuid().ToString());
|
||||
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.UpdElement(post);
|
||||
var posts = PuferFishDbContext.Posts.Where(x => x.PostId ==
|
||||
post.Id).OrderByDescending(x => x.ChangeDate);
|
||||
Assert.That(posts.Count(), Is.EqualTo(2));
|
||||
AssertElement(posts.First(), CreateModel(post.Id));
|
||||
AssertElement(posts.Last(), CreateModel(post.Id));
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_postStorageContract.UpdElement(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.UpdElement(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.UpdElement(post),
|
||||
Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
isActual: true);
|
||||
_postStorageContract.DelElement(post.PostId);
|
||||
var element = GetPostFromDatabaseByPostId(post.PostId);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(!element!.IsActual);
|
||||
});
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_postStorageContract.DelElement(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
isActual: false);
|
||||
Assert.That(() => _postStorageContract.DelElement(post.PostId),
|
||||
Throws.TypeOf<ElementDeletedException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_ResElement_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
isActual: false);
|
||||
_postStorageContract.ResElement(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.ResElement(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_ResElement_WhenRecordNotWasDeleted_Test()
|
||||
{
|
||||
var post = InsertPostToDatabaseAndReturn(Guid.NewGuid().ToString(),
|
||||
isActual: true);
|
||||
Assert.That(() => _postStorageContract.ResElement(post.PostId),
|
||||
Throws.Nothing);
|
||||
}
|
||||
private Post InsertPostToDatabaseAndReturn(string id, string postName =
|
||||
"test", PostType postType = PostType.chef, bool isActual = true, DateTime? changeDate = null)
|
||||
{
|
||||
var post = new Post()
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
PostId = id,
|
||||
PostName = postName,
|
||||
PostType = postType,
|
||||
IsActual = isActual,
|
||||
ChangeDate = changeDate ?? DateTime.UtcNow
|
||||
};
|
||||
PuferFishDbContext.Posts.Add(post);
|
||||
PuferFishDbContext.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));
|
||||
});
|
||||
}
|
||||
private static PostDataModel CreateModel(string postId, string postName = "test", PostType postType = PostType.chef) => new(postId, postName, postType);
|
||||
|
||||
private Post? GetPostFromDatabaseByPostId(string id) =>
|
||||
PuferFishDbContext.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));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,325 @@
|
||||
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 ProductStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private ProductStorageContract _productStorageContract;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_productStorageContract = new
|
||||
ProductStorageContract(PuferFishDbContext);
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Products\"CASCADE; ");
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3");
|
||||
var list = _productStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(x => x.Id == product.Id), product);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _productStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_OnlyActual_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isDeleted: false);
|
||||
var list = _productStorageContract.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()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 2", isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 3", isDeleted: false);
|
||||
var list = _productStorageContract.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_GetHistoryByProductId_WhenHaveRecords_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _productStorageContract.GetHistoryByProductId(product.Id);
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetHistoryByProductId_WhenNoRecords_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), "name 1");
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 20, DateTime.UtcNow.AddDays(-1));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 30, DateTime.UtcNow.AddMinutes(-10));
|
||||
InsertProductHistoryToDatabaseAndReturn(product.Id, 40, DateTime.UtcNow.AddDays(1));
|
||||
var list = _productStorageContract.GetHistoryByProductId(Guid.NewGuid().ToString());
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(0));
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenHaveRecord_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_productStorageContract.GetElementById(product.Id),
|
||||
product);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _productStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.GetElementById(product.Id),
|
||||
Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenHaveRecord_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_productStorageContract.GetElementByName(product.ProductName), product);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenNoRecord_Test()
|
||||
{
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
Assert.That(() => _productStorageContract.GetElementByName("name"),
|
||||
Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByName_WhenRecordHasDeleted_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.GetElementById(product.ProductName), Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: false);
|
||||
_productStorageContract.AddElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.AddElement(product),
|
||||
Throws.Nothing);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id),
|
||||
CreateModel(product.Id, isDeleted: false));
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name unique");
|
||||
Assert.That(() => _productStorageContract.AddElement(product),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.ProductName, isDeleted: false);
|
||||
Assert.That(() => _productStorageContract.AddElement(product),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.ProductName, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.AddElement(product),
|
||||
Throws.Nothing);
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: false);
|
||||
_productStorageContract.UpdElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id), product);
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenIsDeletedIsTrue_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: false);
|
||||
_productStorageContract.UpdElement(product);
|
||||
AssertElement(GetProductFromDatabaseById(product.Id),
|
||||
CreateModel(product.Id, isDeleted: false));
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_productStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())), Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenHaveRecordWithSameName_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.ProductName);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
[Test]
|
||||
public void
|
||||
Try_UpdElement_WhenHaveRecordWithSameNameButOneWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString(), "name unique", isDeleted: false);
|
||||
InsertProductToDatabaseAndReturn(product.Id, productName: "name");
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), productName: product.ProductName, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product),
|
||||
Throws.Nothing);
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var product = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertProductToDatabaseAndReturn(product.Id, isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.UpdElement(product),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: false);
|
||||
_productStorageContract.DelElement(product.Id);
|
||||
var element = GetProductFromDatabaseById(product.Id);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element!.IsDeleted);
|
||||
});
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_productStorageContract.DelElement(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_WhenRecordWasDeleted_Test()
|
||||
{
|
||||
var product =
|
||||
InsertProductToDatabaseAndReturn(Guid.NewGuid().ToString(), isDeleted: true);
|
||||
Assert.That(() => _productStorageContract.DelElement(product.Id),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
|
||||
private Product InsertProductToDatabaseAndReturn(string id, string productName = "test", ProductType productType =
|
||||
ProductType.Onigiri, double price = 1, bool isDeleted = false)
|
||||
{
|
||||
var product = new Product()
|
||||
{
|
||||
Id = id,
|
||||
ProductName = productName,
|
||||
ProductType = productType,
|
||||
Price = price,
|
||||
IsDeleted = isDeleted
|
||||
};
|
||||
PuferFishDbContext.Products.Add(product);
|
||||
PuferFishDbContext.SaveChanges();
|
||||
return product;
|
||||
}
|
||||
private ProductHistory InsertProductHistoryToDatabaseAndReturn(string productId, double price, DateTime changeDate)
|
||||
{
|
||||
var productHistory = new ProductHistory()
|
||||
{
|
||||
Id = Guid.NewGuid().ToString(),
|
||||
ProductId = productId,
|
||||
OldPrice = price,
|
||||
ChangeDate = changeDate
|
||||
};
|
||||
PuferFishDbContext.ProductHistories.Add(productHistory);
|
||||
PuferFishDbContext.SaveChanges();
|
||||
return productHistory;
|
||||
}
|
||||
private static void AssertElement(ProductDataModel? actual, Product expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.ProductName,
|
||||
Is.EqualTo(expected.ProductName));
|
||||
Assert.That(actual.ProductType,
|
||||
Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
private static ProductDataModel CreateModel(string id, string productName = "test", ProductType productType =
|
||||
ProductType.Onigiri, double price = 1, bool isDeleted = false)
|
||||
=> new(id, productName, productType, price, isDeleted);
|
||||
private Product? GetProductFromDatabaseById(string id) => PuferFishDbContext.Products.FirstOrDefault(x => x.Id == id);
|
||||
private static void AssertElement(Product? actual, ProductDataModel expected)
|
||||
{
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.ProductName,
|
||||
Is.EqualTo(expected.ProductName));
|
||||
Assert.That(actual.ProductType,
|
||||
Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,361 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,266 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PuferFishContracts.DataModels;
|
||||
using PuferFishContracts.Exceptions;
|
||||
using PuferFishDataBase.Implementations;
|
||||
using PuferFishDataBase.Models;
|
||||
|
||||
namespace PuferFishTests.StoragesContracts
|
||||
{
|
||||
[TestFixture]
|
||||
internal class WorkerStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
private WorkerStorageContract _workerStorageContract;
|
||||
[SetUp]
|
||||
public void SetUp()
|
||||
{
|
||||
_workerStorageContract = new
|
||||
WorkerStorageContract(PuferFishDbContext);
|
||||
}
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
PuferFishDbContext.Database.ExecuteSqlRaw("TRUNCATE \"Workers\"CASCADE; ");
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_WhenHaveRecords_Test()
|
||||
{
|
||||
var worker =
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1");
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2");
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
||||
var list = _workerStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Has.Count.EqualTo(3));
|
||||
AssertElement(list.First(), worker);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_WhenNoRecords_Test()
|
||||
{
|
||||
var list = _workerStorageContract.GetList();
|
||||
Assert.That(list, Is.Not.Null);
|
||||
Assert.That(list, Is.Empty);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetList_ByPostId_Test()
|
||||
{
|
||||
var postId = Guid.NewGuid().ToString();
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1",
|
||||
postId);
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2",
|
||||
postId);
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3");
|
||||
var list = _workerStorageContract.GetList(postId: 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_ByBirthDate_Test()
|
||||
{
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1",
|
||||
birthDate: DateTime.UtcNow.AddYears(-25));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2",
|
||||
birthDate: DateTime.UtcNow.AddYears(-21));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3",
|
||||
birthDate: DateTime.UtcNow.AddYears(-20));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4",
|
||||
birthDate: DateTime.UtcNow.AddYears(-19));
|
||||
var list = _workerStorageContract.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()
|
||||
{
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1",
|
||||
employmentDate: DateTime.UtcNow.AddDays(-2));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2",
|
||||
employmentDate: DateTime.UtcNow.AddDays(-1));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3",
|
||||
employmentDate: DateTime.UtcNow.AddDays(1));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4",
|
||||
employmentDate: DateTime.UtcNow.AddDays(2));
|
||||
var list = _workerStorageContract.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();
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 1",
|
||||
postId, birthDate: DateTime.UtcNow.AddYears(-25), employmentDate:
|
||||
DateTime.UtcNow.AddDays(-2));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 2",
|
||||
postId, birthDate: DateTime.UtcNow.AddYears(-22), employmentDate:
|
||||
DateTime.UtcNow.AddDays(-1));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 3",
|
||||
postId, birthDate: DateTime.UtcNow.AddYears(-21), employmentDate:
|
||||
DateTime.UtcNow.AddDays(-1));
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString(), "fio 4",
|
||||
birthDate: DateTime.UtcNow.AddYears(-20), employmentDate:
|
||||
DateTime.UtcNow.AddDays(1));
|
||||
var list = _workerStorageContract.GetList(postId: postId,
|
||||
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 worker =
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_workerStorageContract.GetElementById(worker.Id),
|
||||
worker);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementById_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_workerStorageContract.GetElementById(Guid.NewGuid().ToString()), Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenHaveRecord_Test()
|
||||
{
|
||||
var worker =
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
AssertElement(_workerStorageContract.GetElementByFIO(worker.FIO),
|
||||
worker);
|
||||
}
|
||||
[Test]
|
||||
public void Try_GetElementByFIO_WhenNoRecord_Test()
|
||||
{
|
||||
Assert.That(() => _workerStorageContract.GetElementByFIO("New Fio"),
|
||||
Is.Null);
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
_workerStorageContract.AddElement(worker);
|
||||
AssertElement(GetWorkerFromDatabase(worker.Id), worker);
|
||||
}
|
||||
[Test]
|
||||
public void Try_AddElement_WhenHaveRecordWithSameId_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertWorkerToDatabaseAndReturn(worker.Id);
|
||||
Assert.That(() => _workerStorageContract.AddElement(worker),
|
||||
Throws.TypeOf<ElementExistsException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString(), "New Fio");
|
||||
InsertWorkerToDatabaseAndReturn(worker.Id);
|
||||
_workerStorageContract.UpdElement(worker);
|
||||
AssertElement(GetWorkerFromDatabase(worker.Id), worker);
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_workerStorageContract.UpdElement(CreateModel(Guid.NewGuid().ToString())),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_UpdElement_WhenNoRecordWasDeleted_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
|
||||
Assert.That(() => _workerStorageContract.UpdElement(worker),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_Test()
|
||||
{
|
||||
var worker =
|
||||
InsertWorkerToDatabaseAndReturn(Guid.NewGuid().ToString());
|
||||
_workerStorageContract.DelElement(worker.Id);
|
||||
var element = GetWorkerFromDatabase(worker.Id);
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.IsDeleted);
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWithThisId_Test()
|
||||
{
|
||||
Assert.That(() =>
|
||||
_workerStorageContract.DelElement(Guid.NewGuid().ToString()),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
[Test]
|
||||
public void Try_DelElement_WhenNoRecordWasDeleted_Test()
|
||||
{
|
||||
var worker = CreateModel(Guid.NewGuid().ToString());
|
||||
InsertWorkerToDatabaseAndReturn(worker.Id, isDeleted: true);
|
||||
Assert.That(() => _workerStorageContract.DelElement(worker.Id),
|
||||
Throws.TypeOf<ElementNotFoundException>());
|
||||
}
|
||||
private Worker InsertWorkerToDatabaseAndReturn(string id, string fio =
|
||||
"test", string? postId = null, DateTime? birthDate = null, DateTime?
|
||||
employmentDate = null, bool isDeleted = false)
|
||||
{
|
||||
var worker = new Worker()
|
||||
{
|
||||
Id = id,
|
||||
FIO = fio,
|
||||
PostId = postId ??
|
||||
Guid.NewGuid().ToString(),
|
||||
BirthDate = birthDate ?? DateTime.UtcNow.AddYears(-
|
||||
20),
|
||||
EmploymentDate = employmentDate ?? DateTime.UtcNow,
|
||||
IsDeleted = isDeleted
|
||||
};
|
||||
PuferFishDbContext.Workers.Add(worker);
|
||||
PuferFishDbContext.SaveChanges();
|
||||
return worker;
|
||||
}
|
||||
private static void AssertElement(WorkerDataModel? actual, Worker 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.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||
Assert.That(actual.EmploymentDate,
|
||||
Is.EqualTo(expected.EmploymentDate));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
private static WorkerDataModel CreateModel(string id, string fio = "fio",
|
||||
string? postId = null, DateTime? birthDate = null, DateTime? employmentDate =
|
||||
null, bool isDeleted = false) =>
|
||||
new(id, fio, postId ?? Guid.NewGuid().ToString(), birthDate ??
|
||||
DateTime.UtcNow.AddYears(-20), employmentDate ?? DateTime.UtcNow, isDeleted);
|
||||
private Worker? GetWorkerFromDatabase(string id) =>
|
||||
PuferFishDbContext.Workers.FirstOrDefault(x => x.Id == id);
|
||||
private static void AssertElement(Worker? actual, WorkerDataModel 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.BirthDate, Is.EqualTo(expected.BirthDate));
|
||||
Assert.That(actual.EmploymentDate,
|
||||
Is.EqualTo(expected.EmploymentDate));
|
||||
Assert.That(actual.IsDeleted, Is.EqualTo(expected.IsDeleted));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user