mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-01 18:57:24 +00:00
Covers the provider and reader choices saved with a library, the reconciliation and stale configuration deletion in the provider store, the library type lookups, the ownership and permission checks for update and delete, and the validator behavior for enum names.
334 lines
13 KiB
C#
334 lines
13 KiB
C#
#region ========================================================================= USING =====================================================================================
|
|
using Lumina.Application.Common.DTO.MediaLibrary.Management;
|
|
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
|
|
using Lumina.Application.Core.MediaLibrary.Management.Commands.AddLibrary;
|
|
using Lumina.Contracts.Fixtures.Core.Requests.MediaLibrary.Management;
|
|
using Lumina.Contracts.Requests.MediaLibrary.Management;
|
|
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
|
using System;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
#endregion
|
|
|
|
namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.Management;
|
|
|
|
/// <summary>
|
|
/// Contains unit tests for the <see cref="AddMediaLibraryRequestMapping"/> class.
|
|
/// </summary>
|
|
[ExcludeFromCodeCoverage]
|
|
public class AddMediaLibraryRequestMappingTests
|
|
{
|
|
private readonly AddLibraryRequestFixture _addLibraryRequestFixture = new();
|
|
private readonly LibraryProviderSelectionRequestFixture _libraryProviderSelectionRequestFixture = new();
|
|
private readonly LibraryPathTemplatePartRequestFixture _libraryPathTemplatePartRequestFixture = new();
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenMappingValidRequest_ShouldMapCorrectly()
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(
|
|
title: "My Library",
|
|
libraryType: "Book",
|
|
contentLocations: ["C:/Books", "D:/Media/Books"],
|
|
coverImage: "D:/poster.jpg",
|
|
isEnabled: true,
|
|
isLocked: false,
|
|
canDownloadMetadataFromWeb: true,
|
|
shouldSaveMetadataInMediaDirectories: false,
|
|
shouldSkipUnchangedDirectoriesDuringScan: false,
|
|
isContentOnSsd: true,
|
|
shouldTrackConsumptionProgress: true,
|
|
completionThresholdPercentage: 90
|
|
);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(request.Title, result.Title);
|
|
Assert.Equal(request.LibraryType, result.LibraryType);
|
|
Assert.Equal(request.ContentLocations, result.ContentLocations);
|
|
Assert.Equal(request.CoverImage, result.CoverImage);
|
|
Assert.Equal(request.IsEnabled, result.IsEnabled);
|
|
Assert.Equal(request.IsLocked, result.IsLocked);
|
|
Assert.Equal(request.CanDownloadMetadataFromWeb, result.CanDownloadMetadataFromWeb);
|
|
Assert.Equal(request.ShouldSaveMetadataInMediaDirectories, result.ShouldSaveMetadataInMediaDirectories);
|
|
Assert.Equal(request.IsContentOnSsd, result.IsContentOnSsd);
|
|
Assert.Equal(request.ShouldTrackConsumptionProgress, result.ShouldTrackConsumptionProgress);
|
|
Assert.Equal(request.CompletionThresholdPercentage, result.CompletionThresholdPercentage);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(LibraryType.Book)]
|
|
[InlineData(LibraryType.Movie)]
|
|
[InlineData(LibraryType.TvShow)]
|
|
[InlineData(LibraryType.Music)]
|
|
public void ToCommand_WhenMappingDifferentLibraryTypes_ShouldMapCorrectly(LibraryType libraryType)
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(
|
|
title: "My Library",
|
|
libraryType: libraryType.ToString(),
|
|
contentLocations: ["C:/Media"],
|
|
coverImage: "D:/poster.jpg",
|
|
isEnabled: true,
|
|
isLocked: false,
|
|
canDownloadMetadataFromWeb: true,
|
|
shouldSaveMetadataInMediaDirectories: false,
|
|
shouldSkipUnchangedDirectoriesDuringScan: false
|
|
);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(request.Title, result.Title);
|
|
Assert.Equal(libraryType.ToString(), result.LibraryType);
|
|
Assert.Equal(request.ContentLocations, result.ContentLocations);
|
|
Assert.Equal(request.CoverImage, result.CoverImage);
|
|
Assert.Equal(request.IsEnabled, result.IsEnabled);
|
|
Assert.Equal(request.IsLocked, result.IsLocked);
|
|
Assert.Equal(request.CanDownloadMetadataFromWeb, result.CanDownloadMetadataFromWeb);
|
|
Assert.Equal(request.ShouldSaveMetadataInMediaDirectories, result.ShouldSaveMetadataInMediaDirectories);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenMappingMultipleContentLocations_ShouldMapAllCorrectly()
|
|
{
|
|
// Arrange
|
|
string[] contentLocations =
|
|
[
|
|
"C:/Media/Books",
|
|
"D:/Books",
|
|
"E:/Digital Library/Books",
|
|
"F:/Reading Material"
|
|
];
|
|
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(
|
|
title: "My Library",
|
|
libraryType: "Book",
|
|
contentLocations: contentLocations,
|
|
coverImage: "D:/poster.jpg",
|
|
isEnabled: true,
|
|
isLocked: false,
|
|
canDownloadMetadataFromWeb: true,
|
|
shouldSaveMetadataInMediaDirectories: false,
|
|
shouldSkipUnchangedDirectoriesDuringScan: false
|
|
);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Equal(contentLocations, result.ContentLocations);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenMappingWithNullCover_ShouldMapCorrectly()
|
|
{
|
|
// Arrange
|
|
string[] contentLocations =
|
|
[
|
|
"C:/Media/Books",
|
|
"D:/Books"
|
|
];
|
|
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(
|
|
title: "My Library",
|
|
libraryType: "Book",
|
|
contentLocations: contentLocations,
|
|
coverImage: null,
|
|
isEnabled: true,
|
|
isLocked: false,
|
|
canDownloadMetadataFromWeb: true,
|
|
shouldSaveMetadataInMediaDirectories: false,
|
|
shouldSkipUnchangedDirectoriesDuringScan: false
|
|
);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.NotNull(result);
|
|
Assert.Null(result.CoverImage);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenRequestHasPathTemplateParts_ShouldMapToDtoArray()
|
|
{
|
|
// Arrange
|
|
LibraryPathTemplatePartRequest literalPart = _libraryPathTemplatePartRequestFixture.Create(
|
|
kind: nameof(LibraryPathPartKind.Literal),
|
|
representation: "Various Artists",
|
|
isOptional: false);
|
|
LibraryPathTemplatePartRequest typedPart = _libraryPathTemplatePartRequestFixture.Create(
|
|
kind: nameof(LibraryPathPartKind.Artist),
|
|
representation: "{0}",
|
|
isOptional: true);
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(pathTemplateParts: [literalPart, typedPart]);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.NotNull(result.PathTemplateParts);
|
|
Assert.Equal(2, result.PathTemplateParts.Length);
|
|
Assert.Equal(literalPart.Kind, result.PathTemplateParts[0].Kind);
|
|
Assert.Equal(literalPart.Representation, result.PathTemplateParts[0].Representation);
|
|
Assert.Equal(literalPart.IsOptional, result.PathTemplateParts[0].IsOptional);
|
|
Assert.Equal(typedPart.Kind, result.PathTemplateParts[1].Kind);
|
|
Assert.Equal(typedPart.Representation, result.PathTemplateParts[1].Representation);
|
|
Assert.Equal(typedPart.IsOptional, result.PathTemplateParts[1].IsOptional);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenPathTemplatePartsIsNull_ShouldMapToNull()
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(pathTemplateParts: null);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.Null(result.PathTemplateParts);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenConsumptionSettingsAreSupplied_ShouldMapThem()
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(
|
|
libraryType: "Book",
|
|
shouldTrackConsumptionProgress: true,
|
|
completionThresholdPercentage: 75);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.NotNull(result.ShouldTrackConsumptionProgress);
|
|
Assert.True(result.ShouldTrackConsumptionProgress.Value);
|
|
Assert.Equal(75, result.CompletionThresholdPercentage);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenConsumptionSettingsAreAbsent_ShouldPreserveThemAsNull()
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(
|
|
libraryType: "Book",
|
|
shouldTrackConsumptionProgress: null,
|
|
completionThresholdPercentage: null);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.Null(result.ShouldTrackConsumptionProgress);
|
|
Assert.Null(result.CompletionThresholdPercentage);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenProgressTrackingIsExplicitlyDisabled_ShouldPreserveTheDisabledFlagWithoutThreshold()
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(
|
|
libraryType: "Book",
|
|
shouldTrackConsumptionProgress: false,
|
|
completionThresholdPercentage: null);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.NotNull(result.ShouldTrackConsumptionProgress);
|
|
Assert.False(result.ShouldTrackConsumptionProgress.Value);
|
|
Assert.Null(result.CompletionThresholdPercentage);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenProviderChoicesAreProvided_ShouldMapThemInOrder()
|
|
{
|
|
// Arrange
|
|
LibraryProviderSelectionRequest[] metadataProviders = [_libraryProviderSelectionRequestFixture.Create(isEnabled: true), _libraryProviderSelectionRequestFixture.Create(isEnabled: false)];
|
|
LibraryProviderSelectionRequest[] artworkProviders = [_libraryProviderSelectionRequestFixture.Create(isEnabled: false)];
|
|
LibraryProviderSelectionRequest[] bookReaders = [_libraryProviderSelectionRequestFixture.Create(isEnabled: true)];
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(metadataProviders: metadataProviders, artworkProviders: artworkProviders, bookReaders: bookReaders);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.Equal(metadataProviders.Select(selection => (selection.PluginId, selection.IsEnabled)), result.MetadataProviders!.Select(selection => (selection.PluginId, selection.IsEnabled)));
|
|
Assert.Equal(artworkProviders.Select(selection => (selection.PluginId, selection.IsEnabled)), result.ArtworkProviders!.Select(selection => (selection.PluginId, selection.IsEnabled)));
|
|
Assert.Equal(bookReaders.Select(selection => (selection.PluginId, selection.IsEnabled)), result.BookReaders!.Select(selection => (selection.PluginId, selection.IsEnabled)));
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenProviderChoicesAreNotProvided_ShouldMapThemAsNull()
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(metadataProviders: null, artworkProviders: null, bookReaders: null);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.Null(result.MetadataProviders);
|
|
Assert.Null(result.ArtworkProviders);
|
|
Assert.Null(result.BookReaders);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenAProviderChoiceIsNull_ShouldMapItToAChoiceWithoutPluginId()
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(metadataProviders: [null], artworkProviders: [null], bookReaders: [null]);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
Assert.Equal(Guid.Empty, Assert.Single(result.MetadataProviders!).PluginId);
|
|
Assert.Null(result.MetadataProviders![0].IsEnabled);
|
|
Assert.Equal(Guid.Empty, Assert.Single(result.ArtworkProviders!).PluginId);
|
|
Assert.Equal(Guid.Empty, Assert.Single(result.BookReaders!).PluginId);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenAPathTemplatePartIsNull_ShouldMapItToAPartWithoutKind()
|
|
{
|
|
// Arrange
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(pathTemplateParts: [null]);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
LibraryPathTemplatePartDto part = Assert.Single(result.PathTemplateParts!);
|
|
Assert.Null(part.Kind);
|
|
Assert.Null(part.Representation);
|
|
Assert.False(part.IsOptional);
|
|
}
|
|
|
|
[Fact]
|
|
public void ToCommand_WhenAProviderChoiceHasNoEnabledState_ShouldMapTheStateAsNull()
|
|
{
|
|
// Arrange
|
|
LibraryProviderSelectionRequest choice = _libraryProviderSelectionRequestFixture.Create(includeIsEnabled: false);
|
|
AddLibraryRequest request = _addLibraryRequestFixture.Create(metadataProviders: [choice]);
|
|
|
|
// Act
|
|
AddLibraryCommand result = request.ToCommand();
|
|
|
|
// Assert
|
|
LibraryProviderSelectionDto mappedChoice = Assert.Single(result.MetadataProviders!);
|
|
Assert.Equal(choice.PluginId, mappedChoice.PluginId);
|
|
Assert.Null(mappedChoice.IsEnabled);
|
|
}
|
|
}
|