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

@@ -0,0 +1,50 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Contracts.DTO.Common.Pagination;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Contracts.Fixtures.Core.DTO.Common.Pagination;
/// <summary>
/// Fixture class for the <see cref="PaginationDataDto"/> record.
/// </summary>
[ExcludeFromCodeCoverage]
public class PaginationDataDtoFixture
{
private readonly Faker _faker = new();
/// <summary>
/// Creates a random valid <see cref="PaginationDataDto"/>.
/// </summary>
/// <param name="currentPage">Optional. The current page number in a paginated collection.</param>
/// <param name="perPage">Optional. The number of items to include per page in paginated results.</param>
/// <param name="includeCurrentPage">Whether the current page should be included, or left at its default value.</param>
/// <param name="includePerPage">Whether the per page count should be included, or left at its default value.</param>
/// <returns>The created <see cref="PaginationDataDto"/>.</returns>
public PaginationDataDto Create(
int? currentPage = null,
int? perPage = null,
bool includeCurrentPage = true,
bool includePerPage = true)
{
PaginationDataDto dto = new();
if (includeCurrentPage)
dto.CurrentPage = currentPage ?? _faker.Random.Int(1, 100);
if (includePerPage)
dto.PerPage = perPage ?? _faker.Random.Int(1, 200);
return dto;
}
/// <summary>
/// Creates a list of <see cref="PaginationDataDto"/> instances with randomized test data.
/// </summary>
/// <param name="count">Number of instances to create.</param>
/// <returns>List of configured <see cref="PaginationDataDto"/> instances.</returns>
public List<PaginationDataDto> CreateMany(int count = 3)
{
return [.. Enumerable.Range(0, count).Select(_ => Create())];
}
}
@@ -16,6 +16,7 @@ namespace Lumina.Contracts.Fixtures.Core.DTO.MediaContributors;
public class MediaContributorDtoFixture
{
private readonly Faker _faker = new();
private readonly MediaContributorNameDtoFixture _mediaContributorNameDtoFixture = new();
/// <summary>
/// Creates a random valid <see cref="MediaContributorDto"/>.
@@ -38,9 +39,12 @@ public class MediaContributorDtoFixture
bool includeRole = true)
{
return new MediaContributorDto(
Name: includeName ? new MediaContributorNameDto(
DisplayName: includeDisplayName ? displayName ?? _faker.Name.FullName() : null,
LegalName: includeLegalName ? legalName : null) : null,
Name: includeName ? _mediaContributorNameDtoFixture.Create(
displayName: displayName,
legalName: legalName,
includeDisplayName: includeDisplayName,
// this fixture historically left the legal name null unless one was explicitly supplied, so the name fixture is only asked for one when a value is passed
includeLegalName: includeLegalName && legalName is not null) : null,
Role: includeRole ? role ?? _faker.PickRandom<MediaContributorRole>() : null);
}
@@ -21,14 +21,18 @@ public class MediaContributorNameDtoFixture
/// </summary>
/// <param name="displayName">Optional. The display name of the media contributor.</param>
/// <param name="legalName">Optional. The legal name of the media contributor.</param>
/// <param name="includeDisplayName">Whether the display name should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeLegalName">Whether the legal name should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="MediaContributorNameDto"/>.</returns>
public MediaContributorNameDto Create(
string? displayName = null,
string? legalName = null)
string? legalName = null,
bool includeDisplayName = true,
bool includeLegalName = true)
{
return new MediaContributorNameDto(
displayName ?? _faker.Name.FullName(),
legalName ?? _faker.Name.FullName());
includeDisplayName ? displayName ?? _faker.Name.FullName() : null,
includeLegalName ? legalName ?? _faker.Name.FullName() : null);
}
/// <summary>
@@ -23,19 +23,27 @@ public class ChangePasswordRequestFixture
/// <param name="currentPassword">Optional. The current password.</param>
/// <param name="newPassword">Optional. The new password.</param>
/// <param name="newPasswordConfirm">Optional. The new password confirmation.</param>
/// <param name="includeUsername">Whether the username should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeCurrentPassword">Whether the current password should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeNewPassword">Whether the new password should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeNewPasswordConfirm">Whether the new password confirmation should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="ChangePasswordRequest"/>.</returns>
public ChangePasswordRequest Create(
string? username = null,
string? currentPassword = null,
string? newPassword = null,
string? newPasswordConfirm = null)
string? newPasswordConfirm = null,
bool includeUsername = true,
bool includeCurrentPassword = true,
bool includeNewPassword = true,
bool includeNewPasswordConfirm = true)
{
string generatedNewPassword = newPassword ?? _faker.Internet.Password();
return new ChangePasswordRequest(
username ?? _faker.Internet.UserName(),
currentPassword ?? _faker.Internet.Password(),
generatedNewPassword,
newPasswordConfirm ?? generatedNewPassword
includeUsername ? (username ?? _faker.Internet.UserName()) : null,
includeCurrentPassword ? (currentPassword ?? _faker.Internet.Password()) : null,
includeNewPassword ? generatedNewPassword : null,
includeNewPasswordConfirm ? (newPasswordConfirm ?? generatedNewPassword) : null
);
}
@@ -22,16 +22,22 @@ public class LoginRequestFixture
/// <param name="username">Optional. The username for login.</param>
/// <param name="password">Optional. The password for login.</param>
/// <param name="totpCode">Optional. The TOTP code for two-factor authentication.</param>
/// <param name="includeUsername">Whether the username should be included, or forced to <see langword="null"/>.</param>
/// <param name="includePassword">Whether the password should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeTotpCode">Whether the TOTP code should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="LoginRequest"/>.</returns>
public LoginRequest Create(
string? username = null,
string? password = null,
string? totpCode = null)
string? totpCode = null,
bool includeUsername = true,
bool includePassword = true,
bool includeTotpCode = true)
{
return new LoginRequest(
username ?? _faker.Internet.UserName(),
password ?? _faker.Internet.Password(),
totpCode ?? _faker.Random.Number(100000, 999999).ToString()
includeUsername ? (username ?? _faker.Internet.UserName()) : null,
includePassword ? (password ?? _faker.Internet.Password()) : null,
includeTotpCode ? (totpCode ?? _faker.Random.Number(100000, 999999).ToString()) : null
);
}
@@ -21,14 +21,18 @@ public class RecoverPasswordRequestFixture
/// </summary>
/// <param name="username">Optional. The username for password recovery.</param>
/// <param name="totpCode">Optional. The TOTP code for verification.</param>
/// <param name="includeUsername">Whether the username should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeTotpCode">Whether the TOTP code should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="RecoverPasswordRequest"/>.</returns>
public RecoverPasswordRequest Create(
string? username = null,
string? totpCode = null)
string? totpCode = null,
bool includeUsername = true,
bool includeTotpCode = true)
{
return new RecoverPasswordRequest(
username ?? _faker.Internet.UserName(),
totpCode ?? _faker.Random.Number(100000, 999999).ToString()
includeUsername ? (username ?? _faker.Internet.UserName()) : null,
includeTotpCode ? (totpCode ?? _faker.Random.Number(100000, 999999).ToString()) : null
);
}
@@ -23,18 +23,24 @@ public class RegistrationRequestFixture
/// <param name="password">Optional. The password for registration.</param>
/// <param name="passwordConfirm">Optional. The password confirmation.</param>
/// <param name="use2fa">Optional. Whether to use two-factor authentication.</param>
/// <param name="includeUsername">Whether the username should be included, or forced to <see langword="null"/>.</param>
/// <param name="includePassword">Whether the password should be included, or forced to <see langword="null"/>.</param>
/// <param name="includePasswordConfirm">Whether the password confirmation should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="RegistrationRequest"/>.</returns>
public RegistrationRequest Create(
string? username = null,
string? password = null,
string? passwordConfirm = null,
bool? use2fa = null)
bool? use2fa = null,
bool includeUsername = true,
bool includePassword = true,
bool includePasswordConfirm = true)
{
string generatedPassword = password ?? _faker.Internet.Password();
return new RegistrationRequest(
username ?? _faker.Internet.UserName(),
generatedPassword,
passwordConfirm ?? generatedPassword,
includeUsername ? (username ?? _faker.Internet.UserName()) : null,
includePassword ? generatedPassword : null,
includePasswordConfirm ? (passwordConfirm ?? generatedPassword) : null,
use2fa ?? _faker.Random.Bool()
);
}
@@ -22,12 +22,14 @@ public class AddRoleRequestFixture
/// </summary>
/// <param name="roleName">Optional. The name of the role.</param>
/// <param name="permissions">Optional. The Ids of the permissions granted to the role.</param>
/// <param name="includePermissions">Whether the permissions should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="AddRoleRequest"/>.</returns>
public AddRoleRequest Create(
string? roleName = null,
List<Guid>? permissions = null)
List<Guid>? permissions = null,
bool includePermissions = true)
{
return new AddRoleRequest(roleName ?? _faker.Commerce.Department(), permissions ?? []);
return new AddRoleRequest(roleName ?? _faker.Commerce.Department(), includePermissions ? (permissions ?? []) : null);
}
/// <summary>
@@ -21,11 +21,13 @@ public class DeleteRoleRequestFixture
/// Creates a random valid <see cref="DeleteRoleRequest"/>.
/// </summary>
/// <param name="roleId">Optional. The Id of the role to delete.</param>
/// <param name="includeRoleId">Whether the role Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="DeleteRoleRequest"/>.</returns>
public DeleteRoleRequest Create(
Guid? roleId = null)
Guid? roleId = null,
bool includeRoleId = true)
{
return new DeleteRoleRequest(roleId ?? Guid.NewGuid());
return new DeleteRoleRequest(includeRoleId ? (roleId ?? Guid.NewGuid()) : null);
}
/// <summary>
@@ -18,11 +18,12 @@ public class GetAuthorizationRequestFixture
/// Creates a <see cref="GetAuthorizationRequest"/> with default or specified values.
/// </summary>
/// <param name="userId">Optional. The user Id to use.</param>
/// <param name="includeUserId">Whether the user Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="GetAuthorizationRequest"/>.</returns>
public GetAuthorizationRequest Create(Guid? userId = null)
public GetAuthorizationRequest Create(Guid? userId = null, bool includeUserId = true)
{
return new GetAuthorizationRequest(
UserId: userId ?? Guid.NewGuid()
UserId: includeUserId ? (userId ?? Guid.NewGuid()) : null
);
}
@@ -18,11 +18,13 @@ public class GetUserPermissionsRequestFixture
/// Creates a random valid <see cref="GetUserPermissionsRequest"/>.
/// </summary>
/// <param name="userId">Optional. The Id of the user whose permissions are retrieved.</param>
/// <param name="includeUserId">Whether the user Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="GetUserPermissionsRequest"/>.</returns>
public GetUserPermissionsRequest Create(
Guid? userId = null)
Guid? userId = null,
bool includeUserId = true)
{
return new GetUserPermissionsRequest(userId ?? Guid.NewGuid());
return new GetUserPermissionsRequest(includeUserId ? (userId ?? Guid.NewGuid()) : null);
}
/// <summary>
@@ -18,11 +18,13 @@ public class GetUserRoleRequestFixture
/// Creates a random valid <see cref="GetUserRoleRequest"/>.
/// </summary>
/// <param name="userId">Optional. The Id of the user whose role is retrieved.</param>
/// <param name="includeUserId">Whether the user Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="GetUserRoleRequest"/>.</returns>
public GetUserRoleRequest Create(
Guid? userId = null)
Guid? userId = null,
bool includeUserId = true)
{
return new GetUserRoleRequest(userId ?? Guid.NewGuid());
return new GetUserRoleRequest(includeUserId ? (userId ?? Guid.NewGuid()) : null);
}
/// <summary>
@@ -23,15 +23,17 @@ public class UpdateUserRoleAndPermissionsRequestFixture
/// <param name="userId">Optional. The Id of the user whose role and permissions are updated.</param>
/// <param name="roleId">Optional. The Id of the role assigned to the user.</param>
/// <param name="permissions">Optional. The Ids of the permissions assigned to the user.</param>
/// <param name="includeRoleId">Whether the role Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="UpdateUserRoleAndPermissionsRequest"/>.</returns>
public UpdateUserRoleAndPermissionsRequest Create(
Guid? userId = null,
Guid? roleId = null,
List<Guid>? permissions = null)
List<Guid>? permissions = null,
bool includeRoleId = true)
{
return new UpdateUserRoleAndPermissionsRequest(
userId ?? Guid.NewGuid(),
roleId ?? Guid.NewGuid(),
includeRoleId ? (roleId ?? Guid.NewGuid()) : null,
permissions ?? [Guid.NewGuid(), Guid.NewGuid()]
);
}
@@ -22,17 +22,21 @@ public class UpdatePluginSettingsRequestFixture
/// </summary>
/// <param name="pluginId">Optional. The Id of the plugin whose settings are updated.</param>
/// <param name="settings">Optional. The settings to apply to the plugin.</param>
/// <param name="includeSettings">Whether the settings should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="UpdatePluginSettingsRequest"/>.</returns>
public UpdatePluginSettingsRequest Create(
Guid? pluginId = null,
IReadOnlyDictionary<string, string>? settings = null)
IReadOnlyDictionary<string, string>? settings = null,
bool includeSettings = true)
{
return new UpdatePluginSettingsRequest(
pluginId ?? _faker.Random.Guid(),
settings ?? new Dictionary<string, string>
{
[_faker.Lorem.Word()] = _faker.Lorem.Word()
}
includeSettings
? settings ?? new Dictionary<string, string>
{
[_faker.Lorem.Word()] = _faker.Lorem.Word()
}
: null
);
}
@@ -20,10 +20,11 @@ public class DeleteThemeRequestFixture
/// Creates a random valid <see cref="DeleteThemeRequest"/>.
/// </summary>
/// <param name="themeId">Optional. The manifest id of the theme to delete.</param>
/// <param name="includeThemeId">Whether the theme Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="DeleteThemeRequest"/>.</returns>
public DeleteThemeRequest Create(string? themeId = null)
public DeleteThemeRequest Create(string? themeId = null, bool includeThemeId = true)
{
return new DeleteThemeRequest(themeId ?? _faker.Lorem.Slug(2));
return new DeleteThemeRequest(includeThemeId ? (themeId ?? _faker.Lorem.Slug(2)) : null);
}
/// <summary>
@@ -20,10 +20,11 @@ public class GetThemeArchiveRequestFixture
/// Creates a random valid <see cref="GetThemeArchiveRequest"/>.
/// </summary>
/// <param name="themeId">Optional. The manifest id of the theme.</param>
/// <param name="includeThemeId">Whether the theme Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="GetThemeArchiveRequest"/>.</returns>
public GetThemeArchiveRequest Create(string? themeId = null)
public GetThemeArchiveRequest Create(string? themeId = null, bool includeThemeId = true)
{
return new GetThemeArchiveRequest(themeId ?? _faker.Lorem.Slug(2));
return new GetThemeArchiveRequest(includeThemeId ? (themeId ?? _faker.Lorem.Slug(2)) : null);
}
/// <summary>
@@ -21,12 +21,19 @@ public class GetThemeAssetRequestFixture
/// </summary>
/// <param name="themeId">Optional. The manifest id of the theme.</param>
/// <param name="assetPath">Optional. The asset path relative to the theme pack root.</param>
/// <param name="includeThemeId">Whether the theme Id should be included, or forced to <see langword="null"/>.</param>
/// <param name="includeAssetPath">Whether the asset path should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="GetThemeAssetRequest"/>.</returns>
public GetThemeAssetRequest Create(string? themeId = null, string? assetPath = null)
public GetThemeAssetRequest Create(
string? themeId = null,
string? assetPath = null,
bool includeThemeId = true,
bool includeAssetPath = true)
{
return new GetThemeAssetRequest(
themeId ?? _faker.Lorem.Slug(2),
assetPath ?? _faker.System.FilePath());
includeThemeId ? (themeId ?? _faker.Lorem.Slug(2)) : null,
includeAssetPath ? (assetPath ?? _faker.System.FilePath()) : null
);
}
/// <summary>
@@ -21,12 +21,19 @@ public class GetThemeTemplateRequestFixture
/// </summary>
/// <param name="themeId">Optional. The manifest id of the theme.</param>
/// <param name="pageKey">Optional. The page key that selects the template.</param>
/// <param name="includeThemeId">Whether the theme Id should be included, or forced to <see langword="null"/>.</param>
/// <param name="includePageKey">Whether the page key should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="GetThemeTemplateRequest"/>.</returns>
public GetThemeTemplateRequest Create(string? themeId = null, string? pageKey = null)
public GetThemeTemplateRequest Create(
string? themeId = null,
string? pageKey = null,
bool includeThemeId = true,
bool includePageKey = true)
{
return new GetThemeTemplateRequest(
themeId ?? _faker.Lorem.Slug(2),
pageKey ?? _faker.Lorem.Word());
includeThemeId ? (themeId ?? _faker.Lorem.Slug(2)) : null,
includePageKey ? (pageKey ?? _faker.Lorem.Word()) : null
);
}
/// <summary>
@@ -20,10 +20,11 @@ public class SetCurrentThemeRequestFixture
/// Creates a random valid <see cref="SetCurrentThemeRequest"/>.
/// </summary>
/// <param name="themeId">Optional. The manifest id of the theme to activate.</param>
/// <param name="includeThemeId">Whether the theme Id should be included, or forced to <see langword="null"/>.</param>
/// <returns>The created <see cref="SetCurrentThemeRequest"/>.</returns>
public SetCurrentThemeRequest Create(string? themeId = null)
public SetCurrentThemeRequest Create(string? themeId = null, bool includeThemeId = true)
{
return new SetCurrentThemeRequest(themeId ?? _faker.Lorem.Slug(2));
return new SetCurrentThemeRequest(includeThemeId ? (themeId ?? _faker.Lorem.Slug(2)) : null);
}
/// <summary>