feat(api): enhanced facet number handling (#2772)

This commit is contained in:
Alex
2026-10-18 09:07:15 +01:00
committed by GitHub
parent bb0e6cc1a7
commit 0cb1d73327
5 changed files with 141 additions and 10 deletions
@@ -123,7 +123,7 @@ class BookFacetServiceTest {
.build();
}
private void book(String title, String genre, String authorName) {
private BookMetadataEntity book(String title, String genre, String authorName) {
BookEntity bookEntity = BookEntity.builder()
.library(library).libraryPath(libraryPath).addedOn(Instant.now()).deleted(false).build();
em.persist(bookEntity);
@@ -132,6 +132,7 @@ class BookFacetServiceTest {
metadata.setAuthors(List.of(author(authorName)));
em.persist(metadata);
bookEntity.setMetadata(metadata);
return metadata;
}
private CategoryEntity category(String name) {
@@ -300,6 +301,51 @@ class BookFacetServiceTest {
assertThat(group(response, "author").links()).hasSize(100);
}
@Test
void numberFacetBoundsCoverValuesPastTheCap() {
for (int i = 1; i <= 101; i++) {
book("T" + i, "Genre", "Author").setPageCount(i);
}
em.flush();
FacetGroupsResponse response = facetService.getFacets(null, null, null);
FacetGroup pageCount = group(response, "page_count");
assertThat(pageCount.links()).hasSize(1);
FacetLink range = pageCount.links().getFirst();
assertThat(range.templated()).isTrue();
assertThat(range.href()).isEqualTo("/api/v1/books/page?facet=page_count%3A{min}..{max}");
assertThat(range.properties().min().intValue()).isEqualTo(1);
assertThat(range.properties().max().intValue()).isEqualTo(101);
assertThat(group(response, "genre").links()).noneMatch(l -> Boolean.TRUE.equals(l.templated()));
}
@Test
void ratingFacetCountsBandsAcrossEveryValue() {
for (int i = 0; i < 150; i++) {
book("R" + i, "Genre", "Author").setGoodreadsRating(3.5 + i / 1000.0);
}
book("Four", "Genre", "Author").setGoodreadsRating(4.0);
book("Edge", "Genre", "Author").setGoodreadsRating(4.5);
book("High", "Genre", "Author").setGoodreadsRating(4.8);
em.flush();
FacetGroup goodreads = group(facetService.getFacets(List.of("goodreads_rating:4..<4.5"), null, null), "goodreads_rating");
FacetLink range = goodreads.links().getFirst();
assertThat(range.templated()).isTrue();
assertThat(range.href()).isEqualTo("/api/v1/books/page?facet=goodreads_rating%3A4..%3C4.5&facet=goodreads_rating%3A{min}..{max}");
assertThat(range.properties().min().doubleValue()).isEqualTo(3.5);
assertThat(range.properties().max().doubleValue()).isEqualTo(4.8);
assertThat(goodreads.links()).filteredOn(l -> l.templated() == null).extracting(FacetLink::value)
.containsExactly("0..<1", "1..<2", "2..<3", "3..<4", "4..<4.5", "4.5..*");
assertThat(count(goodreads, "0..<1")).isZero();
assertThat(count(goodreads, "3..<4")).isEqualTo(150);
assertThat(count(goodreads, "4..<4.5")).isEqualTo(1);
assertThat(count(goodreads, "4.5..*")).isEqualTo(2);
assertThat(link(goodreads, "4..<4.5").rel()).containsExactly("self", "facet");
}
@Test
void activeFacetIsMarkedSelfWithCurrentPageHref() {
book("A", "Horror", "Alice");