-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Test-Authentication): auth tests added
* feat(Test-Authentication): Add RoleRepositoryTests class and a test method for GetRole function. * feat(Test-Authentication): Add a test method for AddRole function in RoleRepositoryTests class. * feat(Test-Authentication): Add a test method for DeleteRole function in RoleRepositoryTests class. * fix(Test-Authentication): Some changes were made in UserRepositoryTests and RoleRepositoryTests classes. * feat(Test-Authentication): Add UserRoleRepositoryTests class and test methods. --------- Co-authored-by: aliyaghini <[email protected]>
- Loading branch information
1 parent
2092a6c
commit 1b923f5
Showing
11 changed files
with
350 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,54 @@ | ||
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> | ||
<s:Boolean x:Key="/Default/ResxEditorPersonal/CheckedGroups/=AnalysisData_002FResources/@EntryIndexedValue">True</s:Boolean> | ||
<s:Boolean x:Key="/Default/ResxEditorPersonal/Initialized/@EntryValue">True</s:Boolean></wpf:ResourceDictionary> | ||
|
||
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">C:\Users\kimia\AppData\Local\JetBrains\Rider2024.1\resharper-host\temp\Rider\vAny\CoverageData\_AnalysisData.-35481945\Snapshot\snapshot.utdcvr</s:String> | ||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=02d9fd71_002D0a38_002D43c7_002Da834_002Dc2e3e53d102a/@EntryIndexedValue"><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></s:String> | ||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=1e8f93b2_002D4572_002D4c58_002Db711_002Dacb713304847/@EntryIndexedValue"><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></s:String> | ||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=282f0196_002Dd9ab_002D4d1c_002Db9ba_002D372bc37ea58a/@EntryIndexedValue"><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></s:String> | ||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=2c41609a_002D70c7_002D48cb_002D9eed_002D07fa2ea2f4f4/@EntryIndexedValue"><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></s:String> | ||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=648b354f_002D77fb_002D41ba_002D80a0_002D6eeb12c1c94d/@EntryIndexedValue"><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></s:String> | ||
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=8ae15fa0_002Dd719_002D4ce1_002D847c_002De586feccc67f/@EntryIndexedValue"><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></s:String></wpf:ResourceDictionary> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
AnalysisData/TestProject/Repository/RoleRepository/RoleRepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
using AnalysisData.Data; | ||
using AnalysisData.UserManage.Model; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace TestProject.Repository.RoleRepository; | ||
|
||
public class RoleRepositoryTests | ||
{ | ||
private readonly DbContextOptions<ApplicationDbContext> _options; | ||
private readonly ApplicationDbContext _context; | ||
private readonly AnalysisData.Repository.RoleRepository.RoleRepository _sut; | ||
|
||
public RoleRepositoryTests() | ||
{ | ||
_options = new DbContextOptionsBuilder<ApplicationDbContext>().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(); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
AnalysisData/TestProject/Repository/UserRepository/UserRepositoryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using AnalysisData.Data; | ||
using AnalysisData.UserManage.Model; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace TestProject.Repository.UserRepository; | ||
|
||
public class UserRepositoryTests | ||
{ | ||
private readonly DbContextOptions<ApplicationDbContext> _options; | ||
private readonly ApplicationDbContext _context; | ||
private readonly AnalysisData.Repository.UserRepository.UserRepository _sut; | ||
|
||
public UserRepositoryTests() | ||
{ | ||
_options = new DbContextOptionsBuilder<ApplicationDbContext>().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(); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.