mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-08-19 09:13:24 +00:00
refactor: minor cleanup from final whoosh-compat migration review
- Update stale test comments in test_query.py that described string rewriting / raw-query fallback behavior that no longer exists post whoosh-compat migration; rename test_date_rewriting_applied_before_tantivy_parse to test_date_keyword_resolves_without_raising to match. - views.py: move the local MultipleSearchQueryErrors import up into the existing local-import block near the top of list(), consistent with the other documents.search imports there, instead of importing it again inside the except SearchQueryError clause. - test_api_search.py: assert response.status_code explicitly before indexing into response.data["results"] in test_search_added_previous_month_excludes_next_period_start, and tie the xfail marker to AssertionError instead of the incidental KeyError that indexing a 400 response's missing "results" key produced.
This commit is contained in:
@@ -59,12 +59,13 @@ class TestParseUserQuery:
|
||||
settings.ADVANCED_FUZZY_SEARCH_THRESHOLD = 0.5
|
||||
assert isinstance(parse_user_query(query_index, raw_query, UTC), tantivy.Query)
|
||||
|
||||
def test_date_rewriting_applied_before_tantivy_parse(
|
||||
def test_date_keyword_resolves_without_raising(
|
||||
self,
|
||||
query_index: tantivy.Index,
|
||||
) -> None:
|
||||
# created:today must be rewritten to an ISO range before Tantivy parses it;
|
||||
# if passed raw, Tantivy would reject "today" as an invalid date value
|
||||
# whoosh-compat's DateParserPlugin resolves "today" against the AST
|
||||
# directly (no string rewrite to an ISO range happens anywhere in
|
||||
# this pipeline); the emitted tantivy query must still build cleanly.
|
||||
with time_machine.travel(datetime(2026, 3, 28, 12, 0, tzinfo=UTC), tick=False):
|
||||
q = parse_user_query(query_index, "created:today", UTC)
|
||||
assert isinstance(q, tantivy.Query)
|
||||
@@ -92,9 +93,10 @@ class TestParseUserQuery:
|
||||
self,
|
||||
query_index: tantivy.Index,
|
||||
) -> None:
|
||||
# parse_user_query falls back to the raw query on unexpected translation
|
||||
# errors, but an InvalidDateQuery is intentional and must propagate so the
|
||||
# view can return a 400 instead of silently parsing the raw (invalid) date.
|
||||
# parse_user_query never falls back to the raw query string on a parse
|
||||
# error — a bad date diagnostic from whoosh-compat always maps to an
|
||||
# InvalidDateQuery and must propagate, so the view can return a 400
|
||||
# instead of silently parsing the raw (invalid) date.
|
||||
with pytest.raises(InvalidDateQuery) as exc_info:
|
||||
parse_user_query(query_index, "created:202023", UTC)
|
||||
assert exc_info.value.field == "created"
|
||||
|
||||
@@ -731,7 +731,7 @@ class TestDocumentSearchApi(DirectoriesMixin, APITestCase):
|
||||
"which already quotes. CONFIRMED REGRESSION vs whoosh-compat "
|
||||
"migration; see task-10-report.md."
|
||||
),
|
||||
raises=KeyError,
|
||||
raises=AssertionError,
|
||||
)
|
||||
def test_search_added_previous_month_excludes_next_period_start(self) -> None:
|
||||
"""
|
||||
@@ -769,6 +769,10 @@ class TestDocumentSearchApi(DirectoriesMixin, APITestCase):
|
||||
tick=False,
|
||||
):
|
||||
response = self.client.get("/api/documents/?query=added:previous month")
|
||||
assert response.status_code == 200, (
|
||||
f"expected a successful search response, got {response.status_code}: "
|
||||
f"{response.data!r}"
|
||||
)
|
||||
results = response.data["results"]
|
||||
|
||||
self.assertEqual(len(results), 1)
|
||||
|
||||
@@ -2415,6 +2415,7 @@ class UnifiedSearchViewSet(DocumentViewSet):
|
||||
if not self._is_search_request():
|
||||
return super().list(request)
|
||||
|
||||
from documents.search import MultipleSearchQueryErrors
|
||||
from documents.search import SearchHit
|
||||
from documents.search import SearchQueryError
|
||||
from documents.search import TantivyBackend
|
||||
@@ -2613,8 +2614,6 @@ class UnifiedSearchViewSet(DocumentViewSet):
|
||||
# User-fixable query error(s) (e.g. unparsable dates/numbers):
|
||||
# surface every offending field's message, not just the first,
|
||||
# so the user can fix them all in one round-trip.
|
||||
from documents.search import MultipleSearchQueryErrors
|
||||
|
||||
messages = (
|
||||
[str(sub) for sub in e.errors]
|
||||
if isinstance(e, MultipleSearchQueryErrors)
|
||||
|
||||
Reference in New Issue
Block a user