From fa1ee76d3591f2dacfbb035ac19a8816468661b2 Mon Sep 17 00:00:00 2001 From: csimonapastore Date: Wed, 26 Mar 2025 22:37:50 +0100 Subject: [PATCH] Added in-memory database for tests --- MainProject.Tests/MainProject.Tests.csproj | 1 + .../Services/RoleService_Tests.cs | 23 ++-- .../Services/UserService_Tests.cs | 116 ++++-------------- MainProject.Tests/TestsUtils/TestUtils.cs | 23 +++- 4 files changed, 52 insertions(+), 111 deletions(-) diff --git a/MainProject.Tests/MainProject.Tests.csproj b/MainProject.Tests/MainProject.Tests.csproj index c926d4b..a899faa 100644 --- a/MainProject.Tests/MainProject.Tests.csproj +++ b/MainProject.Tests/MainProject.Tests.csproj @@ -11,6 +11,7 @@ + diff --git a/MainProject.Tests/Services/RoleService_Tests.cs b/MainProject.Tests/Services/RoleService_Tests.cs index be620cb..4a84867 100644 --- a/MainProject.Tests/Services/RoleService_Tests.cs +++ b/MainProject.Tests/Services/RoleService_Tests.cs @@ -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); diff --git a/MainProject.Tests/Services/UserService_Tests.cs b/MainProject.Tests/Services/UserService_Tests.cs index 8fd59b0..929b2e6 100644 --- a/MainProject.Tests/Services/UserService_Tests.cs +++ b/MainProject.Tests/Services/UserService_Tests.cs @@ -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); - } - } diff --git a/MainProject.Tests/TestsUtils/TestUtils.cs b/MainProject.Tests/TestsUtils/TestUtils.cs index 262a14b..68769b0 100644 --- a/MainProject.Tests/TestsUtils/TestUtils.cs +++ b/MainProject.Tests/TestsUtils/TestUtils.cs @@ -18,6 +18,7 @@ using Moq; using BasicDotnetTemplate.MainProject.Core.Database; using BasicDotnetTemplate.MainProject.Services; using Microsoft.AspNetCore.Http; +using Newtonsoft.Json; namespace BasicDotnetTemplate.MainProject.Tests; @@ -51,12 +52,24 @@ public static class TestUtils return _appSettings.DatabaseSettings?.SqlServerConnectionString ?? String.Empty; } + public static SqlServerContext CreateInMemorySqlContext() + { + var options = new DbContextOptionsBuilder() + .UseSqlite("DataSource=:memory:") // Database in-memory + .Options; + + var context = new SqlServerContext(options); + context.Database.OpenConnection(); + context.Database.EnsureCreated(); + return context; + } + public static AuthService CreateAuthService() { IConfiguration configuration = CreateConfiguration(); var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration)); - SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options); + SqlServerContext sqlServerContext = CreateInMemorySqlContext(); var userServiceMock = new Mock(); var httpContextAccessor = new Mock(); return new AuthService(httpContextAccessor.Object, configuration, sqlServerContext, userServiceMock.Object); @@ -65,9 +78,7 @@ public static class TestUtils public static UserService CreateUserService() { IConfiguration configuration = CreateConfiguration(); - var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration)); - SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options); + SqlServerContext sqlServerContext = CreateInMemorySqlContext(); var httpContextAccessor = new Mock(); return new UserService(httpContextAccessor.Object, configuration, sqlServerContext); } @@ -77,7 +88,7 @@ public static class TestUtils IConfiguration configuration = CreateConfiguration(); var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration)); - SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options); + SqlServerContext sqlServerContext = CreateInMemorySqlContext(); var httpContextAccessor = new Mock(); return new JwtService(httpContextAccessor.Object, configuration, sqlServerContext); } @@ -87,7 +98,7 @@ public static class TestUtils IConfiguration configuration = CreateConfiguration(); var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlServer(GetSqlConnectionString(configuration)); - SqlServerContext sqlServerContext = new SqlServerContext(optionsBuilder.Options); + SqlServerContext sqlServerContext = CreateInMemorySqlContext(); var httpContextAccessor = new Mock(); return new RoleService(httpContextAccessor.Object, configuration, sqlServerContext); }