Adds the test suites for the library path templates and centralizes their resolution in the domain service

This commit is contained in:
BinaryAlley committed 2026-10-05 20:15:00 +03:00
1 parent ca4f308b0a
commit 7ed6f05632
52 files changed
+4724 -40

No files matched your search

@@ -0,0 +1,155 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
/// <summary>
/// Contains unit tests for the <see cref="LibraryPathPart"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class LibraryPathPartTests
{
private readonly LibraryPathPartFixture _libraryPathPartFixture = new();
[Fact]
public void Create_WhenKindIsSeparator_ShouldCreateSeparatorWithoutRepresentation()
{
// Act
Result<LibraryPathPart> result = LibraryPathPart.Create(LibraryPathPartKind.Separator, "/", isOptional: false);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(LibraryPathPartKind.Separator, result.Value.Kind);
Assert.Equal(string.Empty, result.Value.Representation);
}
[Fact]
public void Create_WhenKindIsLiteralWithText_ShouldCreateLiteralWithThatRepresentation()
{
// Act
Result<LibraryPathPart> result = LibraryPathPart.Create(LibraryPathPartKind.Literal, " - ", isOptional: false);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(LibraryPathPartKind.Literal, result.Value.Kind);
Assert.Equal(" - ", result.Value.Representation);
}
[Theory]
[InlineData(null)] // the representation is absent
[InlineData("")] // the representation is empty
[InlineData(" ")] // the representation is whitespace
public void Create_WhenKindIsNotSeparatorAndRepresentationIsEmpty_ShouldReturnLiteralCannotBeEmptyError(string? representation)
{
// Act
Result<LibraryPathPart> result = LibraryPathPart.Create(LibraryPathPartKind.Literal, representation, isOptional: false);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Lumina.Domain.Common.Errors.Errors.Library.PathTemplateLiteralCannotBeEmpty, result.FirstError);
}
[Fact]
public void Create_WhenTypedPartContainsSinglePlaceholder_ShouldCreateThePart()
{
// Act
Result<LibraryPathPart> result = LibraryPathPart.Create(LibraryPathPartKind.Artist, "{0}", isOptional: false);
// Assert
Assert.False(result.IsFailure);
Assert.Equal("{0}", result.Value.Representation);
}
[Fact]
public void Create_WhenTypedPartContainsFixedWidthPlaceholder_ShouldCreateThePart()
{
// Act
Result<LibraryPathPart> result = LibraryPathPart.Create(LibraryPathPartKind.TrackNumber, "Disc {0:00}", isOptional: true);
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Disc {0:00}", result.Value.Representation);
Assert.True(result.Value.IsOptional);
}
[Theory]
[InlineData("no placeholder")] // the mask carries no value placeholder
[InlineData("{0} {0}")] // the mask carries more than one value placeholder
public void Create_WhenTypedPartDoesNotContainExactlyOnePlaceholder_ShouldReturnError(string representation)
{
// Act
Result<LibraryPathPart> result = LibraryPathPart.Create(LibraryPathPartKind.Artist, representation, isOptional: false);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Lumina.Domain.Common.Errors.Errors.Library.PathTemplateValuePartMustContainSinglePlaceholder, result.FirstError);
}
[Fact]
public void Create_WhenLiteralContainsNoPlaceholder_ShouldCreateThePart()
{
// Act
Result<LibraryPathPart> result = LibraryPathPart.Create(LibraryPathPartKind.Literal, "Disk ", isOptional: false);
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Disk ", result.Value.Representation);
}
[Fact]
public void Equals_WithSameValues_ShouldReturnTrue()
{
// Arrange
LibraryPathPart firstPart = _libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: true);
// Act
LibraryPathPart secondPart = _libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: true);
// Assert
Assert.Equal(firstPart, secondPart);
}
[Fact]
public void Equals_WithDifferentKind_ShouldReturnFalse()
{
// Arrange
LibraryPathPart firstPart = _libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
// Act
LibraryPathPart secondPart = _libraryPathPartFixture.Create(kind: LibraryPathPartKind.ReleaseName, representation: "{0}", isOptional: false);
// Assert
Assert.NotEqual(firstPart, secondPart);
}
[Fact]
public void Equals_WithDifferentRepresentation_ShouldReturnFalse()
{
// Arrange
LibraryPathPart firstPart = _libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}", isOptional: false);
// Act
LibraryPathPart secondPart = _libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "Artist: {0}", isOptional: false);
// Assert
Assert.NotEqual(firstPart, secondPart);
}
[Fact]
public void Equals_WithDifferentIsOptional_ShouldReturnFalse()
{
// Arrange
LibraryPathPart firstPart = _libraryPathPartFixture.Create(kind: LibraryPathPartKind.DiscNumber, representation: "{0:00}", isOptional: false);
// Act
LibraryPathPart secondPart = _libraryPathPartFixture.Create(kind: LibraryPathPartKind.DiscNumber, representation: "{0:00}", isOptional: true);
// Assert
Assert.NotEqual(firstPart, secondPart);
}
}
@@ -0,0 +1,94 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
/// <summary>
/// Contains unit tests for the <see cref="LibraryPathTemplate"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class LibraryPathTemplateTests
{
private readonly LibraryPathTemplateFixture _libraryPathTemplateFixture = new();
[Fact]
public void Create_WhenCalledWithParts_ShouldCreateTemplateWithThoseParts()
{
// Arrange
LibraryPathTemplate template = _libraryPathTemplateFixture.Create();
// Act
LibraryPathTemplate result = LibraryPathTemplate.Create(template.Parts);
// Assert
Assert.Equal(template.Parts, result.Parts);
Assert.False(result.IsEmpty);
}
[Fact]
public void Empty_WhenCalled_ShouldCreateTemplateWithoutParts()
{
// Act
LibraryPathTemplate result = LibraryPathTemplate.Empty();
// Assert
Assert.Empty(result.Parts);
Assert.True(result.IsEmpty);
}
[Fact]
public void Create_WhenSourceCollectionIsLaterModified_ShouldNotAffectTheTemplate()
{
// Arrange
List<LibraryPathPart> sourceParts = [.. _libraryPathTemplateFixture.Create().Parts];
int originalCount = sourceParts.Count;
// Act
LibraryPathTemplate template = LibraryPathTemplate.Create(sourceParts);
sourceParts.Clear();
// Assert
Assert.Equal(originalCount, template.Parts.Count);
}
[Fact]
public void Equals_WithSameValues_ShouldReturnTrue()
{
// Arrange
LibraryPathTemplate firstTemplate = _libraryPathTemplateFixture.Create();
// Act
LibraryPathTemplate secondTemplate = LibraryPathTemplate.Create(firstTemplate.Parts);
// Assert
Assert.Equal(firstTemplate, secondTemplate);
}
[Fact]
public void Equals_WithDifferentParts_ShouldReturnFalse()
{
// Arrange
LibraryPathTemplate firstTemplate = _libraryPathTemplateFixture.Create();
// Act
LibraryPathTemplate secondTemplate = _libraryPathTemplateFixture.Create();
// Assert
Assert.NotEqual(firstTemplate, secondTemplate);
}
[Fact]
public void Equals_WhenBothTemplatesAreEmpty_ShouldReturnTrue()
{
// Act
LibraryPathTemplate firstTemplate = LibraryPathTemplate.Empty();
LibraryPathTemplate secondTemplate = LibraryPathTemplate.Empty();
// Assert
Assert.Equal(firstTemplate, secondTemplate);
}
}
@@ -0,0 +1,497 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.PathTemplate;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using NSubstitute;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.PathTemplate;
/// <summary>
/// Contains unit tests for the <see cref="LibraryPathTemplateService"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class LibraryPathTemplateServiceTests
{
private readonly ILibraryPathPartCatalog _mockCatalog = Substitute.For<ILibraryPathPartCatalog>();
private readonly LibraryPathTemplateService _sut;
private readonly LibraryPathPartFixture _libraryPathPartFixture = new();
private readonly LibraryPathTemplateFixture _libraryPathTemplateFixture = new();
private readonly LibraryPathPartDefinitionFixture _libraryPathPartDefinitionFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="LibraryPathTemplateServiceTests"/> class.
/// </summary>
public LibraryPathTemplateServiceTests()
{
_mockCatalog.SupportedLibraryType.Returns(LibraryType.Music);
_sut = new LibraryPathTemplateService([_mockCatalog]);
}
[Fact]
public void GetDefaultTemplate_WhenCatalogExists_ShouldReturnTheCatalogDefaultTemplate()
{
// Arrange
LibraryPathTemplate defaultTemplate = _libraryPathTemplateFixture.Create();
_mockCatalog.GetDefaultTemplate().Returns(defaultTemplate);
// Act
LibraryPathTemplate result = _sut.GetDefaultTemplate(LibraryType.Music);
// Assert
Assert.Equal(defaultTemplate, result);
}
[Fact]
public void GetDefaultTemplate_WhenNoCatalogExistsForTheLibraryType_ShouldReturnAnEmptyTemplate()
{
// Act
LibraryPathTemplate result = _sut.GetDefaultTemplate(LibraryType.Book);
// Assert
Assert.True(result.IsEmpty);
}
[Fact]
public void ResolveTemplate_WhenProvidedPartsAreEmpty_ShouldReturnTheCatalogDefaultTemplate()
{
// Arrange
SetupCatalog();
LibraryPathTemplate defaultTemplate = CreateMusicTemplate();
_mockCatalog.GetDefaultTemplate().Returns(defaultTemplate);
// Act
Result<LibraryPathTemplate> result = _sut.ResolveTemplate(LibraryType.Music, []);
// Assert
Assert.False(result.IsFailure);
Assert.Equal(defaultTemplate, result.Value);
_mockCatalog.Received(1).GetDefaultTemplate();
}
[Fact]
public void ResolveTemplate_WhenProvidedPartsAreEmptyAndNoCatalogExists_ShouldReturnAnEmptyTemplate()
{
// Act
Result<LibraryPathTemplate> result = _sut.ResolveTemplate(LibraryType.Book, []);
// Assert
Assert.False(result.IsFailure);
Assert.True(result.Value.IsEmpty);
}
[Fact]
public void ResolveTemplate_WhenProvidedPartsAreNotEmpty_ShouldReturnATemplateWithThoseParts()
{
// Arrange
SetupCatalog();
LibraryPathPart providedPart = _libraryPathPartFixture.Create(
kind: LibraryPathPartKind.Artist,
representation: "{0}",
isOptional: false);
// Act
Result<LibraryPathTemplate> result = _sut.ResolveTemplate(LibraryType.Music, [providedPart]);
// Assert
Assert.False(result.IsFailure);
LibraryPathPart pathPart = Assert.Single(result.Value.Parts);
Assert.Equal(LibraryPathPartKind.Artist, pathPart.Kind);
Assert.Equal("{0}", pathPart.Representation);
Assert.False(pathPart.IsOptional);
_mockCatalog.DidNotReceive().GetDefaultTemplate();
}
[Fact]
public void ResolveTemplate_WhenProvidedPartsAreNotSupportedByTheCatalog_ShouldReturnError()
{
// Arrange
SetupCatalog(
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Artist, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Separator, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty)
]);
LibraryPathPart releaseNamePart = _libraryPathPartFixture.Create(
kind: LibraryPathPartKind.ReleaseName,
representation: "{0}",
isOptional: false);
// Act
Result<LibraryPathTemplate> result = _sut.ResolveTemplate(LibraryType.Music, [releaseNamePart]);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Library.PathTemplatePartKindNotSupportedForLibraryType, result.FirstError);
}
[Fact]
public void ResolveTemplate_WhenNoCatalogExistsForTheLibraryTypeAndPartsAreProvided_ShouldReturnNotSupportedError()
{
// Arrange
LibraryPathPart artistPart = _libraryPathPartFixture.Create(
kind: LibraryPathPartKind.Artist,
representation: "{0}",
isOptional: false);
// Act
Result<LibraryPathTemplate> result = _sut.ResolveTemplate(LibraryType.Book, [artistPart]);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Library.PathTemplateIsNotSupportedForLibraryType, result.FirstError);
}
[Fact]
public void Validate_WhenTemplateIsEmpty_ShouldReturnSuccess()
{
// Act
Result<Success> result = _sut.Validate(LibraryType.Music, LibraryPathTemplate.Empty());
// Assert
Assert.False(result.IsFailure);
}
[Fact]
public void Validate_WhenTemplateIsEmptyAndNoCatalogExists_ShouldReturnSuccess()
{
// Act
Result<Success> result = _sut.Validate(LibraryType.Book, LibraryPathTemplate.Empty());
// Assert
Assert.False(result.IsFailure);
}
[Fact]
public void Validate_WhenNoCatalogExistsForTheLibraryType_ShouldReturnNotSupportedError()
{
// Act
Result<Success> result = _sut.Validate(LibraryType.Book, CreateMusicTemplate());
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Library.PathTemplateIsNotSupportedForLibraryType, result.FirstError);
}
[Fact]
public void Validate_WhenPartKindIsNotSupportedByTheCatalog_ShouldReturnError()
{
// Arrange
SetupCatalog(definitions:
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Artist, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Separator, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.TrackName, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}")
]);
LibraryPathTemplate template = _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Separator, representation: string.Empty),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.ReleaseName, representation: "{0}")
]);
// Act
Result<Success> result = _sut.Validate(LibraryType.Music, template);
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Library.PathTemplatePartKindNotSupportedForLibraryType, result.FirstError);
}
[Fact]
public void Validate_WhenAllPartKindsAreSupportedByTheCatalog_ShouldReturnSuccess()
{
// Arrange
SetupCatalog();
LibraryPathTemplate template = CreateMusicTemplate();
// Act
Result<Success> result = _sut.Validate(LibraryType.Music, template);
// Assert
Assert.False(result.IsFailure);
}
[Theory]
[InlineData("")] // the relative path is empty
[InlineData(" ")] // the relative path is whitespace
public void Parse_WhenRelativePathIsEmpty_ShouldReturnNull(string relativePath)
{
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, CreateMusicTemplate(), relativePath, '/');
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public void Parse_WhenTemplateIsEmpty_ShouldReturnNull()
{
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, LibraryPathTemplate.Empty(), "Pink Floyd/1973 - Time.mp3", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public void Parse_WhenTemplateIsNotSupportedForTheLibraryType_ShouldReturnError()
{
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Book, CreateMusicTemplate(), "Pink Floyd/1973 - Time.mp3", '/');
// Assert
Assert.True(result.IsFailure);
Assert.Equal(Errors.Library.PathTemplateIsNotSupportedForLibraryType, result.FirstError);
}
[Fact]
public void Parse_WhenPathMatchesTheTemplate_ShouldReturnTheCapturedValues()
{
// Arrange
SetupCatalog();
LibraryPathTemplate template = CreateMusicTemplate();
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "Pink Floyd/1973 - Time.mp3", '/');
// Assert
Assert.False(result.IsFailure);
ParsedLibraryPath parsedPath = result.Value!;
Assert.Equal("Pink Floyd", parsedPath.GetString(LibraryPathPartKind.Artist).Value);
Assert.Equal(1973, parsedPath.GetInt(LibraryPathPartKind.ReleaseYear).Value);
Assert.Equal("Time", parsedPath.GetString(LibraryPathPartKind.TrackName).Value);
Assert.Equal("mp3", parsedPath.GetString(LibraryPathPartKind.Extension).Value);
}
[Fact]
public void Parse_WhenPathDoesNotMatchTheTemplate_ShouldReturnNull()
{
// Arrange
SetupCatalog();
LibraryPathTemplate template = CreateMusicTemplate();
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "Pink Floyd/no year/Time.mp3", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public void Parse_WhenIntegerPartHasAFixedWidth_ShouldCaptureIt()
{
// Arrange
SetupCatalog(
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Artist, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Separator, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.TrackNumber, valueType: LibraryPathValueType.Integer, defaultRepresentation: "{0:00}")
]);
LibraryPathTemplate template = _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Separator, representation: string.Empty),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.TrackNumber, representation: "{0:00}")
]);
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "Pink Floyd/07", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Equal(7, result.Value!.GetInt(LibraryPathPartKind.TrackNumber).Value);
}
[Fact]
public void Parse_WhenIntegerPartDoesNotMatchTheFixedWidth_ShouldReturnNull()
{
// Arrange
SetupCatalog(
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Artist, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Separator, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.TrackNumber, valueType: LibraryPathValueType.Integer, defaultRepresentation: "{0:00}")
]);
LibraryPathTemplate template = _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Separator, representation: string.Empty),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.TrackNumber, representation: "{0:00}")
]);
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "Pink Floyd/007", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public void Parse_WhenYearPartHasAFourDigitValue_ShouldCaptureIt()
{
// Arrange
SetupCatalog(
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.ReleaseYear, valueType: LibraryPathValueType.Year, defaultRepresentation: "{0}")
]);
LibraryPathTemplate template = _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.ReleaseYear, representation: "{0}")
]);
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "1973", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Equal(1973, result.Value!.GetInt(LibraryPathPartKind.ReleaseYear).Value);
}
[Fact]
public void Parse_WhenYearPartDoesNotHaveAFourDigitValue_ShouldReturnNull()
{
// Arrange
SetupCatalog(
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.ReleaseYear, valueType: LibraryPathValueType.Year, defaultRepresentation: "{0}")
]);
LibraryPathTemplate template = _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.ReleaseYear, representation: "{0}")
]);
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "73", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Null(result.Value);
}
[Fact]
public void Parse_WhenAnOptionalRunIsAbsent_ShouldStillMatchThePath()
{
// Arrange
SetupCatalog(
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Artist, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Separator, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.DiscNumber, valueType: LibraryPathValueType.Integer, defaultRepresentation: "{0:00}")
]);
LibraryPathTemplate template = _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Separator, representation: string.Empty),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.DiscNumber, representation: "{0:00}", isOptional: true)
]);
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "Pink Floyd/", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Pink Floyd", result.Value!.GetString(LibraryPathPartKind.Artist).Value);
Assert.False(result.Value.GetInt(LibraryPathPartKind.DiscNumber).HasValue);
}
[Fact]
public void Parse_WhenAnOptionalRunIsPresent_ShouldCaptureIt()
{
// Arrange
SetupCatalog(
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Artist, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Separator, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.DiscNumber, valueType: LibraryPathValueType.Integer, defaultRepresentation: "{0:00}")
]);
LibraryPathTemplate template = _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Separator, representation: string.Empty),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.DiscNumber, representation: "{0:00}", isOptional: true)
]);
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "Pink Floyd/02", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Pink Floyd", result.Value!.GetString(LibraryPathPartKind.Artist).Value);
Assert.Equal(2, result.Value.GetInt(LibraryPathPartKind.DiscNumber).Value);
}
[Fact]
public void Parse_WhenTheSameKindAppearsMoreThanOnce_ShouldKeepTheFirstCapture()
{
// Arrange
SetupCatalog(
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Author, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Separator, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Title, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Literal, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty)
]);
LibraryPathTemplate template = _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Author, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Separator, representation: string.Empty),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Title, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Literal, representation: " - "),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Author, representation: "{0}")
]);
// Act
Result<ParsedLibraryPath?> result = _sut.Parse(LibraryType.Music, template, "Tolkien/The Lord of the Rings - J.R.R. Tolkien", '/');
// Assert
Assert.False(result.IsFailure);
Assert.Equal("Tolkien", result.Value!.GetString(LibraryPathPartKind.Author).Value);
}
/// <summary>
/// Sets up the mock catalog to return a music-like set of part definitions and a default template.
/// </summary>
/// <param name="definitions">Optional. The part definitions the catalog returns. When omitted, a standard music set is used.</param>
private void SetupCatalog(IReadOnlyList<LibraryPathPartDefinition>? definitions = null)
{
_mockCatalog.GetPartDefinitions().Returns(definitions ??
[
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Literal, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Artist, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Separator, valueType: LibraryPathValueType.None, defaultRepresentation: string.Empty),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.ReleaseYear, valueType: LibraryPathValueType.Year, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.TrackNumber, valueType: LibraryPathValueType.Integer, defaultRepresentation: "{0:00}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.TrackName, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}"),
_libraryPathPartDefinitionFixture.Create(kind: LibraryPathPartKind.Extension, valueType: LibraryPathValueType.Text, defaultRepresentation: "{0}")
]);
}
/// <summary>
/// Creates a music-like path template whose parts are all supported by the standard music set of definitions.
/// </summary>
/// <returns>The created path template.</returns>
private LibraryPathTemplate CreateMusicTemplate()
{
return _libraryPathTemplateFixture.Create(
[
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Artist, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Separator, representation: string.Empty),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.ReleaseYear, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Literal, representation: " - "),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.TrackName, representation: "{0}"),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Literal, representation: "."),
_libraryPathPartFixture.Create(kind: LibraryPathPartKind.Extension, representation: "{0}")
]);
}
}
@@ -0,0 +1,129 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
/// <summary>
/// Contains unit tests for the <see cref="LibraryPathPartDefinition"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class LibraryPathPartDefinitionTests
{
private readonly LibraryPathPartDefinitionFixture _libraryPathPartDefinitionFixture = new();
[Fact]
public void Constructor_WhenCalled_ShouldSetAllProperties()
{
// Act
LibraryPathPartDefinition definition = new(LibraryPathPartKind.TrackNumber, LibraryPathValueType.Integer, "{0:00}", isOptionalByDefault: true);
// Assert
Assert.Equal(LibraryPathPartKind.TrackNumber, definition.Kind);
Assert.Equal(LibraryPathValueType.Integer, definition.ValueType);
Assert.Equal("{0:00}", definition.DefaultRepresentation);
Assert.True(definition.IsOptionalByDefault);
}
[Fact]
public void Equals_WithSameValues_ShouldReturnTrue()
{
// Arrange
LibraryPathPartDefinition firstDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.Artist,
valueType: LibraryPathValueType.Text,
defaultRepresentation: "{0}",
isOptionalByDefault: false);
// Act
LibraryPathPartDefinition secondDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.Artist,
valueType: LibraryPathValueType.Text,
defaultRepresentation: "{0}",
isOptionalByDefault: false);
// Assert
Assert.Equal(firstDefinition, secondDefinition);
}
[Fact]
public void Equals_WithDifferentKind_ShouldReturnFalse()
{
// Arrange
LibraryPathPartDefinition firstDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.Artist,
valueType: LibraryPathValueType.Text,
defaultRepresentation: "{0}");
// Act
LibraryPathPartDefinition secondDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.ReleaseName,
valueType: LibraryPathValueType.Text,
defaultRepresentation: "{0}");
// Assert
Assert.NotEqual(firstDefinition, secondDefinition);
}
[Fact]
public void Equals_WithDifferentValueType_ShouldReturnFalse()
{
// Arrange
LibraryPathPartDefinition firstDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.TrackNumber,
valueType: LibraryPathValueType.Integer,
defaultRepresentation: "{0}");
// Act
LibraryPathPartDefinition secondDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.TrackNumber,
valueType: LibraryPathValueType.Text,
defaultRepresentation: "{0}");
// Assert
Assert.NotEqual(firstDefinition, secondDefinition);
}
[Fact]
public void Equals_WithDifferentDefaultRepresentation_ShouldReturnFalse()
{
// Arrange
LibraryPathPartDefinition firstDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.ReleaseYear,
valueType: LibraryPathValueType.Year,
defaultRepresentation: "{0}");
// Act
LibraryPathPartDefinition secondDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.ReleaseYear,
valueType: LibraryPathValueType.Year,
defaultRepresentation: "Year {0}");
// Assert
Assert.NotEqual(firstDefinition, secondDefinition);
}
[Fact]
public void Equals_WithDifferentIsOptionalByDefault_ShouldReturnFalse()
{
// Arrange
LibraryPathPartDefinition firstDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.DiscNumber,
valueType: LibraryPathValueType.Integer,
defaultRepresentation: "{0:00}",
isOptionalByDefault: false);
// Act
LibraryPathPartDefinition secondDefinition = _libraryPathPartDefinitionFixture.Create(
kind: LibraryPathPartKind.DiscNumber,
valueType: LibraryPathValueType.Integer,
defaultRepresentation: "{0:00}",
isOptionalByDefault: true);
// Assert
Assert.NotEqual(firstDefinition, secondDefinition);
}
}
@@ -0,0 +1,230 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Domain.Common.Primitives;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Domain.UnitTests.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
/// <summary>
/// Contains unit tests for the <see cref="ParsedLibraryPath"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ParsedLibraryPathTests
{
private readonly ParsedLibraryPathFixture _parsedLibraryPathFixture = new();
[Fact]
public void Create_WhenCalledWithValues_ShouldExposeThoseValues()
{
// Arrange
Dictionary<LibraryPathPartKind, string> values = new()
{
[LibraryPathPartKind.Artist] = "Pink Floyd",
[LibraryPathPartKind.TrackName] = "Time"
};
// Act
ParsedLibraryPath result = ParsedLibraryPath.Create(values);
// Assert
Assert.Equal(2, result.Values.Count);
Assert.Equal("Pink Floyd", result.Values[LibraryPathPartKind.Artist]);
Assert.Equal("Time", result.Values[LibraryPathPartKind.TrackName]);
}
[Fact]
public void GetString_WhenKindIsCaptured_ShouldReturnTheTrimmedValue()
{
// Arrange
ParsedLibraryPath parsedPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Artist] = " Pink Floyd "
});
// Act
Optional<string> result = parsedPath.GetString(LibraryPathPartKind.Artist);
// Assert
Assert.True(result.HasValue);
Assert.Equal("Pink Floyd", result.Value);
}
[Fact]
public void GetString_WhenKindIsNotCaptured_ShouldReturnNoValue()
{
// Arrange
ParsedLibraryPath parsedPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Artist] = "Pink Floyd"
});
// Act
Optional<string> result = parsedPath.GetString(LibraryPathPartKind.TrackName);
// Assert
Assert.False(result.HasValue);
}
[Fact]
public void GetString_WhenCapturedValueIsWhitespace_ShouldReturnNoValue()
{
// Arrange
ParsedLibraryPath parsedPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Artist] = " "
});
// Act
Optional<string> result = parsedPath.GetString(LibraryPathPartKind.Artist);
// Assert
Assert.False(result.HasValue);
}
[Fact]
public void GetInt_WhenKindIsCapturedWithAnInteger_ShouldReturnTheValue()
{
// Arrange
ParsedLibraryPath parsedPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.ReleaseYear] = "1973"
});
// Act
Optional<int> result = parsedPath.GetInt(LibraryPathPartKind.ReleaseYear);
// Assert
Assert.True(result.HasValue);
Assert.Equal(1973, result.Value);
}
[Fact]
public void GetInt_WhenKindIsNotCaptured_ShouldReturnNoValue()
{
// Arrange
ParsedLibraryPath parsedPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Artist] = "Pink Floyd"
});
// Act
Optional<int> result = parsedPath.GetInt(LibraryPathPartKind.ReleaseYear);
// Assert
Assert.False(result.HasValue);
}
[Fact]
public void GetInt_WhenCapturedValueIsNotAnInteger_ShouldReturnNoValue()
{
// Arrange
ParsedLibraryPath parsedPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.ReleaseYear] = "not a year"
});
// Act
Optional<int> result = parsedPath.GetInt(LibraryPathPartKind.ReleaseYear);
// Assert
Assert.False(result.HasValue);
}
[Fact]
public void ToDictionary_WhenCalled_ShouldReturnAllCapturedValues()
{
// Arrange
Dictionary<LibraryPathPartKind, string> values = new()
{
[LibraryPathPartKind.Artist] = "Pink Floyd",
[LibraryPathPartKind.TrackName] = "Time"
};
ParsedLibraryPath parsedPath = _parsedLibraryPathFixture.Create(values);
// Act
IReadOnlyDictionary<LibraryPathPartKind, string> result = parsedPath.ToDictionary();
// Assert
Assert.Equal(values, result);
}
[Fact]
public void ToDictionary_WhenCalled_ShouldReturnACopy()
{
// Arrange
Dictionary<LibraryPathPartKind, string> values = new()
{
[LibraryPathPartKind.Artist] = "Pink Floyd"
};
ParsedLibraryPath parsedPath = _parsedLibraryPathFixture.Create(values);
// Act
IReadOnlyDictionary<LibraryPathPartKind, string> result = parsedPath.ToDictionary();
// Assert
Assert.NotSame(values, result);
}
[Fact]
public void Equals_WithSameValuesInDifferentOrder_ShouldReturnTrue()
{
// Arrange
ParsedLibraryPath firstPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Artist] = "Pink Floyd",
[LibraryPathPartKind.TrackName] = "Time"
});
// Act
ParsedLibraryPath secondPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.TrackName] = "Time",
[LibraryPathPartKind.Artist] = "Pink Floyd"
});
// Assert
Assert.Equal(firstPath, secondPath);
}
[Fact]
public void Equals_WithDifferentValues_ShouldReturnFalse()
{
// Arrange
ParsedLibraryPath firstPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Artist] = "Pink Floyd"
});
// Act
ParsedLibraryPath secondPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Artist] = "Queen"
});
// Assert
Assert.NotEqual(firstPath, secondPath);
}
[Fact]
public void Equals_WithDifferentKeys_ShouldReturnFalse()
{
// Arrange
ParsedLibraryPath firstPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Artist] = "Pink Floyd"
});
// Act
ParsedLibraryPath secondPath = _parsedLibraryPathFixture.Create(new Dictionary<LibraryPathPartKind, string>
{
[LibraryPathPartKind.Title] = "Pink Floyd"
});
// Assert
Assert.NotEqual(firstPath, secondPath);
}
}