Add support for sorting by various fields
This commit is contained in:
@@ -329,33 +329,54 @@ class TestBookSearchCommand:
|
||||
[
|
||||
# String field filters
|
||||
("title:Hobbit", "", ["The Hobbit"]),
|
||||
("author:Tolkien", "", ["The Hobbit", "The Fellowship"]),
|
||||
("genre:Fantasy", "", ["The Hobbit", "The Fellowship"]),
|
||||
("owner:alice", "", ["The Hobbit", "The Fellowship", "Dune"]),
|
||||
("place:home", "", ["The Hobbit", "Programming Book"]),
|
||||
("bookshelf:fantasy", "", ["The Hobbit", "The Fellowship"]),
|
||||
("author:Tolkien", "", ["The Fellowship", "The Hobbit"]),
|
||||
("genre:Fantasy", "", ["The Fellowship", "The Hobbit"]),
|
||||
("owner:alice", "", ["Dune", "The Fellowship", "The Hobbit"]),
|
||||
("place:home", "", ["Programming Book", "The Hobbit"]),
|
||||
("bookshelf:fantasy", "", ["The Fellowship", "The Hobbit"]),
|
||||
# Numeric field filters
|
||||
("rating>=4", "", ["The Hobbit", "Programming Book"]),
|
||||
("rating>=4", "", ["Programming Book", "The Hobbit"]),
|
||||
("rating=3", "", ["Dune"]),
|
||||
("shelf>1", "", ["The Fellowship", "Programming Book"]),
|
||||
("year>=1954", "", ["The Fellowship", "Dune", "Programming Book"]),
|
||||
("shelf>1", "", ["Programming Book", "The Fellowship"]),
|
||||
("year>=1954", "", ["Programming Book", "Dune", "The Fellowship"]),
|
||||
# Date field filters
|
||||
(
|
||||
"added>=2026-03-15",
|
||||
"",
|
||||
["The Hobbit", "The Fellowship", "Dune", "Programming Book"],
|
||||
["Programming Book", "Dune", "The Fellowship", "The Hobbit"],
|
||||
),
|
||||
("bought<2026-01-01", "", ["Programming Book"]),
|
||||
# Negation
|
||||
("-genre:Fantasy", "", ["Dune", "Programming Book"]),
|
||||
("-owner:bob", "", ["The Hobbit", "The Fellowship", "Dune"]),
|
||||
("-genre:Fantasy", "", ["Programming Book", "Dune"]),
|
||||
("-owner:bob", "", ["Dune", "The Fellowship", "The Hobbit"]),
|
||||
# Complex query with multiple filters
|
||||
("-genre:Fantasy owner:alice", "", ["Dune"]),
|
||||
# User-specific queries
|
||||
("rating>=4", "alice", ["The Hobbit"]),
|
||||
("is:reading", "alice", ["The Fellowship"]),
|
||||
("is:read", "alice", ["The Hobbit", "Dune"]),
|
||||
("is:read", "alice", ["Dune", "The Hobbit"]),
|
||||
("is:wished", "alice", ["Programming Book"]),
|
||||
# Sorting
|
||||
(
|
||||
"sort:added-desc",
|
||||
"",
|
||||
["Programming Book", "Dune", "The Fellowship", "The Hobbit"],
|
||||
),
|
||||
(
|
||||
"sort:added-asc",
|
||||
"",
|
||||
["The Hobbit", "The Fellowship", "Dune", "Programming Book"],
|
||||
),
|
||||
(
|
||||
"sort:read-desc",
|
||||
"alice",
|
||||
["Dune", "The Hobbit", "Programming Book", "The Fellowship"],
|
||||
),
|
||||
(
|
||||
"sort:owner-asc",
|
||||
"",
|
||||
["Dune", "The Fellowship", "The Hobbit", "Programming Book"],
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_book_search_advanced_queries(
|
||||
@@ -383,7 +404,7 @@ class TestBookSearchCommand:
|
||||
actual_titles = [book["title"] for book in search_results]
|
||||
|
||||
# Verify expected titles are present (order doesn't matter)
|
||||
assert set(expected_titles) == set(actual_titles), (
|
||||
assert expected_titles == actual_titles, (
|
||||
f"Query '{query}' expected {expected_titles}, got {actual_titles}"
|
||||
)
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ from hxbooks.search import (
|
||||
IsOperatorValue,
|
||||
QueryParser,
|
||||
SearchQuery,
|
||||
SortDirection,
|
||||
_convert_value, # noqa: PLC2701
|
||||
)
|
||||
|
||||
@@ -267,6 +268,21 @@ class TestTypeConversion:
|
||||
result = _convert_value(Field.IS, "invalid-status")
|
||||
assert result == IsOperatorValue.UNKNOWN
|
||||
|
||||
def test_convert_sort_field(self, parser: QueryParser) -> None:
|
||||
"""Test converting values for 'sort' field."""
|
||||
result = _convert_value(Field.SORT, "added")
|
||||
assert result == (Field.ADDED_DATE, SortDirection.ASC)
|
||||
|
||||
result = _convert_value(Field.SORT, "added-desc")
|
||||
assert result == (Field.ADDED_DATE, SortDirection.DESC)
|
||||
|
||||
# Invalid field or direction should fallback to a default value
|
||||
result = _convert_value(Field.SORT, "added-invalid")
|
||||
assert result == (Field.SORT, SortDirection.ASC)
|
||||
|
||||
result = _convert_value(Field.SORT, "invalid-asc")
|
||||
assert result == (Field.SORT, SortDirection.ASC)
|
||||
|
||||
|
||||
class TestParsingEdgeCases:
|
||||
"""Test edge cases and error handling in query parsing."""
|
||||
|
||||
Reference in New Issue
Block a user