mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Implements tests for commit fa8513cc71
This commit is contained in:
1 parent
5c436fc13d
commit
f7c2186660
15 files changed
+2970
No files matched your search
+55
@@ -0,0 +1,55 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaPosition"/> value object.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaPositionFixture
|
||||
{
|
||||
private const int MAXIMUM_OFFSET_MILLISECONDS = 7_200_000;
|
||||
|
||||
private readonly Faker _faker = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="MediaPosition"/>.
|
||||
/// </summary>
|
||||
/// <param name="kind">Optional. The kind of representation that the position uses. Defaults to a random valid kind.</param>
|
||||
/// <param name="timeOffset">Optional. The time offset of the position. Only used when <paramref name="kind"/> is <see cref="MediaPositionKind.TimeOffset"/>.</param>
|
||||
/// <param name="location">Optional. The location of the position. Only used when <paramref name="kind"/> is <see cref="MediaPositionKind.Location"/>.</param>
|
||||
/// <returns>The created <see cref="MediaPosition"/>.</returns>
|
||||
/// <remarks>
|
||||
/// The kind selects which representation is populated, so exactly one of <paramref name="timeOffset"/> and <paramref name="location"/> is used. A
|
||||
/// position always carries the representation of its own kind and can never carry both or neither, so no include flag is offered for them.
|
||||
/// There is no flag for an explicit empty <paramref name="location"/> either, because an empty string is already an explicit value.
|
||||
/// </remarks>
|
||||
public MediaPosition Create(
|
||||
MediaPositionKind? kind = null,
|
||||
TimeSpan? timeOffset = null,
|
||||
string? location = null)
|
||||
{
|
||||
MediaPositionKind resolvedKind = kind ?? _faker.Random.ArrayElement([MediaPositionKind.TimeOffset, MediaPositionKind.Location]);
|
||||
if (resolvedKind == MediaPositionKind.Location)
|
||||
return MediaPosition.CreateLocation(location ?? _faker.Lorem.Word());
|
||||
return MediaPosition.CreateTimeOffset(timeOffset ?? TimeSpan.FromMilliseconds(_faker.Random.Int(0, MAXIMUM_OFFSET_MILLISECONDS)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="MediaPosition"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="MediaPosition"/> instances.</returns>
|
||||
public List<MediaPosition> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.Events;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaConsumptionCompletedDomainEvent"/> record.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaConsumptionCompletedDomainEventFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
private readonly MediaConsumptionRecordIdFixture _mediaConsumptionRecordIdFixture = new();
|
||||
private readonly UserIdFixture _userIdFixture = new();
|
||||
private readonly LibraryIdFixture _libraryIdFixture = new();
|
||||
private readonly MediaItemIdFixture _mediaItemIdFixture = new();
|
||||
private readonly MediaPositionFixture _mediaPositionFixture = new();
|
||||
private readonly MediaKind[] _supportedMediaKinds =
|
||||
[
|
||||
MediaKind.MusicTrack,
|
||||
MediaKind.PodcastEpisode,
|
||||
MediaKind.Audiobook,
|
||||
MediaKind.Movie,
|
||||
MediaKind.TvEpisode,
|
||||
MediaKind.Book,
|
||||
MediaKind.Photo,
|
||||
MediaKind.Video
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="MediaConsumptionCompletedDomainEvent"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">Optional. The Id of the domain event.</param>
|
||||
/// <param name="mediaConsumptionRecordId">Optional. The unique identifier of the consumption record.</param>
|
||||
/// <param name="userId">Optional. The unique identifier of the user that owns the consumption record.</param>
|
||||
/// <param name="libraryId">Optional. The unique identifier of the media library that contains the consumed item.</param>
|
||||
/// <param name="mediaItemId">Optional. The unique identifier of the consumed media item.</param>
|
||||
/// <param name="mediaKind">Optional. The kind of media that was consumed. Defaults to a random supported kind.</param>
|
||||
/// <param name="lastPosition">Optional. The last known position for the media item. Defaults to no position, unless <paramref name="includeLastPosition"/> is set.</param>
|
||||
/// <param name="includeLastPosition">Whether to generate a last position when none is supplied. Must be <see langword="false"/> for a photo, which has no position kind and accepts no position at all.</param>
|
||||
/// <param name="lastProgressFraction">Optional. The final progress fraction reported for the media item. Defaults to a bounded random fraction between zero and one.</param>
|
||||
/// <param name="consumedDuration">Optional. The active duration consumed by the session. Defaults to a bounded random duration.</param>
|
||||
/// <param name="consecutivePagesRead">Optional. The number of consecutive pages read in the session. Defaults to a bounded random count for a page based kind, and to zero for every other kind.</param>
|
||||
/// <param name="occurredOnUtc">Optional. The date and time when the domain event occurred.</param>
|
||||
/// <returns>The created <see cref="MediaConsumptionCompletedDomainEvent"/>.</returns>
|
||||
public MediaConsumptionCompletedDomainEvent Create(
|
||||
Guid? id = null,
|
||||
MediaConsumptionRecordId? mediaConsumptionRecordId = null,
|
||||
UserId? userId = null,
|
||||
LibraryId? libraryId = null,
|
||||
MediaItemId? mediaItemId = null,
|
||||
MediaKind? mediaKind = null,
|
||||
Optional<MediaPosition>? lastPosition = null,
|
||||
bool includeLastPosition = false,
|
||||
double? lastProgressFraction = null,
|
||||
TimeSpan? consumedDuration = null,
|
||||
int? consecutivePagesRead = null,
|
||||
DateTime? occurredOnUtc = null)
|
||||
{
|
||||
MediaKind resolvedMediaKind = mediaKind ?? _faker.Random.ArrayElement(_supportedMediaKinds);
|
||||
Optional<MediaConsumptionRule> ruleResult = MediaConsumptionRule.For(resolvedMediaKind);
|
||||
bool isPageBased = ruleResult.HasValue && ruleResult.Value is PageThresholdConsumptionRule;
|
||||
|
||||
return new MediaConsumptionCompletedDomainEvent(
|
||||
id ?? Guid.NewGuid(),
|
||||
mediaConsumptionRecordId ?? _mediaConsumptionRecordIdFixture.Create(),
|
||||
userId ?? _userIdFixture.Create(),
|
||||
libraryId ?? _libraryIdFixture.Create(),
|
||||
mediaItemId ?? _mediaItemIdFixture.Create(),
|
||||
resolvedMediaKind,
|
||||
lastPosition ?? ResolveLastPosition(includeLastPosition, ruleResult),
|
||||
lastProgressFraction ?? _faker.Random.Double(0, 1),
|
||||
consumedDuration ?? TimeSpan.FromSeconds(_faker.Random.Int(0, 36000)),
|
||||
consecutivePagesRead ?? (isPageBased ? _faker.Random.Int(0, 100) : 0),
|
||||
occurredOnUtc ?? DateTime.UtcNow);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the last position of the domain event, keeping it consistent with the position kind of the media kind rule.
|
||||
/// </summary>
|
||||
/// <param name="includeLastPosition">Whether to generate a position.</param>
|
||||
/// <param name="ruleResult">The consumption rule of the media kind of the domain event, if the kind has one.</param>
|
||||
/// <returns>The resolved last position, or no value when the domain event carries no position.</returns>
|
||||
private Optional<MediaPosition> ResolveLastPosition(bool includeLastPosition, Optional<MediaConsumptionRule> ruleResult)
|
||||
{
|
||||
if (!includeLastPosition)
|
||||
return Optional<MediaPosition>.None();
|
||||
if (!ruleResult.HasValue || !ruleResult.Value.PositionKind.HasValue)
|
||||
return Optional<MediaPosition>.None();
|
||||
return Optional<MediaPosition>.Some(_mediaPositionFixture.Create(kind: ruleResult.Value.PositionKind.Value));
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.Events;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaConsumptionProgressedDomainEvent"/> record.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaConsumptionProgressedDomainEventFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
private readonly MediaConsumptionRecordIdFixture _mediaConsumptionRecordIdFixture = new();
|
||||
private readonly UserIdFixture _userIdFixture = new();
|
||||
private readonly LibraryIdFixture _libraryIdFixture = new();
|
||||
private readonly MediaItemIdFixture _mediaItemIdFixture = new();
|
||||
private readonly MediaPositionFixture _mediaPositionFixture = new();
|
||||
private readonly MediaKind[] _supportedMediaKinds =
|
||||
[
|
||||
MediaKind.MusicTrack,
|
||||
MediaKind.PodcastEpisode,
|
||||
MediaKind.Audiobook,
|
||||
MediaKind.Movie,
|
||||
MediaKind.TvEpisode,
|
||||
MediaKind.Book,
|
||||
MediaKind.Photo,
|
||||
MediaKind.Video
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="MediaConsumptionProgressedDomainEvent"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">Optional. The Id of the domain event.</param>
|
||||
/// <param name="mediaConsumptionRecordId">Optional. The unique identifier of the consumption record.</param>
|
||||
/// <param name="userId">Optional. The unique identifier of the user that owns the consumption record.</param>
|
||||
/// <param name="libraryId">Optional. The unique identifier of the media library that contains the consumed item.</param>
|
||||
/// <param name="mediaItemId">Optional. The unique identifier of the consumed media item.</param>
|
||||
/// <param name="mediaKind">Optional. The kind of media that is being consumed. Defaults to a random supported kind.</param>
|
||||
/// <param name="lastPosition">Optional. The last known position for the media item. Defaults to no position, unless <paramref name="includeLastPosition"/> is set.</param>
|
||||
/// <param name="includeLastPosition">Whether to generate a last position when none is supplied. Must be <see langword="false"/> for a photo, which has no position kind and accepts no position at all.</param>
|
||||
/// <param name="lastProgressFraction">Optional. The last progress fraction reported by the client. Defaults to a bounded random fraction between zero and one.</param>
|
||||
/// <param name="consumedDuration">Optional. The active duration consumed so far. Defaults to a bounded random duration.</param>
|
||||
/// <param name="consecutivePagesRead">Optional. The number of consecutive pages read so far. Defaults to a bounded random count for a page based kind, and to zero for every other kind.</param>
|
||||
/// <param name="occurredOnUtc">Optional. The date and time when the domain event occurred.</param>
|
||||
/// <returns>The created <see cref="MediaConsumptionProgressedDomainEvent"/>.</returns>
|
||||
public MediaConsumptionProgressedDomainEvent Create(
|
||||
Guid? id = null,
|
||||
MediaConsumptionRecordId? mediaConsumptionRecordId = null,
|
||||
UserId? userId = null,
|
||||
LibraryId? libraryId = null,
|
||||
MediaItemId? mediaItemId = null,
|
||||
MediaKind? mediaKind = null,
|
||||
Optional<MediaPosition>? lastPosition = null,
|
||||
bool includeLastPosition = false,
|
||||
double? lastProgressFraction = null,
|
||||
TimeSpan? consumedDuration = null,
|
||||
int? consecutivePagesRead = null,
|
||||
DateTime? occurredOnUtc = null)
|
||||
{
|
||||
MediaKind resolvedMediaKind = mediaKind ?? _faker.Random.ArrayElement(_supportedMediaKinds);
|
||||
Optional<MediaConsumptionRule> ruleResult = MediaConsumptionRule.For(resolvedMediaKind);
|
||||
bool isPageBased = ruleResult.HasValue && ruleResult.Value is PageThresholdConsumptionRule;
|
||||
|
||||
return new MediaConsumptionProgressedDomainEvent(
|
||||
id ?? Guid.NewGuid(),
|
||||
mediaConsumptionRecordId ?? _mediaConsumptionRecordIdFixture.Create(),
|
||||
userId ?? _userIdFixture.Create(),
|
||||
libraryId ?? _libraryIdFixture.Create(),
|
||||
mediaItemId ?? _mediaItemIdFixture.Create(),
|
||||
resolvedMediaKind,
|
||||
lastPosition ?? ResolveLastPosition(includeLastPosition, ruleResult),
|
||||
lastProgressFraction ?? _faker.Random.Double(0, 1),
|
||||
consumedDuration ?? TimeSpan.FromSeconds(_faker.Random.Int(0, 36000)),
|
||||
consecutivePagesRead ?? (isPageBased ? _faker.Random.Int(0, 100) : 0),
|
||||
occurredOnUtc ?? DateTime.UtcNow);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the last position of the domain event, keeping it consistent with the position kind of the media kind rule.
|
||||
/// </summary>
|
||||
/// <param name="includeLastPosition">Whether to generate a position.</param>
|
||||
/// <param name="ruleResult">The consumption rule of the media kind of the domain event, if the kind has one.</param>
|
||||
/// <returns>The resolved last position, or no value when the domain event carries no position.</returns>
|
||||
private Optional<MediaPosition> ResolveLastPosition(bool includeLastPosition, Optional<MediaConsumptionRule> ruleResult)
|
||||
{
|
||||
if (!includeLastPosition)
|
||||
return Optional<MediaPosition>.None();
|
||||
if (!ruleResult.HasValue || !ruleResult.Value.PositionKind.HasValue)
|
||||
return Optional<MediaPosition>.None();
|
||||
return Optional<MediaPosition>.Some(_mediaPositionFixture.Create(kind: ruleResult.Value.PositionKind.Value));
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.Events;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaConsumptionStartedDomainEvent"/> record.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaConsumptionStartedDomainEventFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
private readonly MediaConsumptionRecordIdFixture _mediaConsumptionRecordIdFixture = new();
|
||||
private readonly UserIdFixture _userIdFixture = new();
|
||||
private readonly LibraryIdFixture _libraryIdFixture = new();
|
||||
private readonly MediaItemIdFixture _mediaItemIdFixture = new();
|
||||
private readonly MediaKind[] _supportedMediaKinds =
|
||||
[
|
||||
MediaKind.MusicTrack,
|
||||
MediaKind.PodcastEpisode,
|
||||
MediaKind.Audiobook,
|
||||
MediaKind.Movie,
|
||||
MediaKind.TvEpisode,
|
||||
MediaKind.Book,
|
||||
MediaKind.Photo,
|
||||
MediaKind.Video
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="MediaConsumptionStartedDomainEvent"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">Optional. The Id of the domain event.</param>
|
||||
/// <param name="mediaConsumptionRecordId">Optional. The unique identifier of the consumption record.</param>
|
||||
/// <param name="userId">Optional. The unique identifier of the user that started the session.</param>
|
||||
/// <param name="libraryId">Optional. The unique identifier of the media library that contains the consumed item.</param>
|
||||
/// <param name="mediaItemId">Optional. The unique identifier of the consumed media item.</param>
|
||||
/// <param name="mediaKind">Optional. The kind of media that is being consumed. Defaults to a random supported kind.</param>
|
||||
/// <param name="occurredOnUtc">Optional. The date and time when the domain event occurred.</param>
|
||||
/// <returns>The created <see cref="MediaConsumptionStartedDomainEvent"/>.</returns>
|
||||
public MediaConsumptionStartedDomainEvent Create(
|
||||
Guid? id = null,
|
||||
MediaConsumptionRecordId? mediaConsumptionRecordId = null,
|
||||
UserId? userId = null,
|
||||
LibraryId? libraryId = null,
|
||||
MediaItemId? mediaItemId = null,
|
||||
MediaKind? mediaKind = null,
|
||||
DateTime? occurredOnUtc = null)
|
||||
{
|
||||
return new MediaConsumptionStartedDomainEvent(
|
||||
id ?? Guid.NewGuid(),
|
||||
mediaConsumptionRecordId ?? _mediaConsumptionRecordIdFixture.Create(),
|
||||
userId ?? _userIdFixture.Create(),
|
||||
libraryId ?? _libraryIdFixture.Create(),
|
||||
mediaItemId ?? _mediaItemIdFixture.Create(),
|
||||
mediaKind ?? _faker.Random.ArrayElement(_supportedMediaKinds),
|
||||
occurredOnUtc ?? DateTime.UtcNow);
|
||||
}
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.Events;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.Events;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaConsumptionStoppedDomainEvent"/> record.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaConsumptionStoppedDomainEventFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
private readonly MediaConsumptionRecordIdFixture _mediaConsumptionRecordIdFixture = new();
|
||||
private readonly UserIdFixture _userIdFixture = new();
|
||||
private readonly LibraryIdFixture _libraryIdFixture = new();
|
||||
private readonly MediaItemIdFixture _mediaItemIdFixture = new();
|
||||
private readonly MediaPositionFixture _mediaPositionFixture = new();
|
||||
private readonly MediaKind[] _supportedMediaKinds =
|
||||
[
|
||||
MediaKind.MusicTrack,
|
||||
MediaKind.PodcastEpisode,
|
||||
MediaKind.Audiobook,
|
||||
MediaKind.Movie,
|
||||
MediaKind.TvEpisode,
|
||||
MediaKind.Book,
|
||||
MediaKind.Photo,
|
||||
MediaKind.Video
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="MediaConsumptionStoppedDomainEvent"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">Optional. The Id of the domain event.</param>
|
||||
/// <param name="mediaConsumptionRecordId">Optional. The unique identifier of the consumption record.</param>
|
||||
/// <param name="userId">Optional. The unique identifier of the user that owns the consumption record.</param>
|
||||
/// <param name="libraryId">Optional. The unique identifier of the media library that contains the consumed item.</param>
|
||||
/// <param name="mediaItemId">Optional. The unique identifier of the consumed media item.</param>
|
||||
/// <param name="mediaKind">Optional. The kind of media that was consumed. Defaults to a random supported kind.</param>
|
||||
/// <param name="lastPosition">Optional. The last known position for the media item. Defaults to no position, unless <paramref name="includeLastPosition"/> is set.</param>
|
||||
/// <param name="includeLastPosition">Whether to generate a last position when none is supplied. Must be <see langword="false"/> for a photo, which has no position kind and accepts no position at all.</param>
|
||||
/// <param name="lastProgressFraction">Optional. The final progress fraction reported for the media item. Defaults to a bounded random fraction between zero and one.</param>
|
||||
/// <param name="consumedDuration">Optional. The active duration consumed by the session. Defaults to a bounded random duration.</param>
|
||||
/// <param name="consecutivePagesRead">Optional. The number of consecutive pages read in the session. Defaults to a bounded random count for a page based kind, and to zero for every other kind.</param>
|
||||
/// <param name="occurredOnUtc">Optional. The date and time when the domain event occurred.</param>
|
||||
/// <returns>The created <see cref="MediaConsumptionStoppedDomainEvent"/>.</returns>
|
||||
public MediaConsumptionStoppedDomainEvent Create(
|
||||
Guid? id = null,
|
||||
MediaConsumptionRecordId? mediaConsumptionRecordId = null,
|
||||
UserId? userId = null,
|
||||
LibraryId? libraryId = null,
|
||||
MediaItemId? mediaItemId = null,
|
||||
MediaKind? mediaKind = null,
|
||||
Optional<MediaPosition>? lastPosition = null,
|
||||
bool includeLastPosition = false,
|
||||
double? lastProgressFraction = null,
|
||||
TimeSpan? consumedDuration = null,
|
||||
int? consecutivePagesRead = null,
|
||||
DateTime? occurredOnUtc = null)
|
||||
{
|
||||
MediaKind resolvedMediaKind = mediaKind ?? _faker.Random.ArrayElement(_supportedMediaKinds);
|
||||
Optional<MediaConsumptionRule> ruleResult = MediaConsumptionRule.For(resolvedMediaKind);
|
||||
bool isPageBased = ruleResult.HasValue && ruleResult.Value is PageThresholdConsumptionRule;
|
||||
|
||||
return new MediaConsumptionStoppedDomainEvent(
|
||||
id ?? Guid.NewGuid(),
|
||||
mediaConsumptionRecordId ?? _mediaConsumptionRecordIdFixture.Create(),
|
||||
userId ?? _userIdFixture.Create(),
|
||||
libraryId ?? _libraryIdFixture.Create(),
|
||||
mediaItemId ?? _mediaItemIdFixture.Create(),
|
||||
resolvedMediaKind,
|
||||
lastPosition ?? ResolveLastPosition(includeLastPosition, ruleResult),
|
||||
lastProgressFraction ?? _faker.Random.Double(0, 1),
|
||||
consumedDuration ?? TimeSpan.FromSeconds(_faker.Random.Int(0, 36000)),
|
||||
consecutivePagesRead ?? (isPageBased ? _faker.Random.Int(0, 100) : 0),
|
||||
occurredOnUtc ?? DateTime.UtcNow);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the last position of the domain event, keeping it consistent with the position kind of the media kind rule.
|
||||
/// </summary>
|
||||
/// <param name="includeLastPosition">Whether to generate a position.</param>
|
||||
/// <param name="ruleResult">The consumption rule of the media kind of the domain event, if the kind has one.</param>
|
||||
/// <returns>The resolved last position, or no value when the domain event carries no position.</returns>
|
||||
private Optional<MediaPosition> ResolveLastPosition(bool includeLastPosition, Optional<MediaConsumptionRule> ruleResult)
|
||||
{
|
||||
if (!includeLastPosition)
|
||||
return Optional<MediaPosition>.None();
|
||||
if (!ruleResult.HasValue || !ruleResult.Value.PositionKind.HasValue)
|
||||
return Optional<MediaPosition>.None();
|
||||
return Optional<MediaPosition>.Some(_mediaPositionFixture.Create(kind: ruleResult.Value.PositionKind.Value));
|
||||
}
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate;
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.Common.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.LibraryManagementBoundedContext.LibraryAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.MediaLibraryBoundedContext.MediaItemAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaConsumption;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaConsumptionRecord"/> domain aggregate.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaConsumptionRecordFixture
|
||||
{
|
||||
private readonly Faker _faker = new();
|
||||
private readonly MediaConsumptionRecordIdFixture _mediaConsumptionRecordIdFixture = new();
|
||||
private readonly UserIdFixture _userIdFixture = new();
|
||||
private readonly LibraryIdFixture _libraryIdFixture = new();
|
||||
private readonly MediaItemIdFixture _mediaItemIdFixture = new();
|
||||
private readonly MediaPositionFixture _mediaPositionFixture = new();
|
||||
private readonly MediaKind[] _supportedMediaKinds =
|
||||
[
|
||||
MediaKind.MusicTrack,
|
||||
MediaKind.PodcastEpisode,
|
||||
MediaKind.Audiobook,
|
||||
MediaKind.Movie,
|
||||
MediaKind.TvEpisode,
|
||||
MediaKind.Book,
|
||||
MediaKind.Photo,
|
||||
MediaKind.Video
|
||||
];
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="MediaConsumptionRecord"/> domain aggregate, with pre-existing data.
|
||||
/// </summary>
|
||||
/// <param name="id">Optional. The Id of the consumption record.</param>
|
||||
/// <param name="userId">Optional. The Id of the user that owns the consumption record.</param>
|
||||
/// <param name="libraryId">Optional. The Id of the media library that contains the consumed media item.</param>
|
||||
/// <param name="mediaItemId">Optional. The Id of the consumed media item.</param>
|
||||
/// <param name="mediaKind">Optional. The kind of media that is consumed. Defaults to a random supported kind.</param>
|
||||
/// <param name="status">Optional. The status of the consumption session. Defaults to a random valid status.</param>
|
||||
/// <param name="startedOnUtc">Optional. The date and time when the consumption session started. Defaults to a few days in the past.</param>
|
||||
/// <param name="lastActivityOnUtc">Optional. The date and time of the last activity of the consumption session. Defaults to a moment at or after the start.</param>
|
||||
/// <param name="endedOnUtc">Optional. The date and time when the consumption session ended. Defaults to no value for an in-progress session, and to a moment at or after the last activity for a terminal session.</param>
|
||||
/// <param name="consumedDuration">Optional. The active duration consumed by the session. Defaults to a bounded random duration.</param>
|
||||
/// <param name="lastPosition">Optional. The last position of the session. When omitted, a position matching the position kind of the media kind rule is generated.</param>
|
||||
/// <param name="includeLastPosition">Whether to generate a last position when <paramref name="lastPosition"/> is not supplied. Must be <see langword="false"/> for a photo, which has no position kind and accepts no position at all.</param>
|
||||
/// <param name="lastProgressFraction">Optional. The last progress fraction of the session. Defaults to a bounded random fraction between zero and one.</param>
|
||||
/// <param name="consecutivePagesRead">Optional. The number of consecutive pages read. Defaults to a bounded random count for a page based kind, and to zero for every other kind.</param>
|
||||
/// <returns>The created <see cref="MediaConsumptionRecord"/>.</returns>
|
||||
/// <remarks>
|
||||
/// The record is built through <see cref="MediaConsumptionRecord.Reconstitute"/>, so every default produced here is internally consistent: the
|
||||
/// start is in the past, the last activity never precedes it, an end time exists exactly when the status is terminal and never precedes the last
|
||||
/// activity, consecutive pages are only counted for a book, and a generated position always uses the position kind of its media kind.
|
||||
/// </remarks>
|
||||
public MediaConsumptionRecord Create(
|
||||
MediaConsumptionRecordId? id = null,
|
||||
UserId? userId = null,
|
||||
LibraryId? libraryId = null,
|
||||
MediaItemId? mediaItemId = null,
|
||||
MediaKind? mediaKind = null,
|
||||
ConsumptionStatus? status = null,
|
||||
DateTime? startedOnUtc = null,
|
||||
DateTime? lastActivityOnUtc = null,
|
||||
Optional<DateTime>? endedOnUtc = null,
|
||||
TimeSpan? consumedDuration = null,
|
||||
Optional<MediaPosition>? lastPosition = null,
|
||||
bool includeLastPosition = true,
|
||||
double? lastProgressFraction = null,
|
||||
int? consecutivePagesRead = null)
|
||||
{
|
||||
MediaKind resolvedMediaKind = mediaKind ?? _faker.Random.ArrayElement(_supportedMediaKinds);
|
||||
ConsumptionStatus resolvedStatus = status ?? _faker.Random.ArrayElement([ConsumptionStatus.InProgress, ConsumptionStatus.Completed, ConsumptionStatus.Stopped]);
|
||||
DateTime resolvedStartedOnUtc = startedOnUtc ?? DateTime.UtcNow.AddDays(-_faker.Random.Int(1, 30)).AddMinutes(-_faker.Random.Int(0, 59));
|
||||
DateTime resolvedLastActivityOnUtc = lastActivityOnUtc ?? resolvedStartedOnUtc.AddMinutes(_faker.Random.Int(0, 120));
|
||||
Optional<DateTime> resolvedEndedOnUtc = endedOnUtc ?? (resolvedStatus == ConsumptionStatus.InProgress
|
||||
? Optional<DateTime>.None()
|
||||
: Optional<DateTime>.Some(resolvedLastActivityOnUtc == DateTime.MaxValue
|
||||
? DateTime.MaxValue
|
||||
: resolvedLastActivityOnUtc.AddMinutes(_faker.Random.Int(0, 60))));
|
||||
Optional<MediaConsumptionRule> ruleResult = MediaConsumptionRule.For(resolvedMediaKind);
|
||||
bool isPageBased = ruleResult.HasValue && ruleResult.Value is PageThresholdConsumptionRule;
|
||||
|
||||
Result<MediaConsumptionRecord> result = MediaConsumptionRecord.Reconstitute(
|
||||
id ?? _mediaConsumptionRecordIdFixture.Create(),
|
||||
userId ?? _userIdFixture.Create(),
|
||||
libraryId ?? _libraryIdFixture.Create(),
|
||||
mediaItemId ?? _mediaItemIdFixture.Create(),
|
||||
resolvedMediaKind,
|
||||
resolvedStatus,
|
||||
resolvedStartedOnUtc,
|
||||
resolvedLastActivityOnUtc,
|
||||
resolvedEndedOnUtc,
|
||||
consumedDuration ?? TimeSpan.FromSeconds(_faker.Random.Int(0, 36000)),
|
||||
ResolveLastPosition(lastPosition, includeLastPosition, ruleResult),
|
||||
lastProgressFraction ?? _faker.Random.Double(0, 1),
|
||||
consecutivePagesRead ?? (isPageBased ? _faker.Random.Int(0, 100) : 0));
|
||||
return result.Value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates multiple <see cref="MediaConsumptionRecord"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="MediaConsumptionRecord"/> instances.</returns>
|
||||
public List<MediaConsumptionRecord> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the last position of the record, keeping it consistent with the position kind of its media kind rule.
|
||||
/// </summary>
|
||||
/// <param name="lastPosition">The explicitly supplied last position, if any.</param>
|
||||
/// <param name="includeLastPosition">Whether to generate a position when none is supplied.</param>
|
||||
/// <param name="ruleResult">The consumption rule of the media kind of the record, if the kind has one.</param>
|
||||
/// <returns>The resolved last position, or no value when the record carries no position.</returns>
|
||||
private Optional<MediaPosition> ResolveLastPosition(Optional<MediaPosition>? lastPosition, bool includeLastPosition, Optional<MediaConsumptionRule> ruleResult)
|
||||
{
|
||||
if (!includeLastPosition)
|
||||
return Optional<MediaPosition>.None();
|
||||
if (lastPosition.HasValue)
|
||||
return lastPosition.Value;
|
||||
if (!ruleResult.HasValue || !ruleResult.Value.PositionKind.HasValue)
|
||||
return Optional<MediaPosition>.None();
|
||||
return Optional<MediaPosition>.Some(_mediaPositionFixture.Create(kind: ruleResult.Value.PositionKind.Value));
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.MediaConsumptionBoundedContext.MediaConsumptionRecordAggregate.ValueObjects;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="MediaConsumptionRecordId"/> value object.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaConsumptionRecordIdFixture
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a random valid <see cref="MediaConsumptionRecordId"/>.
|
||||
/// </summary>
|
||||
/// <param name="value">Optional. The raw value of the consumption record Id.</param>
|
||||
/// <returns>The created <see cref="MediaConsumptionRecordId"/>.</returns>
|
||||
public MediaConsumptionRecordId Create(
|
||||
Guid? value = null)
|
||||
{
|
||||
return MediaConsumptionRecordId.Create(value ?? Guid.NewGuid());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a list of <see cref="MediaConsumptionRecordId"/> instances with randomized test data.
|
||||
/// </summary>
|
||||
/// <param name="count">Number of instances to create.</param>
|
||||
/// <returns>List of configured <see cref="MediaConsumptionRecordId"/> instances.</returns>
|
||||
public List<MediaConsumptionRecordId> CreateMany(int count = 3)
|
||||
{
|
||||
return [.. Enumerable.Range(0, count).Select(_ => Create())];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user