mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 23:01:10 +00:00
407 lines
16 KiB
C#
407 lines
16 KiB
C#
#region ========================================================================= USING =====================================================================================
|
|
using Nemera.Application.Common.DataAccess.Entities.MediaLibrary.Management;
|
|
using Nemera.Application.Common.Mapping.MediaLibrary.Management;
|
|
using Nemera.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.Management;
|
|
using Nemera.Contracts.Responses.MediaLibrary.Management;
|
|
using Nemera.Domain.Common.Primitives;
|
|
using Nemera.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate;
|
|
using Nemera.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
|
|
using Nemera.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using DomainErrors = Nemera.Domain.Common.Errors.Errors;
|
|
#endregion
|
|
|
|
namespace Nemera.Application.UnitTests.Common.Mapping.MediaLibrary.Management;
|
|
|
|
/// <summary>
|
|
/// Contains unit tests for the <see cref="LibraryEntityMapping"/> class.
|
|
/// </summary>
|
|
[ExcludeFromCodeCoverage]
|
|
public class LibraryEntityMappingTests
|
|
{
|
|
private readonly LibraryEntityFixture _libraryEntityFixture = new();
|
|
private readonly LibraryPathTemplatePartEntityFixture _libraryPathTemplatePartEntityFixture = new();
|
|
|
|
[Fact]
|
|
public void ToResponse_WhenMappingValidLibraryEntity_ShouldMapCorrectly()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: LibraryType.Book,
|
|
contentLocations: ["C:/Books", "D:/Media/Books"],
|
|
coverImage: "D:/myPoster.jpg",
|
|
shouldSkipHiddenFilesAndDirectoriesDuringScan: true,
|
|
isContentOnSsd: true);
|
|
|
|
// Act
|
|
LibraryResponse result = entity.ToResponse();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(entity.Id, result.Id);
|
|
Assert.Equal(entity.UserId, result.UserId);
|
|
Assert.Equal(entity.Title, result.Title);
|
|
Assert.Equal(entity.LibraryType, result.LibraryType);
|
|
Assert.Equal(entity.ContentLocations.Select(l => l.Path), result.ContentLocations);
|
|
Assert.Equal(entity.CoverImage, result.CoverImage);
|
|
Assert.Equal(entity.ShouldSkipHiddenFilesAndDirectoriesDuringScan, result.ShouldSkipHiddenFilesAndDirectoriesDuringScan);
|
|
Assert.Equal(entity.IsContentOnSsd, result.IsContentOnSsd);
|
|
Assert.Equal(entity.ShouldTrackConsumptionProgress, result.ShouldTrackConsumptionProgress);
|
|
Assert.Equal(entity.CompletionThresholdPercentage, result.CompletionThresholdPercentage);
|
|
Assert.Equal(entity.CreatedOnUtc, result.CreatedOnUtc);
|
|
Assert.Equal(entity.UpdatedOnUtc, result.UpdatedOnUtc);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(LibraryType.Book)]
|
|
[InlineData(LibraryType.Movie)]
|
|
[InlineData(LibraryType.TvShow)]
|
|
[InlineData(LibraryType.Music)]
|
|
public void ToResponse_WhenMappingDifferentLibraryTypes_ShouldMapCorrectly(LibraryType libraryType)
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: libraryType,
|
|
contentLocations: ["C:/Media"],
|
|
coverImage: "D:/myPoster.jpg");
|
|
|
|
// Act
|
|
LibraryResponse result = entity.ToResponse();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(libraryType, result.LibraryType);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToResponse_WhenMappingMultipleContentLocations_ShouldMapAllCorrectly()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: LibraryType.Book,
|
|
contentLocations: ["C:/Media/Books", "D:/Books", "E:/Digital Library/Books", "F:/Reading Material"],
|
|
coverImage: "D:/myPoster.jpg");
|
|
|
|
// Act
|
|
LibraryResponse result = entity.ToResponse();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(entity.ContentLocations.Select(l => l.Path), result.ContentLocations);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToResponse_WhenMappingWithUpdatedDateTime_ShouldMapCorrectly()
|
|
{
|
|
// Arrange
|
|
DateTime updated = DateTime.UtcNow.AddDays(-1);
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: LibraryType.Book,
|
|
contentLocations: ["C:/Books"],
|
|
coverImage: "D:/myPoster.jpg");
|
|
entity.UpdatedOnUtc = updated;
|
|
|
|
// Act
|
|
LibraryResponse result = entity.ToResponse();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(updated, result.UpdatedOnUtc);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToResponse_WhenMappingWithNullCoverImage_ShouldMapCorrectly()
|
|
{
|
|
// Arrange
|
|
DateTime updated = DateTime.UtcNow.AddDays(-1);
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: LibraryType.Book,
|
|
contentLocations: ["C:/Books"]);
|
|
entity.CoverImage = null;
|
|
entity.UpdatedOnUtc = updated;
|
|
|
|
// Act
|
|
LibraryResponse result = entity.ToResponse();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Null(result.CoverImage);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToResponse_WhenLibraryHasPathTemplateParts_ShouldOrderByPositionAndMapProperties()
|
|
{
|
|
// Arrange
|
|
LibraryPathTemplatePartEntity firstPart = _libraryPathTemplatePartEntityFixture.Create(
|
|
position: 2,
|
|
kind: LibraryPathPartKind.Artist,
|
|
representation: "{0}",
|
|
isOptional: true);
|
|
LibraryPathTemplatePartEntity secondPart = _libraryPathTemplatePartEntityFixture.Create(
|
|
position: 0,
|
|
kind: LibraryPathPartKind.Literal,
|
|
representation: "Various Artists");
|
|
LibraryPathTemplatePartEntity thirdPart = _libraryPathTemplatePartEntityFixture.Create(
|
|
position: 1,
|
|
kind: LibraryPathPartKind.Separator,
|
|
representation: string.Empty);
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: LibraryType.Book,
|
|
contentLocations: ["C:/Books"],
|
|
pathTemplateParts: [firstPart, secondPart, thirdPart]);
|
|
|
|
// Act
|
|
LibraryResponse result = entity.ToResponse();
|
|
|
|
// Assert
|
|
Assert.Equal(3, result.PathTemplateParts.Count);
|
|
Assert.Equal(LibraryPathPartKind.Literal.ToString(), result.PathTemplateParts[0].Kind);
|
|
Assert.Equal("Various Artists", result.PathTemplateParts[0].Representation);
|
|
Assert.False(result.PathTemplateParts[0].IsOptional);
|
|
Assert.Equal(LibraryPathPartKind.Separator.ToString(), result.PathTemplateParts[1].Kind);
|
|
Assert.Equal(string.Empty, result.PathTemplateParts[1].Representation);
|
|
Assert.False(result.PathTemplateParts[1].IsOptional);
|
|
Assert.Equal(LibraryPathPartKind.Artist.ToString(), result.PathTemplateParts[2].Kind);
|
|
Assert.Equal("{0}", result.PathTemplateParts[2].Representation);
|
|
Assert.True(result.PathTemplateParts[2].IsOptional);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainEntity_WhenLibraryHasPathTemplateParts_ShouldBuildTemplateFromOrderedParts()
|
|
{
|
|
// Arrange
|
|
LibraryPathTemplatePartEntity firstPart = _libraryPathTemplatePartEntityFixture.Create(
|
|
position: 1,
|
|
kind: LibraryPathPartKind.Artist,
|
|
representation: "{0}",
|
|
isOptional: true);
|
|
LibraryPathTemplatePartEntity secondPart = _libraryPathTemplatePartEntityFixture.Create(
|
|
position: 0,
|
|
kind: LibraryPathPartKind.Literal,
|
|
representation: "Various Artists");
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: LibraryType.Book,
|
|
contentLocations: ["C:/Books"],
|
|
coverImage: "D:/cover.jpg",
|
|
shouldSkipHiddenFilesAndDirectoriesDuringScan: true,
|
|
isContentOnSsd: true,
|
|
pathTemplateParts: [firstPart, secondPart]);
|
|
|
|
// Act
|
|
Result<Library> result = entity.ToDomainEntity();
|
|
|
|
// Assert
|
|
Assert.False(result.IsFailure);
|
|
Assert.Equal(entity.Id, result.Value.Id.Value);
|
|
Assert.Equal(entity.UserId, result.Value.UserId.Value);
|
|
Assert.Equal(entity.Title, result.Value.Title);
|
|
Assert.Equal(entity.LibraryType, result.Value.LibraryType);
|
|
Assert.Equal("D:/cover.jpg", result.Value.CoverImage.Value);
|
|
Assert.Equal(entity.ShouldSkipHiddenFilesAndDirectoriesDuringScan, result.Value.ShouldSkipHiddenFilesAndDirectoriesDuringScan);
|
|
Assert.Equal(entity.IsContentOnSsd, result.Value.IsContentOnSsd);
|
|
Assert.Equal(entity.ShouldTrackConsumptionProgress, result.Value.ConsumptionSettings.ShouldTrackConsumptionProgress);
|
|
Assert.Equal(entity.CompletionThresholdPercentage, result.Value.ConsumptionSettings.CompletionThresholdPercentage.HasValue ? result.Value.ConsumptionSettings.CompletionThresholdPercentage.Value : null);
|
|
Assert.Equal(2, result.Value.PathTemplate.Parts.Count);
|
|
Assert.Equal(LibraryPathPartKind.Literal, result.Value.PathTemplate.Parts[0].Kind);
|
|
Assert.Equal("Various Artists", result.Value.PathTemplate.Parts[0].Representation);
|
|
Assert.False(result.Value.PathTemplate.Parts[0].IsOptional);
|
|
Assert.Equal(LibraryPathPartKind.Artist, result.Value.PathTemplate.Parts[1].Kind);
|
|
Assert.Equal("{0}", result.Value.PathTemplate.Parts[1].Representation);
|
|
Assert.True(result.Value.PathTemplate.Parts[1].IsOptional);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainEntity_WhenStoredTypedPartHasEmptyRepresentation_ShouldReturnPathTemplateLiteralCannotBeEmpty()
|
|
{
|
|
// Arrange
|
|
LibraryPathTemplatePartEntity invalidPart = _libraryPathTemplatePartEntityFixture.Create(
|
|
position: 0,
|
|
kind: LibraryPathPartKind.Artist,
|
|
representation: string.Empty);
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: LibraryType.Book,
|
|
contentLocations: ["C:/Books"],
|
|
pathTemplateParts: [invalidPart]);
|
|
|
|
// Act
|
|
Result<Library> result = entity.ToDomainEntity();
|
|
|
|
// Assert
|
|
Assert.True(result.IsFailure);
|
|
Assert.Equal(DomainErrors.Library.PathTemplateLiteralCannotBeEmpty, result.FirstError);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainEntity_WhenStoredTypedPartHasNoPlaceholder_ShouldReturnPathTemplateValuePartMustContainSinglePlaceholder()
|
|
{
|
|
// Arrange
|
|
LibraryPathTemplatePartEntity invalidPart = _libraryPathTemplatePartEntityFixture.Create(
|
|
position: 0,
|
|
kind: LibraryPathPartKind.TrackNumber,
|
|
representation: "Track");
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
title: "My Library",
|
|
libraryType: LibraryType.Book,
|
|
contentLocations: ["C:/Books"],
|
|
pathTemplateParts: [invalidPart]);
|
|
|
|
// Act
|
|
Result<Library> result = entity.ToDomainEntity();
|
|
|
|
// Assert
|
|
Assert.True(result.IsFailure);
|
|
Assert.Equal(DomainErrors.Library.PathTemplateValuePartMustContainSinglePlaceholder, result.FirstError);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToResponse_WhenProgressTrackingIsEnabled_ShouldMapTrackingAndThreshold()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
libraryType: LibraryType.Book,
|
|
shouldTrackConsumptionProgress: true,
|
|
completionThresholdPercentage: 65);
|
|
|
|
// Act
|
|
LibraryResponse result = entity.ToResponse();
|
|
|
|
// Assert
|
|
Assert.True(result.ShouldTrackConsumptionProgress);
|
|
Assert.Equal(65, result.CompletionThresholdPercentage);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToResponse_WhenProgressTrackingIsDisabled_ShouldMapTrackingOffWithoutThreshold()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
libraryType: LibraryType.Book,
|
|
shouldTrackConsumptionProgress: false,
|
|
completionThresholdPercentage: null);
|
|
|
|
// Act
|
|
LibraryResponse result = entity.ToResponse();
|
|
|
|
// Assert
|
|
Assert.False(result.ShouldTrackConsumptionProgress);
|
|
Assert.Null(result.CompletionThresholdPercentage);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainEntity_WhenProgressTrackingIsEnabled_ShouldMapConsumptionSettings()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
libraryType: LibraryType.Book,
|
|
shouldTrackConsumptionProgress: true,
|
|
completionThresholdPercentage: 65);
|
|
|
|
// Act
|
|
Result<Library> result = entity.ToDomainEntity();
|
|
|
|
// Assert
|
|
Assert.False(result.IsFailure);
|
|
Assert.True(result.Value.ConsumptionSettings.ShouldTrackConsumptionProgress);
|
|
Assert.True(result.Value.ConsumptionSettings.CompletionThresholdPercentage.HasValue);
|
|
Assert.Equal(65, result.Value.ConsumptionSettings.CompletionThresholdPercentage.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainEntity_WhenProgressTrackingIsEnabledWithoutThreshold_ShouldMapNoThreshold()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
libraryType: LibraryType.Book,
|
|
shouldTrackConsumptionProgress: true,
|
|
completionThresholdPercentage: null);
|
|
|
|
// Act
|
|
Result<Library> result = entity.ToDomainEntity();
|
|
|
|
// Assert
|
|
Assert.False(result.IsFailure);
|
|
Assert.True(result.Value.ConsumptionSettings.ShouldTrackConsumptionProgress);
|
|
Assert.False(result.Value.ConsumptionSettings.CompletionThresholdPercentage.HasValue);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainEntity_WhenProgressTrackingIsDisabledButStoredThresholdExists_ShouldNotRetainThreshold()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
libraryType: LibraryType.Book,
|
|
shouldTrackConsumptionProgress: false,
|
|
completionThresholdPercentage: 65);
|
|
|
|
// Act
|
|
Result<Library> result = entity.ToDomainEntity();
|
|
|
|
// Assert
|
|
Assert.False(result.IsFailure);
|
|
Assert.False(result.Value.ConsumptionSettings.ShouldTrackConsumptionProgress);
|
|
Assert.False(result.Value.ConsumptionSettings.CompletionThresholdPercentage.HasValue);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainValueObject_WhenProgressTrackingIsEnabled_ShouldMapTrackingAndThreshold()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
libraryType: LibraryType.Book,
|
|
shouldTrackConsumptionProgress: true,
|
|
completionThresholdPercentage: 65);
|
|
|
|
// Act
|
|
LibraryConsumptionSettings result = entity.ToDomainValueObject();
|
|
|
|
// Assert
|
|
Assert.True(result.ShouldTrackConsumptionProgress);
|
|
Assert.True(result.CompletionThresholdPercentage.HasValue);
|
|
Assert.Equal(65, result.CompletionThresholdPercentage.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainValueObject_WhenProgressTrackingIsDisabled_ShouldNotRetainThreshold()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
libraryType: LibraryType.Book,
|
|
shouldTrackConsumptionProgress: false,
|
|
completionThresholdPercentage: 65);
|
|
|
|
// Act
|
|
LibraryConsumptionSettings result = entity.ToDomainValueObject();
|
|
|
|
// Assert
|
|
Assert.False(result.ShouldTrackConsumptionProgress);
|
|
Assert.False(result.CompletionThresholdPercentage.HasValue);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToDomainValueObject_WhenProgressTrackingIsEnabledWithoutThreshold_ShouldMapNoThreshold()
|
|
{
|
|
// Arrange
|
|
LibraryEntity entity = _libraryEntityFixture.Create(
|
|
libraryType: LibraryType.Book,
|
|
shouldTrackConsumptionProgress: true,
|
|
completionThresholdPercentage: null);
|
|
|
|
// Act
|
|
LibraryConsumptionSettings result = entity.ToDomainValueObject();
|
|
|
|
// Assert
|
|
Assert.True(result.ShouldTrackConsumptionProgress);
|
|
Assert.False(result.CompletionThresholdPercentage.HasValue);
|
|
}
|
|
}
|