Skip to content

Commit

Permalink
Refactor functests
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomilov committed Dec 4, 2024
1 parent 7380779 commit d9e33cc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
5 changes: 1 addition & 4 deletions src/slack_annotations/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 14 additions & 10 deletions tests/functional/core_test.py
Original file line number Diff line number Diff line change
@@ -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"
}
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d9e33cc

Please sign in to comment.