Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

3.4 KB
Raw Blame
"""Pins the correctness gained by deleting the pre-parse
_quote_date_keyword_phrases rewrite.

That rewrite matched date-keyword phrases (e.g. "previous month" after a
date field) anywhere in the raw query string, including inside an
unrelated quoted string, and inserted quotes mid-phrase there too. Its
own docstring gave ``title:"see added:previous month notes"`` as the
example of what it corrupted. whoosh-compat's grammar accepts the same
phrase vocabulary unquoted natively (see TestUnquotedDateKeywordPhrases
in test_acceptance.py), so the rewrite was redundant everywhere it was
safe and actively wrong everywhere it was not. This is the one case that
tells the two apart: a literal title phrase that happens to contain
"added:previous month" as running text.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

if TYPE_CHECKING:
    from collections.abc import Callable

    from documents.models import Document

pytestmark = [pytest.mark.search, pytest.mark.django_db]


class TestQuotedStringContainingDateKeywordText:
    """A quoted title phrase containing the literal text
    "added:previous month" as running words must match on that literal
    text alone, never spill into an unfielded search for "previous" and
    "month" across the default search fields the way the deleted rewrite
    would have decomposed it into."""

    def test_matches_only_the_literal_phrase(
        self,
        index_document: Callable[..., Document],
        matched_ids: Callable[[str], set[int]],
    ) -> None:
        """
        GIVEN:
            - A document whose title literally contains "see
              added:previous month notes", and a decoy document whose
              title/content carry the individual fragments the deleted
              _quote_date_keyword_phrases rewrite would have decomposed
              the phrase into (the decoy would incorrectly match under
              the deleted rewrite: its title contains the "see added:"
              and " notes" fragments the corrupted parse required as
              title phrases, and its content supplies "previous" and
              "month" as the decomposed word-match clauses the rewrite
              turned the middle of the phrase into)
        WHEN:
            - 'title:"see added:previous month notes"' is searched
        THEN:
            - Only the document with the literal phrase matches; it must
              never spill into an unfielded search for "previous" and
              "month" across the default search fields
        """
        literal = index_document(
            title="see added:previous month notes",
            content="quarterly filing",
            archive_serial_number=920,
        )
        # Under the deleted rewrite, this decoy would incorrectly match:
        # its title contains the "see added:" and " notes" fragments the
        # corrupted parse required as title phrases, and its content
        # supplies "previous" and "month" as the decomposed word-match
        # clauses the rewrite turned the middle of the phrase into.
        decoy = index_document(
            title="see added: quarterly report notes",
            content="we reviewed the previous statement about month end",
            archive_serial_number=921,
        )
        query = 'title:"see added:previous month notes"'
        assert matched_ids(query) == {literal.pk}
        assert decoy.pk not in matched_ids(query)