diff --git a/NorthwindCRUD.Tests/BaseFixture.cs b/NorthwindCRUD.Tests/BaseFixture.cs index 97d4775..05fa5a0 100644 --- a/NorthwindCRUD.Tests/BaseFixture.cs +++ b/NorthwindCRUD.Tests/BaseFixture.cs @@ -1,13 +1,15 @@ -using Microsoft.Data.Sqlite; +using System.Data.Common; +using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using NorthwindCRUD.Helpers; -using System.Data.Common; +using NorthwindCRUD.Services; namespace NorthwindCRUD.Tests { public class BaseFixture { - private DbConnection? connection; + // null! indicates that the member is initialized in other code + private DbConnection? connection = null!; protected DataContext GetInMemoryDatabaseContext() { @@ -25,15 +27,14 @@ protected DataContext GetInMemoryDatabaseContext() } } - protected static DbConnection CreateDbConnection() + protected DbConnection CreateDbConnection() { var connection = new SqliteConnection("DataSource=:memory:"); connection.Open(); return connection; } - - protected static DataContext CreateInMemoryDatabaseContext(DbConnection connection) + protected DataContext CreateInMemoryDatabaseContext(DbConnection connection) { var options = new DbContextOptionsBuilder() .UseSqlite(connection) @@ -45,6 +46,5 @@ protected static DataContext CreateInMemoryDatabaseContext(DbConnection connecti return new DataContext(options); } - } } diff --git a/NorthwindCRUD.Tests/CategroyServiceFixture.cs b/NorthwindCRUD.Tests/CategroyServiceFixture.cs index ef0b5d9..b02f6f4 100644 --- a/NorthwindCRUD.Tests/CategroyServiceFixture.cs +++ b/NorthwindCRUD.Tests/CategroyServiceFixture.cs @@ -1,13 +1,13 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using NorthwindCRUD.Services; using NorthwindCRUD.Models.DbModels; +using NorthwindCRUD.Services; namespace NorthwindCRUD.Tests { [TestClass] public class CategoryServiceFixture : BaseFixture { - private CategoryService categoryService; + private CategoryService categoryService = null!; [TestInitialize] public void Initialize() @@ -22,7 +22,7 @@ public void ShouldCreateCategory() var category = new CategoryDb { Name = "New Category", - Description = "New Category Description" + Description = "New Category Description", }; var createdCategory = categoryService.Create(category); @@ -40,7 +40,7 @@ public void ShouldUpdateCategory() var category = new CategoryDb { Name = "New Category", - Description = "New Category Description" + Description = "New Category Description", }; var createdCategory = categoryService.Create(category); @@ -59,9 +59,9 @@ public void ShouldDeleteCategory() var category = new CategoryDb { Name = "New Category", - Description = "New Category Description" + Description = "New Category Description", }; - + var createdCategory = categoryService.Create(category); categoryService.Delete(createdCategory.CategoryId); @@ -76,7 +76,7 @@ public void ShouldReturnAllCategories() var result = categoryService.GetAll(); Assert.IsNotNull(result); - Assert.IsTrue(result.Count() > 0); + Assert.IsTrue(result.Length > 0); } } } \ No newline at end of file diff --git a/NorthwindCRUD.Tests/CustomerServiceFixture.cs b/NorthwindCRUD.Tests/CustomerServiceFixture.cs index f04e388..21c1920 100644 --- a/NorthwindCRUD.Tests/CustomerServiceFixture.cs +++ b/NorthwindCRUD.Tests/CustomerServiceFixture.cs @@ -1,13 +1,13 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using NorthwindCRUD.Services; using NorthwindCRUD.Models.DbModels; +using NorthwindCRUD.Services; namespace NorthwindCRUD.Tests { [TestClass] public class CustomerServiceFixture : BaseFixture { - private CustomerService customerService; + private CustomerService customerService = null!; [TestInitialize] public void Initialize() @@ -24,7 +24,7 @@ public void ShouldCreateCustomer() Street = "6955 Union Park Center Suite 500", City = "Midvale", PostalCode = "84047", - Region = "", + Region = string.Empty, Country = "USA", Phone = "(800) 231-8588", }; @@ -39,7 +39,7 @@ public void ShouldCreateCustomer() }; var createdCustomer = customerService.Create(customer); - + Assert.IsNotNull(createdCustomer); Assert.AreEqual(customer.CompanyName, createdCustomer.CompanyName); Assert.AreEqual(customer.ContactName, createdCustomer.ContactName); @@ -51,27 +51,12 @@ public void ShouldCreateCustomer() 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() - { + Assert.IsTrue(result.Length > 0); } } } \ No newline at end of file diff --git a/NorthwindCRUD.Tests/DataHelper.cs b/NorthwindCRUD.Tests/DataHelper.cs index 2e1a20a..f7fc88e 100644 --- a/NorthwindCRUD.Tests/DataHelper.cs +++ b/NorthwindCRUD.Tests/DataHelper.cs @@ -19,6 +19,5 @@ public static T GetRandomElement(this T[] array) int randomIndex = random.Next(array.Length); return array[randomIndex]; } - } } diff --git a/NorthwindCRUD.Tests/EmployeeServiceFixture.cs b/NorthwindCRUD.Tests/EmployeeServiceFixture.cs index da7bfaa..0b9797a 100644 --- a/NorthwindCRUD.Tests/EmployeeServiceFixture.cs +++ b/NorthwindCRUD.Tests/EmployeeServiceFixture.cs @@ -1,13 +1,13 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using NorthwindCRUD.Services; using NorthwindCRUD.Models.DbModels; +using NorthwindCRUD.Services; namespace NorthwindCRUD.Tests { [TestClass] public class EmployeeServiceFixture : BaseFixture { - private EmployeeService employeeService; + private EmployeeService employeeService = null!; [TestInitialize] public void Initialize() @@ -23,7 +23,7 @@ public void ShouldCreateEmployee() { FirstName = "John", LastName = "Doe", - Title = "Manager" + Title = "Manager", }; var createdEmployee = employeeService.Create(employee); @@ -41,7 +41,7 @@ public void ShouldUpdateEmployee() { FirstName = "John", LastName = "Doe", - Title = "Manager" + Title = "Manager", }; var createdEmployee = employeeService.Create(employee); @@ -59,13 +59,13 @@ public void ShouldDeleteEmployee() { FirstName = "John", LastName = "Doe", - Title = "Manager" + Title = "Manager", }; var createdEmployee = employeeService.Create(employee); employeeService.Delete(createdEmployee.EmployeeId); var deletedEmployee = employeeService.GetById(createdEmployee.EmployeeId); - + Assert.IsNull(deletedEmployee); } @@ -75,17 +75,17 @@ public void ShouldReturnAllEmployees() var result = employeeService.GetAll(); Assert.IsNotNull(result); - Assert.IsTrue(result.Count() > 0); + Assert.IsTrue(result.Length > 0); } [TestMethod] public void ShouldReturnEmployeesByReportsTo() - { + { var manager = new EmployeeDb { FirstName = "Manager", LastName = "Doe", - Title = "Manager" + Title = "Manager", }; var createdManager = employeeService.Create(manager); @@ -94,7 +94,7 @@ public void ShouldReturnEmployeesByReportsTo() FirstName = "Employee1", LastName = "Smith", Title = "Employee", - ReportsTo = createdManager.EmployeeId + ReportsTo = createdManager.EmployeeId, }; var employee2 = new EmployeeDb @@ -102,18 +102,15 @@ public void ShouldReturnEmployeesByReportsTo() FirstName = "Employee2", LastName = "Johnson", Title = "Employee", - ReportsTo = createdManager.EmployeeId + 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.AreEqual(2, result.Length); Assert.IsTrue(result.All(e => e.ReportsTo == createdManager.EmployeeId)); } @@ -124,7 +121,7 @@ public void ShouldReturnEmployeeById() { FirstName = "John", LastName = "Doe", - Title = "Manager" + Title = "Manager", }; var createdEmployee = employeeService.Create(employee); diff --git a/NorthwindCRUD.Tests/EmployeeTerritoryServiceFixture.cs b/NorthwindCRUD.Tests/EmployeeTerritoryServiceFixture.cs index f3d6515..4006103 100644 --- a/NorthwindCRUD.Tests/EmployeeTerritoryServiceFixture.cs +++ b/NorthwindCRUD.Tests/EmployeeTerritoryServiceFixture.cs @@ -1,15 +1,15 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using NorthwindCRUD.Services; using NorthwindCRUD.Models.DbModels; +using NorthwindCRUD.Services; namespace NorthwindCRUD.Tests { [TestClass] public class EmployeeTerritoryServiceFixture : BaseFixture { - private EmployeeTerritoryService employeeTerritoryService; - private EmployeeService employeeService; - private TerritoryService territoryService; + private EmployeeTerritoryService employeeTerritoryService = null!; + private EmployeeService employeeService = null!; + private TerritoryService territoryService = null!; [TestInitialize] public void Initialize() @@ -45,8 +45,8 @@ public void ShouldReturnTerritoriesForEmployee() var employeeId = employeeService.GetAll().GetRandomElement().EmployeeId; var territoryId1 = territoryService.GetAll().GetRandomElement().TerritoryId; var territoryId2 = territoryService.GetAll().GetRandomElement().TerritoryId; - - var initialTerritoryCount = employeeTerritoryService.GetTeritoriesByEmployeeId(employeeId).Count(); + + var initialTerritoryCount = employeeTerritoryService.GetTeritoriesByEmployeeId(employeeId).Length; var employeeTerritory = new EmployeeTerritoryDb { @@ -55,7 +55,7 @@ public void ShouldReturnTerritoriesForEmployee() }; employeeTerritoryService.AddTerritoryToEmployee(employeeTerritory); - + employeeTerritory = new EmployeeTerritoryDb { EmployeeId = employeeId, @@ -63,11 +63,11 @@ public void ShouldReturnTerritoriesForEmployee() }; employeeTerritoryService.AddTerritoryToEmployee(employeeTerritory); - + var territories = employeeTerritoryService.GetTeritoriesByEmployeeId(employeeId); Assert.IsNotNull(territories); - Assert.AreEqual(initialTerritoryCount + 2, territories.Count()); + Assert.AreEqual(initialTerritoryCount + 2, territories.Length); Assert.IsTrue(territories.Any(t => t.TerritoryId == territoryId1)); Assert.IsTrue(territories.Any(t => t.TerritoryId == territoryId2)); } @@ -76,10 +76,10 @@ public void ShouldReturnTerritoriesForEmployee() public void ShouldReturnEmployeesForTerritory() { var employeeId1 = employeeService.GetAll().GetRandomElement().EmployeeId; - var employeeId2= employeeService.GetAll().GetRandomElement().EmployeeId; + var employeeId2 = employeeService.GetAll().GetRandomElement().EmployeeId; var territoryId = territoryService.GetAll().GetRandomElement().TerritoryId; - var initialEmployeeCount = employeeTerritoryService.GetEmployeesByTerritoryId(territoryId).Count(); + var initialEmployeeCount = employeeTerritoryService.GetEmployeesByTerritoryId(territoryId).Length; var employeeTerritory = new EmployeeTerritoryDb { @@ -100,7 +100,7 @@ public void ShouldReturnEmployeesForTerritory() var employees = employeeTerritoryService.GetEmployeesByTerritoryId(territoryId); Assert.IsNotNull(employees); - Assert.AreEqual(initialEmployeeCount + 2, employees.Count()); + Assert.AreEqual(initialEmployeeCount + 2, employees.Length); Assert.IsTrue(employees.Any(e => e.EmployeeId == employeeId1)); Assert.IsTrue(employees.Any(e => e.EmployeeId == employeeId2)); } diff --git a/NorthwindCRUD.Tests/NorthwindCRUD.Tests.csproj b/NorthwindCRUD.Tests/NorthwindCRUD.Tests.csproj index 9e0ec45..66bd546 100644 --- a/NorthwindCRUD.Tests/NorthwindCRUD.Tests.csproj +++ b/NorthwindCRUD.Tests/NorthwindCRUD.Tests.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,14 +6,26 @@ enable false + ../NorthwindCRUD.ruleset + 6.0-recommended - + + true + + + + - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/NorthwindCRUD.Tests/ProductServiceFixture.cs b/NorthwindCRUD.Tests/ProductServiceFixture.cs index f4d71cd..812fd07 100644 --- a/NorthwindCRUD.Tests/ProductServiceFixture.cs +++ b/NorthwindCRUD.Tests/ProductServiceFixture.cs @@ -1,15 +1,15 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; -using NorthwindCRUD.Services; using NorthwindCRUD.Models.DbModels; +using NorthwindCRUD.Services; namespace NorthwindCRUD.Tests { [TestClass] public class ProductServiceFixture : BaseFixture { - private ProductService productService; - private CategoryService categoryService; - private SupplierService supplierService; + private ProductService productService = null!; + private CategoryService categoryService = null!; + private SupplierService supplierService = null!; [TestInitialize] public void Initialize() @@ -54,7 +54,7 @@ public void ShouldUpdateProduct() }; var createdProduct = productService.Create(product); - + createdProduct.UnitPrice = 15; createdProduct.UnitsInStock = 50; @@ -80,7 +80,6 @@ public void ShouldDeleteProduct() var createdProduct = productService.Create(product); - productService.Delete(createdProduct.ProductId); var deletedProduct = productService.GetById(createdProduct.ProductId); @@ -93,7 +92,7 @@ public void ShouldGetAllProducts() var result = productService.GetAll(); Assert.IsNotNull(result); - Assert.IsTrue(result.Count() > 0); + Assert.IsTrue(result.Length > 0); } [TestMethod] diff --git a/NorthwindCRUD.ruleset b/NorthwindCRUD.ruleset new file mode 100644 index 0000000..942df3b --- /dev/null +++ b/NorthwindCRUD.ruleset @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/NorthwindCRUD.sln b/NorthwindCRUD.sln index 82f9b68..66b437d 100644 --- a/NorthwindCRUD.sln +++ b/NorthwindCRUD.sln @@ -7,7 +7,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NorthwindCRUD", "NorthwindC EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NorthwindCRUD.Tests", "NorthwindCRUD.Tests\NorthwindCRUD.Tests.csproj", "{F2C392E4-219B-41F3-8010-10E69EBF434D}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Github Workflows", "Github Workflows", "{816E41A3-F9E9-4244-88C7-930196599CFF}" +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{816E41A3-F9E9-4244-88C7-930196599CFF}" + ProjectSection(SolutionItems) = preProject + NorthwindCRUD.ruleset = NorthwindCRUD.ruleset + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "github-workflows", "github-workflows", "{CA0B0BD2-B68E-4511-9038-02261DE7EE0D}" ProjectSection(SolutionItems) = preProject .github\workflows\build-test-ci.yml = .github\workflows\build-test-ci.yml EndProjectSection @@ -30,6 +35,9 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {CA0B0BD2-B68E-4511-9038-02261DE7EE0D} = {816E41A3-F9E9-4244-88C7-930196599CFF} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {562EFEC3-EDD3-4783-A44E-BAFEE6B3E989} EndGlobalSection diff --git a/NorthwindCRUD/NorthwindCRUD.csproj b/NorthwindCRUD/NorthwindCRUD.csproj index e7f0d58..dd5f7d4 100644 --- a/NorthwindCRUD/NorthwindCRUD.csproj +++ b/NorthwindCRUD/NorthwindCRUD.csproj @@ -11,7 +11,6 @@ -