mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 17:11:10 +00:00
Centralizes and standardizes test fixtures.
This commit is contained in:
1 parent
dfc513da4f
commit
b648e2f054
294 files changed
+3807
-4437
No files matched your search
+48
@@ -0,0 +1,48 @@
|
||||
#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;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="PermissionEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PermissionEntityFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="PermissionEntity"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">Optional. The permission Id.</param>
|
||||
/// <param name="permissionName">Optional. The permission name.</param>
|
||||
/// <returns>The created <see cref="PermissionEntity"/>.</returns>
|
||||
public PermissionEntity Create(Guid? id = null, AuthorizationPermission? permissionName = 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.UpdatedOnUtc, f => f.Date.Recent())
|
||||
.RuleFor(x => x.UpdatedBy, f => f.Random.Guid())
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="PermissionEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="PermissionEntity"/> instances.</returns>
|
||||
public List<PermissionEntity> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Authorization;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="RolePermissionEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RolePermissionEntityFixture
|
||||
{
|
||||
private readonly Faker _faker;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RolePermissionEntityFixture"/> class.
|
||||
/// </summary>
|
||||
public RolePermissionEntityFixture()
|
||||
{
|
||||
_faker = new Faker();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="RolePermissionEntity"/>.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="RolePermissionEntity"/>.</returns>
|
||||
public RolePermissionEntity Create()
|
||||
{
|
||||
PermissionEntity permission = new()
|
||||
{
|
||||
Id = _faker.Random.Guid(),
|
||||
PermissionName = _faker.PickRandom<AuthorizationPermission>(),
|
||||
CreatedOnUtc = _faker.Date.Past(),
|
||||
CreatedBy = _faker.Random.Guid(),
|
||||
UpdatedOnUtc = _faker.Date.Recent(),
|
||||
UpdatedBy = _faker.Random.Guid()
|
||||
};
|
||||
|
||||
RoleEntity role = new()
|
||||
{
|
||||
Id = _faker.Random.Guid(),
|
||||
RoleName = _faker.Random.String2(10),
|
||||
CreatedOnUtc = _faker.Date.Past(),
|
||||
CreatedBy = _faker.Random.Guid(),
|
||||
UpdatedOnUtc = _faker.Date.Recent(),
|
||||
UpdatedBy = _faker.Random.Guid()
|
||||
};
|
||||
|
||||
return new Faker<RolePermissionEntity>()
|
||||
.CustomInstantiator(f => new RolePermissionEntity
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
RoleId = role.Id,
|
||||
Role = role,
|
||||
PermissionId = permission.Id,
|
||||
Permission = permission,
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid(),
|
||||
UpdatedOnUtc = f.Date.Recent(),
|
||||
UpdatedBy = f.Random.Guid()
|
||||
})
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="RolePermissionEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="RolePermissionEntity"/> instances.</returns>
|
||||
public List<RolePermissionEntity> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Authorization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="UserAuthorizationEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UserAuthorizationEntityFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a random valid authorization entity.
|
||||
/// </summary>
|
||||
/// <param name="userId">Optional. The user Id to use. If null, a random GUID will be generated.</param>
|
||||
/// <param name="isAdmin">Whether the user should have admin role.</param>
|
||||
/// <returns>The created entity.</returns>
|
||||
public UserAuthorizationEntity Create(Guid? userId = null, bool isAdmin = false)
|
||||
{
|
||||
string role = isAdmin ? "Admin" : string.Empty;
|
||||
|
||||
return new UserAuthorizationEntity
|
||||
{
|
||||
UserId = userId ?? Guid.NewGuid(),
|
||||
Role = role,
|
||||
Permissions = new HashSet<AuthorizationPermission>
|
||||
{
|
||||
AuthorizationPermission.CanViewUsers,
|
||||
AuthorizationPermission.CanRegisterUsers
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="UserAuthorizationEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <param name="isAdmin">Whether the users should have admin role.</param>
|
||||
/// <returns>List of configured <see cref="UserAuthorizationEntity"/> instances.</returns>
|
||||
public List<UserAuthorizationEntity> CreateMany(int count = 3, bool isAdmin = false)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create(isAdmin: isAdmin))];
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="UserPermissionEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UserPermissionEntityFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="UserPermissionEntity"/>.
|
||||
/// </summary>
|
||||
/// <param name="user">The user the permission is granted to.</param>
|
||||
/// <param name="permission">The permission granted to the user.</param>
|
||||
/// <returns>The created <see cref="UserPermissionEntity"/>.</returns>
|
||||
public UserPermissionEntity Create(UserEntity user, PermissionEntity permission)
|
||||
{
|
||||
return new Faker<UserPermissionEntity>()
|
||||
.CustomInstantiator(f => new UserPermissionEntity
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
UserId = user.Id,
|
||||
User = user,
|
||||
PermissionId = permission.Id,
|
||||
Permission = permission,
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = user.Id,
|
||||
UpdatedOnUtc = f.Random.Bool() ? f.Date.Recent() : null,
|
||||
UpdatedBy = f.Random.Bool() ? user.Id : null
|
||||
})
|
||||
.Generate();
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="UserRoleEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UserRoleEntityFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="UserRoleEntity"/>.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="UserRoleEntity"/>.</returns>
|
||||
public UserRoleEntity Create()
|
||||
{
|
||||
return new Faker<UserRoleEntity>()
|
||||
.CustomInstantiator(f => new UserRoleEntity
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
UserId = f.Random.Guid(),
|
||||
User = new()
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
Username = f.Internet.UserName(),
|
||||
Password = f.Internet.Password(),
|
||||
Libraries = [],
|
||||
UserRole = null,
|
||||
UserPermissions = [],
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid()
|
||||
},
|
||||
RoleId = f.Random.Guid(),
|
||||
Role = new()
|
||||
{
|
||||
Id = f.Random.Guid(),
|
||||
RoleName = f.Random.String2(f.Random.Number(1, 50)),
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid()
|
||||
},
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = f.Random.Guid()
|
||||
})
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="UserRoleEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="UserRoleEntity"/> instances.</returns>
|
||||
public List<UserRoleEntity> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
|
||||
}
|
||||
+240
@@ -0,0 +1,240 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Common;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Fixtures.Common.Setup;using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="BookEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookEntityFixture
|
||||
{
|
||||
private readonly Random _random = new();
|
||||
private readonly Faker _faker;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BookEntityFixture"/> class.
|
||||
/// </summary>
|
||||
public BookEntityFixture()
|
||||
{
|
||||
_faker = new Faker();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="BookEntity"/>.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="BookEntity"/>.</returns>
|
||||
public BookEntity Create()
|
||||
{
|
||||
int releaseYear = _random.Next(2000, 2010);
|
||||
int reReleaseYear = _random.Next(2010, 2020);
|
||||
|
||||
return new Faker<BookEntity>()
|
||||
.RuleFor(x => x.Id, f => f.Random.Guid())
|
||||
.RuleFor(x => x.LibraryId, f => f.Random.Guid())
|
||||
.RuleFor(x => x.Path, f => f.System.FilePath())
|
||||
.RuleFor(x => x.Title, f => f.Random.String2(f.Random.Number(1, 255)))
|
||||
.RuleFor(x => x.OriginalTitle, f => f.Random.String2(f.Random.Number(1, 255)))
|
||||
.RuleFor(x => x.Description, f => f.Random.String2(f.Random.Number(1, 2000)))
|
||||
.RuleFor(x => x.OriginalReleaseDate, _faker.DateOnlyBetween(new DateOnly(releaseYear, 1, 1), new DateOnly(releaseYear, 12, 31)))
|
||||
.RuleFor(x => x.OriginalReleaseYear, releaseYear)
|
||||
.RuleFor(x => x.ReReleaseDate, _faker.DateOnlyBetween(new DateOnly(reReleaseYear, 1, 1), new DateOnly(reReleaseYear, 12, 31)))
|
||||
.RuleFor(x => x.ReReleaseYear, reReleaseYear)
|
||||
.RuleFor(x => x.ReleaseCountry, f => f.Random.String2(2))
|
||||
.RuleFor(x => x.ReleaseVersion, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.LanguageCode, f => f.Random.String2(2))
|
||||
.RuleFor(x => x.LanguageName, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.LanguageNativeName, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.OriginalLanguageCode, f => f.Random.String2(2))
|
||||
.RuleFor(x => x.OriginalLanguageName, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.OriginalLanguageNativeName, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.Tags, f => [.. CreateTags(f.Random.Number(1, 5))])
|
||||
.RuleFor(x => x.Genres, f => [.. CreateGenres(f.Random.Number(1, 5))])
|
||||
.RuleFor(x => x.Publisher, f => f.Random.String2(f.Random.Number(1, 100)))
|
||||
.RuleFor(x => x.PageCount, _random.Next(100, 300))
|
||||
.RuleFor(x => x.Format, f => f.PickRandom<BookFormat>())
|
||||
.RuleFor(x => x.Edition, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.VolumeNumber, _random.Next(1, 3))
|
||||
.RuleFor(x => x.ASIN, f => f.Random.String2(10))
|
||||
.RuleFor(x => x.GoodreadsId, _random.Next(100000, 500000).ToString())
|
||||
.RuleFor(x => x.LCCN, CreateLCCN)
|
||||
.RuleFor(x => x.OCLCNumber, CreateOCLCNumber)
|
||||
.RuleFor(x => x.OpenLibraryId, CreateOpenLibraryId)
|
||||
.RuleFor(x => x.LibraryThingId, f => f.Random.String2(f.Random.Number(1, 50)))
|
||||
.RuleFor(x => x.GoogleBooksId, CreateGoogleBooksId)
|
||||
.RuleFor(x => x.BarnesAndNobleId, f => f.Random.String2(10, "0123456789"))
|
||||
.RuleFor(x => x.AppleBooksId, f => $"id{f.Random.Number(1, 999999)}")
|
||||
.RuleFor(x => x.ISBNs, f => CreateIsbns(f.Random.Number(1, 5)))
|
||||
.RuleFor(x => x.Ratings, f => CreateBookRatings(f.Random.Number(1, 5)))
|
||||
.RuleFor(x => x.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(x => x.UpdatedOnUtc, f => f.Date.Recent())
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="BookEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="BookEntity"/> instances.</returns>
|
||||
public List<BookEntity> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of tag entities with random data.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of tags to create.</param>
|
||||
/// <returns>A list of randomly generated tag entities.</returns>
|
||||
private static List<TagEntity> CreateTags(int count)
|
||||
{
|
||||
return new Faker<TagEntity>()
|
||||
.CustomInstantiator(f => new TagEntity(f.Random.String2(f.Random.Number(1, 50))))
|
||||
.Generate(count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of genre entities with random data.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of genres to create.</param>
|
||||
/// <returns>A list of randomly generated genre entities.</returns>
|
||||
private static List<GenreEntity> CreateGenres(int count)
|
||||
{
|
||||
return new Faker<GenreEntity>()
|
||||
.CustomInstantiator(f => new GenreEntity(f.Random.String2(f.Random.Number(1, 50))))
|
||||
.Generate(count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a valid Library of Congress Control Number (LCCN).
|
||||
/// </summary>
|
||||
/// <param name="f">The Faker instance used for generating random data.</param>
|
||||
/// <returns>A properly formatted LCCN string.</returns>
|
||||
private string CreateLCCN(Faker f)
|
||||
{
|
||||
string letters = new([.. Enumerable.Range(0, f.Random.Number(0, 3)).Select(_ => f.Random.Char('a', 'z'))]);
|
||||
string digits = f.Random.String2(f.Random.Number(8, 10), "0123456789");
|
||||
return letters + digits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a valid OCLC (Online Computer Library Center) number.
|
||||
/// </summary>
|
||||
/// <param name="f">The Faker instance used for generating random data.</param>
|
||||
/// <returns>A properly formatted OCLC number string with valid prefix.</returns>
|
||||
private string CreateOCLCNumber(Faker f)
|
||||
{
|
||||
string[] prefixes = ["ocm", "ocn", "on", "(OCoLC)"];
|
||||
string prefix = f.Random.ArrayElement(prefixes);
|
||||
string number = prefix switch
|
||||
{
|
||||
"ocm" => f.Random.String2(8, "0123456789"),
|
||||
"ocn" => f.Random.String2(f.Random.Number(9, 11), "0123456789"),
|
||||
"on" => f.Random.String2(10, "0123456789"),
|
||||
"(OCoLC)" => f.Random.String2(f.Random.Number(8, 10), "0123456789"),
|
||||
_ => f.Random.String2(f.Random.Number(8, 10), "0123456789")
|
||||
};
|
||||
return prefix + number;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a valid Open Library ID.
|
||||
/// </summary>
|
||||
/// <param name="f">The Faker instance used for generating random data.</param>
|
||||
/// <returns>A properly formatted Open Library ID string.</returns>
|
||||
private string CreateOpenLibraryId(Faker f)
|
||||
{
|
||||
int firstDigit = f.Random.Number(1, 9);
|
||||
string remainingDigits = f.Random.String2(f.Random.Number(0, 6), "0123456789");
|
||||
char suffix = f.Random.ArrayElement(['A', 'M', 'W']);
|
||||
return $"OL{firstDigit}{remainingDigits}{suffix}";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a valid Google Books ID.
|
||||
/// </summary>
|
||||
/// <param name="f">The Faker instance used for generating random data.</param>
|
||||
/// <returns>A properly formatted Google Books ID string.</returns>
|
||||
private string CreateGoogleBooksId(Faker f)
|
||||
{
|
||||
const string VALID_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
||||
return new string([.. Enumerable.Repeat(VALID_CHARS, 12).Select(s => s[f.Random.Number(VALID_CHARS.Length - 1)])]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of ISBN entities with random data.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of ISBNs to create.</param>
|
||||
/// <returns>A list of randomly generated ISBN entities.</returns>
|
||||
private static List<IsbnEntity> CreateIsbns(int count)
|
||||
{
|
||||
return new Faker<IsbnEntity>()
|
||||
.CustomInstantiator(f =>
|
||||
{
|
||||
string isbnValue = CreateIsbn(f);
|
||||
return new IsbnEntity(isbnValue, isbnValue.Length > 13 ? IsbnFormat.Isbn13 : IsbnFormat.Isbn10);
|
||||
})
|
||||
.Generate(count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a valid ISBN (International Standard Book Number).
|
||||
/// </summary>
|
||||
/// <param name="f">The Faker instance used for generating random data.</param>
|
||||
/// <returns>A properly formatted ISBN-10 or ISBN-13 string with valid checksum.</returns>
|
||||
private static string CreateIsbn(Faker f)
|
||||
{
|
||||
bool isIsbn13 = f.Random.Bool();
|
||||
if (isIsbn13)
|
||||
{
|
||||
string prefix = f.Random.Bool() ? "978" : "979";
|
||||
string group = f.Random.Number(0, 99999).ToString().PadLeft(5, '0');
|
||||
string publisher = f.Random.Number(0, 999999).ToString().PadLeft(6, '0');
|
||||
string title = f.Random.Number(0, 99).ToString().PadLeft(2, '0');
|
||||
string isbn = $"{prefix}{group[..1]}{publisher}{title}";
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 12; i++)
|
||||
sum += (i % 2 == 0 ? 1 : 3) * int.Parse(isbn[i].ToString());
|
||||
int checkDigit = (10 - (sum % 10)) % 10;
|
||||
return $"{prefix}-{group[..1]}-{publisher}-{title}-{checkDigit}";
|
||||
}
|
||||
else
|
||||
{
|
||||
int[] digits = new int[9];
|
||||
for (int i = 0; i < 9; i++)
|
||||
digits[i] = f.Random.Number(0, 9);
|
||||
int sum = 0;
|
||||
for (int i = 0; i < 9; i++)
|
||||
sum += (10 - i) * digits[i];
|
||||
int checkDigit = (11 - (sum % 11)) % 11;
|
||||
string checkChar = checkDigit == 10 ? "X" : checkDigit.ToString();
|
||||
return $"{digits[0]}-{digits[1]}{digits[2]}-{digits[3]}{digits[4]}{digits[5]}{digits[6]}{digits[7]}{digits[8]}-{checkChar}";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of book rating entities with random data.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of ratings to create.</param>
|
||||
/// <returns>A list of randomly generated book rating entities.</returns>
|
||||
private static List<BookRatingEntity> CreateBookRatings(int count)
|
||||
{
|
||||
return new Faker<BookRatingEntity>()
|
||||
.CustomInstantiator(f => new BookRatingEntity(
|
||||
f.Random.Decimal(1, 5),
|
||||
5,
|
||||
f.PickRandom<BookRatingSource>(),
|
||||
f.Random.Number(1, 1000)
|
||||
))
|
||||
.Generate(count);
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="BookRatingEntity"/>.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookRatingEntityFixture
|
||||
{
|
||||
private readonly Random _random = new();
|
||||
private readonly Faker _faker;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BookRatingEntityFixture"/> class.
|
||||
/// </summary>
|
||||
public BookRatingEntityFixture()
|
||||
{
|
||||
_faker = new Faker();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="BookRatingEntity"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">Optional. The rating value.</param>
|
||||
/// <param name="maxValue">Optional. The maximum possible rating value.</param>
|
||||
/// <param name="source">Optional. The rating source.</param>
|
||||
/// <param name="voteCount">Optional. The number of votes.</param>
|
||||
/// <param name="includeValues">Whether the rating properties should be populated. Set to <see langword="false"/> to create an invalid entity.</param>
|
||||
/// <returns>The created book rating entity.</returns>
|
||||
public BookRatingEntity Create(
|
||||
decimal? value = null,
|
||||
decimal? maxValue = null,
|
||||
BookRatingSource? source = null,
|
||||
int? voteCount = null,
|
||||
bool includeValues = true)
|
||||
{
|
||||
return new BookRatingEntity(
|
||||
includeValues ? (value ?? _random.Next(1, 5)) : null,
|
||||
includeValues ? (maxValue ?? 5) : null,
|
||||
includeValues ? (source ?? _faker.PickRandom<BookRatingSource>()) : null,
|
||||
includeValues ? (voteCount ?? _random.Next(1, 1000)) : null
|
||||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="BookRatingEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="BookRatingEntity"/> instances.</returns>
|
||||
public List<BookRatingEntity> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="LibraryMetadataProviderConfigurationEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class LibraryMetadataProviderConfigurationEntityFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="LibraryMetadataProviderConfigurationEntity"/> for the provided media library, plugin and rank.
|
||||
/// </summary>
|
||||
/// <param name="libraryId">The Id of the media library the configuration belongs to.</param>
|
||||
/// <param name="pluginId">The Id of the plugin providing the metadata.</param>
|
||||
/// <param name="rank">The rank of the metadata provider, determining the order in which providers are tried.</param>
|
||||
/// <returns>The created <see cref="LibraryMetadataProviderConfigurationEntity"/>.</returns>
|
||||
public LibraryMetadataProviderConfigurationEntity Create(Guid libraryId, Guid pluginId, int rank)
|
||||
{
|
||||
return new Faker<LibraryMetadataProviderConfigurationEntity>()
|
||||
.RuleFor(configuration => configuration.Id, faker => faker.Random.Guid())
|
||||
.RuleFor(configuration => configuration.LibraryId, libraryId)
|
||||
.RuleFor(configuration => configuration.PluginId, pluginId)
|
||||
.RuleFor(configuration => configuration.IsEnabled, faker => faker.Random.Bool())
|
||||
.RuleFor(configuration => configuration.Rank, rank)
|
||||
.RuleFor(configuration => configuration.CreatedOnUtc, faker => faker.Date.Past())
|
||||
.RuleFor(configuration => configuration.CreatedBy, faker => faker.Random.Guid())
|
||||
.RuleFor(configuration => configuration.UpdatedOnUtc, faker => faker.Date.Recent())
|
||||
.RuleFor(configuration => configuration.UpdatedBy, faker => faker.Random.Guid())
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="LibraryMetadataProviderConfigurationEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <param name="libraryId">The Id of the media library the configurations belong to.</param>
|
||||
/// <param name="pluginId">The Id of the plugin providing the metadata.</param>
|
||||
/// <param name="rank">The rank of the metadata providers.</param>
|
||||
/// <returns>List of configured <see cref="LibraryMetadataProviderConfigurationEntity"/> instances.</returns>
|
||||
public List<LibraryMetadataProviderConfigurationEntity> CreateMany(int count, Guid libraryId, Guid pluginId, int rank)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create(libraryId, pluginId, rank))];
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Plugins;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Text.Json;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="PluginEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class PluginEntityFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="PluginEntity"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">The Id of the plugin, or <see langword="null"/> to generate a random one.</param>
|
||||
/// <returns>The created <see cref="PluginEntity"/>.</returns>
|
||||
public PluginEntity Create(Guid? id = null)
|
||||
{
|
||||
return new Faker<PluginEntity>()
|
||||
.RuleFor(plugin => plugin.Id, faker => id ?? faker.Random.Guid())
|
||||
.RuleFor(plugin => plugin.Name, faker => faker.Company.CompanyName())
|
||||
.RuleFor(plugin => plugin.Author, faker => faker.Name.FullName())
|
||||
.RuleFor(plugin => plugin.Version, faker => faker.System.Semver())
|
||||
.RuleFor(plugin => plugin.Description, faker => faker.Lorem.Sentence())
|
||||
.RuleFor(plugin => plugin.LoadStatus, faker => faker.PickRandom<PluginLoadStatus>())
|
||||
.RuleFor(plugin => plugin.SettingsJson, faker => JsonSerializer.Serialize(new Dictionary<string, string> { [faker.Random.String2(1, 10)] = faker.Random.String2(1, 20) }))
|
||||
.RuleFor(plugin => plugin.CreatedOnUtc, faker => faker.Date.Past())
|
||||
.RuleFor(plugin => plugin.CreatedBy, faker => faker.Random.Guid())
|
||||
.RuleFor(plugin => plugin.UpdatedOnUtc, faker => faker.Date.Recent())
|
||||
.RuleFor(plugin => plugin.UpdatedBy, faker => faker.Random.Guid())
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="PluginEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="PluginEntity"/> instances.</returns>
|
||||
public List<PluginEntity> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="UserEntity"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UserEntityFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="UserEntity"/>.
|
||||
/// </summary>
|
||||
/// <param name="libraryCount">Number of libraries to generate. Default is 0.</param>
|
||||
/// <returns>The created user entity.</returns>
|
||||
public UserEntity Create(int libraryCount = 0)
|
||||
{
|
||||
Guid userId = Guid.NewGuid();
|
||||
List<LibraryEntity> libraries = libraryCount > 0
|
||||
? CreateLibraries(libraryCount, userId)
|
||||
: [];
|
||||
|
||||
return new Faker<UserEntity>()
|
||||
.CustomInstantiator(f => new UserEntity
|
||||
{
|
||||
Id = userId,
|
||||
Username = default!,
|
||||
Password = default!,
|
||||
CreatedOnUtc = default,
|
||||
TotpSecret = default,
|
||||
Libraries = libraries,
|
||||
UserPermissions = [],
|
||||
UserRole = null,
|
||||
CreatedBy = userId
|
||||
})
|
||||
.RuleFor(x => x.Username, f => f.Internet.UserName())
|
||||
.RuleFor(x => x.Password, f => Uri.EscapeDataString(f.Internet.Password()))
|
||||
.RuleFor(x => x.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(x => x.TotpSecret, f => f.Random.Bool() ? f.Random.String2(32) : null)
|
||||
.Generate();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="UserEntity"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <param name="libraryCount">Number of libraries to generate for each user.</param>
|
||||
/// <returns>List of configured <see cref="UserEntity"/> instances.</returns>
|
||||
public List<UserEntity> CreateMany(int count = 3, int libraryCount = 0)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create(libraryCount))];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a valid collection of user libraries.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of user libraries to create.</param>
|
||||
/// <param name="userId">The unique identifier of the user for whom to create the libraries.</param>
|
||||
/// <returns>A collection of <see cref="LibraryEntity"/>.</returns>
|
||||
private static List<LibraryEntity> CreateLibraries(int count, Guid userId)
|
||||
{
|
||||
return new Faker<LibraryEntity>()
|
||||
.CustomInstantiator(f => new LibraryEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = userId,
|
||||
Title = f.Commerce.ProductName(),
|
||||
LibraryType = f.PickRandom<LibraryType>(),
|
||||
ContentLocations = [],
|
||||
CreatedOnUtc = f.Date.Past(),
|
||||
CreatedBy = userId,
|
||||
UpdatedOnUtc = null,
|
||||
UpdatedBy = null
|
||||
})
|
||||
.Generate(count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Contains extension methods for the <see cref="Faker"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public static class BogusExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds <see cref="DateOnly"/> random data generation support to the <see cref="Faker"/> class.
|
||||
/// </summary>
|
||||
/// <param name="faker">The faker instance to extend.</param>
|
||||
/// <returns>A random <see cref="DateOnly"/> instance.</returns>
|
||||
public static DateOnly DateOnly(this Faker faker)
|
||||
{
|
||||
return System.DateOnly.FromDateTime(faker.Date.Past());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds <see cref="DateOnly"/> random data generation within a specified interval support to the <see cref="Faker"/> class.
|
||||
/// </summary>
|
||||
/// <param name="faker">The faker instance to extend.</param>
|
||||
/// <param name="start">The start of the interval.</param>
|
||||
/// <param name="end">The end of the interval.</param>
|
||||
/// <returns>A random <see cref="DateOnly"/> instance.</returns>
|
||||
public static DateOnly DateOnlyBetween(this Faker faker, DateOnly start, DateOnly end)
|
||||
{
|
||||
DateTime startDateTime = start.ToDateTime(TimeOnly.MinValue);
|
||||
DateTime endDateTime = end.ToDateTime(TimeOnly.MinValue);
|
||||
return System.DateOnly.FromDateTime(faker.Date.Between(startDateTime, endDateTime));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture.Kernel;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a specimen builder for the <see cref="DateOnly"/> type.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class DateOnlySpecimenBuilder : ISpecimenBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds <see cref="DateOnly"/> support to the AutoFixture library.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to process.</param>
|
||||
/// <param name="context">The context within which the specimen is created.</param>
|
||||
/// <returns>A specimen based on the request.</returns>
|
||||
public object Create(object request, ISpecimenContext context)
|
||||
{
|
||||
if (request is Type type && type == typeof(DateOnly))
|
||||
return DateOnly.FromDateTime(DateTime.Now);
|
||||
return new NoSpecimen();
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture.Kernel;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Application.Fixtures.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a specimen builder for the <see cref="DateOnly?"/> type.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class NullableDateOnlySpecimenBuilder : ISpecimenBuilder
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds <see cref="DateOnly?"/> support to the AutoFixture library.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to process.</param>
|
||||
/// <param name="context">The context within which the specimen is created.</param>
|
||||
/// <returns>A specimen based on the request.</returns>
|
||||
public object Create(object request, ISpecimenContext context)
|
||||
{
|
||||
if (request is Type type && type == typeof(DateOnly?))
|
||||
return (DateOnly?)DateOnly.FromDateTime(DateTime.Now);
|
||||
return new NoSpecimen();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user