Skip to content

Commit

Permalink
Added ProductService fixture.
Browse files Browse the repository at this point in the history
  • Loading branch information
claudioig committed Oct 8, 2023
1 parent 158a761 commit 51c12e5
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 40 deletions.
24 changes: 24 additions & 0 deletions NorthwindCRUD.Tests/DataHelper.cs
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];
}

}
}
28 changes: 8 additions & 20 deletions NorthwindCRUD.Tests/EmployeeTerritoryServiceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,11 @@ public void Initialize()
employeeTerritoryService = new EmployeeTerritoryService(context);
}

static T GetRandomElement<T>(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];
}

[TestMethod]
public void ShouldAddTerritoryToEmployee()
{
var employeeId = GetRandomElement(employeeService.GetAll()).EmployeeId;
var territoryId = GetRandomElement(territoryService.GetAll()).TerritoryId;
var employeeId = employeeService.GetAll().GetRandomElement().EmployeeId;
var territoryId = territoryService.GetAll().GetRandomElement().TerritoryId;

var employeeTerritory = new EmployeeTerritoryDb
{
Expand All @@ -54,9 +42,9 @@ public void ShouldAddTerritoryToEmployee()
[TestMethod]
public void ShouldReturnTerritoriesForEmployee()
{
var employeeId = GetRandomElement(employeeService.GetAll()).EmployeeId;
var territoryId1 = GetRandomElement(territoryService.GetAll()).TerritoryId;
var territoryId2 = GetRandomElement(territoryService.GetAll()).TerritoryId;
var employeeId = employeeService.GetAll().GetRandomElement().EmployeeId;
var territoryId1 = territoryService.GetAll().GetRandomElement().TerritoryId;
var territoryId2 = territoryService.GetAll().GetRandomElement().TerritoryId;

var initialTerritoryCount = employeeTerritoryService.GetTeritoriesByEmployeeId(employeeId).Count();

Expand Down Expand Up @@ -87,9 +75,9 @@ public void ShouldReturnTerritoriesForEmployee()
[TestMethod]
public void ShouldReturnEmployeesForTerritory()
{
var employeeId1 = GetRandomElement(employeeService.GetAll()).EmployeeId;
var employeeId2= GetRandomElement(employeeService.GetAll()).EmployeeId;
var territoryId = GetRandomElement(territoryService.GetAll()).TerritoryId;
var employeeId1 = employeeService.GetAll().GetRandomElement().EmployeeId;
var employeeId2= employeeService.GetAll().GetRandomElement().EmployeeId;
var territoryId = territoryService.GetAll().GetRandomElement().TerritoryId;

var initialEmployeeCount = employeeTerritoryService.GetEmployeesByTerritoryId(territoryId).Count();

Expand Down
120 changes: 120 additions & 0 deletions NorthwindCRUD.Tests/ProductServiceFixture.cs
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);
}
}
}
16 changes: 6 additions & 10 deletions NorthwindCRUD/Services/ProductService.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
namespace NorthwindCRUD.Services
{
using AutoMapper;
using NorthwindCRUD.Constants;
using NorthwindCRUD.Helpers;
using NorthwindCRUD.Models.DbModels;
using NorthwindCRUD.Constants;
using NorthwindCRUD.Helpers;
using NorthwindCRUD.Models.DbModels;

namespace NorthwindCRUD.Services
{
public class ProductService
{

private readonly IMapper mapper;
private readonly DataContext dataContext;

public ProductService(IMapper mapper, DataContext dataContext)
public ProductService(DataContext dataContext)
{
this.mapper = mapper;
this.dataContext = dataContext;
}

Expand Down
15 changes: 5 additions & 10 deletions NorthwindCRUD/Services/SupplierService.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
namespace NorthwindCRUD.Services
{
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using NorthwindCRUD.Helpers;
using NorthwindCRUD.Models.DbModels;
using NorthwindCRUD.Models.Dtos;
using NorthwindCRUD.Helpers;
using NorthwindCRUD.Models.DbModels;

namespace NorthwindCRUD.Services
{
public class SupplierService
{

private readonly IMapper mapper;
private readonly DataContext dataContext;

public SupplierService(IMapper mapper, DataContext dataContext)
public SupplierService( DataContext dataContext)
{
this.mapper = mapper;
this.dataContext = dataContext;
}

Expand Down

0 comments on commit 51c12e5

Please sign in to comment.