mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Refactors the test fixtures and their consumers to compose other fixtures instead of instantiating models inline
This commit is contained in:
1 parent
0abda472c6
commit
3aadd6de84
157 files changed
+1312
-597
No files matched your search
+9
-3
@@ -21,16 +21,22 @@ public class PermissionEntityFixture
|
||||
/// </summary>
|
||||
/// <param name="id">Optional. The permission Id.</param>
|
||||
/// <param name="permissionName">Optional. The permission name.</param>
|
||||
/// <param name="createdBy">Optional. The Id of the user that created the permission.</param>
|
||||
/// <param name="createdOnUtc">Optional. The time and date when the permission was created.</param>
|
||||
/// <returns>The created <see cref="PermissionEntity"/>.</returns>
|
||||
public PermissionEntity Create(Guid? id = null, AuthorizationPermission? permissionName = null)
|
||||
public PermissionEntity Create(
|
||||
Guid? id = null,
|
||||
AuthorizationPermission? permissionName = null,
|
||||
Guid? createdBy = null,
|
||||
DateTime? createdOnUtc = null)
|
||||
{
|
||||
return new Faker<PermissionEntity>()
|
||||
.RuleFor(x => x.Id, f => id ?? f.Random.Guid())
|
||||
.RuleFor(x => x.PermissionName, f => permissionName ?? f.PickRandom<AuthorizationPermission>())
|
||||
.RuleFor(x => x.RolePermissions, [])
|
||||
.RuleFor(x => x.UserPermissions, [])
|
||||
.RuleFor(x => x.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(x => x.CreatedBy, f => f.Random.Guid())
|
||||
.RuleFor(x => x.CreatedOnUtc, f => createdOnUtc ?? f.Date.Past())
|
||||
.RuleFor(x => x.CreatedBy, f => createdBy ?? f.Random.Guid())
|
||||
.RuleFor(x => x.UpdatedOnUtc, f => f.Date.Recent())
|
||||
.RuleFor(x => x.UpdatedBy, f => f.Random.Guid())
|
||||
.Generate();
|
||||
|
||||
+12
-24
@@ -1,7 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Authorization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -16,7 +15,9 @@ namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RolePermissionEntityFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
// RoleEntityFixture composes this fixture, so the role fixture is resolved lazily to avoid an eager mutual composition cycle.
|
||||
private readonly Lazy<RoleEntityFixture> _roleEntityFixture = new();
|
||||
private readonly PermissionEntityFixture _permissionEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="RolePermissionEntity"/>.
|
||||
@@ -26,33 +27,20 @@ public class RolePermissionEntityFixture
|
||||
/// <param name="role">Optional. The role the permission is granted to.</param>
|
||||
/// <param name="permissionId">Optional. The Id of the permission granted to the role.</param>
|
||||
/// <param name="permission">Optional. The permission granted to the role.</param>
|
||||
/// <param name="createdOnUtc">Optional. The time and date when the association was created.</param>
|
||||
/// <param name="createdBy">Optional. The Id of the user that created the association.</param>
|
||||
/// <returns>The created <see cref="RolePermissionEntity"/>.</returns>
|
||||
public RolePermissionEntity Create(
|
||||
Guid? id = null,
|
||||
Guid? roleId = null,
|
||||
RoleEntity? role = null,
|
||||
Guid? permissionId = null,
|
||||
PermissionEntity? permission = null)
|
||||
PermissionEntity? permission = null,
|
||||
DateTime? createdOnUtc = null,
|
||||
Guid? createdBy = null)
|
||||
{
|
||||
RoleEntity resolvedRole = role ?? new RoleEntity
|
||||
{
|
||||
Id = roleId ?? _faker.Random.Guid(),
|
||||
RoleName = _faker.Random.String2(10),
|
||||
CreatedOnUtc = _faker.Date.Past(),
|
||||
CreatedBy = _faker.Random.Guid(),
|
||||
UpdatedOnUtc = _faker.Date.Recent(),
|
||||
UpdatedBy = _faker.Random.Guid()
|
||||
};
|
||||
|
||||
PermissionEntity resolvedPermission = permission ?? new PermissionEntity
|
||||
{
|
||||
Id = permissionId ?? _faker.Random.Guid(),
|
||||
PermissionName = _faker.PickRandom<AuthorizationPermission>(),
|
||||
CreatedOnUtc = _faker.Date.Past(),
|
||||
CreatedBy = _faker.Random.Guid(),
|
||||
UpdatedOnUtc = _faker.Date.Recent(),
|
||||
UpdatedBy = _faker.Random.Guid()
|
||||
};
|
||||
RoleEntity resolvedRole = role ?? _roleEntityFixture.Value.Create(id: roleId);
|
||||
PermissionEntity resolvedPermission = permission ?? _permissionEntityFixture.Create(id: permissionId);
|
||||
|
||||
return new Faker<RolePermissionEntity>()
|
||||
.CustomInstantiator(f => new RolePermissionEntity
|
||||
@@ -62,8 +50,8 @@ public class RolePermissionEntityFixture
|
||||
Role = role ?? resolvedRole,
|
||||
PermissionId = resolvedPermission.Id,
|
||||
Permission = permission ?? resolvedPermission,
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid(),
|
||||
CreatedOnUtc = createdOnUtc ?? f.Date.Past(),
|
||||
CreatedBy = createdBy ?? f.Random.Guid(),
|
||||
UpdatedOnUtc = f.Date.Recent(),
|
||||
UpdatedBy = f.Random.Guid()
|
||||
})
|
||||
|
||||
+10
-3
@@ -2,6 +2,7 @@
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
@@ -18,8 +19,14 @@ public class UserPermissionEntityFixture
|
||||
/// </summary>
|
||||
/// <param name="user">The user the permission is granted to.</param>
|
||||
/// <param name="permission">The permission granted to the user.</param>
|
||||
/// <param name="createdOnUtc">Optional. The time and date when the association was created.</param>
|
||||
/// <param name="createdBy">Optional. The Id of the user that created the association.</param>
|
||||
/// <returns>The created <see cref="UserPermissionEntity"/>.</returns>
|
||||
public UserPermissionEntity Create(UserEntity user, PermissionEntity permission)
|
||||
public UserPermissionEntity Create(
|
||||
UserEntity user,
|
||||
PermissionEntity permission,
|
||||
DateTime? createdOnUtc = null,
|
||||
Guid? createdBy = null)
|
||||
{
|
||||
return new Faker<UserPermissionEntity>()
|
||||
.CustomInstantiator(f => new UserPermissionEntity
|
||||
@@ -29,8 +36,8 @@ public class UserPermissionEntityFixture
|
||||
User = user,
|
||||
PermissionId = permission.Id,
|
||||
Permission = permission,
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = user.Id,
|
||||
CreatedOnUtc = createdOnUtc ?? f.Date.Past(),
|
||||
CreatedBy = createdBy ?? user.Id,
|
||||
UpdatedOnUtc = f.Random.Bool() ? f.Date.Recent() : null,
|
||||
UpdatedBy = f.Random.Bool() ? user.Id : null
|
||||
})
|
||||
|
||||
+15
-24
@@ -2,6 +2,7 @@
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -16,7 +17,8 @@ namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UserRoleEntityFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly RoleEntityFixture _roleEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="UserRoleEntity"/>.
|
||||
@@ -26,44 +28,33 @@ public class UserRoleEntityFixture
|
||||
/// <param name="user">Optional. The user the role is granted to.</param>
|
||||
/// <param name="roleId">Optional. The Id of the role granted to the user.</param>
|
||||
/// <param name="role">Optional. The role granted to the user.</param>
|
||||
/// <param name="includeUser">Whether the user should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="createdOnUtc">Optional. The time and date when the association was created.</param>
|
||||
/// <param name="createdBy">Optional. The Id of the user that created the association.</param>
|
||||
/// <returns>The created <see cref="UserRoleEntity"/>.</returns>
|
||||
public UserRoleEntity Create(
|
||||
Guid? id = null,
|
||||
Guid? userId = null,
|
||||
UserEntity? user = null,
|
||||
Guid? roleId = null,
|
||||
RoleEntity? role = null)
|
||||
RoleEntity? role = null,
|
||||
bool includeUser = true,
|
||||
DateTime? createdOnUtc = null,
|
||||
Guid? createdBy = null)
|
||||
{
|
||||
UserEntity resolvedUser = user ?? new UserEntity
|
||||
{
|
||||
Id = userId ?? _faker.Random.Guid(),
|
||||
Username = _faker.Internet.UserName(),
|
||||
Password = _faker.Internet.Password(),
|
||||
Libraries = [],
|
||||
UserRole = null,
|
||||
UserPermissions = [],
|
||||
CreatedOnUtc = _faker.Date.Past(),
|
||||
CreatedBy = _faker.Random.Guid()
|
||||
};
|
||||
|
||||
RoleEntity resolvedRole = role ?? new RoleEntity
|
||||
{
|
||||
Id = roleId ?? _faker.Random.Guid(),
|
||||
RoleName = _faker.Random.String2(_faker.Random.Number(1, 50)),
|
||||
CreatedOnUtc = _faker.Date.Past(),
|
||||
CreatedBy = _faker.Random.Guid()
|
||||
};
|
||||
UserEntity resolvedUser = user ?? _userEntityFixture.Create(id: userId);
|
||||
RoleEntity resolvedRole = role ?? _roleEntityFixture.Create(id: roleId);
|
||||
|
||||
return new Faker<UserRoleEntity>()
|
||||
.CustomInstantiator(f => new UserRoleEntity
|
||||
{
|
||||
Id = id ?? f.Random.Guid(),
|
||||
UserId = resolvedUser.Id,
|
||||
User = user ?? resolvedUser,
|
||||
User = includeUser ? (user ?? resolvedUser) : null!,
|
||||
RoleId = resolvedRole.Id,
|
||||
Role = role ?? resolvedRole,
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid()
|
||||
CreatedOnUtc = createdOnUtc ?? f.Date.Past(),
|
||||
CreatedBy = createdBy ?? f.Random.Guid()
|
||||
})
|
||||
.Generate();
|
||||
}
|
||||
|
||||
+3
-1
@@ -16,6 +16,8 @@ namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Ma
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class LibraryEntityFixture
|
||||
{
|
||||
private readonly LibraryContentLocationEntityFixture _libraryContentLocationEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="LibraryEntity"/>.
|
||||
/// </summary>
|
||||
@@ -76,7 +78,7 @@ public class LibraryEntityFixture
|
||||
.RuleFor(library => library.ContentLocations, f =>
|
||||
{
|
||||
IEnumerable<string> paths = contentLocations ?? [f.System.DirectoryPath(), f.System.DirectoryPath()];
|
||||
return [.. paths.Select(path => new LibraryContentLocationEntity() { Path = path })];
|
||||
return [.. paths.Select(path => _libraryContentLocationEntityFixture.Create(path: path))];
|
||||
})
|
||||
.RuleFor(library => library.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(library => library.CreatedBy, f => f.Random.Guid())
|
||||
|
||||
+7
-3
@@ -32,6 +32,8 @@ public class UserEntityFixture
|
||||
/// <param name="libraries">Optional collection of libraries to pin for the user, or <see langword="null"/> to generate them based on <paramref name="libraryCount"/>.</param>
|
||||
/// <param name="includeUserRole">Whether the user role association should be included, or forced to <see langword="null"/>.</param>
|
||||
/// <param name="includeUserPermissions">Whether the user permission associations should be included, or forced to an empty collection.</param>
|
||||
/// <param name="createdBy">Optional. The Id of the user that created the user.</param>
|
||||
/// <param name="createdOnUtc">Optional. The time and date when the user was created.</param>
|
||||
/// <returns>The created user entity.</returns>
|
||||
public UserEntity Create(
|
||||
int libraryCount = 0,
|
||||
@@ -42,7 +44,9 @@ public class UserEntityFixture
|
||||
IEnumerable<UserPermissionEntity>? userPermissions = null,
|
||||
ICollection<LibraryEntity>? libraries = null,
|
||||
bool includeUserRole = false,
|
||||
bool includeUserPermissions = false)
|
||||
bool includeUserPermissions = false,
|
||||
Guid? createdBy = null,
|
||||
DateTime? createdOnUtc = null)
|
||||
{
|
||||
Guid userId = id ?? Guid.NewGuid();
|
||||
ICollection<LibraryEntity> resolvedLibraries = libraries ?? (libraryCount > 0
|
||||
@@ -60,11 +64,11 @@ public class UserEntityFixture
|
||||
Libraries = resolvedLibraries,
|
||||
UserPermissions = includeUserPermissions ? (userPermissions ?? []).ToList() : [],
|
||||
UserRole = includeUserRole ? userRole : null,
|
||||
CreatedBy = userId
|
||||
CreatedBy = createdBy ?? userId
|
||||
})
|
||||
.RuleFor(x => x.Username, f => username ?? f.Internet.UserName())
|
||||
.RuleFor(x => x.Password, f => password ?? Uri.EscapeDataString(f.Internet.Password()))
|
||||
.RuleFor(x => x.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(x => x.CreatedOnUtc, f => createdOnUtc ?? f.Date.Past())
|
||||
.RuleFor(x => x.TotpSecret, f => f.Random.Bool() ? f.Random.String2(32) : null)
|
||||
.Generate();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user