Refactors the test fixtures and their consumers to compose other fixtures instead of instantiating models inline

This commit is contained in:
BinaryAlley committed 2026-10-02 14:37:23 +03:00
1 parent 0abda472c6
commit 3aadd6de84
157 files changed
+1312 -597

No files matched your search

@@ -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();
@@ -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()
})
@@ -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
})
@@ -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();
}
@@ -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())
@@ -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();
}
@@ -19,15 +19,16 @@ public class CheckPathExistsQueryFixture
/// </summary>
/// <param name="path">Optional. The file system path.</param>
/// <param name="shouldIncludeHiddenElements">Whether to include hidden file system elements or not.</param>
/// <param name="includePath">Whether the path should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created query.</returns>
public CheckPathExistsQuery Create(string? path = null, bool shouldIncludeHiddenElements = true)
public CheckPathExistsQuery Create(string? path = null, bool shouldIncludeHiddenElements = true, bool includePath = true)
{
return new Faker<CheckPathExistsQuery>()
.CustomInstantiator(f => new CheckPathExistsQuery(
default!,
default
))
.RuleFor(x => x.Path, f => path ?? f.System.FilePath())
.RuleFor(x => x.Path, f => includePath ? (path ?? f.System.FilePath()) : null)
.RuleFor(x => x.ShouldIncludeHiddenElements, shouldIncludeHiddenElements);
}
@@ -18,13 +18,14 @@ public class ValidatePathQueryFixture
/// Creates a random valid query to validate a path.
/// </summary>
/// <param name="path">Optional. The file system path.</param>
/// <param name="includePath">Whether the path should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created query.</returns>
public ValidatePathQuery Create(string? path = null)
public ValidatePathQuery Create(string? path = null, bool includePath = true)
{
return new Faker<ValidatePathQuery>()
.CustomInstantiator(f => new ValidatePathQuery(
default!))
.RuleFor(x => x.Path, f => path ?? f.System.FilePath());
.RuleFor(x => x.Path, f => includePath ? (path ?? f.System.FilePath()) : null);
}
/// <summary>
@@ -19,15 +19,16 @@ public class GetThumbnailQueryFixture
/// </summary>
/// <param name="path">Optional. The file system path.</param>
/// <param name="quality">Optional. The thumbnail quality.</param>
/// <param name="includePath">Whether the path should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created query.</returns>
public GetThumbnailQuery Create(string? path = null, int? quality = null)
public GetThumbnailQuery Create(string? path = null, int? quality = null, bool includePath = true)
{
return new Faker<GetThumbnailQuery>()
.CustomInstantiator(f => new GetThumbnailQuery(
default!,
default
))
.RuleFor(x => x.Path, f => path ?? f.System.FilePath())
.RuleFor(x => x.Path, f => includePath ? (path ?? f.System.FilePath()) : null)
.RuleFor(x => x.Quality, f => quality ?? f.Random.Int());
}
@@ -19,8 +19,9 @@ public class RecoverPasswordCommandFixture
/// </summary>
/// <param name="username">Optional. The username.</param>
/// <param name="totpCode">Optional. The TOTP code.</param>
/// <param name="includeTotpCode">Whether the TOTP code should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created command.</returns>
public RecoverPasswordCommand Create(string? username = null, string? totpCode = null)
public RecoverPasswordCommand Create(string? username = null, string? totpCode = null, bool includeTotpCode = true)
{
return new Faker<RecoverPasswordCommand>()
.CustomInstantiator(f => new RecoverPasswordCommand(
@@ -28,7 +29,7 @@ public class RecoverPasswordCommandFixture
default!
))
.RuleFor(x => x.Username, f => username ?? f.Person.UserName)
.RuleFor(x => x.TotpCode, f => totpCode ?? f.Random.Replace("######"));
.RuleFor(x => x.TotpCode, f => includeTotpCode ? (totpCode ?? f.Random.Replace("######")) : null);
}
/// <summary>
@@ -20,13 +20,16 @@ public class LoginUserQueryFixture
/// <param name="username">Optional. The username.</param>
/// <param name="password">Optional. The password.</param>
/// <param name="includeTotpCode">Whether the TOTP code should be included, or forced to <see langword="null"/>.</param>
/// <param name="totpCode">Optional. The TOTP code. When provided, the TOTP code is included even if <paramref name="includeTotpCode"/> is <see langword="false"/>.</param>
/// <returns>The created query.</returns>
public LoginUserQuery Create(
string? username = null,
string? password = null,
bool includeTotpCode = false)
bool includeTotpCode = false,
string? totpCode = null)
{
string resolvedPassword = password ?? "Abcd123$";
bool shouldIncludeTotpCode = includeTotpCode || totpCode is not null;
Faker<LoginUserQuery> faker = new Faker<LoginUserQuery>()
.CustomInstantiator(f => new LoginUserQuery(
default!,
@@ -36,8 +39,8 @@ public class LoginUserQueryFixture
.RuleFor(x => x.Username, f => username ?? f.Person.UserName)
.RuleFor(x => x.Password, resolvedPassword);
if (includeTotpCode)
faker.RuleFor(x => x.TotpCode, f => f.Random.Replace("######")); // generates 6 random digits
if (shouldIncludeTotpCode)
faker.RuleFor(x => x.TotpCode, f => totpCode ?? f.Random.Replace("######")); // generates 6 random digits when no code is pinned
return faker.Generate();
}
@@ -22,19 +22,23 @@ public class UpdateUserRoleAndPermissionsCommandFixture
/// <param name="roleId">Optional. The role Id to assign. If null, no role will be assigned.</param>
/// <param name="permissions">Optional. The permission Ids to assign.</param>
/// <param name="permissionCount">The number of permissions to generate when none are provided.</param>
/// <param name="includePermissions">Whether the permissions should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created command.</returns>
public UpdateUserRoleAndPermissionsCommand Create(
Guid? userId = null,
Guid? roleId = null,
IEnumerable<Guid>? permissions = null,
int permissionCount = 3)
int permissionCount = 3,
bool includePermissions = true)
{
return new Faker<UpdateUserRoleAndPermissionsCommand>()
.CustomInstantiator(f => new UpdateUserRoleAndPermissionsCommand(
userId ?? Guid.NewGuid(),
roleId,
default!))
.RuleFor(x => x.Permissions, f => [.. permissions ?? Enumerable.Range(0, permissionCount).Select(_ => Guid.NewGuid())])
.RuleFor(x => x.Permissions, f => includePermissions
? [.. permissions ?? Enumerable.Range(0, permissionCount).Select(_ => Guid.NewGuid())]
: null!)
.Generate();
}
@@ -19,12 +19,13 @@ public class GetUserPermissionsQueryFixture
/// Creates a random valid query to get user permissions.
/// </summary>
/// <param name="userId">Optional. The user Id.</param>
/// <param name="includeUserId">Whether the user Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created query.</returns>
public GetUserPermissionsQuery Create(Guid? userId = null)
public GetUserPermissionsQuery Create(Guid? userId = null, bool includeUserId = true)
{
return new Faker<GetUserPermissionsQuery>()
.CustomInstantiator(f => new GetUserPermissionsQuery(
userId ?? Guid.NewGuid()))
includeUserId ? (userId ?? Guid.NewGuid()) : null))
.Generate();
}
@@ -19,12 +19,13 @@ public class GetUserRoleQueryFixture
/// Creates a random valid query to get a user's role.
/// </summary>
/// <param name="userId">Optional. The user Id.</param>
/// <param name="includeUserId">Whether the user Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created query.</returns>
public GetUserRoleQuery Create(Guid? userId = null)
public GetUserRoleQuery Create(Guid? userId = null, bool includeUserId = true)
{
return new Faker<GetUserRoleQuery>()
.CustomInstantiator(f => new GetUserRoleQuery(
userId ?? Guid.NewGuid()))
includeUserId ? (userId ?? Guid.NewGuid()) : null))
.Generate();
}