mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 20:11:10 +00:00
Refactors the update book slice across the stack
This commit is contained in:
1 parent
9a71a6d32b
commit
001146eefc
27 files changed
+4065
-632
No files matched your search
+122
-33
@@ -1,8 +1,17 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBook;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Fixtures.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBook;
|
||||
using Lumina.Contracts.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.Common;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.Core.BoundedContexts.WrittenContentLibraryBoundedContext.BookLibraryAggregate;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
@@ -14,45 +23,125 @@ namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.WrittenConten
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UpdateBookCommandMappingTests
|
||||
{
|
||||
private readonly UpdateBookCommandFixture _commandFixture = new();
|
||||
private readonly UpdateBookCommandFixture _commandBookFixture = new();
|
||||
private readonly BookEntityFixture _bookEntityFixture = new();
|
||||
private readonly IsbnDtoFixture _isbnDtoFixture = new();
|
||||
private readonly BookRatingDtoFixture _bookRatingDtoFixture = new();
|
||||
private readonly GenreDtoFixture _genreDtoFixture = new();
|
||||
private readonly WrittenContentMetadataDtoFixture _writtenContentMetadataDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void ToBookMetadataDto_WhenMappingCompleteCommand_ShouldMapAllPropertiesCorrectly()
|
||||
public void ToDomainEntity_WhenMappingCompleteCommand_ShouldMapAllPropertiesCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCommand command = _commandFixture.Create();
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
UpdateBookCommand command = _commandBookFixture.Create(libraryId: libraryId.ToString(), bookId: bookId.ToString());
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: bookId, libraryId: libraryId, path: "/books/the-book.epub");
|
||||
DateTime createdOnUtc = existingBook.CreatedOnUtc;
|
||||
|
||||
// Act
|
||||
BookMetadataDto result = command.ToBookMetadataDto();
|
||||
Result<Book> result = command.ToDomainEntity(existingBook);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(command.Metadata!.Title, result.Title);
|
||||
Assert.Equal(command.Metadata.OriginalTitle, result.OriginalTitle);
|
||||
Assert.Equal(command.Metadata.Description, result.Description);
|
||||
Assert.Equal(command.Metadata.ReleaseInfo, result.ReleaseInfo);
|
||||
Assert.Equal(command.Metadata.Genres, result.Genres);
|
||||
Assert.Equal(command.Metadata.Tags, result.Tags);
|
||||
Assert.Equal(command.Metadata.Language, result.Language);
|
||||
Assert.Equal(command.Metadata.OriginalLanguage, result.OriginalLanguage);
|
||||
Assert.Equal(command.Metadata.Publisher, result.Publisher);
|
||||
Assert.Equal(command.Metadata.PageCount, result.PageCount);
|
||||
Assert.Equal(command.Format, result.Format);
|
||||
Assert.Equal(command.Edition, result.Edition);
|
||||
Assert.Equal(command.VolumeNumber, result.VolumeNumber);
|
||||
Assert.Equal(command.Series, result.Series);
|
||||
Assert.Equal(command.ASIN, result.ASIN);
|
||||
Assert.Equal(command.GoodreadsId, result.GoodreadsId);
|
||||
Assert.Equal(command.LCCN, result.LCCN);
|
||||
Assert.Equal(command.OCLCNumber, result.OCLCNumber);
|
||||
Assert.Equal(command.OpenLibraryId, result.OpenLibraryId);
|
||||
Assert.Equal(command.LibraryThingId, result.LibraryThingId);
|
||||
Assert.Equal(command.GoogleBooksId, result.GoogleBooksId);
|
||||
Assert.Equal(command.BarnesAndNobleId, result.BarnesAndNobleId);
|
||||
Assert.Equal(command.AppleBooksId, result.AppleBooksId);
|
||||
Assert.Equal(command.ISBNs, result.Isbns);
|
||||
Assert.Equal(command.Contributors, result.Contributors);
|
||||
Assert.Equal(command.Ratings, result.Ratings);
|
||||
Assert.Null(result.CoverImagePath);
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(bookId, result.Value.Id.Value);
|
||||
Assert.Equal(libraryId, result.Value.LibraryId.Value);
|
||||
Assert.Equal(existingBook.Path, result.Value.Path);
|
||||
Assert.Equal(createdOnUtc, result.Value.CreatedOnUtc);
|
||||
Assert.Equal(command.Metadata!.Title, result.Value.Metadata.Title);
|
||||
Assert.Equal(command.Metadata.ReleaseInfo!.OriginalReleaseYear, result.Value.Metadata.ReleaseInfo.OriginalReleaseYear.Value);
|
||||
Assert.Equal(command.Metadata.Genres!.Count, result.Value.Metadata.Genres.Count);
|
||||
Assert.Equal(command.Metadata.Tags!.Count, result.Value.Metadata.Tags.Count);
|
||||
Assert.Equal(command.ISBNs!.Count, result.Value.ISBNs.Count);
|
||||
Assert.Equal(command.Contributors!.Count, result.Value.Contributors.Count);
|
||||
Assert.Equal(command.Ratings!.Count, result.Value.Ratings.Count);
|
||||
Assert.True(result.Value.Format.HasValue);
|
||||
Assert.Equal(command.Format, result.Value.Format.Value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenOptionalPropertiesAreMissing_ShouldMapRequiredPropertiesCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
UpdateBookCommand command = _commandBookFixture.Create(
|
||||
libraryId: libraryId.ToString(),
|
||||
bookId: bookId.ToString(),
|
||||
isbns: [],
|
||||
contributors: [],
|
||||
ratings: [],
|
||||
includeOptionalProperties: false);
|
||||
BookEntity existingBook = _bookEntityFixture.Create(id: bookId, libraryId: libraryId, path: "/books/the-book.epub");
|
||||
|
||||
// Act
|
||||
Result<Book> result = command.ToDomainEntity(existingBook);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(bookId, result.Value.Id.Value);
|
||||
Assert.Equal(libraryId, result.Value.LibraryId.Value);
|
||||
Assert.Equal(existingBook.Path, result.Value.Path);
|
||||
Assert.Empty(result.Value.ISBNs);
|
||||
Assert.Empty(result.Value.Contributors);
|
||||
Assert.Empty(result.Value.Ratings);
|
||||
Assert.False(result.Value.Format.HasValue);
|
||||
Assert.False(result.Value.Edition.HasValue);
|
||||
Assert.False(result.Value.VolumeNumber.HasValue);
|
||||
Assert.False(result.Value.ASIN.HasValue);
|
||||
Assert.False(result.Value.GoodreadsId.HasValue);
|
||||
Assert.False(result.Value.LCCN.HasValue);
|
||||
Assert.False(result.Value.OCLCNumber.HasValue);
|
||||
Assert.False(result.Value.OpenLibraryId.HasValue);
|
||||
Assert.False(result.Value.LibraryThingId.HasValue);
|
||||
Assert.False(result.Value.GoogleBooksId.HasValue);
|
||||
Assert.False(result.Value.BarnesAndNobleId.HasValue);
|
||||
Assert.False(result.Value.AppleBooksId.HasValue);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenRatingCreationFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCommand command = _commandBookFixture.Create(ratings: [_bookRatingDtoFixture.Create(value: -1, maxValue: 5, includeOptionalProperties: false)]);
|
||||
BookEntity existingBook = _bookEntityFixture.Create();
|
||||
|
||||
// Act
|
||||
Result<Book> result = command.ToDomainEntity(existingBook);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains(result.Errors, error => error.Description == Errors.Metadata.RatingValueMustBePositive.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenIsbnCreationFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCommand command = _commandBookFixture.Create(isbns: [_isbnDtoFixture.Create(value: "invalid", format: IsbnFormat.Isbn13)]);
|
||||
BookEntity existingBook = _bookEntityFixture.Create();
|
||||
|
||||
// Act
|
||||
Result<Book> result = command.ToDomainEntity(existingBook);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains(result.Errors, error => error.Description == Errors.WrittenContent.InvalidIsbn13Format.Description);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToDomainEntity_WhenMetadataCreationFails_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookCommand command = _commandBookFixture.Create(metadata: _writtenContentMetadataDtoFixture.Create(genres: [_genreDtoFixture.Create(name: "")]));
|
||||
BookEntity existingBook = _bookEntityFixture.Create();
|
||||
|
||||
// Act
|
||||
Result<Book> result = command.ToDomainEntity(existingBook);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Contains(result.Errors, error => error.Description == Errors.Metadata.GenreNameCannotBeEmpty.Description);
|
||||
}
|
||||
}
|
||||
+21
-42
@@ -1,6 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Application.Core.MediaLibrary.WrittenContentLibrary.BookLibrary.Books.Commands.UpdateBook;
|
||||
using Lumina.Contracts.Fixtures.Core.DTO.MediaContributors;
|
||||
using Lumina.Contracts.Fixtures.Core.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Contracts.Requests.MediaLibrary.WrittenContentLibrary.BookLibrary.Books;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
@@ -18,20 +19,23 @@ namespace Lumina.Application.UnitTests.Common.Mapping.MediaLibrary.WrittenConten
|
||||
public class UpdateBookRequestMappingTests
|
||||
{
|
||||
private readonly UpdateBookRequestFixture _requestFixture = new();
|
||||
private readonly MediaContributorReferenceDtoFixture _mediaContributorReferenceDtoFixture = new();
|
||||
|
||||
[Fact]
|
||||
public void ToCommand_WhenMappingCompleteRequest_ShouldMapAllPropertiesCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
UpdateBookRequest request = _requestFixture.Create(id: bookId);
|
||||
UpdateBookRequest request = _requestFixture.Create();
|
||||
|
||||
// Act
|
||||
UpdateBookCommand result = request.ToCommand();
|
||||
UpdateBookCommand result = request.ToCommand(libraryId.ToString(), bookId.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(bookId, result.Id);
|
||||
Assert.Equal(libraryId.ToString(), result.LibraryId);
|
||||
Assert.Equal(bookId.ToString(), result.BookId);
|
||||
Assert.Equal(request.Metadata, result.Metadata);
|
||||
Assert.Equal(request.Format, result.Format);
|
||||
Assert.Equal(request.Edition, result.Edition);
|
||||
@@ -55,15 +59,16 @@ public class UpdateBookRequestMappingTests
|
||||
public void ToCommand_WhenMappingMinimalRequest_ShouldMapCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
UpdateBookRequest request = _requestFixture.Create(id: bookId, includeOptionalProperties: false);
|
||||
UpdateBookRequest request = _requestFixture.Create(includeOptionalProperties: false);
|
||||
|
||||
// Act
|
||||
UpdateBookCommand result = request.ToCommand();
|
||||
UpdateBookCommand result = request.ToCommand(libraryId.ToString(), bookId.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(bookId, result.Id);
|
||||
Assert.Equal(bookId.ToString(), result.BookId);
|
||||
Assert.NotNull(result.Metadata);
|
||||
Assert.Equal(request.Metadata, result.Metadata);
|
||||
Assert.Null(result.Format);
|
||||
@@ -88,21 +93,20 @@ public class UpdateBookRequestMappingTests
|
||||
public void ToCommand_WhenMappingRequestWithCollections_ShouldMapCollectionsCorrectly()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
Guid bookId = Guid.NewGuid();
|
||||
UpdateBookRequest request = _requestFixture.Create(
|
||||
isbns: [new("978-0-123456-78-9", IsbnFormat.Isbn13)],
|
||||
contributors:
|
||||
[
|
||||
new(
|
||||
new("John Doe", "John Smith Doe"),
|
||||
new("Author", MediaContributorRoleCategory.Author)
|
||||
)
|
||||
_mediaContributorReferenceDtoFixture.Create(role: MediaContributorRole.Author)
|
||||
],
|
||||
ratings: [new(4.5m, 5m, BookRatingSource.Goodreads, 1000)],
|
||||
includeOptionalProperties: false
|
||||
);
|
||||
|
||||
// Act
|
||||
UpdateBookCommand result = request.ToCommand();
|
||||
UpdateBookCommand result = request.ToCommand(libraryId.ToString(), bookId.ToString());
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
@@ -112,41 +116,16 @@ public class UpdateBookRequestMappingTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToCommand_WhenMappingRequestWithNullId_ShouldMapEmptyGuid()
|
||||
public void ToCommand_WhenRouteValuesAreNotParseable_ShouldMapThemAsIs()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookRequest request = _requestFixture.Create(includeId: false);
|
||||
UpdateBookRequest request = _requestFixture.Create(includeOptionalProperties: false);
|
||||
|
||||
// Act
|
||||
UpdateBookCommand result = request.ToCommand();
|
||||
UpdateBookCommand result = request.ToCommand("not-a-library-guid", "not-a-guid");
|
||||
|
||||
// Assert
|
||||
Assert.Equal(Guid.Empty, result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToCommand_WhenMappingRequestWithEmptyId_ShouldMapEmptyGuid()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookRequest request = _requestFixture.Create(rawId: string.Empty);
|
||||
|
||||
// Act
|
||||
UpdateBookCommand result = request.ToCommand();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(Guid.Empty, result.Id);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToCommand_WhenMappingRequestWithUnparseableId_ShouldMapEmptyGuid()
|
||||
{
|
||||
// Arrange
|
||||
UpdateBookRequest request = _requestFixture.Create(rawId: "not-a-guid");
|
||||
|
||||
// Act
|
||||
UpdateBookCommand result = request.ToCommand();
|
||||
|
||||
// Assert
|
||||
Assert.Equal(Guid.Empty, result.Id);
|
||||
Assert.Equal("not-a-library-guid", result.LibraryId);
|
||||
Assert.Equal("not-a-guid", result.BookId);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user