Added unit tests

This commit is contained in:
thermalthrottle
2021-03-20 11:04:58 +02:00
parent bb4f2b4f16
commit bec08c5851
7 changed files with 118 additions and 19 deletions

View File

@@ -44,7 +44,7 @@ namespace Business.Business.Sales
/// Gets All Roles
/// </summary>
/// <remarks>
/// Requires no special roles
/// Requires no special roles.
/// </remarks>
/// <returns>
/// Returns a ICollection of all roles.
@@ -56,12 +56,13 @@ namespace Business.Business.Sales
}
/// <summary>
/// Searches the role by given Id
/// Searches the role by given Id.
/// </summary>
/// <remarks>
/// Requires no special roles.
/// </remarks>
/// <returns>
/// Returns an object of the role with the given Id.
///
/// Requires no special roles
/// </returns>
public Product Get(int id)
@@ -80,10 +81,11 @@ namespace Business.Business.Sales
/// <summary>
/// Searches the role by given name
/// </summary>
/// <remarks>
/// Requires no special roles.
/// </remarks>
/// <returns>
/// Returns an object of the role with the given name.
///
/// Requires no special roles
/// </returns>
public Product Get(string name)
@@ -102,10 +104,11 @@ namespace Business.Business.Sales
/// <summary>
/// Searches the role by a given substring
/// </summary>
/// <remarks>
/// Requires no special roles.
/// </remarks>
/// <returns>
/// Returns an ICollection of all roles that contain the given substring in their name.
///
/// Requires no special roles
/// </returns>
public ICollection<Product> GetByApproximateName(string name)

View File

@@ -51,14 +51,17 @@ namespace Business.Business.UserManagment.Controllers
this.context = context;
this.currentUser = currentUser;
}
/// <summary>
/// Creates the roles
/// </summary>
/// <remarks>
/// Requires no special roles. Not even an registered user.
/// </remarks>
/// <remarks>
/// Almost every method of each class checks if the user has suffficient roles for the task
/// </remarks>
public void CreateInitialRoles()
{
var Cashier = new Role("Cashier");

View File

@@ -161,10 +161,13 @@ namespace Business.Business.UserManagment
/// Checks if the password is valid
/// </summary>
/// <remarks>
/// Password is used to log in the user
/// Requires no special roles.
/// </remarks>
/// <remarks>
/// Password is used to log in the user.
/// </remarks>
/// <returns>
/// Returns an object of the found user
/// Returns an object of the found user.
/// </returns>
public User ValidatePassword(string password)
@@ -181,7 +184,10 @@ namespace Business.Business.UserManagment
/// Registers an user
/// </summary>
/// <remarks>
/// Used for the creation of the initial user, so it assigns admin role by default
/// Requires no special roles.
/// </remarks>
/// <remarks>
/// Used for the creation of the initial user, so it assigns admin role by default.
/// </remarks>
public void RegisterItem(string name, string password)

View File

@@ -7,7 +7,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data", "Models\Data.csproj"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Business", "Business\Business.csproj", "{E1661853-3081-4C40-8E68-C063B3BC88A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Display", "Display\Display.csproj", "{61E060C5-4360-4880-9ED7-6A139C988F0F}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Display", "Display\Display.csproj", "{61E060C5-4360-4880-9ED7-6A139C988F0F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LuminousTests", "LuminousTests\LuminousTests.csproj", "{8475489D-3593-43D0-A6E2-006A732B982A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -27,6 +29,10 @@ Global
{61E060C5-4360-4880-9ED7-6A139C988F0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{61E060C5-4360-4880-9ED7-6A139C988F0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{61E060C5-4360-4880-9ED7-6A139C988F0F}.Release|Any CPU.Build.0 = Release|Any CPU
{8475489D-3593-43D0-A6E2-006A732B982A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8475489D-3593-43D0-A6E2-006A732B982A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8475489D-3593-43D0-A6E2-006A732B982A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8475489D-3593-43D0-A6E2-006A732B982A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Business\Business.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,61 @@
using NUnit.Framework;
using Moq;
using Models.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Models;
using Business.Business.UserManagment;
using Business.Business.UserManagment.Controllers;
namespace LuminousUnitTests
{
public class Tests
{
private UserController userctrl;
private RoleController rolectrl;
private IQueryable<User> testUsers;
[SetUp]
public void Setup()
{
var mock = new Mock<DbSet<User>>();
var mocka = new Mock<DbSet<User>>();
testUsers = new List<User>
{
new User(){ Name = "Admin", Password = "adm123", RoleId = 1},
new User(){ Name = "Goso", Password = "goso123", RoleId = 2},
new User(){ Name = "Pesho", Password = "peso123", RoleId = 3},
}.AsQueryable();
mock.As<IQueryable<User>>().Setup(m => m.Provider).Returns(testUsers.Provider);
mock.As<IQueryable<User>>().Setup(m => m.Expression).Returns(testUsers.Expression);
mock.As<IQueryable<User>>().Setup(m => m.ElementType).Returns(testUsers.ElementType);
mock.As<IQueryable<User>>().Setup(m => m.GetEnumerator()).Returns(testUsers.GetEnumerator());
var testContext = new Mock<LuminousContext>();
testContext.Setup(s => s.User).Returns(mock.Object);
userctrl = new UserController(testUsers.ToList()[0], testContext.Object);
}
[Test]
public void UsersController_GetAll()
{
List<User> users = userctrl.GetAll().ToList();
List<User> testUsersList = testUsers.ToList();
Assert.AreEqual(users.Count, testUsers.Count());
for (int i = 0; i < users.Count; i++)
{
Assert.AreEqual(users[i].Name, testUsersList[i].Name);
Assert.AreEqual(users[i].Password, testUsersList[i].Password);
Assert.AreEqual(users[i].RoleId, testUsersList[i].RoleId);
}
}
[Test]
public void UserController_AddItem()
{
userctrl.RegisterItem("Penka", "penka123", 3);
List<User> users = userctrl.GetAll().ToList();
Assert.AreEqual(users.Count, 4);
}
}
}

View File

@@ -16,11 +16,11 @@ namespace Models
}
public DbSet<User> User { get; set; }
public DbSet<Role> Role { get; set; }
public DbSet<Product> Product { get; set; }
public DbSet<Deal> Deal { get; set; }
public DbSet<Stock> Stock { get; set; }
public virtual DbSet<User> User { get; set; }
public virtual DbSet<Role> Role { get; set; }
public virtual DbSet<Product> Product { get; set; }
public virtual DbSet<Deal> Deal { get; set; }
public virtual DbSet<Stock> Stock { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{