little update
This commit is contained in:
@@ -10,7 +10,7 @@ public class ProductBindingModel
|
||||
{
|
||||
public string? Id { get; set; }
|
||||
public string? ProductName { get; set; }
|
||||
public string? ProductCountry { get; set; }
|
||||
public string? ProductDescription { get; set; }
|
||||
public double Price { get; set; }
|
||||
public string? ProductType { get; set; }
|
||||
}
|
||||
|
||||
@@ -12,11 +12,11 @@ using System.Xml.Linq;
|
||||
|
||||
namespace CandyHouseContracts.DataModels;
|
||||
|
||||
public class ProductDataModel(string id, string productName, string productCountry, double price, ProductType productType) : IValidation
|
||||
public class ProductDataModel(string id, string productName, string productDescription, double price, ProductType productType) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string ProductName { get; private set; } = productName;
|
||||
public string ProductCountry { get; private set; } = productCountry;
|
||||
public string ProductDescription { get; private set; } = productDescription;
|
||||
public double Price { get; private set; } = price;
|
||||
public ProductType ProductType { get; private set; } = productType;
|
||||
|
||||
@@ -28,8 +28,8 @@ public class ProductDataModel(string id, string productName, string productCount
|
||||
throw new ValidationException("The value in the field Id is not a unique identifier");
|
||||
if (ProductName.IsEmpty())
|
||||
throw new ValidationException("Field ProductName is empty");
|
||||
if (ProductCountry.IsEmpty())
|
||||
throw new ValidationException("Field ProductCountry is empty");
|
||||
if (ProductDescription.IsEmpty())
|
||||
throw new ValidationException("Field ProductDescription is empty");
|
||||
if (Price <= 0)
|
||||
throw new ValidationException("Field Price is less than or equal to 0");
|
||||
if (ProductType == ProductType.None)
|
||||
|
||||
@@ -14,7 +14,7 @@ public class ProductViewModel
|
||||
|
||||
public required string ProductType { get; set; }
|
||||
|
||||
public string? ProductCountry { get; set; }
|
||||
public string? ProductDescription { get; set; }
|
||||
|
||||
public double Price { get; set; }
|
||||
}
|
||||
|
||||
@@ -179,7 +179,7 @@ internal class ProductBusinessLogicContractTests
|
||||
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductByData_GetByCountry_ReturnRecord_Test()
|
||||
public void GetProductByData_GetByDescription_ReturnRecord_Test()
|
||||
{
|
||||
//Arrange
|
||||
var country = "country";
|
||||
@@ -189,7 +189,7 @@ internal class ProductBusinessLogicContractTests
|
||||
var element = _productBusinessLogicContract.GetProductByData(country);
|
||||
//Assert
|
||||
Assert.That(element, Is.Not.Null);
|
||||
Assert.That(element.ProductCountry, Is.EqualTo(country));
|
||||
Assert.That(element.ProductDescription, Is.EqualTo(country));
|
||||
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ internal class ProductBusinessLogicContractTests
|
||||
_productStorageContract.Verify(x => x.GetElementByName(It.IsAny<string>()), Times.Once);
|
||||
}
|
||||
[Test]
|
||||
public void GetProductByData_GetByCountry_NotFoundRecord_ThrowException_Test()
|
||||
public void GetProductByData_GetByDescription_NotFoundRecord_ThrowException_Test()
|
||||
{
|
||||
//Act&Assert
|
||||
Assert.That(() => _productBusinessLogicContract.GetProductByData("country"), Throws.TypeOf<ElementNotFoundException>());
|
||||
@@ -248,7 +248,7 @@ internal class ProductBusinessLogicContractTests
|
||||
_productStorageContract.Setup(x => x.AddElement(It.IsAny<ProductDataModel>()))
|
||||
.Callback((ProductDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.ProductName == record.ProductName && x.ProductCountry == record.ProductCountry
|
||||
flag = x.Id == record.Id && x.ProductName == record.ProductName && x.ProductDescription == record.ProductDescription
|
||||
&& x.Price == record.Price && x.ProductType == record.ProductType;
|
||||
});
|
||||
//Act
|
||||
@@ -303,7 +303,7 @@ internal class ProductBusinessLogicContractTests
|
||||
_productStorageContract.Setup(x => x.UpdElement(It.IsAny<ProductDataModel>()))
|
||||
.Callback((ProductDataModel x) =>
|
||||
{
|
||||
flag = x.Id == record.Id && x.ProductName == record.ProductName && x.ProductCountry == record.ProductCountry
|
||||
flag = x.Id == record.Id && x.ProductName == record.ProductName && x.ProductDescription == record.ProductDescription
|
||||
&& x.Price == record.Price && x.ProductType == record.ProductType;
|
||||
});
|
||||
//Act
|
||||
|
||||
@@ -36,7 +36,7 @@ internal class ProductDataModelTests
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
}
|
||||
|
||||
public void ProductCountryIsNullOrEmptyTest()
|
||||
public void ProductDescriptionIsNullOrEmptyTest()
|
||||
{
|
||||
var cocktail = CreateDataModel(Guid.NewGuid().ToString(), "name", null, 10.5, ProductType.Cake);
|
||||
Assert.That(() => cocktail.Validate(), Throws.TypeOf<ValidationException>());
|
||||
@@ -65,16 +65,16 @@ internal class ProductDataModelTests
|
||||
{
|
||||
var productId = Guid.NewGuid().ToString();
|
||||
var productName = "name";
|
||||
var productCountry = "country";
|
||||
var productDescription = "country";
|
||||
var price = 10.5;
|
||||
var productType = ProductType.Chocolate;
|
||||
var product = CreateDataModel(productId, productName, productCountry, price, productType);
|
||||
var product = CreateDataModel(productId, productName, productDescription, price, productType);
|
||||
Assert.That(() => product.Validate(), Throws.Nothing);
|
||||
Assert.Multiple(() =>
|
||||
{
|
||||
Assert.That(product.Id, Is.EqualTo(productId));
|
||||
Assert.That(product.ProductName, Is.EqualTo(productName));
|
||||
Assert.That(product.ProductCountry, Is.EqualTo(productCountry));
|
||||
Assert.That(product.ProductDescription, Is.EqualTo(productDescription));
|
||||
Assert.That(product.Price, Is.EqualTo(price));
|
||||
Assert.That(product.ProductType, Is.EqualTo(productType));
|
||||
});
|
||||
|
||||
@@ -28,9 +28,9 @@ internal static class CandyHouseDbContextExtensions
|
||||
return post;
|
||||
}
|
||||
|
||||
public static Product InsertProductToDatabaseAndReturn(this CandyHouseDbContext dbContext, string? id = null, string productName = "test", string productCountry = "country", ProductType productType = ProductType.Cake, double price = 1)
|
||||
public static Product InsertProductToDatabaseAndReturn(this CandyHouseDbContext dbContext, string? id = null, string productName = "test", string productDescription = "country", ProductType productType = ProductType.Cake, double price = 1)
|
||||
{
|
||||
var product = new Product() { Id = id ?? Guid.NewGuid().ToString(), ProductName = productName, ProductCountry = productCountry, ProductType = productType, Price = price };
|
||||
var product = new Product() { Id = id ?? Guid.NewGuid().ToString(), ProductName = productName, ProductDescription = productDescription, ProductType = productType, Price = price };
|
||||
dbContext.Products.Add(product);
|
||||
dbContext.SaveChanges();
|
||||
return product;
|
||||
|
||||
@@ -166,14 +166,14 @@ internal class ProductStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.ProductName, Is.EqualTo(expected.ProductName));
|
||||
Assert.That(actual.ProductCountry, Is.EqualTo(expected.ProductCountry));
|
||||
Assert.That(actual.ProductDescription, Is.EqualTo(expected.ProductDescription));
|
||||
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||
});
|
||||
}
|
||||
|
||||
private static ProductDataModel CreateModel(string id, string productName = "test", string productCountry = "country", ProductType productType = ProductType.Chocolate, double price = 1)
|
||||
=> new(id, productName,productCountry, price, productType);
|
||||
private static ProductDataModel CreateModel(string id, string productName = "test", string productDescription = "country", ProductType productType = ProductType.Chocolate, double price = 1)
|
||||
=> new(id, productName,productDescription, price, productType);
|
||||
|
||||
private static void AssertElement(Product? actual, ProductDataModel expected)
|
||||
{
|
||||
@@ -182,7 +182,7 @@ internal class ProductStorageContractTests : BaseStorageContractTest
|
||||
{
|
||||
Assert.That(actual.Id, Is.EqualTo(expected.Id));
|
||||
Assert.That(actual.ProductName, Is.EqualTo(expected.ProductName));
|
||||
Assert.That(actual.ProductCountry, Is.EqualTo(expected.ProductCountry));
|
||||
Assert.That(actual.ProductDescription, Is.EqualTo(expected.ProductDescription));
|
||||
Assert.That(actual.ProductType, Is.EqualTo(expected.ProductType));
|
||||
Assert.That(actual.Price, Is.EqualTo(expected.Price));
|
||||
});
|
||||
|
||||
@@ -188,15 +188,15 @@ internal class ProductControllerTests : BaseWebApiControllerTest
|
||||
public async Task Post_WhenDataIsIncorrect_ShouldBadRequest_Test()
|
||||
{
|
||||
//Arrange
|
||||
var productModelWithIdIncorrect = new ProductBindingModel { Id = "Id", ProductName = "name", ProductCountry = "country", Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithNameIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = string.Empty, ProductCountry = "country", Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithCountryIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", ProductCountry = string.Empty, Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithProductTypeIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", ProductCountry = "country", Price = 100, ProductType = string.Empty };
|
||||
var productModelWithPriceIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", ProductCountry = "country", Price = 0, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithIdIncorrect = new ProductBindingModel { Id = "Id", ProductName = "name", ProductDescription = "country", Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithNameIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = string.Empty, ProductDescription = "country", Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithDescriptionIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", ProductDescription = string.Empty, Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithProductTypeIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", ProductDescription = "country", Price = 100, ProductType = string.Empty };
|
||||
var productModelWithPriceIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", ProductDescription = "country", Price = 0, ProductType = ProductType.Cake.ToString() };
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PostAsync($"/api/products/register", MakeContent(productModelWithIdIncorrect));
|
||||
var responseWithNameIncorrect = await HttpClient.PostAsync($"/api/products/register", MakeContent(productModelWithNameIncorrect));
|
||||
var responseWithCountryIncorrect = await HttpClient.PostAsync($"/api/products/register", MakeContent(productModelWithCountryIncorrect));
|
||||
var responseWithDescriptionIncorrect = await HttpClient.PostAsync($"/api/products/register", MakeContent(productModelWithDescriptionIncorrect));
|
||||
var responseWithProductTypeIncorrect = await HttpClient.PostAsync($"/api/products/register", MakeContent(productModelWithProductTypeIncorrect));
|
||||
var responseWithPriceIncorrect = await HttpClient.PostAsync($"/api/products/register", MakeContent(productModelWithPriceIncorrect));
|
||||
//Assert
|
||||
@@ -204,7 +204,7 @@ internal class ProductControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
|
||||
Assert.That(responseWithCountryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Country is incorrect");
|
||||
Assert.That(responseWithDescriptionIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Description is incorrect");
|
||||
Assert.That(responseWithProductTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "ProductType is incorrect");
|
||||
Assert.That(responseWithPriceIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Price is incorrect");
|
||||
});
|
||||
@@ -273,13 +273,13 @@ internal class ProductControllerTests : BaseWebApiControllerTest
|
||||
//Arrange
|
||||
var productModelWithIdIncorrect = new ProductBindingModel { Id = "Id", ProductName = "name", Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithNameIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = string.Empty, Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithCountryIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", ProductCountry = string.Empty, Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithDescriptionIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", ProductDescription = string.Empty, Price = 100, ProductType = ProductType.Cake.ToString() };
|
||||
var productModelWithProductTypeIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", Price = 100, ProductType = string.Empty };
|
||||
var productModelWithPriceIncorrect = new ProductBindingModel { Id = Guid.NewGuid().ToString(), ProductName = "name", Price = 0, ProductType = ProductType.Cake.ToString() };
|
||||
//Act
|
||||
var responseWithIdIncorrect = await HttpClient.PutAsync($"/api/products/changeinfo", MakeContent(productModelWithIdIncorrect));
|
||||
var responseWithNameIncorrect = await HttpClient.PutAsync($"/api/products/changeinfo", MakeContent(productModelWithNameIncorrect));
|
||||
var responseWithCountryIncorrect = await HttpClient.PostAsync($"/api/products/register", MakeContent(productModelWithCountryIncorrect));
|
||||
var responseWithDescriptionIncorrect = await HttpClient.PostAsync($"/api/products/register", MakeContent(productModelWithDescriptionIncorrect));
|
||||
var responseWithProductTypeIncorrect = await HttpClient.PutAsync($"/api/products/changeinfo", MakeContent(productModelWithProductTypeIncorrect));
|
||||
var responseWithPriceIncorrect = await HttpClient.PutAsync($"/api/products/changeinfo", MakeContent(productModelWithPriceIncorrect));
|
||||
//Assert
|
||||
@@ -287,7 +287,7 @@ internal class ProductControllerTests : BaseWebApiControllerTest
|
||||
{
|
||||
Assert.That(responseWithIdIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Id is incorrect");
|
||||
Assert.That(responseWithNameIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Name is incorrect");
|
||||
Assert.That(responseWithCountryIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Country is incorrect");
|
||||
Assert.That(responseWithDescriptionIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Description is incorrect");
|
||||
Assert.That(responseWithProductTypeIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "ProductType is incorrect");
|
||||
Assert.That(responseWithPriceIncorrect.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest), "Price is incorrect");
|
||||
});
|
||||
@@ -370,12 +370,12 @@ internal class ProductControllerTests : BaseWebApiControllerTest
|
||||
});
|
||||
}
|
||||
|
||||
private static ProductBindingModel CreateModel(string? id = null, string productName = "name", string productCountry = "country", ProductType productType = ProductType.Cake, double price = 1)
|
||||
private static ProductBindingModel CreateModel(string? id = null, string productName = "name", string productDescription = "country", ProductType productType = ProductType.Cake, double price = 1)
|
||||
=> new()
|
||||
{
|
||||
Id = id ?? Guid.NewGuid().ToString(),
|
||||
ProductName = productName,
|
||||
ProductCountry = productCountry,
|
||||
ProductDescription = productDescription,
|
||||
ProductType = productType.ToString(),
|
||||
Price = price
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user