mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 11:11:10 +00:00
Extends the music library listings with covers, sorting and alpha filtering.
This commit is contained in:
1 parent
0019821e87
commit
9d82f283e4
69 files changed
+3613
-268
No files matched your search
+17
@@ -261,6 +261,8 @@ public class AlbumEntityMappingTests
|
||||
Assert.Equal(entity.Id, result.Id);
|
||||
Assert.Equal(entity.ArtistId, result.ArtistId);
|
||||
Assert.Equal(entity.LibraryId, result.LibraryId);
|
||||
// No cover path is supplied by default, so the response exposes none.
|
||||
Assert.Null(result.CoverPath);
|
||||
Assert.Equal(entity.Title, result.Metadata.Title);
|
||||
Assert.Equal(entity.OriginalTitle, result.Metadata.OriginalTitle);
|
||||
Assert.Equal(entity.Description, result.Metadata.Description);
|
||||
@@ -346,4 +348,19 @@ public class AlbumEntityMappingTests
|
||||
Assert.Equal(entities[i].Title, results[i].Metadata.Title);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToResponse_WhenCoverPathIsProvided_ShouldMapIt()
|
||||
{
|
||||
// Arrange
|
||||
AlbumEntity entity = _albumEntityFixture.Create();
|
||||
string coverPath = "/media/albums/cover.jpg";
|
||||
|
||||
// Act
|
||||
AlbumResponse result = entity.ToResponse(coverPath);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(coverPath, result.CoverPath);
|
||||
}
|
||||
}
|
||||
+6
@@ -36,6 +36,9 @@ public class AlbumLiteRowMappingTests
|
||||
Assert.Equal(readModel.Id, result.Id);
|
||||
Assert.Equal(readModel.Title, result.Title);
|
||||
Assert.Equal(readModel.TotalTracks, result.TotalTracks);
|
||||
Assert.Equal(readModel.CoverPath, result.CoverPath);
|
||||
Assert.Equal(readModel.ReleaseYear, result.ReleaseYear);
|
||||
Assert.Equal(readModel.ReleaseTypes, result.ReleaseTypes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -53,6 +56,9 @@ public class AlbumLiteRowMappingTests
|
||||
Assert.Equal(readModels.Select(readModel => readModel.Id), results.Select(response => response.Id));
|
||||
Assert.Equal(readModels.Select(readModel => readModel.Title), results.Select(response => response.Title));
|
||||
Assert.Equal(readModels.Select(readModel => readModel.TotalTracks), results.Select(response => response.TotalTracks));
|
||||
Assert.Equal(readModels.Select(readModel => readModel.CoverPath), results.Select(response => response.CoverPath));
|
||||
Assert.Equal(readModels.Select(readModel => readModel.ReleaseYear), results.Select(response => response.ReleaseYear));
|
||||
Assert.Equal(readModels.Select(readModel => readModel.ReleaseTypes), results.Select(response => response.ReleaseTypes));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+17
@@ -113,6 +113,8 @@ public class ArtistEntityMappingTests
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(entity.Id, result.Id);
|
||||
Assert.Equal(entity.LibraryId, result.LibraryId);
|
||||
// No cover path is supplied by default, so the response exposes none.
|
||||
Assert.Null(result.CoverPath);
|
||||
Assert.Equal(entity.Name, result.Metadata.Name);
|
||||
Assert.Equal(entity.Website, result.Website);
|
||||
Assert.Equal(entity.MusicBrainzArtistId, result.MusicBrainzArtistId);
|
||||
@@ -186,4 +188,19 @@ public class ArtistEntityMappingTests
|
||||
Assert.Equal(paginatedEntities.Count, result.Count);
|
||||
Assert.Equal(paginatedEntities.NumberOfPages, result.NumberOfPages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToResponse_WhenCoverPathIsProvided_ShouldMapIt()
|
||||
{
|
||||
// Arrange
|
||||
ArtistEntity entity = _artistEntityFixture.Create();
|
||||
string coverPath = "/media/artists/cover.jpg";
|
||||
|
||||
// Act
|
||||
ArtistResponse result = entity.ToResponse(coverPath);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(coverPath, result.CoverPath);
|
||||
}
|
||||
}
|
||||
+2
@@ -35,6 +35,7 @@ public class ArtistLiteRowMappingTests
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(readModel.Id, result.Id);
|
||||
Assert.Equal(readModel.Name, result.Name);
|
||||
Assert.Equal(readModel.CoverPath, result.CoverPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -51,6 +52,7 @@ public class ArtistLiteRowMappingTests
|
||||
Assert.Equal(readModels.Count, results.Count);
|
||||
Assert.Equal(readModels.Select(readModel => readModel.Id), results.Select(response => response.Id));
|
||||
Assert.Equal(readModels.Select(readModel => readModel.Name), results.Select(response => response.Name));
|
||||
Assert.Equal(readModels.Select(readModel => readModel.CoverPath), results.Select(response => response.CoverPath));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+21
@@ -3,6 +3,7 @@ using Lumina.Application.Common.Mapping.MediaLibrary.AudioLibrary.MusicLibrary.A
|
||||
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Queries.GetArtistsLite;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
@@ -99,4 +100,24 @@ public class GetArtistsLiteRequestMappingTests
|
||||
Assert.Equal(2, result.PaginationData.CurrentPage);
|
||||
Assert.Equal(25, result.PaginationData.PerPage);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToQuery_WhenAlphaAndSortingAreProvided_ShouldMapThem()
|
||||
{
|
||||
// Arrange
|
||||
GetArtistsLiteRequest request = _getArtistsLiteRequestFixture.Create(
|
||||
filterAlphaKey: "q",
|
||||
shouldIgnoreThePrefixForAlphaPicker: true,
|
||||
sortBy: "name",
|
||||
sortOrder: SortOrder.Descending);
|
||||
|
||||
// Act
|
||||
GetArtistsLiteQuery result = request.ToQuery("library-route");
|
||||
|
||||
// Assert
|
||||
Assert.Equal("q", result.FilterAlphaKey);
|
||||
Assert.True(result.ShouldIgnoreThePrefixForAlphaPicker);
|
||||
Assert.Equal("name", result.SortBy);
|
||||
Assert.Equal(SortOrder.Descending, result.SortOrder);
|
||||
}
|
||||
}
|
||||
+1
@@ -37,6 +37,7 @@ public class TrackLiteRowMappingTests
|
||||
Assert.Equal(readModel.Title, result.Title);
|
||||
Assert.Equal(readModel.TrackNumber, result.TrackNumber);
|
||||
Assert.Equal(readModel.DiscNumber, result.DiscNumber);
|
||||
Assert.Equal(readModel.DurationInSeconds, result.DurationInSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+132
-1
@@ -12,8 +12,11 @@ using Lumina.Application.Fixtures.Core.MediaLibrary.AudioLibrary.MusicLibrary.Al
|
||||
using Lumina.Contracts.Responses.MediaLibrary.AudioLibrary.MusicLibrary.Albums;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -30,12 +33,14 @@ public class GetAlbumQueryHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IAlbumRepository _mockAlbumRepository;
|
||||
private readonly IMusicArtworkRepository _mockMusicArtworkRepository;
|
||||
private readonly IAuthorizationService _mockAuthorizationService;
|
||||
private readonly ICurrentUserService _mockCurrentUserService;
|
||||
private readonly IValidator<GetAlbumQuery> _mockValidator;
|
||||
private readonly GetAlbumQueryHandler _sut;
|
||||
private readonly Guid _userId;
|
||||
private readonly AlbumEntityFixture _albumEntityFixture = new();
|
||||
private readonly MusicArtworkEntityFixture _musicArtworkEntityFixture = new();
|
||||
private readonly GetAlbumQueryFixture _getAlbumQueryFixture = new();
|
||||
|
||||
/// <summary>
|
||||
@@ -44,18 +49,22 @@ public class GetAlbumQueryHandlerTests
|
||||
public GetAlbumQueryHandlerTests()
|
||||
{
|
||||
_mockAlbumRepository = Substitute.For<IAlbumRepository>();
|
||||
_mockMusicArtworkRepository = Substitute.For<IMusicArtworkRepository>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.AlbumRepository.Returns(_mockAlbumRepository);
|
||||
_mockUnitOfWork.MusicArtworkRepository.Returns(_mockMusicArtworkRepository);
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
|
||||
_mockValidator = Substitute.For<IValidator<GetAlbumQuery>>();
|
||||
_userId = Guid.NewGuid();
|
||||
|
||||
// Default stubs: the current user is authenticated, the library ownership policy allows access and the validator passes.
|
||||
// Default stubs: the current user is authenticated, the library ownership policy allows access, the validator passes, and the album has no stored artwork.
|
||||
_mockCurrentUserService.UserId.Returns(_userId);
|
||||
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
|
||||
.Returns(true);
|
||||
_mockValidator.Validate(Arg.Any<GetAlbumQuery>()).Returns([]);
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(Arg.Any<MusicArtworkOwnerType>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>([]));
|
||||
|
||||
_sut = new GetAlbumQueryHandler(_mockUnitOfWork, _mockAuthorizationService, _mockCurrentUserService, _mockValidator);
|
||||
}
|
||||
@@ -70,6 +79,13 @@ public class GetAlbumQueryHandlerTests
|
||||
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, libraryId: libraryId, includeTracks: true, includeMetadata: true);
|
||||
_mockAlbumRepository.GetByIdAsync(albumId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<AlbumEntity?>(album));
|
||||
List<MusicArtworkEntity> artwork = [
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Cover, ordinal: 0, fileName: "/media/cover.jpg"),
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Back, ordinal: 0, fileName: "/media/back.jpg"),
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Booklet, ordinal: 0, includeFileName: false)
|
||||
];
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>(artwork));
|
||||
GetAlbumQuery query = _getAlbumQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString(), albumId: albumId.ToString());
|
||||
|
||||
// Act
|
||||
@@ -83,6 +99,13 @@ public class GetAlbumQueryHandlerTests
|
||||
Assert.Equal(album.Title, result.Value.Metadata.Title);
|
||||
Assert.NotNull(result.Value.Tracks);
|
||||
Assert.Equal(album.Tracks.Count, result.Value.Tracks!.Count);
|
||||
// The cover is exposed on its own, and every stored piece of artwork is exposed in order, while the ones whose file is not stored yet are left out.
|
||||
Assert.Equal("/media/cover.jpg", result.Value.CoverPath);
|
||||
Assert.Equal(2, result.Value.Artwork.Count);
|
||||
Assert.Equal(ArtworkType.Cover, result.Value.Artwork[0].Type);
|
||||
Assert.Equal("/media/cover.jpg", result.Value.Artwork[0].Path);
|
||||
Assert.Equal(ArtworkType.Back, result.Value.Artwork[1].Type);
|
||||
Assert.Equal("/media/back.jpg", result.Value.Artwork[1].Path);
|
||||
await _mockAlbumRepository.Received(1).GetByIdAsync(albumId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>());
|
||||
await _mockAuthorizationService.Received(1).EvaluatePolicyAsync<ILibraryOwnershipPolicy>(
|
||||
_userId, Arg.Is<LibraryOwnershipPolicyContext>(context => context.LibraryId == libraryId), Arg.Any<CancellationToken>());
|
||||
@@ -220,4 +243,112 @@ public class GetAlbumQueryHandlerTests
|
||||
await _mockAlbumRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenArtworkRepositoryFails_ShouldReturnFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
Guid albumId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, libraryId: libraryId);
|
||||
GetAlbumQuery query = _getAlbumQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString(), albumId: albumId.ToString());
|
||||
_mockAlbumRepository.GetByIdAsync(albumId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<AlbumEntity?>(album));
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
|
||||
.Returns(ApplicationErrors.Persistence.UniqueConstraintViolation);
|
||||
|
||||
// Act
|
||||
Result<AlbumResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Persistence.UniqueConstraintViolation, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenAlbumHasNoArtwork_ShouldReturnNullCoverPathAndEmptyArtwork()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
Guid albumId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, libraryId: libraryId);
|
||||
GetAlbumQuery query = _getAlbumQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString(), albumId: albumId.ToString());
|
||||
_mockAlbumRepository.GetByIdAsync(albumId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<AlbumEntity?>(album));
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>([]));
|
||||
|
||||
// Act
|
||||
Result<AlbumResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Null(result.Value.CoverPath);
|
||||
Assert.Empty(result.Value.Artwork);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenMultipleArtworkExist_ShouldMapEveryStoredArtworkAndExcludeUnstoredOnes()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
Guid albumId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, libraryId: libraryId);
|
||||
GetAlbumQuery query = _getAlbumQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString(), albumId: albumId.ToString());
|
||||
_mockAlbumRepository.GetByIdAsync(albumId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<AlbumEntity?>(album));
|
||||
List<MusicArtworkEntity> artwork = [
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Cover, ordinal: 0, fileName: "/media/cover.jpg"),
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Booklet, ordinal: 1, includeFileName: false),
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Booklet, ordinal: 2, fileName: string.Empty),
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Booklet, ordinal: 3, fileName: " "),
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Back, ordinal: 0, fileName: "/media/back.jpg")
|
||||
];
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>(artwork));
|
||||
|
||||
// Act
|
||||
Result<AlbumResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
// Artwork whose file has not been stored yet (null, empty or whitespace) is left out, and the stored ones keep their order, type and ordinal.
|
||||
Assert.Equal(2, result.Value.Artwork.Count);
|
||||
Assert.Equal(ArtworkType.Cover, result.Value.Artwork[0].Type);
|
||||
Assert.Equal(0, result.Value.Artwork[0].Ordinal);
|
||||
Assert.Equal("/media/cover.jpg", result.Value.Artwork[0].Path);
|
||||
Assert.Equal(ArtworkType.Back, result.Value.Artwork[1].Type);
|
||||
Assert.Equal(0, result.Value.Artwork[1].Ordinal);
|
||||
Assert.Equal("/media/back.jpg", result.Value.Artwork[1].Path);
|
||||
await _mockMusicArtworkRepository.Received(1).GetByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenMultipleCoversExist_ShouldUseTheFirstCoverFileName()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
Guid albumId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(id: albumId, artistId: artistId, libraryId: libraryId);
|
||||
GetAlbumQuery query = _getAlbumQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString(), albumId: albumId.ToString());
|
||||
_mockAlbumRepository.GetByIdAsync(albumId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<AlbumEntity?>(album));
|
||||
List<MusicArtworkEntity> artwork = [
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Cover, ordinal: 0, fileName: "/media/front.jpg"),
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: albumId, artworkType: ArtworkType.Cover, ordinal: 1, fileName: "/media/alternate.jpg")
|
||||
];
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Album, albumId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>(artwork));
|
||||
|
||||
// Act
|
||||
Result<AlbumResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("/media/front.jpg", result.Value.CoverPath);
|
||||
}
|
||||
}
|
||||
+103
-1
@@ -12,8 +12,11 @@ using Lumina.Application.Fixtures.Core.MediaLibrary.AudioLibrary.MusicLibrary.Ar
|
||||
using Lumina.Contracts.Responses.MediaLibrary.AudioLibrary.MusicLibrary.Artists;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.AudioLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
@@ -30,12 +33,14 @@ public class GetArtistQueryHandlerTests
|
||||
{
|
||||
private readonly IUnitOfWork _mockUnitOfWork;
|
||||
private readonly IArtistRepository _mockArtistRepository;
|
||||
private readonly IMusicArtworkRepository _mockMusicArtworkRepository;
|
||||
private readonly IAuthorizationService _mockAuthorizationService;
|
||||
private readonly ICurrentUserService _mockCurrentUserService;
|
||||
private readonly IValidator<GetArtistQuery> _mockValidator;
|
||||
private readonly GetArtistQueryHandler _sut;
|
||||
private readonly Guid _userId;
|
||||
private readonly ArtistEntityFixture _artistEntityFixture = new();
|
||||
private readonly MusicArtworkEntityFixture _musicArtworkEntityFixture = new();
|
||||
private readonly GetArtistQueryFixture _getArtistQueryFixture = new();
|
||||
|
||||
/// <summary>
|
||||
@@ -44,18 +49,22 @@ public class GetArtistQueryHandlerTests
|
||||
public GetArtistQueryHandlerTests()
|
||||
{
|
||||
_mockArtistRepository = Substitute.For<IArtistRepository>();
|
||||
_mockMusicArtworkRepository = Substitute.For<IMusicArtworkRepository>();
|
||||
_mockUnitOfWork = Substitute.For<IUnitOfWork>();
|
||||
_mockUnitOfWork.ArtistRepository.Returns(_mockArtistRepository);
|
||||
_mockUnitOfWork.MusicArtworkRepository.Returns(_mockMusicArtworkRepository);
|
||||
_mockAuthorizationService = Substitute.For<IAuthorizationService>();
|
||||
_mockCurrentUserService = Substitute.For<ICurrentUserService>();
|
||||
_mockValidator = Substitute.For<IValidator<GetArtistQuery>>();
|
||||
_userId = Guid.NewGuid();
|
||||
|
||||
// Default stubs: the current user is authenticated, the library ownership policy allows access and the validator passes.
|
||||
// Default stubs: the current user is authenticated, the library ownership policy allows access, the validator passes, and the artist has no stored artwork.
|
||||
_mockCurrentUserService.UserId.Returns(_userId);
|
||||
_mockAuthorizationService.EvaluatePolicyAsync<ILibraryOwnershipPolicy>(_userId, Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>())
|
||||
.Returns(true);
|
||||
_mockValidator.Validate(Arg.Any<GetArtistQuery>()).Returns([]);
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(Arg.Any<MusicArtworkOwnerType>(), Arg.Any<Guid>(), Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>([]));
|
||||
|
||||
_sut = new GetArtistQueryHandler(_mockUnitOfWork, _mockAuthorizationService, _mockCurrentUserService, _mockValidator);
|
||||
}
|
||||
@@ -199,4 +208,97 @@ public class GetArtistQueryHandlerTests
|
||||
await _mockArtistRepository.DidNotReceive().GetByIdAsync(Arg.Any<Guid>(), Arg.Any<bool>(), Arg.Any<bool>(), Arg.Any<CancellationToken>());
|
||||
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenArtworkRepositoryFails_ShouldReturnFailureResult()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
ArtistEntity artist = _artistEntityFixture.Create(id: artistId, libraryId: libraryId);
|
||||
GetArtistQuery query = _getArtistQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString());
|
||||
_mockArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ArtistEntity?>(artist));
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, Arg.Any<CancellationToken>())
|
||||
.Returns(ApplicationErrors.Persistence.UniqueConstraintViolation);
|
||||
|
||||
// Act
|
||||
Result<ArtistResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(ApplicationErrors.Persistence.UniqueConstraintViolation, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenArtistHasNoArtwork_ShouldReturnNullCoverPath()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
ArtistEntity artist = _artistEntityFixture.Create(id: artistId, libraryId: libraryId);
|
||||
GetArtistQuery query = _getArtistQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString());
|
||||
_mockArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ArtistEntity?>(artist));
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>([]));
|
||||
|
||||
// Act
|
||||
Result<ArtistResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Null(result.Value.CoverPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenArtistHasCoverArtwork_ShouldMapCoverPathAndRequestArtistArtwork()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
ArtistEntity artist = _artistEntityFixture.Create(id: artistId, libraryId: libraryId);
|
||||
GetArtistQuery query = _getArtistQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString());
|
||||
_mockArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ArtistEntity?>(artist));
|
||||
List<MusicArtworkEntity> artwork = [
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Artist, ownerId: artistId, artworkType: ArtworkType.Cover, ordinal: 0, fileName: "/media/artist.jpg")
|
||||
];
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>(artwork));
|
||||
|
||||
// Act
|
||||
Result<ArtistResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal("/media/artist.jpg", result.Value.CoverPath);
|
||||
await _mockMusicArtworkRepository.Received(1).GetByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenTheCoverHasNoStoredFileName_ShouldReturnNullCoverPath()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid artistId = Guid.NewGuid();
|
||||
ArtistEntity artist = _artistEntityFixture.Create(id: artistId, libraryId: libraryId);
|
||||
GetArtistQuery query = _getArtistQueryFixture.Create(libraryId: libraryId.ToString(), artistId: artistId.ToString());
|
||||
_mockArtistRepository.GetByIdAsync(artistId, shouldIncludeNavigationProperties: true, shouldTrackEntities: false, cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<ArtistEntity?>(artist));
|
||||
// A cover whose file has not been stored yet yields no cover path, even when another piece of artwork is already stored.
|
||||
List<MusicArtworkEntity> artwork = [
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Artist, ownerId: artistId, artworkType: ArtworkType.Cover, ordinal: 0, includeFileName: false),
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Artist, ownerId: artistId, artworkType: ArtworkType.Back, ordinal: 0, fileName: "/media/back.jpg")
|
||||
];
|
||||
_mockMusicArtworkRepository.GetByOwnerAsync(MusicArtworkOwnerType.Artist, artistId, Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From<IReadOnlyList<MusicArtworkEntity>>(artwork));
|
||||
|
||||
// Act
|
||||
Result<ArtistResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Null(result.Value.CoverPath);
|
||||
}
|
||||
}
|
||||
+37
@@ -247,4 +247,41 @@ public class GetArtistsLiteQueryHandlerTests
|
||||
await _mockAuthorizationService.DidNotReceive().EvaluatePolicyAsync<ILibraryOwnershipPolicy>(Arg.Any<Guid>(), Arg.Any<LibraryOwnershipPolicyContext>(), Arg.Any<CancellationToken>());
|
||||
await _mockArtistRepository.DidNotReceive().GetAllLiteAsync(Arg.Any<PaginationDataDto?>(), Arg.Any<string?>(), Arg.Any<SortOrder?>(), Arg.Any<LibraryFilterDto>(), cancellationToken: Arg.Any<CancellationToken>());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task HandleAsync_WhenQueryHasAlphaFilterAndSorting_ShouldPassThemToTheRepository()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
GetArtistsLiteQuery query = _getArtistsLiteQueryFixture.Create(
|
||||
libraryId: libraryId.ToString(),
|
||||
searchTerm: "Queen",
|
||||
filterAlphaKey: "Q",
|
||||
shouldIgnoreThePrefixForAlphaPicker: true,
|
||||
sortBy: "name",
|
||||
sortOrder: SortOrder.Descending);
|
||||
PaginatedResultDto<ArtistLiteRow> paginatedArtists = _paginatedResultDtoFixture.Create(data: [], currentPage: 1, perPage: 10, count: 0, numberOfPages: 0);
|
||||
_mockArtistRepository.GetAllLiteAsync(
|
||||
Arg.Any<PaginationDataDto?>(),
|
||||
Arg.Any<string?>(),
|
||||
Arg.Any<SortOrder?>(),
|
||||
Arg.Any<LibraryFilterDto>(),
|
||||
cancellationToken: Arg.Any<CancellationToken>())
|
||||
.Returns(Result.From(paginatedArtists));
|
||||
|
||||
// Act
|
||||
await _sut.HandleAsync(query, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
await _mockArtistRepository.Received(1).GetAllLiteAsync(
|
||||
Arg.Any<PaginationDataDto?>(),
|
||||
Arg.Is<string?>(sortBy => sortBy == "name"),
|
||||
Arg.Is<SortOrder?>(sortOrder => sortOrder == SortOrder.Descending),
|
||||
Arg.Is<LibraryFilterDto>(filter =>
|
||||
filter.LibraryId == libraryId &&
|
||||
filter.SearchTerm == "Queen" &&
|
||||
filter.FilterAlphaKey == "Q" &&
|
||||
filter.ShouldIgnoreThePrefixForAlphaPicker),
|
||||
cancellationToken: Arg.Any<CancellationToken>());
|
||||
}
|
||||
}
|
||||
+152
@@ -1,4 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Queries.GetArtistsLite;
|
||||
using Lumina.Application.Fixtures.Core.MediaLibrary.AudioLibrary.MusicLibrary.Artists.Queries.GetArtistsLite;
|
||||
using Lumina.Application.UnitTests.Common.Setup;
|
||||
@@ -63,4 +64,155 @@ public class GetArtistsLiteQueryValidatorTests
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("AB")] // more than one character
|
||||
[InlineData("1")] // a digit, which is not a letter, the number bucket, or the symbol bucket
|
||||
public void Validate_WhenFilterAlphaKeyIsNotASingleLetterOrSpecialKey_ShouldHaveValidationError(string filterAlphaKey)
|
||||
{
|
||||
// Arrange
|
||||
GetArtistsLiteQuery query = _getArtistsLiteQueryFixture.Create(filterAlphaKey: filterAlphaKey);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Library.InvalidFilterAlphaKey);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("Q")] // a single letter
|
||||
[InlineData("q")] // a single lower case letter
|
||||
[InlineData("#")] // the number bucket
|
||||
[InlineData("*")] // the symbol bucket
|
||||
public void Validate_WhenFilterAlphaKeyIsASingleLetterOrSpecialKey_ShouldNotHaveValidationError(string filterAlphaKey)
|
||||
{
|
||||
// Arrange
|
||||
GetArtistsLiteQuery query = _getArtistsLiteQueryFixture.Create(filterAlphaKey: filterAlphaKey);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Validate_WhenFilterAlphaKeyIsNull_ShouldNotHaveValidationError()
|
||||
{
|
||||
// Arrange
|
||||
GetArtistsLiteQuery query = _getArtistsLiteQueryFixture.Create(filterAlphaKey: null);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(LibraryItemAlphaKeys.NUMBER)] // the number bucket constant
|
||||
[InlineData(LibraryItemAlphaKeys.SYMBOL)] // the symbol bucket constant
|
||||
public void Validate_WhenFilterAlphaKeyIsTheSpecialConstant_ShouldNotHaveValidationError(string filterAlphaKey)
|
||||
{
|
||||
// Arrange
|
||||
GetArtistsLiteQuery query = _getArtistsLiteQueryFixture.Create(filterAlphaKey: filterAlphaKey);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("A")] // upper case A
|
||||
[InlineData("B")] // upper case B
|
||||
[InlineData("C")] // upper case C
|
||||
[InlineData("D")] // upper case D
|
||||
[InlineData("E")] // upper case E
|
||||
[InlineData("F")] // upper case F
|
||||
[InlineData("G")] // upper case G
|
||||
[InlineData("H")] // upper case H
|
||||
[InlineData("I")] // upper case I
|
||||
[InlineData("J")] // upper case J
|
||||
[InlineData("K")] // upper case K
|
||||
[InlineData("L")] // upper case L
|
||||
[InlineData("M")] // upper case M
|
||||
[InlineData("N")] // upper case N
|
||||
[InlineData("O")] // upper case O
|
||||
[InlineData("P")] // upper case P
|
||||
[InlineData("Q")] // upper case Q
|
||||
[InlineData("R")] // upper case R
|
||||
[InlineData("S")] // upper case S
|
||||
[InlineData("T")] // upper case T
|
||||
[InlineData("U")] // upper case U
|
||||
[InlineData("V")] // upper case V
|
||||
[InlineData("W")] // upper case W
|
||||
[InlineData("X")] // upper case X
|
||||
[InlineData("Y")] // upper case Y
|
||||
[InlineData("Z")] // upper case Z
|
||||
[InlineData("a")] // lower case a
|
||||
[InlineData("b")] // lower case b
|
||||
[InlineData("c")] // lower case c
|
||||
[InlineData("d")] // lower case d
|
||||
[InlineData("e")] // lower case e
|
||||
[InlineData("f")] // lower case f
|
||||
[InlineData("g")] // lower case g
|
||||
[InlineData("h")] // lower case h
|
||||
[InlineData("i")] // lower case i
|
||||
[InlineData("j")] // lower case j
|
||||
[InlineData("k")] // lower case k
|
||||
[InlineData("l")] // lower case l
|
||||
[InlineData("m")] // lower case m
|
||||
[InlineData("n")] // lower case n
|
||||
[InlineData("o")] // lower case o
|
||||
[InlineData("p")] // lower case p
|
||||
[InlineData("q")] // lower case q
|
||||
[InlineData("r")] // lower case r
|
||||
[InlineData("s")] // lower case s
|
||||
[InlineData("t")] // lower case t
|
||||
[InlineData("u")] // lower case u
|
||||
[InlineData("v")] // lower case v
|
||||
[InlineData("w")] // lower case w
|
||||
[InlineData("x")] // lower case x
|
||||
[InlineData("y")] // lower case y
|
||||
[InlineData("z")] // lower case z
|
||||
public void Validate_WhenFilterAlphaKeyIsAnySingleAsciiLetter_ShouldNotHaveValidationError(string filterAlphaKey)
|
||||
{
|
||||
// Arrange
|
||||
GetArtistsLiteQuery query = _getArtistsLiteQueryFixture.Create(filterAlphaKey: filterAlphaKey);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldNotHaveAnyValidationErrors();
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("")] // empty string, which is not a single letter or a special key
|
||||
[InlineData("AB")] // two letters
|
||||
[InlineData("ABC")] // several letters
|
||||
[InlineData("1")] // a digit, which is not a letter, a number bucket or a symbol bucket
|
||||
[InlineData("9")] // another digit
|
||||
[InlineData("_")] // an underscore, which is not an ASCII letter
|
||||
[InlineData("é")] // a non-ASCII letter
|
||||
[InlineData("#!")] // the number bucket followed by another character
|
||||
[InlineData("**")] // two symbol buckets, which is not the exact symbol constant
|
||||
[InlineData("#A")] // the number bucket followed by a letter
|
||||
[InlineData("*A")] // the symbol bucket followed by a letter
|
||||
[InlineData(" A")] // a leading space followed by a letter
|
||||
[InlineData("A ")] // a letter followed by a trailing space
|
||||
public void Validate_WhenFilterAlphaKeyIsNotAValidAlphaKey_ShouldHaveValidationError(string filterAlphaKey)
|
||||
{
|
||||
// Arrange
|
||||
GetArtistsLiteQuery query = _getArtistsLiteQueryFixture.Create(filterAlphaKey: filterAlphaKey);
|
||||
|
||||
// Act
|
||||
List<Error> result = _validator.TestValidate(query);
|
||||
|
||||
// Assert
|
||||
result.ShouldHaveValidationError(Errors.Library.InvalidFilterAlphaKey);
|
||||
}
|
||||
}
|
||||
+107
@@ -47,6 +47,7 @@ public class AlbumRepositoryTests
|
||||
private readonly TagEntityFixture _tagEntityFixture = new();
|
||||
private readonly GenreEntityFixture _genreEntityFixture = new();
|
||||
private readonly ArtistEntityFixture _artistEntityFixture = new();
|
||||
private readonly MusicArtworkEntityFixture _musicArtworkEntityFixture = new();
|
||||
private readonly PaginationDataDtoFixture _paginationDataDtoFixture = new();
|
||||
|
||||
/// <summary>
|
||||
@@ -741,6 +742,112 @@ public class AlbumRepositoryTests
|
||||
Assert.Equal(12, row.TotalTracks);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlbumsLiteByArtistIdAsync_WhenReReleaseYearIsSet_ShouldProjectItAsReleaseYear()
|
||||
{
|
||||
// Arrange
|
||||
Guid artistId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(
|
||||
artistId: artistId, originalReleaseYear: 1975, reReleaseYear: 2011, includeTracks: false, includeMetadata: false);
|
||||
_mockContext.Albums.Add(album);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = _paginationDataDtoFixture.Create(currentPage: 1, perPage: 10);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<AlbumLiteRow>> result = await _sut.GetAlbumsLiteByArtistIdAsync(artistId, paginationData, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
AlbumLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.Equal(2011, row.ReleaseYear);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlbumsLiteByArtistIdAsync_WhenReReleaseYearIsNull_ShouldFallBackToOriginalReleaseYear()
|
||||
{
|
||||
// Arrange
|
||||
Guid artistId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(
|
||||
artistId: artistId, originalReleaseYear: 1975, includeReReleaseYear: false, includeTracks: false, includeMetadata: false);
|
||||
_mockContext.Albums.Add(album);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = _paginationDataDtoFixture.Create(currentPage: 1, perPage: 10);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<AlbumLiteRow>> result = await _sut.GetAlbumsLiteByArtistIdAsync(artistId, paginationData, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
AlbumLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.Equal(1975, row.ReleaseYear);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlbumsLiteByArtistIdAsync_WhenTheReleaseHasTypes_ShouldProjectThem()
|
||||
{
|
||||
// Arrange
|
||||
Guid artistId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(
|
||||
artistId: artistId, releaseType: MusicReleaseType.Single, includeTracks: false, includeMetadata: false);
|
||||
_mockContext.Albums.Add(album);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = _paginationDataDtoFixture.Create(currentPage: 1, perPage: 10);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<AlbumLiteRow>> result = await _sut.GetAlbumsLiteByArtistIdAsync(artistId, paginationData, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
AlbumLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.NotNull(row.ReleaseTypes);
|
||||
Assert.Equal(MusicReleaseType.Single, Assert.Single(row.ReleaseTypes));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlbumsLiteByArtistIdAsync_WhenCoverArtworkExists_ShouldProjectTheCoverPath()
|
||||
{
|
||||
// Arrange
|
||||
Guid artistId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(artistId: artistId, includeTracks: false, includeMetadata: false);
|
||||
_mockContext.Albums.Add(album);
|
||||
_mockContext.MusicArtwork.Add(
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Album, ownerId: album.Id, artworkType: ArtworkType.Cover, fileName: "covers/opera.jpg"));
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = _paginationDataDtoFixture.Create(currentPage: 1, perPage: 10);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<AlbumLiteRow>> result = await _sut.GetAlbumsLiteByArtistIdAsync(artistId, paginationData, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
AlbumLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.Equal("covers/opera.jpg", row.CoverPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlbumsLiteByArtistIdAsync_WhenNoCoverArtworkExists_ShouldProjectNullCoverPath()
|
||||
{
|
||||
// Arrange
|
||||
Guid artistId = Guid.NewGuid();
|
||||
AlbumEntity album = _albumEntityFixture.Create(artistId: artistId, includeTracks: false, includeMetadata: false);
|
||||
_mockContext.Albums.Add(album);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = _paginationDataDtoFixture.Create(currentPage: 1, perPage: 10);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<AlbumLiteRow>> result = await _sut.GetAlbumsLiteByArtistIdAsync(artistId, paginationData, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
AlbumLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.Null(row.CoverPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAlbumsNeedingMetadataCountAsync_WhenAlbumsHaveMixedMetadataStatuses_ShouldCountOnlyTheNotEnrichedOnesOfTheLibrary()
|
||||
{
|
||||
|
||||
+109
@@ -53,6 +53,7 @@ public class ArtistRepositoryTests
|
||||
private readonly PaginationDataDtoFixture _paginationDataDtoFixture = new();
|
||||
private readonly LibraryFilterDtoFixture _libraryFilterDtoFixture = new();
|
||||
private readonly BaseFilterDtoFixture _baseFilterDtoFixture = new();
|
||||
private readonly MusicArtworkEntityFixture _musicArtworkEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ArtistRepositoryTests"/> class.
|
||||
@@ -1041,6 +1042,28 @@ public class ArtistRepositoryTests
|
||||
Assert.Equal([betaArtist.Id, alphaArtist.Id], result.Value.Data.Select(artist => artist.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllLiteAsync_WhenIgnoringTheNamePrefixForSorting_ShouldSortByNameStrippedOfItsPrefix()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
ArtistEntity beneathArtist = _artistEntityFixture.Create(libraryId: libraryId, name: "Beneath the Surface", includeAlbums: false, includeContributors: false);
|
||||
ArtistEntity beatlesArtist = _artistEntityFixture.Create(libraryId: libraryId, name: "The Beatles", includeAlbums: false, includeContributors: false);
|
||||
_mockContext.Artists.AddRange(beneathArtist, beatlesArtist);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = _paginationDataDtoFixture.Create(currentPage: 1, perPage: 10);
|
||||
LibraryFilterDto filter = _libraryFilterDtoFixture.Create(libraryId: libraryId, shouldIgnoreThePrefixForAlphaPicker: true);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<ArtistLiteRow>> result = await _sut.GetAllLiteAsync(paginationData, null, null, filter, cancellationToken: CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
// Without stripping the prefix, "The Beatles" would be sorted after "Beneath the Surface", since its sort key would start with "the" instead of "beatles".
|
||||
Assert.Equal(["The Beatles", "Beneath the Surface"], result.Value.Data.Select(row => row.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllLiteAsync_WhenCalled_ShouldProjectOnlyTheIdAndTheName()
|
||||
{
|
||||
@@ -1202,6 +1225,92 @@ public class ArtistRepositoryTests
|
||||
Assert.Equal(1, result.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllLiteAsync_WhenCoverArtworkExists_ShouldReturnItsPath()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
ArtistEntity artist = _artistEntityFixture.Create(libraryId: libraryId, name: "Queen", includeAlbums: false, includeContributors: false);
|
||||
_mockContext.Artists.Add(artist);
|
||||
_mockContext.MusicArtwork.Add(
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Artist, ownerId: artist.Id, artworkType: ArtworkType.Cover, fileName: "covers/queen.jpg"));
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
LibraryFilterDto filter = _libraryFilterDtoFixture.Create(libraryId: libraryId);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<ArtistLiteRow>> result = await _sut.GetAllLiteAsync(null, null, null, filter, cancellationToken: CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
ArtistLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.Equal("covers/queen.jpg", row.CoverPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllLiteAsync_WhenOnlyNonCoverArtworkExists_ShouldReturnNullCoverPath()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
ArtistEntity artist = _artistEntityFixture.Create(libraryId: libraryId, name: "Queen", includeAlbums: false, includeContributors: false);
|
||||
_mockContext.Artists.Add(artist);
|
||||
_mockContext.MusicArtwork.Add(
|
||||
_musicArtworkEntityFixture.Create(ownerType: MusicArtworkOwnerType.Artist, ownerId: artist.Id, artworkType: ArtworkType.Front, fileName: "front/queen.jpg"));
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
LibraryFilterDto filter = _libraryFilterDtoFixture.Create(libraryId: libraryId);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<ArtistLiteRow>> result = await _sut.GetAllLiteAsync(null, null, null, filter, cancellationToken: CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
ArtistLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.Null(row.CoverPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllLiteAsync_WhenNoCoverArtworkExists_ShouldReturnNullCoverPath()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
ArtistEntity artist = _artistEntityFixture.Create(libraryId: libraryId, name: "Queen", includeAlbums: false, includeContributors: false);
|
||||
_mockContext.Artists.Add(artist);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
LibraryFilterDto filter = _libraryFilterDtoFixture.Create(libraryId: libraryId);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<ArtistLiteRow>> result = await _sut.GetAllLiteAsync(null, null, null, filter, cancellationToken: CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
ArtistLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.Null(row.CoverPath);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllLiteAsync_WhenNotIgnoringTheNamePrefixForSorting_ShouldKeepThePrefix()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
ArtistEntity beneathArtist = _artistEntityFixture.Create(libraryId: libraryId, name: "Beneath the Surface", includeAlbums: false, includeContributors: false);
|
||||
ArtistEntity beatlesArtist = _artistEntityFixture.Create(libraryId: libraryId, name: "The Beatles", includeAlbums: false, includeContributors: false);
|
||||
_mockContext.Artists.AddRange(beneathArtist, beatlesArtist);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = _paginationDataDtoFixture.Create(currentPage: 1, perPage: 10);
|
||||
LibraryFilterDto filter = _libraryFilterDtoFixture.Create(libraryId: libraryId, shouldIgnoreThePrefixForAlphaPicker: false);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<ArtistLiteRow>> result = await _sut.GetAllLiteAsync(paginationData, null, null, filter, cancellationToken: CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
// The prefix is not stripped, so the sort key of "The Beatles" starts with "the" and it sorts after "Beneath the Surface".
|
||||
Assert.Equal(["Beneath the Surface", "The Beatles"], result.Value.Data.Select(row => row.Name));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks the provided artist, its albums and its tracks as enriched.
|
||||
/// </summary>
|
||||
|
||||
+64
-1
@@ -637,7 +637,7 @@ public class TrackRepositoryTests
|
||||
|
||||
List<TrackEntity> albumTracks = [firstDiscFirstTrack, firstDiscFirstTieTrack, firstDiscSecondTrack, secondDiscTrack];
|
||||
Guid[] expectedOrder = [.. albumTracks
|
||||
.OrderBy(track => track.DiscNumber)
|
||||
.OrderBy(track => track.DiscNumber ?? 1)
|
||||
.ThenBy(track => track.TrackNumber)
|
||||
.ThenBy(track => track.Id)
|
||||
.Select(track => track.Id)];
|
||||
@@ -651,6 +651,27 @@ public class TrackRepositoryTests
|
||||
Assert.DoesNotContain(result.Value, track => track.Id == trackOfAnotherAlbum.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByAlbumIdAsync_WhenATrackHasNoDiscNumber_ShouldTreatItAsTheFirstDisc()
|
||||
{
|
||||
// Arrange
|
||||
Guid albumId = Guid.NewGuid();
|
||||
TrackEntity secondDiscTrack = _trackEntityFixture.Create(albumId: albumId, path: "/music/queen/second-disc.flac", trackNumber: 1, includeMetadata: false);
|
||||
secondDiscTrack.DiscNumber = 2;
|
||||
TrackEntity firstDiscTrackWithoutDiscNumber = _trackEntityFixture.Create(albumId: albumId, path: "/music/queen/first-disc.flac", trackNumber: 2, includeMetadata: false);
|
||||
firstDiscTrackWithoutDiscNumber.DiscNumber = null;
|
||||
_mockContext.Tracks.AddRange(secondDiscTrack, firstDiscTrackWithoutDiscNumber);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
Result<IReadOnlyList<TrackEntity>> result = await _sut.GetByAlbumIdAsync(albumId, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
// A track without a disc number belongs to the first disc, so it is ordered before the tracks of disc 2.
|
||||
Assert.Equal([firstDiscTrackWithoutDiscNumber.Id, secondDiscTrack.Id], result.Value.Select(track => track.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetByAlbumIdAsync_WhenCalled_ShouldIncludeTheRelatedCollections()
|
||||
{
|
||||
@@ -707,6 +728,26 @@ public class TrackRepositoryTests
|
||||
Assert.Equal([firstTrack.Id, secondTrack.Id], page.Data.Select(row => row.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetTracksLiteByAlbumIdAsync_WhenATrackHasNoDiscNumber_ShouldTreatItAsTheFirstDisc()
|
||||
{
|
||||
// Arrange
|
||||
Guid albumId = Guid.NewGuid();
|
||||
TrackEntity thirdTrack = _trackEntityFixture.Create(albumId: albumId, path: "/music/queen/third.flac", trackNumber: 3, includeMetadata: false);
|
||||
thirdTrack.DiscNumber = 1;
|
||||
TrackEntity secondTrackWithoutDiscNumber = _trackEntityFixture.Create(albumId: albumId, path: "/music/queen/second.flac", trackNumber: 2, includeMetadata: false);
|
||||
secondTrackWithoutDiscNumber.DiscNumber = null;
|
||||
_mockContext.Tracks.AddRange(thirdTrack, secondTrackWithoutDiscNumber);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<TrackLiteRow>> result = await _sut.GetTracksLiteByAlbumIdAsync(albumId, null, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal([secondTrackWithoutDiscNumber.Id, thirdTrack.Id], result.Value.Data.Select(row => row.Id));
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1, 2, 1, 2)] // The first page is fully returned.
|
||||
[InlineData(2, 2, 2, 2)] // A middle page is fully returned.
|
||||
@@ -792,6 +833,28 @@ public class TrackRepositoryTests
|
||||
Assert.Equal("Killer Queen", row.Title);
|
||||
Assert.Equal(9, row.TrackNumber);
|
||||
Assert.Equal(3, row.DiscNumber);
|
||||
Assert.Equal(track.DurationInSeconds, row.DurationInSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetTracksLiteByAlbumIdAsync_WhenCalled_ShouldProjectTheDurationInSeconds()
|
||||
{
|
||||
// Arrange
|
||||
Guid albumId = Guid.NewGuid();
|
||||
TrackEntity track = _trackEntityFixture.Create(albumId: albumId, path: "/music/queen/the-prophet-song.flac", includeMetadata: false);
|
||||
track.DurationInSeconds = 367;
|
||||
_mockContext.Tracks.Add(track);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = _paginationDataDtoFixture.Create(currentPage: 1, perPage: 10);
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<TrackLiteRow>> result = await _sut.GetTracksLiteByAlbumIdAsync(albumId, paginationData, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
TrackLiteRow row = Assert.Single(result.Value.Data);
|
||||
Assert.Equal(367, row.DurationInSeconds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
Reference in new issue
Block a user