mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 07:11:10 +00:00
Refactors the book, artist, and album slices across the stack
This commit is contained in:
1 parent
8aaf9fe64e
commit
ea6eb9f796
439 files changed
+28235
-3156
No files matched your search
+47
-20
@@ -11,7 +11,7 @@ using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaContributors;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Fixtures.Common.DTO.Filtering;
|
||||
using Lumina.Application.Fixtures.Common.DTO.Pagination;
|
||||
using Lumina.DataAccess.Core.Repositories.Books;
|
||||
using Lumina.DataAccess.Core.Repositories.BookLibrary;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
@@ -29,7 +29,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Books;
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.BookLibrary;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="BookRepository"/> class.
|
||||
@@ -441,7 +441,7 @@ public class BookRepositoryTests
|
||||
.Include(book => book.Genres)
|
||||
.Include(book => book.ISBNs)
|
||||
.Include(book => book.Ratings)
|
||||
.Include(book => book.BookContributors)
|
||||
.Include(book => book.Contributors)
|
||||
.FirstOrDefaultAsync(book => book.Id == storedBook.Id);
|
||||
Assert.NotNull(retrievedBook);
|
||||
Assert.Equal("Updated Title", retrievedBook!.Title);
|
||||
@@ -455,6 +455,33 @@ public class BookRepositoryTests
|
||||
Assert.Equal(storedCreatedBy, retrievedBook.CreatedBy);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenStoredBookHasArtwork_ShouldPreserveTheStoredArtwork()
|
||||
{
|
||||
// Arrange
|
||||
BookEntity storedBook = _bookEntityFixture.Create();
|
||||
storedBook.Artwork = [_bookArtworkEntityFixture.Create(bookId: storedBook.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Enriched, fileName: "cover.jpg")];
|
||||
_mockContext.Books.Add(storedBook);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
BookEntity data = _bookEntityFixture.Create(id: storedBook.Id);
|
||||
data.Title = "Updated Title";
|
||||
|
||||
// Act
|
||||
Result<Updated> result = await _sut.UpdateAsync(data, CancellationToken.None);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
BookEntity? retrievedBook = await _mockContext.Books
|
||||
.Include(book => book.Artwork)
|
||||
.FirstOrDefaultAsync(book => book.Id == storedBook.Id);
|
||||
Assert.NotNull(retrievedBook);
|
||||
BookArtworkEntity retrievedArtwork = Assert.Single(retrievedBook!.Artwork);
|
||||
Assert.Equal("cover.jpg", retrievedArtwork.FileName);
|
||||
Assert.Equal(ArtworkStatus.Enriched, retrievedArtwork.Status);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateAsync_WhenDataContainsExistingTagAndGenreNames_ShouldReuseTheStoredTagAndGenreEntities()
|
||||
{
|
||||
@@ -498,14 +525,14 @@ public class BookRepositoryTests
|
||||
BookEntity storedBook = _bookEntityFixture.Create();
|
||||
storedBook.ISBNs = [_isbnEntityFixture.Create(value: "9780395272237", format: IsbnFormat.Isbn13)];
|
||||
storedBook.Ratings = [_bookRatingEntityFixture.Create(value: 4.5M, maxValue: 5, source: BookRatingSource.Goodreads, voteCount: 100)];
|
||||
storedBook.BookContributors = [_bookContributorEntityFixture.Create(bookId: storedBook.Id, role: MediaContributorRole.Author)];
|
||||
storedBook.Contributors = [_bookContributorEntityFixture.Create(bookId: storedBook.Id, role: MediaContributorRole.Author)];
|
||||
_mockContext.Books.Add(storedBook);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
BookEntity data = _bookEntityFixture.Create(id: storedBook.Id);
|
||||
data.ISBNs = [_isbnEntityFixture.Create(value: "9780261102693", format: IsbnFormat.Isbn13)];
|
||||
data.Ratings = [_bookRatingEntityFixture.Create(value: 3.5M, maxValue: 5, source: BookRatingSource.Amazon, voteCount: 50)];
|
||||
data.BookContributors = [_bookContributorEntityFixture.Create(bookId: storedBook.Id, mediaContributorId: Guid.NewGuid(), role: MediaContributorRole.Illustrator)];
|
||||
data.Contributors = [_bookContributorEntityFixture.Create(bookId: storedBook.Id, mediaContributorId: Guid.NewGuid(), role: MediaContributorRole.Illustrator)];
|
||||
|
||||
// Act
|
||||
Result<Updated> result = await _sut.UpdateAsync(data, CancellationToken.None);
|
||||
@@ -516,12 +543,12 @@ public class BookRepositoryTests
|
||||
BookEntity? retrievedBook = await _mockContext.Books
|
||||
.Include(book => book.ISBNs)
|
||||
.Include(book => book.Ratings)
|
||||
.Include(book => book.BookContributors)
|
||||
.Include(book => book.Contributors)
|
||||
.FirstOrDefaultAsync(book => book.Id == storedBook.Id);
|
||||
Assert.NotNull(retrievedBook);
|
||||
IsbnEntity retrievedIsbn = Assert.Single(retrievedBook!.ISBNs);
|
||||
BookRatingEntity retrievedRating = Assert.Single(retrievedBook.Ratings);
|
||||
BookContributorEntity retrievedContributor = Assert.Single(retrievedBook.BookContributors);
|
||||
BookContributorEntity retrievedContributor = Assert.Single(retrievedBook.Contributors);
|
||||
Assert.Equal("9780261102693", retrievedIsbn.Value);
|
||||
Assert.Equal(3.5M, retrievedRating.Value);
|
||||
Assert.Equal(MediaContributorRole.Illustrator, retrievedContributor.Role);
|
||||
@@ -724,18 +751,18 @@ public class BookRepositoryTests
|
||||
BookEntity bookWithoutCover = _bookEntityFixture.Create();
|
||||
bookWithoutCover.LibraryId = libraryId;
|
||||
bookWithoutCover.Path = "/books/a.epub";
|
||||
bookWithoutCover.BookArtwork = [];
|
||||
bookWithoutCover.Artwork = [];
|
||||
BookEntity bookWithEnrichedCover = _bookEntityFixture.Create();
|
||||
bookWithEnrichedCover.LibraryId = libraryId;
|
||||
bookWithEnrichedCover.Path = "/books/b.epub";
|
||||
bookWithEnrichedCover.BookArtwork = [_bookArtworkEntityFixture.Create(bookId: bookWithEnrichedCover.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Enriched)];
|
||||
bookWithEnrichedCover.Artwork = [_bookArtworkEntityFixture.Create(bookId: bookWithEnrichedCover.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Enriched)];
|
||||
BookEntity bookWithFailedCover = _bookEntityFixture.Create();
|
||||
bookWithFailedCover.LibraryId = libraryId;
|
||||
bookWithFailedCover.Path = "/books/c.epub";
|
||||
bookWithFailedCover.BookArtwork = [_bookArtworkEntityFixture.Create(bookId: bookWithFailedCover.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Failed)];
|
||||
bookWithFailedCover.Artwork = [_bookArtworkEntityFixture.Create(bookId: bookWithFailedCover.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Failed)];
|
||||
BookEntity bookOfAnotherLibrary = _bookEntityFixture.Create();
|
||||
bookOfAnotherLibrary.LibraryId = Guid.NewGuid();
|
||||
bookOfAnotherLibrary.BookArtwork = [];
|
||||
bookOfAnotherLibrary.Artwork = [];
|
||||
_mockContext.Books.AddRange(bookWithoutCover, bookWithEnrichedCover, bookWithFailedCover, bookOfAnotherLibrary);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
@@ -756,13 +783,13 @@ public class BookRepositoryTests
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
BookEntity bookWithoutCover = _bookEntityFixture.Create();
|
||||
bookWithoutCover.LibraryId = libraryId;
|
||||
bookWithoutCover.BookArtwork = [];
|
||||
bookWithoutCover.Artwork = [];
|
||||
BookEntity bookWithEnrichedCover = _bookEntityFixture.Create();
|
||||
bookWithEnrichedCover.LibraryId = libraryId;
|
||||
bookWithEnrichedCover.BookArtwork = [_bookArtworkEntityFixture.Create(bookId: bookWithEnrichedCover.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Enriched)];
|
||||
bookWithEnrichedCover.Artwork = [_bookArtworkEntityFixture.Create(bookId: bookWithEnrichedCover.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Enriched)];
|
||||
BookEntity bookOfAnotherLibrary = _bookEntityFixture.Create();
|
||||
bookOfAnotherLibrary.LibraryId = Guid.NewGuid();
|
||||
bookOfAnotherLibrary.BookArtwork = [];
|
||||
bookOfAnotherLibrary.Artwork = [];
|
||||
_mockContext.Books.AddRange(bookWithoutCover, bookWithEnrichedCover, bookOfAnotherLibrary);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
@@ -813,7 +840,7 @@ public class BookRepositoryTests
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
// Act
|
||||
Result<Deleted> result = await _sut.DeleteAsync(book.Id, CancellationToken.None);
|
||||
Result<Deleted> result = await _sut.DeleteByIdAsync(book.Id, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
@@ -827,7 +854,7 @@ public class BookRepositoryTests
|
||||
public async Task DeleteAsync_WhenBookDoesNotExist_ShouldReturnError()
|
||||
{
|
||||
// Act
|
||||
Result<Deleted> result = await _sut.DeleteAsync(Guid.NewGuid(), CancellationToken.None);
|
||||
Result<Deleted> result = await _sut.DeleteByIdAsync(Guid.NewGuid(), CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
@@ -869,7 +896,7 @@ public class BookRepositoryTests
|
||||
book.Tags = [_tagEntityFixture.Create(name: "Tag1"), _tagEntityFixture.Create(name: "Tag2")];
|
||||
book.Genres = [_genreEntityFixture.Create(name: "Genre1"), _genreEntityFixture.Create(name: "Genre2")];
|
||||
book.ISBNs = [_isbnEntityFixture.Create(value: "1234567890", format: IsbnFormat.Isbn10), _isbnEntityFixture.Create(value: "1234567890123", format: IsbnFormat.Isbn13)];
|
||||
book.BookArtwork = [_bookArtworkEntityFixture.Create(bookId: book.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Enriched)];
|
||||
book.Artwork = [_bookArtworkEntityFixture.Create(bookId: book.Id, artworkType: ArtworkType.Cover, ordinal: 0, status: ArtworkStatus.Enriched)];
|
||||
_mockContext.Books.Add(book);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
@@ -882,7 +909,7 @@ public class BookRepositoryTests
|
||||
Assert.Equal(2, retrievedBook.Tags.Count);
|
||||
Assert.Equal(2, retrievedBook.Genres.Count);
|
||||
Assert.Equal(2, retrievedBook.ISBNs.Count);
|
||||
Assert.Single(retrievedBook.BookArtwork);
|
||||
Assert.Single(retrievedBook.Artwork);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -1320,7 +1347,7 @@ public class BookRepositoryTests
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
BookEntity book = _bookEntityFixture.Create();
|
||||
book.LibraryId = libraryId;
|
||||
book.BookArtwork = [_bookArtworkEntityFixture.Create(bookId: book.Id, artworkType: ArtworkType.Cover, fileName: "cover.jpg")];
|
||||
book.Artwork = [_bookArtworkEntityFixture.Create(bookId: book.Id, artworkType: ArtworkType.Cover, fileName: "cover.jpg")];
|
||||
_mockContext.Books.Add(book);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
@@ -1342,7 +1369,7 @@ public class BookRepositoryTests
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
BookEntity book = _bookEntityFixture.Create();
|
||||
book.LibraryId = libraryId;
|
||||
book.BookArtwork = [];
|
||||
book.Artwork = [];
|
||||
_mockContext.Books.Add(book);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.DataAccess.Core.Repositories.BookLibrary.Specifications;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.BookLibrary.Specifications;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="BookAlphaFilterSpecification"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class BookAlphaFilterSpecificationTests : IDisposable
|
||||
{
|
||||
private readonly SqliteConnection _connection;
|
||||
private readonly LuminaDbContext _context;
|
||||
private readonly BookEntityFixture _bookEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BookAlphaFilterSpecificationTests"/> class.
|
||||
/// </summary>
|
||||
public BookAlphaFilterSpecificationTests()
|
||||
{
|
||||
// the specification translates to the SQLite glob() function, so it can only be exercised against a real SQLite database
|
||||
_connection = new SqliteConnection("Data Source=:memory:");
|
||||
_connection.Open();
|
||||
_context = new LuminaDbContext(new DbContextOptionsBuilder<LuminaDbContext>().UseSqlite(_connection).Options);
|
||||
_context.Database.EnsureCreated();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenAlphaKeyIsNull_ShouldMatchEveryBook()
|
||||
{
|
||||
// Arrange
|
||||
List<BookEntity> books = SeedBooks(("Alpha", null), ("Beta", null), ("1984", null));
|
||||
BookAlphaFilterSpecification specification = new(null, false);
|
||||
|
||||
// Act
|
||||
List<BookEntity> result = [.. _context.Books.Where(specification.ToExpression())];
|
||||
|
||||
// Assert
|
||||
Assert.Equal(books.Count, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenAlphaKeyIsALowercaseLetter_ShouldMatchOnlyBooksWhoseTitleStartsWithIt()
|
||||
{
|
||||
// Arrange
|
||||
SeedBooks(("Fellowship", null), ("Hobbit", null), ("The Two Towers", null));
|
||||
BookAlphaFilterSpecification specification = new("f", false);
|
||||
|
||||
// Act
|
||||
List<string> result = [.. _context.Books.Where(specification.ToExpression()).Select(book => book.Title)];
|
||||
|
||||
// Assert
|
||||
Assert.Equal(["Fellowship"], result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenAlphaKeyIsAnUppercaseLetter_ShouldMatchBooksWhoseTitleStartsWithItsLowercase()
|
||||
{
|
||||
// Arrange
|
||||
SeedBooks(("Fellowship", null), ("Hobbit", null));
|
||||
BookAlphaFilterSpecification specification = new("F", false);
|
||||
|
||||
// Act
|
||||
List<string> result = [.. _context.Books.Where(specification.ToExpression()).Select(book => book.Title)];
|
||||
|
||||
// Assert
|
||||
Assert.Equal(["Fellowship"], result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenNoTitleStartsWithTheAlphaKey_ShouldNotMatchAnyBook()
|
||||
{
|
||||
// Arrange
|
||||
SeedBooks(("Fellowship", null), ("Hobbit", null));
|
||||
BookAlphaFilterSpecification specification = new("z", false);
|
||||
|
||||
// Act
|
||||
List<BookEntity> result = [.. _context.Books.Where(specification.ToExpression())];
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenAlphaKeyIsTheNumberKey_ShouldMatchOnlyBooksWhoseTitleStartsWithADigit()
|
||||
{
|
||||
// Arrange
|
||||
SeedBooks(("1984", null), ("Alpha", null), ("Beta", null));
|
||||
BookAlphaFilterSpecification specification = new(LibraryItemAlphaKeys.NUMBER, false);
|
||||
|
||||
// Act
|
||||
List<string> result = [.. _context.Books.Where(specification.ToExpression()).Select(book => book.Title)];
|
||||
|
||||
// Assert
|
||||
Assert.Equal(["1984"], result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenAlphaKeyIsTheSymbolKey_ShouldMatchOnlyBooksWhoseTitleStartsWithASymbol()
|
||||
{
|
||||
// Arrange
|
||||
SeedBooks(("!Important", null), ("1984", null), ("Alpha", null));
|
||||
BookAlphaFilterSpecification specification = new(LibraryItemAlphaKeys.SYMBOL, false);
|
||||
|
||||
// Act
|
||||
List<string> result = [.. _context.Books.Where(specification.ToExpression()).Select(book => book.Title)];
|
||||
|
||||
// Assert
|
||||
Assert.Equal(["!Important"], result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenTitleIsEmpty_ShouldMatchOnTheOriginalTitle()
|
||||
{
|
||||
// Arrange
|
||||
BookEntity book = _bookEntityFixture.Create(title: string.Empty, originalTitle: "Rendezvous", includeMetadata: false);
|
||||
_context.Books.Add(book);
|
||||
_context.SaveChanges();
|
||||
BookAlphaFilterSpecification specification = new("r", false);
|
||||
|
||||
// Act
|
||||
List<string> result = [.. _context.Books.Where(specification.ToExpression()).Select(entity => entity.OriginalTitle!)];
|
||||
|
||||
// Assert
|
||||
Assert.Equal(["Rendezvous"], result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenIgnoringThePrefixAndTitleStartsWithThe_ShouldMatchOnTheStrippedTitle()
|
||||
{
|
||||
// Arrange
|
||||
SeedBooks(("The Art of War", null), ("Beneath the Surface", null));
|
||||
BookAlphaFilterSpecification specification = new("a", true);
|
||||
|
||||
// Act
|
||||
List<string> result = [.. _context.Books.Where(specification.ToExpression()).Select(book => book.Title)];
|
||||
|
||||
// Assert
|
||||
Assert.Equal(["The Art of War"], result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ToExpression_WhenNotIgnoringThePrefixAndTitleStartsWithThe_ShouldNotMatchOnTheStrippedTitle()
|
||||
{
|
||||
// Arrange
|
||||
SeedBooks(("The Art of War", null), ("Beneath the Surface", null));
|
||||
BookAlphaFilterSpecification specification = new("a", false);
|
||||
|
||||
// Act
|
||||
List<BookEntity> result = [.. _context.Books.Where(specification.ToExpression())];
|
||||
|
||||
// Assert
|
||||
Assert.Empty(result);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Seeds books with the provided titles.
|
||||
/// </summary>
|
||||
/// <param name="books">The title and original title pairs of the books to seed.</param>
|
||||
/// <returns>The seeded <see cref="BookEntity"/> instances.</returns>
|
||||
private List<BookEntity> SeedBooks(params (string Title, string? OriginalTitle)[] books)
|
||||
{
|
||||
List<BookEntity> entities = [];
|
||||
foreach ((string title, string? originalTitle) in books)
|
||||
entities.Add(_bookEntityFixture.Create(title: title, originalTitle: originalTitle, includeMetadata: false));
|
||||
_context.Books.AddRange(entities);
|
||||
_context.SaveChanges();
|
||||
return entities;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes the database context and its underlying connection.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
_context.Dispose();
|
||||
_connection.Dispose();
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -1,14 +1,14 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.DataAccess.Core.Repositories.Books.Specifications;
|
||||
using Lumina.DataAccess.Core.Repositories.BookLibrary.Specifications;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Books.Specifications;
|
||||
namespace Lumina.DataAccess.UnitTests.Core.Repositories.BookLibrary.Specifications;
|
||||
|
||||
/// <summary>
|
||||
/// Contains unit tests for the <see cref="BookSearchSpecification"/> class.
|
||||
Reference in new issue
Block a user