Added in-memory database for tests
This commit is contained in:
@@ -12,6 +12,8 @@ public class RoleService_Tests
|
||||
{
|
||||
private static Role? _expectedRole = ModelsInit.CreateRole();
|
||||
private static Role? _role;
|
||||
private static RoleService _roleService = TestUtils.CreateRoleService();
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void Inizialize()
|
||||
@@ -41,10 +43,10 @@ public class RoleService_Tests
|
||||
try
|
||||
{
|
||||
var expectedRole = ModelsInit.CreateRole();
|
||||
var roleService = TestUtils.CreateRoleService();
|
||||
if (roleService != null)
|
||||
|
||||
if (_roleService != null)
|
||||
{
|
||||
var valid = await roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
else
|
||||
@@ -64,7 +66,6 @@ public class RoleService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var _roleService = TestUtils.CreateRoleService();
|
||||
CreateRoleRequestData data = new CreateRoleRequestData()
|
||||
{
|
||||
Name = _expectedRole?.Name ?? String.Empty,
|
||||
@@ -98,10 +99,9 @@ public class RoleService_Tests
|
||||
try
|
||||
{
|
||||
var expectedRole = ModelsInit.CreateRole();
|
||||
var roleService = TestUtils.CreateRoleService();
|
||||
if (roleService != null)
|
||||
if (_roleService != null)
|
||||
{
|
||||
var valid = await roleService.CheckIfNameIsValid(expectedRole.Name, _role?.Guid ?? String.Empty);
|
||||
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name, _role?.Guid ?? String.Empty);
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
else
|
||||
@@ -122,10 +122,10 @@ public class RoleService_Tests
|
||||
try
|
||||
{
|
||||
var expectedRole = ModelsInit.CreateRole();
|
||||
var roleService = TestUtils.CreateRoleService();
|
||||
if (roleService != null)
|
||||
|
||||
if (_roleService != null)
|
||||
{
|
||||
var valid = await roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||
var valid = await _roleService.CheckIfNameIsValid(expectedRole.Name);
|
||||
Assert.IsFalse(valid);
|
||||
}
|
||||
else
|
||||
@@ -145,7 +145,6 @@ public class RoleService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var _roleService = TestUtils.CreateRoleService();
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.GetRoleByIdAsync(_role?.Id ?? 0);
|
||||
@@ -169,7 +168,6 @@ public class RoleService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var _roleService = TestUtils.CreateRoleService();
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
|
||||
@@ -193,7 +191,6 @@ public class RoleService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var _roleService = TestUtils.CreateRoleService();
|
||||
if (_roleService != null)
|
||||
{
|
||||
var role = await _roleService.GetRoleByGuidAsync(_role?.Guid ?? String.Empty);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using BasicDotnetTemplate.MainProject.Services;
|
||||
using BasicDotnetTemplate.MainProject.Models.Api.Data.User;
|
||||
using BasicDotnetTemplate.MainProject.Models.Database.SqlServer;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
|
||||
|
||||
@@ -10,7 +11,7 @@ namespace BasicDotnetTemplate.MainProject.Tests;
|
||||
public class UserService_Tests
|
||||
{
|
||||
private static User _user = ModelsInit.CreateUser();
|
||||
private static Role _role = ModelsInit.CreateRole();
|
||||
private static UserService _userService = TestUtils.CreateUserService();
|
||||
|
||||
[TestMethod]
|
||||
public void Inizialize()
|
||||
@@ -34,46 +35,15 @@ public class UserService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
// [TestInitialize]
|
||||
// public void Setup()
|
||||
// {
|
||||
// _expectedUser = ModelsInit.CreateUser();
|
||||
// _expectedRole = ModelsInit.CreateRole();
|
||||
// _roleService = TestUtils.CreateRoleService();
|
||||
// _userService = TestUtils.CreateUserService();
|
||||
// }
|
||||
|
||||
// [TestMethod]
|
||||
// public void Inizialize()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// if (_userService != null)
|
||||
// {
|
||||
// Assert.IsInstanceOfType(_userService, typeof(UserService));
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Assert.Fail($"UserService is null");
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine(ex.InnerException);
|
||||
// Assert.Fail($"An exception was thrown: {ex}");
|
||||
// }
|
||||
// }
|
||||
|
||||
[TestMethod]
|
||||
public async Task GetUserByUsernameAndPassword_Null()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
var testString = "test";
|
||||
if (userService != null)
|
||||
if (_userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByUsernameAndPassword(testString, testString);
|
||||
var user = await _userService.GetUserByUsernameAndPassword(testString, testString);
|
||||
Assert.IsTrue(user == null);
|
||||
}
|
||||
else
|
||||
@@ -88,41 +58,14 @@ public class UserService_Tests
|
||||
}
|
||||
}
|
||||
|
||||
// // TODO
|
||||
// // [TestMethod]
|
||||
// public async Task GetUserByUsernameAndPassword_Success()
|
||||
// {
|
||||
// try
|
||||
// {
|
||||
// var testEmail = "test@email.it";
|
||||
// var testPassword = "password";
|
||||
// if (_userService != null)
|
||||
// {
|
||||
// var user = await _userService.GetUserByUsernameAndPassword(testEmail, testPassword);
|
||||
// Assert.IsTrue(user != null);
|
||||
// Assert.IsTrue(user.Email == testEmail);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// Assert.Fail($"UserService is null");
|
||||
// }
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// Console.WriteLine(ex.InnerException);
|
||||
// Assert.Fail($"An exception was thrown: {ex}");
|
||||
// }
|
||||
// }
|
||||
|
||||
[TestMethod]
|
||||
public async Task CheckIfEmailIsValid_EmailNotExists()
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
if (_userService != null)
|
||||
{
|
||||
var valid = await userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
else
|
||||
@@ -142,7 +85,7 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
|
||||
var expectedUser = ModelsInit.CreateUser();
|
||||
|
||||
CreateUserRequestData data = new CreateUserRequestData()
|
||||
@@ -159,7 +102,7 @@ public class UserService_Tests
|
||||
Guid = expectedUser.Role?.Guid ?? String.Empty
|
||||
};
|
||||
|
||||
var user = await userService.CreateUserAsync(data, role);
|
||||
var user = await _userService.CreateUserAsync(data, role);
|
||||
Assert.IsInstanceOfType(user, typeof(User));
|
||||
Assert.IsNotNull(user);
|
||||
Assert.IsTrue(expectedUser.FirstName == user.FirstName);
|
||||
@@ -182,10 +125,10 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var valid = await userService.CheckIfEmailIsValid(_user.Email ?? String.Empty, _user.Guid ?? String.Empty);
|
||||
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty, _user.Guid ?? String.Empty);
|
||||
Assert.IsTrue(valid);
|
||||
}
|
||||
else
|
||||
@@ -205,10 +148,10 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var valid = await userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||
var valid = await _userService.CheckIfEmailIsValid(_user.Email ?? String.Empty);
|
||||
Assert.IsFalse(valid);
|
||||
}
|
||||
else
|
||||
@@ -228,10 +171,10 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByIdAsync(_user.Id);
|
||||
var user = await _userService.GetUserByIdAsync(_user.Id);
|
||||
Assert.IsNotNull(user);
|
||||
Assert.IsTrue(user.Id == _user?.Id);
|
||||
}
|
||||
@@ -252,10 +195,10 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||
var user = await _userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||
Assert.IsNotNull(user);
|
||||
Assert.IsTrue(user.Guid == _user?.Guid);
|
||||
}
|
||||
@@ -276,12 +219,12 @@ public class UserService_Tests
|
||||
{
|
||||
try
|
||||
{
|
||||
var userService = TestUtils.CreateUserService();
|
||||
if (userService != null)
|
||||
|
||||
if (_userService != null)
|
||||
{
|
||||
var user = await userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||
var user = await _userService.GetUserByGuidAsync(_user.Guid ?? String.Empty);
|
||||
Assert.IsNotNull(user);
|
||||
var deleted = await userService.DeleteUserAsync(user);
|
||||
var deleted = await _userService.DeleteUserAsync(user);
|
||||
Assert.IsTrue(deleted);
|
||||
}
|
||||
else
|
||||
@@ -297,17 +240,6 @@ public class UserService_Tests
|
||||
}
|
||||
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public static async Task CleanupAsync()
|
||||
{
|
||||
var roleService = TestUtils.CreateRoleService();
|
||||
var role = await roleService.GetRoleByGuidAsync(_role.Guid ?? String.Empty);
|
||||
Assert.IsNotNull(role);
|
||||
var deleted = await roleService.DeleteRoleAsync(role);
|
||||
Assert.IsTrue(deleted);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user