-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
163 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace NorthwindCRUD.Tests | ||
{ | ||
public static class DataHelper | ||
{ | ||
public static T GetRandomElement<T>(this T[] array) | ||
{ | ||
if (array == null || array.Length == 0) | ||
{ | ||
throw new ArgumentException("The array cannot be null or empty."); | ||
} | ||
|
||
Random random = new Random(); | ||
int randomIndex = random.Next(array.Length); | ||
return array[randomIndex]; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NorthwindCRUD.Services; | ||
using NorthwindCRUD.Models.DbModels; | ||
|
||
namespace NorthwindCRUD.Tests | ||
{ | ||
[TestClass] | ||
public class ProductServiceFixture : BaseFixture | ||
{ | ||
private ProductService productService; | ||
private CategoryService categoryService; | ||
private SupplierService supplierService; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
DataContext context = GetInMemoryDatabaseContext(); | ||
productService = new ProductService(context); | ||
categoryService = new CategoryService(context); | ||
supplierService = new SupplierService(context); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldCreateProduct() | ||
{ | ||
var categoryId = categoryService.GetAll().GetRandomElement().CategoryId; | ||
var supplierId = supplierService.GetAll().GetRandomElement().SupplierId; | ||
var product = new ProductDb | ||
{ | ||
UnitPrice = 10, | ||
UnitsInStock = 100, | ||
CategoryId = categoryId, | ||
SupplierId = supplierId, | ||
}; | ||
|
||
var createdProduct = productService.Create(product); | ||
|
||
Assert.IsNotNull(createdProduct); | ||
Assert.AreEqual(product.UnitPrice, createdProduct.UnitPrice); | ||
Assert.AreEqual(product.UnitsInStock, createdProduct.UnitsInStock); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldUpdateProduct() | ||
{ | ||
var categoryId = categoryService.GetAll().GetRandomElement().CategoryId; | ||
var supplierId = supplierService.GetAll().GetRandomElement().SupplierId; | ||
var product = new ProductDb | ||
{ | ||
UnitPrice = 10, | ||
UnitsInStock = 100, | ||
CategoryId = categoryId, | ||
SupplierId = supplierId, | ||
}; | ||
|
||
var createdProduct = productService.Create(product); | ||
|
||
createdProduct.UnitPrice = 15; | ||
createdProduct.UnitsInStock = 50; | ||
|
||
var updatedProduct = productService.Update(createdProduct); | ||
|
||
Assert.IsNotNull(updatedProduct); | ||
Assert.AreEqual(15, updatedProduct.UnitPrice); | ||
Assert.AreEqual(50, updatedProduct.UnitsInStock); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldDeleteProduct() | ||
{ | ||
var categoryId = categoryService.GetAll().GetRandomElement().CategoryId; | ||
var supplierId = supplierService.GetAll().GetRandomElement().SupplierId; | ||
var product = new ProductDb | ||
{ | ||
UnitPrice = 10, | ||
UnitsInStock = 100, | ||
CategoryId = categoryId, | ||
SupplierId = supplierId, | ||
}; | ||
|
||
var createdProduct = productService.Create(product); | ||
|
||
|
||
productService.Delete(createdProduct.ProductId); | ||
var deletedProduct = productService.GetById(createdProduct.ProductId); | ||
|
||
Assert.IsNull(deletedProduct); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldGetAllProducts() | ||
{ | ||
var result = productService.GetAll(); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsTrue(result.Count() > 0); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldGetProductById() | ||
{ | ||
var categoryId = categoryService.GetAll().GetRandomElement().CategoryId; | ||
var supplierId = supplierService.GetAll().GetRandomElement().SupplierId; | ||
var product = new ProductDb | ||
{ | ||
UnitPrice = 10, | ||
UnitsInStock = 100, | ||
CategoryId = categoryId, | ||
SupplierId = supplierId, | ||
}; | ||
|
||
var createdProduct = productService.Create(product); | ||
var result = productService.GetById(createdProduct.ProductId); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.AreEqual(10, result.UnitPrice); | ||
Assert.AreEqual(100, result.UnitsInStock); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters