diff --git a/src/slack_annotations/core.py b/src/slack_annotations/core.py index 7799182..9176688 100644 --- a/src/slack_annotations/core.py +++ b/src/slack_annotations/core.py @@ -14,10 +14,7 @@ def notify( # values are needed for the algorithm below to work. search_params["sort"] = "created" search_params["order"] = "asc" - if not search_params.get("search_after"): - search_params["search_after"] = ( - datetime.now(UTC) - timedelta(hours=1) - ).isoformat() + search_params["search_after"] = (datetime.now(UTC) - timedelta(hours=1)).isoformat() if cache_path: try: diff --git a/tests/functional/core_test.py b/tests/functional/core_test.py index ba1ce45..4e5a07e 100644 --- a/tests/functional/core_test.py +++ b/tests/functional/core_test.py @@ -1,38 +1,42 @@ import json +from urllib.parse import quote + import pytest +from freezegun import freeze_time from slack_annotations.core import notify @pytest.fixture def slack_annotations(): - with open("tests/data/slack_annotations.json") as f: + with open("tests/data/slack_annotations.json", encoding="utf-8") as f: return json.load(f) @pytest.fixture def search_annotations(): - with open("tests/data/search_annotations.json") as f: + with open("tests/data/search_annotations.json", encoding="utf-8") as f: return json.load(f) +@freeze_time("2024-12-01T01:00:00+00:00") def test_notify_search_after(httpx_mock, search_annotations, slack_annotations): - url = "https://hypothes.is/api/search?search_after=1733007600000.0&sort=created&order=asc" + search_after = quote("2024-12-01T00:00:00+00:00") + url = f"https://hypothes.is/api/search?sort=created&order=asc&search_after={search_after}" httpx_mock.add_response(url=url, content=json.dumps(search_annotations)) - search_params = {"search_after": "1733007600000.0"} - assert notify(search_params=search_params) == json.dumps(slack_annotations) + assert notify() == json.dumps(slack_annotations) +@freeze_time("2024-11-01T01:00:00+00:00") def test_notify_cache_file(httpx_mock, tmp_path, search_annotations, slack_annotations): - url = "https://hypothes.is/api/search?search_after=1733007600000.0&sort=created&order=asc" + search_after = quote("2024-12-01T00:00:00+00:00") + url = f"https://hypothes.is/api/search?sort=created&order=asc&search_after={search_after}" httpx_mock.add_response(url=url, content=json.dumps(search_annotations)) - search_params = {"search_after": "1733007600000.0"} cache_path = tmp_path / "cache.json" - assert notify(search_params=search_params, cache_path=cache_path) == json.dumps( - slack_annotations - ) + cache_path.write_text(json.dumps({"search_after": "2024-12-01T00:00:00+00:00"})) + assert notify(cache_path=cache_path) == json.dumps(slack_annotations) assert json.loads(cache_path.read_text()) == { "search_after": "2024-11-22T17:03:54.248643+00:00" } diff --git a/tox.ini b/tox.ini index 435b78c..706e73d 100644 --- a/tox.ini +++ b/tox.ini @@ -38,6 +38,7 @@ deps = lint,tests,functests: pytest lint,tests,functests: h-testkit lint,functests: pytest-httpx + lint,functests: freezegun tests: pytest-cov coverage: coverage[toml] lint,tests,functests: factory-boy