diff --git a/AnalysisData/AnalysisData.sln.DotSettings.user b/AnalysisData/AnalysisData.sln.DotSettings.user index d864046..714e833 100644 --- a/AnalysisData/AnalysisData.sln.DotSettings.user +++ b/AnalysisData/AnalysisData.sln.DotSettings.user @@ -1,3 +1,54 @@  - True - True \ No newline at end of file + + C:\Users\kimia\AppData\Local\JetBrains\Rider2024.1\resharper-host\temp\Rider\vAny\CoverageData\_AnalysisData.-35481945\Snapshot\snapshot.utdcvr + <SessionState ContinuousTestingMode="0" Name="RoleRepositoryTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests</TestId> + </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" IsActive="True" Name="GetRole_ShouldReturnsRole_WhenRoleExists #3" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests.GetAllUser_ShouldReturnsAllUsers_Whenever</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests.GetUser_ShouldReturnsNull_WhenUserDoesNotExist</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests.GetUser_ShouldReturnsUser_WhenUserExists</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRoleRepository.UserRoleRepositoryTests.DeleteRoleInUserRole_ShouldRemoveAllUsersWithRole_WhenRemoveRole</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRoleRepository.UserRoleRepositoryTests.DeleteUserInUserRole_ShouldRemoveAllRolesOfUser_WhenRemoveUser</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRoleRepository.UserRoleRepositoryTests.Add_ShouldAddUserRoleToDatabase_WhenAddNewUser</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRoleRepository.UserRoleRepositoryTests</TestId> + </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" Name="AddRole_ShouldAddsRoleToDatabase_Whenever #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.AddRole_ShouldAddsRoleToDatabase_Whenever</TestId> + </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" Name="GetRole_ShouldReturnsRole_WhenRoleExists" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.GetRole_ShouldReturnsRole_WhenRoleExists</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.AddRole_ShouldAddsRoleToDatabase_Whenever</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.DeleteRole_ShouldRemovesRole_WhenRoleExists</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.DeleteRole_ShouldReturnsFalse_WhenRoleDoesNotExist</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests.AddUser_ShouldAddsUserToDatabase_Whenever</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests.GetUser_ShouldReturnsNull_WhenUserDoesNotExist</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests.GetUser_ShouldReturnsUser_WhenUserExists</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests.GetAllUser_ShouldReturnsAllUsers_Whenever</TestId> + </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" Name="GetRole_ShouldReturnsRole_WhenRoleExists #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.GetRole_ShouldReturnsRole_WhenRoleExists</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.AddRole_ShouldAddsRoleToDatabase_Whenever</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.DeleteRole_ShouldRemovesRole_WhenRoleExists</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests.GetUser_ShouldReturnsUser_WhenUserExists</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.UserRepository.UserRepositoryTests</TestId> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.DeleteRole_ShouldReturnsFalse_WhenRoleDoesNotExist</TestId> + </TestAncestor> +</SessionState> + <SessionState ContinuousTestingMode="0" Name="AddRole_ShouldAddsRoleToDatabase_Whenever" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::9AEC1F3F-B1B3-47C1-82D4-E432E2D77E0E::net8.0::TestProject.Repository.RoleRepository.RoleRepositoryTests.AddRole_ShouldAddsRoleToDatabase_Whenever</TestId> + </TestAncestor> +</SessionState> + diff --git a/AnalysisData/AnalysisData/Repository/RoleRepository/Abstraction/IRoleRepository.cs b/AnalysisData/AnalysisData/Repository/RoleRepository/Abstraction/IRoleRepository.cs index 5f68dc0..ae85c98 100644 --- a/AnalysisData/AnalysisData/Repository/RoleRepository/Abstraction/IRoleRepository.cs +++ b/AnalysisData/AnalysisData/Repository/RoleRepository/Abstraction/IRoleRepository.cs @@ -5,6 +5,6 @@ namespace AnalysisData.Repository.RoleRepository.Abstraction; public interface IRoleRepository { Task GetRole(int rolId); - bool AddRole(Role role); + void AddRole(Role role); bool DeleteRole(int roleId); } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/Repository/RoleRepository/RoleRepository.cs b/AnalysisData/AnalysisData/Repository/RoleRepository/RoleRepository.cs index 26d2cd5..eae0a21 100644 --- a/AnalysisData/AnalysisData/Repository/RoleRepository/RoleRepository.cs +++ b/AnalysisData/AnalysisData/Repository/RoleRepository/RoleRepository.cs @@ -14,16 +14,15 @@ public RoleRepository(ApplicationDbContext context) _context = context; } - public async Task GetRole(int rolId) + public async Task GetRole(int roleId) { - return await _context.Roles.SingleOrDefaultAsync(x => x.Id == rolId); + return await _context.Roles.SingleOrDefaultAsync(x => x.Id == roleId); } - public bool AddRole(Role role) + public void AddRole(Role role) { _context.Roles.Add(role); _context.SaveChanges(); - return true; } public bool DeleteRole(int roleId) @@ -34,4 +33,5 @@ public bool DeleteRole(int roleId) _context.SaveChanges(); return true; } + } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/Repository/UserRepository/Abstraction/IUserRepository.cs b/AnalysisData/AnalysisData/Repository/UserRepository/Abstraction/IUserRepository.cs index 26c113d..132f4a6 100644 --- a/AnalysisData/AnalysisData/Repository/UserRepository/Abstraction/IUserRepository.cs +++ b/AnalysisData/AnalysisData/Repository/UserRepository/Abstraction/IUserRepository.cs @@ -7,5 +7,5 @@ public interface IUserRepository Task GetUser(string userName); Task> GetAllUser(); bool DeleteUser(string userName); - bool AddUser(User user); + void AddUser(User user); } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/Repository/UserRepository/UserRepository.cs b/AnalysisData/AnalysisData/Repository/UserRepository/UserRepository.cs index 3c307c9..9d3339d 100644 --- a/AnalysisData/AnalysisData/Repository/UserRepository/UserRepository.cs +++ b/AnalysisData/AnalysisData/Repository/UserRepository/UserRepository.cs @@ -34,11 +34,10 @@ public bool DeleteUser(string userName) return true; } - public bool AddUser(User user) + public void AddUser(User user) { _context.Users.Add(user); _context.SaveChanges(); - return true; } } } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/Repository/UserRoleRepository/Abstraction/IUserRoleRepository.cs b/AnalysisData/AnalysisData/Repository/UserRoleRepository/Abstraction/IUserRoleRepository.cs index f43d35a..b067250 100644 --- a/AnalysisData/AnalysisData/Repository/UserRoleRepository/Abstraction/IUserRoleRepository.cs +++ b/AnalysisData/AnalysisData/Repository/UserRoleRepository/Abstraction/IUserRoleRepository.cs @@ -4,7 +4,7 @@ namespace AnalysisData.Repository.UserRoleRepository.Abstraction; public interface IUserRoleRepository { - bool Add(UserRole userRole); + void Add(UserRole userRole); bool DeleteUserInUserRole(int userId); bool DeleteRoleInUserRole(int roleId); } \ No newline at end of file diff --git a/AnalysisData/AnalysisData/Repository/UserRoleRepository/UserRoleRepository.cs b/AnalysisData/AnalysisData/Repository/UserRoleRepository/UserRoleRepository.cs index 1669a4f..71d7822 100644 --- a/AnalysisData/AnalysisData/Repository/UserRoleRepository/UserRoleRepository.cs +++ b/AnalysisData/AnalysisData/Repository/UserRoleRepository/UserRoleRepository.cs @@ -13,11 +13,10 @@ public UserRoleRepository(ApplicationDbContext context) _context = context; } - public bool Add(UserRole userRole) + public void Add(UserRole userRole) { _context.UserRoles.Add(userRole); _context.SaveChanges(); - return true; } public bool DeleteUserInUserRole(int userId) diff --git a/AnalysisData/TestProject/Repository/RoleRepository/RoleRepositoryTests.cs b/AnalysisData/TestProject/Repository/RoleRepository/RoleRepositoryTests.cs new file mode 100644 index 0000000..95f3140 --- /dev/null +++ b/AnalysisData/TestProject/Repository/RoleRepository/RoleRepositoryTests.cs @@ -0,0 +1,89 @@ +using AnalysisData.Data; +using AnalysisData.UserManage.Model; +using Microsoft.EntityFrameworkCore; + +namespace TestProject.Repository.RoleRepository; + +public class RoleRepositoryTests +{ + private readonly DbContextOptions _options; + private readonly ApplicationDbContext _context; + private readonly AnalysisData.Repository.RoleRepository.RoleRepository _sut; + + public RoleRepositoryTests() + { + _options = new DbContextOptionsBuilder().UseInMemoryDatabase("TestDatabase").Options; + _context = new ApplicationDbContext(_options); + _sut = new AnalysisData.Repository.RoleRepository.RoleRepository(_context); + } + + [Fact] + public async Task GetRole_ShouldReturnsRole_WhenRoleExists() + { + // Arrange + var role = new Role { Id = 1, RoleName = "Admin" }; + _context.Roles.Add(role); + await _context.SaveChangesAsync(); + + // Act + var result = await _sut.GetRole(1); + + // Assert + Assert.NotNull(result); + Assert.Equal("Admin", result.RoleName); + _context.Roles.Remove(role); + _context.SaveChangesAsync(); + } + + [Fact] + public void AddRole_ShouldAddsRoleToDatabase_Whenever() + { + // Arrange + var role = new Role { Id = 1, RoleName = "Admin" }; + + // Act + _sut.AddRole(role); + + // Assert + Assert.Equal(1, _context.Roles.Count()); + Assert.Contains(_context.Roles, r => r.RoleName == "Admin"); + _context.Roles.Remove(role); + _context.SaveChangesAsync(); + } + + [Fact] + public void DeleteRole_ShouldRemovesRole_WhenRoleExists() + { + // Arrange + var role = new Role { Id = 1, RoleName = "Admin" }; + _context.Roles.Add(role); + _context.SaveChanges(); + + // Act + var result = _sut.DeleteRole(1); + // Assert + Assert.True(result); + Assert.Equal(0, _context.Roles.Count()); + Assert.DoesNotContain(_context.Roles, r => r.Id == 1); + _context.Roles.Remove(role); + _context.SaveChangesAsync(); + } + + [Fact] + public void DeleteRole_ShouldReturnsFalse_WhenRoleDoesNotExist() + { + // Arrange + var role = new Role { Id = 1, RoleName = "Editor" }; + _context.Roles.Add(role); + _context.SaveChanges(); + + // Act + var result = _sut.DeleteRole(2); + + // Assert + Assert.False(result); + Assert.Equal(1, _context.Roles.Count()); + _context.Roles.Remove(role); + _context.SaveChangesAsync(); + } +} \ No newline at end of file diff --git a/AnalysisData/TestProject/Repository/UserRepository/UserRepositoryTests.cs b/AnalysisData/TestProject/Repository/UserRepository/UserRepositoryTests.cs new file mode 100644 index 0000000..7abcb59 --- /dev/null +++ b/AnalysisData/TestProject/Repository/UserRepository/UserRepositoryTests.cs @@ -0,0 +1,113 @@ +using AnalysisData.Data; +using AnalysisData.UserManage.Model; +using Microsoft.EntityFrameworkCore; + +namespace TestProject.Repository.UserRepository; + +public class UserRepositoryTests +{ + private readonly DbContextOptions _options; + private readonly ApplicationDbContext _context; + private readonly AnalysisData.Repository.UserRepository.UserRepository _sut; + + public UserRepositoryTests() + { + _options = new DbContextOptionsBuilder().UseInMemoryDatabase("TestDatabase").Options; + _context = new ApplicationDbContext(_options); + _sut = new AnalysisData.Repository.UserRepository.UserRepository(_context); + } + + [Fact] + public async Task GetUser_ShouldReturnsUser_WhenUserExists() + { + // Arrange + var user = new User { Id = 1, Username = "user1", Password = "pass1" }; + _context.Users.Add(user); + await _context.SaveChangesAsync(); + + // Act + var result = await _sut.GetUser("user1"); + + // Assert + Assert.NotNull(result); + Assert.Equal("user1", result.Username); + _context.Users.Remove(user); + await _context.SaveChangesAsync(); + } + + [Fact] + public async Task GetUser_ShouldReturnsNull_WhenUserDoesNotExist() + { + // Arrange + + // Act + var result = await _sut.GetUser("NoOne"); + + // Assert + Assert.Null(result); + } + + [Fact] + public async Task GetAllUser_ShouldReturnsAllUsers_Whenever() + { + _context.Users.AddRange( + new User { Id = 1, Username = "user1", Password = "pass1" }, + new User { Id = 2, Username = "user2", Password = "pass2" } + ); + await _context.SaveChangesAsync(); + + // Act + var result = await _sut.GetAllUser(); + + // Assert + Assert.Equal(2, result.Count); + _context.Users.RemoveRange(_context.Users.ToList()); + await _context.SaveChangesAsync(); + } + + [Fact] + public void DeleteUser_RemovesUser_WhenUserExists() + { + // Arrange + var user = new User { Id = 1, Username = "user1", Password = "pass1" }; + _context.Users.Add(user); + _context.SaveChanges(); + + // Act + var result = _sut.DeleteUser("user1"); + + // Assert + Assert.True(result); + Assert.Equal(0, _context.Users.Count()); + } + + [Fact] + public void DeleteUser_ReturnsFalse_WhenUserDoesNotExist() + { + // Arrange + + // Act + var result = _sut.DeleteUser("NoOne"); + + // Assert + Assert.False(result); + } + + [Fact] + public void AddUser_ShouldAddsUserToDatabase_Whenever() + { + // Arrange + var user = new User { Id = 1, Username = "user1", Password = "pass1" }; + + // Act + _sut.AddUser(user); + + // Assert + Assert.Equal(1, _context.Users.Count()); + Assert.Contains(_context.Users, u => u is { Username: "user1", Password: "pass1" }); + _context.Users.Remove(user); + _context.SaveChangesAsync(); + } + + +} \ No newline at end of file diff --git a/AnalysisData/TestProject/Repository/UserRoleRepository/UserRoleRepositoryTests.cs b/AnalysisData/TestProject/Repository/UserRoleRepository/UserRoleRepositoryTests.cs new file mode 100644 index 0000000..cca4b53 --- /dev/null +++ b/AnalysisData/TestProject/Repository/UserRoleRepository/UserRoleRepositoryTests.cs @@ -0,0 +1,75 @@ +using AnalysisData.Data; +using AnalysisData.UserManage.Model; +using Microsoft.EntityFrameworkCore; + +namespace TestProject.Repository.UserRoleRepository; + +public class UserRoleRepositoryTests +{ + private readonly ApplicationDbContext _context; + private readonly AnalysisData.Repository.UserRoleRepository.UserRoleRepository _sut; + + public UserRoleRepositoryTests() + { + var options = new DbContextOptionsBuilder() + .UseInMemoryDatabase(databaseName: "TestDatabase") + .Options; + + _context = new ApplicationDbContext(options); + _sut = new AnalysisData.Repository.UserRoleRepository.UserRoleRepository(_context); + } + + [Fact] + public void Add_ShouldAddUserRoleToDatabase_WhenAddNewUser() + { + // Arrange + var userRole = new UserRole { UserId = 1, RoleId = 1 }; + + // Act + _sut.Add(userRole); + + // Assert + Assert.Equal(1, _context.UserRoles.Count()); + _context.UserRoles.Remove(userRole); + _context.SaveChanges(); + + } + + [Fact] + public void DeleteUserInUserRole_ShouldRemoveAllRolesOfUser_WhenRemoveUser() + { + // Arrange + _context.UserRoles.Add(new UserRole { UserId = 1, RoleId = 2 }); + _context.UserRoles.Add(new UserRole { UserId = 1, RoleId = 3 }); + _context.UserRoles.Add(new UserRole { UserId = 2, RoleId = 3 }); + _context.SaveChanges(); + + // Act + var result = _sut.DeleteUserInUserRole(1); + + // Assert + Assert.True(result); + Assert.Equal(1, _context.UserRoles.Count()); + _context.UserRoles.RemoveRange(_context.UserRoles.ToList()); + _context.SaveChangesAsync(); + } + + [Fact] + public void DeleteRoleInUserRole_ShouldRemoveAllUsersWithRole_WhenRemoveRole() + { + // Arrange + _context.UserRoles.Add(new UserRole { UserId = 1, RoleId = 2 }); + _context.UserRoles.Add(new UserRole { UserId = 2, RoleId = 2 }); + _context.UserRoles.Add(new UserRole { UserId = 1, RoleId = 3 }); + _context.SaveChanges(); + + // Act + var result = _sut.DeleteRoleInUserRole(2); + + // Assert + Assert.True(result); + Assert.Equal(1, _context.UserRoles.Count()); + _context.UserRoles.RemoveRange(_context.UserRoles.ToList()); + _context.SaveChangesAsync(); + } +} \ No newline at end of file diff --git a/AnalysisData/TestProject/TestProject.csproj b/AnalysisData/TestProject/TestProject.csproj index 085bddd..756705f 100644 --- a/AnalysisData/TestProject/TestProject.csproj +++ b/AnalysisData/TestProject/TestProject.csproj @@ -10,14 +10,20 @@ - - - - + + + + + + - + + + + +