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