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

@@ -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();
}