mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 13: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
+128
@@ -0,0 +1,128 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Common.ValueObjects.Metadata;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaContributorBoundedContext.MediaContributorAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.Entities;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Fixtures.Common.Setup;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="Book"/> domain aggregate.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookFixture
|
||||
{
|
||||
private readonly Fixture _fixture;
|
||||
private readonly IsbnFixture _isbnFixture;
|
||||
private readonly BookRatingFixture _bookRatingFixture;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BookFixture"/> class.
|
||||
/// </summary>
|
||||
public BookFixture()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
_fixture.Customizations.Add(new DateOnlySpecimenBuilder());
|
||||
_fixture.Customizations.Add(new NullableDateOnlySpecimenBuilder());
|
||||
_isbnFixture = new IsbnFixture();
|
||||
_bookRatingFixture = new BookRatingFixture();
|
||||
ConfigureCustomDomainTypes();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="Book"/> domain aggregate.
|
||||
/// </summary>
|
||||
/// <returns>The created <see cref="Book"/> domain aggregate.</returns>
|
||||
public Book Create()
|
||||
{
|
||||
return Book.Create(
|
||||
LibraryId.Create(_fixture.Create<Guid>()),
|
||||
_fixture.Create<string>(),
|
||||
_fixture.Create<WrittenContentMetadata>(),
|
||||
_fixture.Create<BookFormat>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<int>>(),
|
||||
_fixture.Create<Optional<BookSeries>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<List<Isbn>>(),
|
||||
_fixture.Create<List<MediaContributorId>>(),
|
||||
_fixture.Create<List<BookRating>>()
|
||||
).Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates multiple <see cref="Book"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="Book"/> instances.</returns>
|
||||
public List<Book> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
|
||||
private void ConfigureCustomDomainTypes()
|
||||
{
|
||||
_fixture.Register(() => Optional<string>.Some(_fixture.Create<string>()));
|
||||
_fixture.Register(() => Optional<int>.Some(_fixture.Create<int>()));
|
||||
|
||||
_fixture.Register(() =>
|
||||
{
|
||||
int originalYear = _fixture.Create<Generator<int>>().First(year => year >= 1900 && year <= 2025);
|
||||
int reReleaseYear = _fixture.Create<Generator<int>>().First(year => year >= originalYear && year <= 2025);
|
||||
|
||||
return ReleaseInfo.Create(
|
||||
Optional<DateOnly>.Some(DateOnly.FromDateTime(new DateTime(originalYear, 1, 1))),
|
||||
Optional<int>.Some(originalYear),
|
||||
Optional<DateOnly>.Some(DateOnly.FromDateTime(new DateTime(reReleaseYear, 1, 1))),
|
||||
Optional<int>.Some(reReleaseYear),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>()
|
||||
).Value;
|
||||
});
|
||||
|
||||
_fixture.Register(() => Genre.Create(
|
||||
_fixture.Create<string>()
|
||||
).Value);
|
||||
|
||||
_fixture.Register(() => Tag.Create(
|
||||
_fixture.Create<string>()
|
||||
).Value);
|
||||
|
||||
_fixture.Register(() => WrittenContentMetadata.Create(
|
||||
_fixture.Create<string>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<ReleaseInfo>(),
|
||||
_fixture.Create<List<Genre>>(),
|
||||
_fixture.Create<List<Tag>>(),
|
||||
_fixture.Create<Optional<LanguageInfo>>(),
|
||||
_fixture.Create<Optional<LanguageInfo>>(),
|
||||
_fixture.Create<Optional<string>>(),
|
||||
_fixture.Create<Optional<int>>()
|
||||
).Value);
|
||||
|
||||
_fixture.Register(() => _isbnFixture.Create());
|
||||
|
||||
_fixture.Register(() => _bookRatingFixture.Create());
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="BookRating"/> domain value object.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookRatingFixture
|
||||
{
|
||||
private readonly Fixture _fixture;
|
||||
private readonly Random _random = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BookRatingFixture"/> class.
|
||||
/// </summary>
|
||||
public BookRatingFixture()
|
||||
{
|
||||
_fixture = new Fixture();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="BookRating"/>.
|
||||
/// </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. If not provided, a random source is selected.</param>
|
||||
/// <param name="voteCount">Optional. The number of votes. If not provided, a random vote count is selected.</param>
|
||||
/// <returns>The created <see cref="BookRating"/>.</returns>
|
||||
public BookRating Create(
|
||||
decimal? value = null,
|
||||
decimal? maxValue = null,
|
||||
Optional<BookRatingSource>? source = null,
|
||||
Optional<int>? voteCount = null)
|
||||
{
|
||||
maxValue ??= _random.Next(5, 10);
|
||||
value ??= _random.Next(1, (int)maxValue.Value);
|
||||
source ??= Optional<BookRatingSource>.Some(_fixture.Create<BookRatingSource>());
|
||||
voteCount ??= Optional<int>.Some(_random.Next(1, 1000));
|
||||
|
||||
return BookRating.Create(value.Value, maxValue.Value, source.Value, voteCount.Value).Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates multiple <see cref="BookRating"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="BookRating"/> instances.</returns>
|
||||
public List<BookRating> CreateMany(int count = 3)
|
||||
{
|
||||
List<BookRating> result = [];
|
||||
for (int i = 0; i < count; i++)
|
||||
result.Add(Create());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="Isbn"/> domain value object.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class IsbnFixture
|
||||
{
|
||||
private readonly Random _random = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a valid <see cref="Isbn"/> with a random or explicitly requested format.
|
||||
/// </summary>
|
||||
/// <param name="format">Optional. The requested <see cref="IsbnFormat"/>. If not provided, a random format is selected.</param>
|
||||
/// <returns>The created <see cref="Isbn"/>.</returns>
|
||||
public Isbn Create(IsbnFormat? format = null)
|
||||
{
|
||||
IsbnFormat targetFormat = format ?? (_random.Next(2) == 0 ? IsbnFormat.Isbn10 : IsbnFormat.Isbn13);
|
||||
string isbn = targetFormat == IsbnFormat.Isbn10 ? GenerateValidIsbn10() : GenerateValidIsbn13();
|
||||
return Isbn.Create(isbn, targetFormat).Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates multiple <see cref="Isbn"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="Isbn"/> instances.</returns>
|
||||
public List<Isbn> CreateMany(int count = 3)
|
||||
{
|
||||
List<Isbn> result = [];
|
||||
for (int i = 0; i < count; i++)
|
||||
result.Add(Create());
|
||||
return result;
|
||||
}
|
||||
|
||||
private string GenerateValidIsbn10()
|
||||
{
|
||||
int[] digits = new int[9];
|
||||
for (int i = 0; i < 9; i++)
|
||||
digits[i] = _random.Next(0, 10);
|
||||
|
||||
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}";
|
||||
}
|
||||
|
||||
private string GenerateValidIsbn13()
|
||||
{
|
||||
string prefix = _random.Next(2) == 0 ? "978" : "979";
|
||||
string group = _random.Next(0, 99999).ToString().PadLeft(5, '0');
|
||||
string publisher = _random.Next(0, 999999).ToString().PadLeft(6, '0');
|
||||
string title = _random.Next(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}";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user