-
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.
Added initial set of service tests and corresponding workflow.
- Loading branch information
Showing
11 changed files
with
545 additions
and
27 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,30 @@ | ||
name: Build / unit tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
jobs: | ||
build-and-test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup .NET | ||
uses: actions/setup-dotnet@v1 | ||
with: | ||
dotnet-version: 6.x | ||
|
||
- name: Restore dependencies | ||
run: dotnet restore | ||
|
||
- name: Build | ||
run: dotnet build --configuration Release | ||
|
||
- name: Run tests | ||
run: dotnet test --configuration Release --no-build |
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,50 @@ | ||
using Microsoft.Data.Sqlite; | ||
using Microsoft.EntityFrameworkCore; | ||
using NorthwindCRUD.Helpers; | ||
using System.Data.Common; | ||
|
||
namespace NorthwindCRUD.Tests | ||
{ | ||
public class BaseFixture | ||
{ | ||
private DbConnection? connection; | ||
|
||
protected DataContext GetInMemoryDatabaseContext() | ||
{ | ||
if (connection == null) | ||
{ | ||
connection = CreateDbConnection(); | ||
var context = CreateInMemoryDatabaseContext(connection); | ||
DBSeeder.Seed(context); | ||
return context; | ||
} | ||
else | ||
{ | ||
// Create a new Context on already initialized DB connection | ||
return CreateInMemoryDatabaseContext(connection); | ||
} | ||
} | ||
|
||
protected static DbConnection CreateDbConnection() | ||
{ | ||
var connection = new SqliteConnection("DataSource=:memory:"); | ||
connection.Open(); | ||
return connection; | ||
} | ||
|
||
|
||
protected static DataContext CreateInMemoryDatabaseContext(DbConnection connection) | ||
{ | ||
var options = new DbContextOptionsBuilder<DataContext>() | ||
.UseSqlite(connection) | ||
.EnableSensitiveDataLogging() | ||
|
||
// Uncomment the following line for detailed sql EF logs. | ||
// .EnableDetailedErrors().LogTo(Console.WriteLine, LogLevel.Debug) | ||
.Options; | ||
|
||
return new DataContext(options); | ||
} | ||
|
||
} | ||
} |
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,82 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NorthwindCRUD.Services; | ||
using NorthwindCRUD.Models.DbModels; | ||
|
||
namespace NorthwindCRUD.Tests | ||
{ | ||
[TestClass] | ||
public class CategoryServiceFixture : BaseFixture | ||
{ | ||
private CategoryService categoryService; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
DataContext context = GetInMemoryDatabaseContext(); | ||
categoryService = new CategoryService(context); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldCreateCategory() | ||
{ | ||
var category = new CategoryDb | ||
{ | ||
Name = "New Category", | ||
Description = "New Category Description" | ||
}; | ||
|
||
var createdCategory = categoryService.Create(category); | ||
|
||
var result = categoryService.GetById(createdCategory.CategoryId); | ||
|
||
Assert.IsNotNull(createdCategory); | ||
Assert.AreEqual(category.Name, result.Name); | ||
Assert.AreEqual(category.Description, result.Description); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldUpdateCategory() | ||
{ | ||
var category = new CategoryDb | ||
{ | ||
Name = "New Category", | ||
Description = "New Category Description" | ||
}; | ||
var createdCategory = categoryService.Create(category); | ||
|
||
createdCategory.Name = "Updated Category"; | ||
createdCategory.Description = "Updated Description"; | ||
var updatedCategory = categoryService.Update(createdCategory); | ||
|
||
Assert.IsNotNull(updatedCategory); | ||
Assert.AreEqual("Updated Category", updatedCategory.Name); | ||
Assert.AreEqual("Updated Description", updatedCategory.Description); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldDeleteCategory() | ||
{ | ||
var category = new CategoryDb | ||
{ | ||
Name = "New Category", | ||
Description = "New Category Description" | ||
}; | ||
|
||
var createdCategory = categoryService.Create(category); | ||
|
||
categoryService.Delete(createdCategory.CategoryId); | ||
var deletedCategory = categoryService.GetById(createdCategory.CategoryId); | ||
|
||
Assert.IsNull(deletedCategory); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldReturnAllCategories() | ||
{ | ||
var result = categoryService.GetAll(); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsTrue(result.Count() > 0); | ||
} | ||
} | ||
} |
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,77 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NorthwindCRUD.Services; | ||
using NorthwindCRUD.Models.DbModels; | ||
|
||
namespace NorthwindCRUD.Tests | ||
{ | ||
[TestClass] | ||
public class CustomerServiceFixture : BaseFixture | ||
{ | ||
private CustomerService customerService; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
DataContext context = GetInMemoryDatabaseContext(); | ||
customerService = new CustomerService(context); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldCreateCustomer() | ||
{ | ||
var address = new AddressDb | ||
{ | ||
Street = "6955 Union Park Center Suite 500", | ||
City = "Midvale", | ||
PostalCode = "84047", | ||
Region = "", | ||
Country = "USA", | ||
Phone = "(800) 231-8588", | ||
}; | ||
|
||
var customer = new CustomerDb | ||
{ | ||
CustomerId = "12345", | ||
CompanyName = "Infragistics", | ||
ContactName = "Maria Anders", | ||
ContactTitle = "Sales Representative", | ||
Address = address, | ||
}; | ||
|
||
var createdCustomer = customerService.Create(customer); | ||
|
||
Assert.IsNotNull(createdCustomer); | ||
Assert.AreEqual(customer.CompanyName, createdCustomer.CompanyName); | ||
Assert.AreEqual(customer.ContactName, createdCustomer.ContactName); | ||
Assert.AreEqual(customer.ContactTitle, createdCustomer.ContactTitle); | ||
Assert.AreEqual(customer.Address.Street, createdCustomer.Address.Street); | ||
Assert.AreEqual(customer.Address.City, createdCustomer.Address.City); | ||
Assert.AreEqual(customer.Address.PostalCode, createdCustomer.Address.PostalCode); | ||
Assert.AreEqual(customer.Address.Country, createdCustomer.Address.Country); | ||
Assert.AreEqual(customer.Address.Phone, createdCustomer.Address.Phone); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldUpdateEmployee() | ||
{ | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldDeleteEmployee() | ||
{ | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldReturnAllCustomers() | ||
{ | ||
var result = customerService.GetAll(); | ||
Assert.IsNotNull(result); | ||
Assert.IsTrue(result.Count() > 0); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldReturnEmployeesByReportsTo() | ||
{ | ||
} | ||
} | ||
} |
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,139 @@ | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NorthwindCRUD.Services; | ||
using NorthwindCRUD.Models.DbModels; | ||
|
||
namespace NorthwindCRUD.Tests | ||
{ | ||
[TestClass] | ||
public class EmployeeServiceFixture : BaseFixture | ||
{ | ||
private EmployeeService employeeService; | ||
|
||
[TestInitialize] | ||
public void Initialize() | ||
{ | ||
DataContext context = GetInMemoryDatabaseContext(); | ||
employeeService = new EmployeeService(context); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldCreateEmployee() | ||
{ | ||
var employee = new EmployeeDb | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Title = "Manager" | ||
}; | ||
|
||
var createdEmployee = employeeService.Create(employee); | ||
|
||
Assert.IsNotNull(createdEmployee); | ||
Assert.AreEqual(employee.FirstName, createdEmployee.FirstName); | ||
Assert.AreEqual(employee.LastName, createdEmployee.LastName); | ||
Assert.AreEqual(employee.Title, createdEmployee.Title); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldUpdateEmployee() | ||
{ | ||
var employee = new EmployeeDb | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Title = "Manager" | ||
}; | ||
var createdEmployee = employeeService.Create(employee); | ||
|
||
createdEmployee.Title = "Director"; | ||
var updatedEmployee = employeeService.Update(createdEmployee); | ||
|
||
Assert.IsNotNull(updatedEmployee); | ||
Assert.AreEqual("Director", updatedEmployee.Title); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldDeleteEmployee() | ||
{ | ||
var employee = new EmployeeDb | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Title = "Manager" | ||
}; | ||
var createdEmployee = employeeService.Create(employee); | ||
|
||
employeeService.Delete(createdEmployee.EmployeeId); | ||
var deletedEmployee = employeeService.GetById(createdEmployee.EmployeeId); | ||
|
||
Assert.IsNull(deletedEmployee); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldReturnAllEmployees() | ||
{ | ||
var result = employeeService.GetAll(); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.IsTrue(result.Count() > 0); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldReturnEmployeesByReportsTo() | ||
{ | ||
var manager = new EmployeeDb | ||
{ | ||
FirstName = "Manager", | ||
LastName = "Doe", | ||
Title = "Manager" | ||
}; | ||
|
||
var createdManager = employeeService.Create(manager); | ||
var employee1 = new EmployeeDb | ||
{ | ||
FirstName = "Employee1", | ||
LastName = "Smith", | ||
Title = "Employee", | ||
ReportsTo = createdManager.EmployeeId | ||
}; | ||
|
||
var employee2 = new EmployeeDb | ||
{ | ||
FirstName = "Employee2", | ||
LastName = "Johnson", | ||
Title = "Employee", | ||
ReportsTo = createdManager.EmployeeId | ||
}; | ||
|
||
|
||
var createdEmployee1 = employeeService.Create(employee1); | ||
var createdEmployee2 = employeeService.Create(employee2); | ||
|
||
|
||
var result = employeeService.GetEmployeesByReportsTo(createdManager.EmployeeId); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.AreEqual(2, result.Count()); | ||
Assert.IsTrue(result.All(e => e.ReportsTo == createdManager.EmployeeId)); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldReturnEmployeeById() | ||
{ | ||
var employee = new EmployeeDb | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Title = "Manager" | ||
}; | ||
var createdEmployee = employeeService.Create(employee); | ||
|
||
var result = employeeService.GetById(createdEmployee.EmployeeId); | ||
|
||
Assert.IsNotNull(result); | ||
Assert.AreEqual("John", result.FirstName); | ||
Assert.AreEqual("Doe", result.LastName); | ||
Assert.AreEqual("Manager", result.Title); | ||
} | ||
} | ||
} |
Oops, something went wrong.