mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 09:11:10 +00:00
Adds pagination and filtering to the API
This commit is contained in:
1 parent
ad984910a2
commit
ce4ebf36bc
59 files changed
+4423
-242
No files matched your search
+192
@@ -2,12 +2,15 @@
|
||||
using EntityFrameworkCore.Testing.NSubstitute;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Common;
|
||||
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.Application.Common.DTO.Filtering;
|
||||
using Lumina.Application.Common.DTO.Pagination;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
|
||||
using Lumina.DataAccess.Core.Repositories.Books;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Lumina.Domain.Common.Primitives;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.BookLibrary;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Common;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
@@ -326,4 +329,193 @@ public class BookRepositoryTests
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.WrittenContent.BookNotFound, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPaginatedAsync_WhenFilterIncludesLibraryId_ShouldReturnPaginatedBooks()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
List<BookEntity> books = _bookEntityFixture.CreateMany(3);
|
||||
foreach (BookEntity book in books)
|
||||
book.LibraryId = libraryId;
|
||||
_mockContext.Books.AddRange(books);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = new() { CurrentPage = 1, PerPage = 10 };
|
||||
LibraryFilterDto filter = new() { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<BookEntity>> result = await _sut.GetPaginatedAsync(paginationData, null, null, filter, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(3, result.Value.Count);
|
||||
Assert.Equal(3, result.Value.Data.Count);
|
||||
Assert.Equal(1, result.Value.CurrentPage);
|
||||
Assert.Equal(10, result.Value.PerPage);
|
||||
Assert.Equal(1, result.Value.NumberOfPages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPaginatedAsync_WhenFilterDoesNotIncludeLibraryId_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
PaginationDataDto paginationData = new() { CurrentPage = 1, PerPage = 10 };
|
||||
BaseFilterDto filter = new();
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<BookEntity>> result = await _sut.GetPaginatedAsync(paginationData, null, null, filter, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.FilterMustIncludeLibraryId, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPaginatedAsync_WhenFilterLibraryIdIsEmpty_ShouldReturnError()
|
||||
{
|
||||
// Arrange
|
||||
PaginationDataDto paginationData = new() { CurrentPage = 1, PerPage = 10 };
|
||||
LibraryFilterDto filter = new() { LibraryId = Guid.Empty };
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<BookEntity>> result = await _sut.GetPaginatedAsync(paginationData, null, null, filter, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.True(result.IsFailure);
|
||||
Assert.Equal(Errors.Library.FilterMustIncludeLibraryId, result.FirstError);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPaginatedAsync_WhenPaginationDataIsNull_ShouldReturnAllBooks()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
List<BookEntity> books = _bookEntityFixture.CreateMany(3);
|
||||
foreach (BookEntity book in books)
|
||||
book.LibraryId = libraryId;
|
||||
_mockContext.Books.AddRange(books);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
LibraryFilterDto filter = new() { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<BookEntity>> result = await _sut.GetPaginatedAsync(null, null, null, filter, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(3, result.Value.Count);
|
||||
Assert.Equal(3, result.Value.Data.Count);
|
||||
Assert.Equal(1, result.Value.CurrentPage);
|
||||
Assert.Equal(3, result.Value.PerPage);
|
||||
Assert.Equal(1, result.Value.NumberOfPages);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPaginatedAsync_WhenSearchTermProvided_ShouldReturnOnlyMatchingBooks()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
BookEntity fellowshipBook = _bookEntityFixture.Create();
|
||||
fellowshipBook.LibraryId = libraryId;
|
||||
fellowshipBook.Title = "The Fellowship of the Ring";
|
||||
BookEntity towersBook = _bookEntityFixture.Create();
|
||||
towersBook.LibraryId = libraryId;
|
||||
towersBook.Title = "The Two Towers";
|
||||
_mockContext.Books.AddRange(fellowshipBook, towersBook);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = new() { CurrentPage = 1, PerPage = 10 };
|
||||
LibraryFilterDto filter = new() { LibraryId = libraryId, SearchTerm = "Fellowship" };
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<BookEntity>> result = await _sut.GetPaginatedAsync(paginationData, null, null, filter, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
BookEntity book = Assert.Single(result.Value.Data);
|
||||
Assert.Equal("The Fellowship of the Ring", book.Title);
|
||||
Assert.Equal(1, result.Value.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPaginatedAsync_WhenPageSizeSmallerThanBookCount_ShouldReturnCorrectPageMetadata()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
List<BookEntity> books = _bookEntityFixture.CreateMany(5);
|
||||
foreach (BookEntity book in books)
|
||||
book.LibraryId = libraryId;
|
||||
_mockContext.Books.AddRange(books);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = new() { CurrentPage = 2, PerPage = 2 };
|
||||
LibraryFilterDto filter = new() { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<BookEntity>> result = await _sut.GetPaginatedAsync(paginationData, null, null, filter, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(5, result.Value.Count);
|
||||
Assert.Equal(3, result.Value.NumberOfPages);
|
||||
Assert.Equal(2, result.Value.CurrentPage);
|
||||
Assert.Equal(2, result.Value.PerPage);
|
||||
Assert.Equal(2, result.Value.Data.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPaginatedAsync_WhenSortByTitleDescending_ShouldReturnBooksInDescendingOrder()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
BookEntity bookA = _bookEntityFixture.Create();
|
||||
bookA.LibraryId = libraryId;
|
||||
bookA.Title = "Book A";
|
||||
BookEntity bookB = _bookEntityFixture.Create();
|
||||
bookB.LibraryId = libraryId;
|
||||
bookB.Title = "Book B";
|
||||
_mockContext.Books.AddRange(bookA, bookB);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = new() { CurrentPage = 1, PerPage = 10 };
|
||||
LibraryFilterDto filter = new() { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<BookEntity>> result = await _sut.GetPaginatedAsync(paginationData, "title", SortOrder.Descending, filter, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
Assert.Equal(["Book B", "Book A"], result.Value.Data.Select(book => book.Title));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetPaginatedAsync_WhenCalled_ShouldIncludeRelatedEntities()
|
||||
{
|
||||
// Arrange
|
||||
Guid libraryId = Guid.NewGuid();
|
||||
BookEntity book = _bookEntityFixture.Create();
|
||||
book.LibraryId = libraryId;
|
||||
book.Tags = [new TagEntity("Tag1"), new TagEntity("Tag2")];
|
||||
book.Genres = [new GenreEntity("Genre1"), new GenreEntity("Genre2")];
|
||||
book.ISBNs = [new IsbnEntity("1234567890", IsbnFormat.Isbn10), new IsbnEntity("1234567890123", IsbnFormat.Isbn13)];
|
||||
book.Ratings = [new BookRatingEntity(4.5M, 5, BookRatingSource.Goodreads, 100)];
|
||||
_mockContext.Books.Add(book);
|
||||
await _mockContext.SaveChangesAsync();
|
||||
|
||||
PaginationDataDto paginationData = new() { CurrentPage = 1, PerPage = 10 };
|
||||
LibraryFilterDto filter = new() { LibraryId = libraryId };
|
||||
|
||||
// Act
|
||||
Result<PaginatedResultDto<BookEntity>> result = await _sut.GetPaginatedAsync(paginationData, null, null, filter, CancellationToken.None);
|
||||
|
||||
// Assert
|
||||
Assert.False(result.IsFailure);
|
||||
BookEntity retrievedBook = Assert.Single(result.Value.Data);
|
||||
Assert.Equal(2, retrievedBook.Tags.Count);
|
||||
Assert.Equal(2, retrievedBook.Genres.Count);
|
||||
Assert.Equal(2, retrievedBook.ISBNs.Count);
|
||||
Assert.Single(retrievedBook.Ratings);
|
||||
Assert.Equal(4.5M, retrievedBook.Ratings.First().Value);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user