Added in-memory database for tests

This commit is contained in:
2025-03-26 22:37:50 +01:00
parent 82d5ede421
commit fa1ee76d35
4 changed files with 52 additions and 111 deletions

View File

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