Skip to content

Commit

Permalink
Added CA and Stylecop for tests project.
Browse files Browse the repository at this point in the history
  • Loading branch information
claudioig committed Oct 8, 2023
1 parent 51c12e5 commit c12185a
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 75 deletions.
14 changes: 7 additions & 7 deletions NorthwindCRUD.Tests/BaseFixture.cs
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -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<DataContext>()
.UseSqlite(connection)
Expand All @@ -45,6 +46,5 @@ protected static DataContext CreateInMemoryDatabaseContext(DbConnection connecti

return new DataContext(options);
}

}
}
14 changes: 7 additions & 7 deletions NorthwindCRUD.Tests/CategroyServiceFixture.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -76,7 +76,7 @@ public void ShouldReturnAllCategories()
var result = categoryService.GetAll();

Assert.IsNotNull(result);
Assert.IsTrue(result.Count() > 0);
Assert.IsTrue(result.Length > 0);
}
}
}
25 changes: 5 additions & 20 deletions NorthwindCRUD.Tests/CustomerServiceFixture.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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",
};
Expand All @@ -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);
Expand All @@ -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);
}
}
}
1 change: 0 additions & 1 deletion NorthwindCRUD.Tests/DataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ public static T GetRandomElement<T>(this T[] array)
int randomIndex = random.Next(array.Length);
return array[randomIndex];
}

}
}
29 changes: 13 additions & 16 deletions NorthwindCRUD.Tests/EmployeeServiceFixture.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -23,7 +23,7 @@ public void ShouldCreateEmployee()
{
FirstName = "John",
LastName = "Doe",
Title = "Manager"
Title = "Manager",
};

var createdEmployee = employeeService.Create(employee);
Expand All @@ -41,7 +41,7 @@ public void ShouldUpdateEmployee()
{
FirstName = "John",
LastName = "Doe",
Title = "Manager"
Title = "Manager",
};
var createdEmployee = employeeService.Create(employee);

Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -94,26 +94,23 @@ public void ShouldReturnEmployeesByReportsTo()
FirstName = "Employee1",
LastName = "Smith",
Title = "Employee",
ReportsTo = createdManager.EmployeeId
ReportsTo = createdManager.EmployeeId,
};

var employee2 = new EmployeeDb
{
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));
}

Expand All @@ -124,7 +121,7 @@ public void ShouldReturnEmployeeById()
{
FirstName = "John",
LastName = "Doe",
Title = "Manager"
Title = "Manager",
};
var createdEmployee = employeeService.Create(employee);

Expand Down
24 changes: 12 additions & 12 deletions NorthwindCRUD.Tests/EmployeeTerritoryServiceFixture.cs
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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
{
Expand All @@ -55,19 +55,19 @@ public void ShouldReturnTerritoriesForEmployee()
};

employeeTerritoryService.AddTerritoryToEmployee(employeeTerritory);

employeeTerritory = new EmployeeTerritoryDb
{
EmployeeId = employeeId,
TerritoryId = territoryId2,
};

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));
}
Expand All @@ -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
{
Expand All @@ -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));
}
Expand Down
18 changes: 15 additions & 3 deletions NorthwindCRUD.Tests/NorthwindCRUD.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<CodeAnalysisRuleSet>../NorthwindCRUD.ruleset</CodeAnalysisRuleSet>
<AnalysisLevel>6.0-recommended</AnalysisLevel>
</PropertyGroup>

<ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>

</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NorthwindCRUD\NorthwindCRUD.csproj" />
Expand Down
Loading

0 comments on commit c12185a

Please sign in to comment.