Skip to content

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

3.4 KB
Raw Blame
"""Date keyword phrases (``today``, etc.) resolved in a non-UTC timezone,
end to end.

paperless's own ``tz=get_current_timezone()`` plumbing
(``TantivyBackend._parse_query``) is exercised elsewhere only for
relative *ranges* (``added:[-1 week to now]``, in
documents/tests/test_api_search.py). This covers a date *keyword*
(``today``), whose day boundary depends on the active timezone the same
way but goes through whoosh-compat's DateParserPlugin resolution instead
of an explicit range.

Discriminating shape: frozen at 2026-06-15T02:00 UTC, which is
2026-06-14T22:00 in America/New_York -- still "today" (06-14) there, but
already "today" (06-15) in UTC. Two documents pin both directions of the
mistake a hardcoded-UTC bug would make:

- ``in_ny_today`` (added 2026-06-14T20:00 UTC = 2026-06-14T16:00 NY) is
  inside New York's "today" window and outside a naive UTC-calendar-day
  window. A ``tz``-ignoring bug would miss it.
- ``in_utc_calendar_day_only`` (added 2026-06-15T10:00 UTC =
  2026-06-15T06:00 NY) is inside a naive UTC-calendar-day window but
  outside New York's actual "today" window. A ``tz``-ignoring bug would
  wrongly match it.
"""

from __future__ import annotations

from datetime import UTC
from datetime import datetime
from typing import TYPE_CHECKING

import pytest
import time_machine

if TYPE_CHECKING:
    from collections.abc import Callable

    from pytest_django.fixtures import SettingsWrapper

    from documents.models import Document

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

FROZEN_NOW = datetime(2026, 6, 15, 2, 0, tzinfo=UTC)


class TestDateKeywordUsesTheActiveTimezone:
    def test_today_matches_the_new_york_calendar_day_not_the_utc_one(
        self,
        index_document: Callable[..., Document],
        matched_ids: Callable[[str], set[int]],
        settings: SettingsWrapper,
    ) -> None:
        """
        GIVEN:
            - TIME_ZONE set to America/New_York, time frozen at
              2026-06-15T02:00 UTC (2026-06-14T22:00 NY -- still "today"
              there, but already "today" in UTC), and two documents: one
              added inside New York's "today" window but outside a naive
              UTC-calendar-day window, the other the reverse (inside a
              naive UTC-calendar-day window but outside New York's actual
              "today")
        WHEN:
            - "added:today" is searched
        THEN:
            - Only the document inside New York's actual "today" window
              matches, proving our tz=get_current_timezone() plumbing
              resolves the date keyword in the active timezone rather
              than a hardcoded UTC calendar day
        """
        settings.TIME_ZONE = "America/New_York"
        with time_machine.travel(FROZEN_NOW, tick=False):
            in_ny_today = index_document(
                title="NY today",
                content="x",
                added=datetime(2026, 6, 14, 20, 0, tzinfo=UTC),
            )
            # Not captured: the exact-set assertion below already proves
            # this document (inside a naive UTC-calendar-day window, but
            # outside New York's actual "today") does not match.
            index_document(
                title="UTC calendar day only",
                content="x",
                added=datetime(2026, 6, 15, 10, 0, tzinfo=UTC),
            )

            assert matched_ids("added:today") == {in_ny_today.pk}