Skip to content

Commit

Permalink
Create unit tests for add product exceptional flows.
Browse files Browse the repository at this point in the history
  • Loading branch information
clodoalves committed May 22, 2022
1 parent 262ab13 commit 37c7283
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 34 deletions.
60 changes: 29 additions & 31 deletions WPFSample/WPFSample.Service/Implementation/ProductService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,6 @@ public void AddProduct(Product product, IList<FileStream> filesWindow)
SaveImages(product, filesWindow);
}

private void ValidateLimitCharacters(Product product)
{
StringBuilder sb = new StringBuilder();

if (product.Title.Length > 25)
{
sb.AppendLine($"{nameof(product.Title)} has limit of 25 characteres");
}

if (product.Description.Length > 200)
{
sb.AppendLine($"{nameof(product.Description)} has limit of 200 characteres");
}

if (product.Price.ToString().Length > 10)
{
sb.AppendLine($"{nameof(product.Price)} has limit of 10 characteres");
}

if (product.Quantity.ToString().Length > 4)
{
sb.AppendLine($"{nameof(product.Quantity)} has limit of 4 characteres");
}

if (sb.Length > 0)
throw new FieldExceedCaracterLimitException(sb.ToString());
}

private void ValidateRequiredFields(Product product)
{
StringBuilder sb = new StringBuilder();
Expand All @@ -90,23 +62,49 @@ private void ValidateRequiredFields(Product product)
if (sb.Length > 0)
throw new RequiredFieldException(sb.ToString());
}

private void ValidateNumericFields(Product product)
{
StringBuilder sb = new StringBuilder();

if (product.Price <= 0)
if (product.Price < 0)
{
sb.AppendLine($"{product.Price} less than zero");
}
if (product.Quantity <= 0)
if (product.Quantity < 0)
{
sb.AppendLine($"{product.Quantity} less than zero");
}

if (sb.Length > 0)
throw new NumericFieldLessThanZeroException(sb.ToString());
}
private void ValidateLimitCharacters(Product product)
{
StringBuilder sb = new StringBuilder();

if (product.Title.Length > 25)
{
sb.AppendLine($"{nameof(product.Title)} has limit of 25 characteres");
}

if (product.Description.Length > 80)
{
sb.AppendLine($"{nameof(product.Description)} has limit of 200 characteres");
}

if (product.Price.ToString().Length > 10)
{
sb.AppendLine($"{nameof(product.Price)} has limit of 10 characteres");
}

if (product.Quantity.ToString().Length > 4)
{
sb.AppendLine($"{nameof(product.Quantity)} has limit of 4 characteres");
}

if (sb.Length > 0)
throw new FieldExceedCaracterLimitException(sb.ToString());
}

private void AddImagesToProduct(Product product, IList<FileStream> filesWindow)
{
Expand Down
108 changes: 105 additions & 3 deletions WPFSample/WPFSample.Test/Service/ProductServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using WPFSample.Domain;
using WPFSample.Service.Contract;
using WPFSample.Service.Implementation;
using WPFSample.Service.Exceptions.Product;

namespace WPFSample.Test.Service
{
Expand All @@ -29,18 +30,119 @@ public void ConfigureDependecies()
[Test]
public void AddProductWithoutTitleTest()
{

_product.Title = string.Empty;
_product.Description = "New product";
_product.Price = 100;
_product.Quantity = 1;

Assert.Throws(typeof(RequiredFieldException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithoutDescriptionTest()
{
_product.Title = "New laptop";
_product.Description = string.Empty;
_product.Price = 100;
_product.Quantity = 1;
_product.Id = 1;
_productService.AddProduct(_product, _files);

Assert.Throws(typeof(RequiredFieldException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithoutQuantity()
{
_product.Title = "New laptop";
_product.Description = "New product";
_product.Price = 100;
_product.Quantity = 0;

Assert.Throws(typeof(RequiredFieldException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithoutPrice()
{
_product.Title = "New laptop";
_product.Description = "New product";
_product.Price = 0;
_product.Quantity = 10;

Assert.Throws(typeof(RequiredFieldException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithPriceLessThanZero()
{
_product.Title = "New laptop";
_product.Description = "New product";
_product.Price = -2;
_product.Quantity = 10;

Assert.Throws(typeof(NumericFieldLessThanZeroException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithQuantityLessThanZero()
{
_product.Title = "New laptop";
_product.Description = "New product";
_product.Price = 3;
_product.Quantity = -5;

Assert.Throws(typeof(NumericFieldLessThanZeroException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithTitleThatExceedsLimitCharacters()
{
_product.Title = "Product with loooooooooooooooooooooooooooooooooooooooooooooooooog title";
_product.Description = "New product";
_product.Price = 3;
_product.Quantity = 1;

Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithDescriptionThatExceedsLimitCharacters()
{
_product.Title = "New laptop";
_product.Description = "Product with loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong description";
_product.Price = 3;
_product.Quantity = 1;

Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithPriceThatExceedsLimitNumbers()
{
_product.Title = "New laptop";
_product.Description = "New product";
_product.Price = 100000000000;
_product.Quantity = 1;

Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddProduct(_product, _files));
}

[Test]
public void AddProductWithQuantityThatExceedsLimitNumbers()
{
_product.Title = "New laptop";
_product.Description = "New product";
_product.Price = 3;
_product.Quantity = 100000000;

Assert.Throws(typeof(FieldExceedCaracterLimitException), () => _productService.AddProduct(_product, _files));
}

[TearDown]
public void ClearConfiguration()
{
_product = null;
_files = null;
_productService = null;
}
}
}

0 comments on commit 37c7283

Please sign in to comment.