Adds alpha picker filtering to the books lite query

This commit is contained in:
BinaryAlley committed 2026-08-16 10:34:08 -07:00
1 parent e72625de7c
commit 6fa154a2e7
12 files changed
+376 -10

No files matched your search

@@ -26,6 +26,8 @@ public class GetBooksLiteQueryFixture
/// <param name="libraryId">Optional. The Id of the media library whose books are retrieved.</param>
/// <param name="paginationData">Optional. The pagination data of the query.</param>
/// <param name="searchTerm">Optional. The search term used to filter results.</param>
/// <param name="filterAlphaKey">Optional. The alpha key used to filter the results by the first character of their title, for the alpha picker.</param>
/// <param name="ignoreThePrefixForAlphaPicker">Optional. Whether the leading "The " prefix of a title should be ignored when computing the alpha key, or not.</param>
/// <param name="sortBy">Optional. The name of the field by which to sort the results.</param>
/// <param name="sortOrder">Optional. The direction in which to sort the results.</param>
/// <returns>The created query to get the lightweight details of books.</returns>
@@ -33,6 +35,8 @@ public class GetBooksLiteQueryFixture
Guid? libraryId = null,
PaginationDataDto? paginationData = null,
string? searchTerm = null,
string? filterAlphaKey = null,
bool ignoreThePrefixForAlphaPicker = false,
string? sortBy = null,
SortOrder? sortOrder = null)
{
@@ -45,7 +49,9 @@ public class GetBooksLiteQueryFixture
new LibraryFilterDto
{
LibraryId = libraryId ?? _faker.Random.Guid(),
SearchTerm = searchTerm
SearchTerm = searchTerm,
FilterAlphaKey = filterAlphaKey,
IgnoreThePrefixForAlphaPicker = ignoreThePrefixForAlphaPicker
},
sortBy,
sortOrder ?? _faker.PickRandom<SortOrder>()
@@ -25,6 +25,8 @@ public class GetBooksLiteRequestFixture
/// <param name="currentPage">Optional. The page of results to retrieve.</param>
/// <param name="perPage">Optional. The maximum number of books to retrieve per page.</param>
/// <param name="searchTerm">Optional. The search term used to filter results.</param>
/// <param name="filterAlphaKey">Optional. The alpha key used to filter the results by the first character of their title, for the alpha picker.</param>
/// <param name="ignoreThePrefixForAlphaPicker">Optional. Whether the leading "The " prefix of a title should be ignored when computing the alpha key, or not.</param>
/// <param name="sortBy">Optional. The name of the field by which to sort the results.</param>
/// <param name="sortOrder">Optional. The direction in which to sort the results.</param>
/// <returns>The created <see cref="GetBooksLiteRequest"/>.</returns>
@@ -33,6 +35,8 @@ public class GetBooksLiteRequestFixture
int? currentPage = null,
int? perPage = null,
string? searchTerm = null,
string? filterAlphaKey = null,
bool ignoreThePrefixForAlphaPicker = false,
string? sortBy = null,
SortOrder? sortOrder = null)
{
@@ -41,6 +45,8 @@ public class GetBooksLiteRequestFixture
CurrentPage: currentPage ?? _faker.Random.Number(1, 100),
PerPage: perPage ?? _faker.Random.Number(1, 200),
SearchTerm: searchTerm ?? _faker.Lorem.Word(),
FilterAlphaKey: filterAlphaKey,
IgnoreThePrefixForAlphaPicker: ignoreThePrefixForAlphaPicker,
SortBy: sortBy ?? _faker.Lorem.Word(),
SortOrder: sortOrder ?? _faker.PickRandom<SortOrder>()
);
@@ -82,6 +82,121 @@ public class GetBooksLiteEndpointTests : IClassFixture<AuthenticatedLuminaApiFac
Assert.Contains(paginatedBooks.Data, book => book.Title == "Book B");
}
[Fact]
public async Task GetBooksLite_WhenCalledWithFilterAlphaKeyLetter_ShouldReturnOnlyBooksStartingWithThatLetter()
{
// Arrange
Guid userId = GetCurrentUserId();
Guid libraryId = Guid.NewGuid();
await SeedLibraryAsync(libraryId, userId);
await SeedBookAsync(libraryId, "Quiet Place");
await SeedBookAsync(libraryId, "The Quiet");
await SeedBookAsync(libraryId, "Racing");
// Act
HttpResponseMessage response = await _client.GetAsync($"/api/v1/books/lite?libraryId={libraryId}&filterAlphaKey=q");
// Assert
response.EnsureSuccessStatusCode();
PaginatedResponse<BookLiteResponse>? paginatedBooks = await response.Content.ReadFromJsonAsync<PaginatedResponse<BookLiteResponse>>(_jsonOptions);
Assert.NotNull(paginatedBooks);
BookLiteResponse book = Assert.Single(paginatedBooks!.Data);
Assert.Equal("Quiet Place", book.Title);
Assert.Equal(1, paginatedBooks.Count);
}
[Fact]
public async Task GetBooksLite_WhenCalledWithFilterAlphaKeyLetterAndIgnoreThePrefix_ShouldIncludeThePrefixedTitles()
{
// Arrange
Guid userId = GetCurrentUserId();
Guid libraryId = Guid.NewGuid();
await SeedLibraryAsync(libraryId, userId);
await SeedBookAsync(libraryId, "Quiet Place");
await SeedBookAsync(libraryId, "The Quiet");
await SeedBookAsync(libraryId, "Racing");
// Act
HttpResponseMessage response = await _client.GetAsync($"/api/v1/books/lite?libraryId={libraryId}&filterAlphaKey=q&ignoreThePrefixForAlphaPicker=true");
// Assert
response.EnsureSuccessStatusCode();
PaginatedResponse<BookLiteResponse>? paginatedBooks = await response.Content.ReadFromJsonAsync<PaginatedResponse<BookLiteResponse>>(_jsonOptions);
Assert.NotNull(paginatedBooks);
Assert.Equal(2, paginatedBooks!.Count);
Assert.Contains(paginatedBooks.Data, book => book.Title == "Quiet Place");
Assert.Contains(paginatedBooks.Data, book => book.Title == "The Quiet");
}
[Fact]
public async Task GetBooksLite_WhenCalledWithFilterAlphaKeyNumber_ShouldReturnOnlyBooksStartingWithADigit()
{
// Arrange
Guid userId = GetCurrentUserId();
Guid libraryId = Guid.NewGuid();
await SeedLibraryAsync(libraryId, userId);
await SeedBookAsync(libraryId, "1984");
await SeedBookAsync(libraryId, "7 Habits of Highly Effective People");
await SeedBookAsync(libraryId, "Brave New World");
// Act
HttpResponseMessage response = await _client.GetAsync($"/api/v1/books/lite?libraryId={libraryId}&filterAlphaKey=%23");
// Assert
response.EnsureSuccessStatusCode();
PaginatedResponse<BookLiteResponse>? paginatedBooks = await response.Content.ReadFromJsonAsync<PaginatedResponse<BookLiteResponse>>(_jsonOptions);
Assert.NotNull(paginatedBooks);
Assert.Equal(2, paginatedBooks!.Count);
Assert.Contains(paginatedBooks.Data, book => book.Title == "1984");
Assert.Contains(paginatedBooks.Data, book => book.Title == "7 Habits of Highly Effective People");
}
[Fact]
public async Task GetBooksLite_WhenCalledWithFilterAlphaKeySymbol_ShouldReturnOnlyBooksStartingWithASymbol()
{
// Arrange
Guid userId = GetCurrentUserId();
Guid libraryId = Guid.NewGuid();
await SeedLibraryAsync(libraryId, userId);
await SeedBookAsync(libraryId, "!Important");
await SeedBookAsync(libraryId, "1984");
await SeedBookAsync(libraryId, "Alpha");
// Act
HttpResponseMessage response = await _client.GetAsync($"/api/v1/books/lite?libraryId={libraryId}&filterAlphaKey=%2A");
// Assert
response.EnsureSuccessStatusCode();
PaginatedResponse<BookLiteResponse>? paginatedBooks = await response.Content.ReadFromJsonAsync<PaginatedResponse<BookLiteResponse>>(_jsonOptions);
Assert.NotNull(paginatedBooks);
BookLiteResponse book = Assert.Single(paginatedBooks!.Data);
Assert.Equal("!Important", book.Title);
Assert.Equal(1, paginatedBooks.Count);
}
[Fact]
public async Task GetBooksLite_WhenTitleIsEmptyAndFilterAlphaKeyIsProvided_ShouldUseOriginalTitle()
{
// Arrange
Guid userId = GetCurrentUserId();
Guid libraryId = Guid.NewGuid();
await SeedLibraryAsync(libraryId, userId);
await SeedBookAsync(libraryId, "", "Rendezvous");
await SeedBookAsync(libraryId, "Racing");
await SeedBookAsync(libraryId, "Quiet Place");
// Act
HttpResponseMessage response = await _client.GetAsync($"/api/v1/books/lite?libraryId={libraryId}&filterAlphaKey=r");
// Assert
response.EnsureSuccessStatusCode();
PaginatedResponse<BookLiteResponse>? paginatedBooks = await response.Content.ReadFromJsonAsync<PaginatedResponse<BookLiteResponse>>(_jsonOptions);
Assert.NotNull(paginatedBooks);
Assert.Equal(2, paginatedBooks!.Count);
Assert.Contains(paginatedBooks.Data, book => book.Title == ""); // the empty title book matches the filter through its original title
Assert.Contains(paginatedBooks.Data, book => book.Title == "Racing");
}
[Fact]
public async Task GetBooksLite_WhenCalledWithSearchTerm_ShouldReturnOnlyMatchingBooks()
{
@@ -204,8 +319,9 @@ public class GetBooksLiteEndpointTests : IClassFixture<AuthenticatedLuminaApiFac
/// </summary>
/// <param name="libraryId">The Id of the media library the book belongs to.</param>
/// <param name="title">The title of the book.</param>
/// <param name="originalTitle">Optional. The original title of the book.</param>
/// <returns>A task representing the asynchronous operation.</returns>
private async Task SeedBookAsync(Guid libraryId, string title)
private async Task SeedBookAsync(Guid libraryId, string title, string? originalTitle = null)
{
using IServiceScope scope = _apiFactory.Services.CreateScope();
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
@@ -215,6 +331,7 @@ public class GetBooksLiteEndpointTests : IClassFixture<AuthenticatedLuminaApiFac
LibraryId = libraryId,
Path = $"/books/{Guid.NewGuid()}.epub",
Title = title,
OriginalTitle = originalTitle,
CreatedBy = Guid.NewGuid(),
CreatedOnUtc = DateTime.UtcNow,
UpdatedBy = null
@@ -46,4 +46,46 @@ public class GetBooksLiteQueryValidatorTests
// Assert
result.ShouldNotHaveAnyValidationErrors();
}
[Fact]
public void Validate_WhenFilterAlphaKeyIsNotASingleLetterOrSpecialKey_ShouldHaveValidationError()
{
// Arrange
GetBooksLiteQuery query = _getBooksLiteQueryFixture.Create();
query = query with { Filter = query.Filter with { FilterAlphaKey = "AB" } };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldHaveValidationError(Errors.Library.InvalidFilterAlphaKey);
}
[Fact]
public void Validate_WhenFilterAlphaKeyIsASingleLetter_ShouldNotHaveValidationError()
{
// Arrange
GetBooksLiteQuery query = _getBooksLiteQueryFixture.Create();
query = query with { Filter = query.Filter with { FilterAlphaKey = "Q" } };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveAnyValidationErrors();
}
[Fact]
public void Validate_WhenFilterAlphaKeyIsNumberOrSymbol_ShouldNotHaveValidationError()
{
// Arrange
GetBooksLiteQuery query = _getBooksLiteQueryFixture.Create();
query = query with { Filter = query.Filter with { FilterAlphaKey = "#" } };
// Act
List<Error> result = _validator.TestValidate(query);
// Assert
result.ShouldNotHaveAnyValidationErrors();
}
}