Skip to content

Commit

Permalink
Merge pull request #8 from hypothesis/add-functests-to-slack-annotations
Browse files Browse the repository at this point in the history
Add functests to slack annotations
  • Loading branch information
mtomilov authored Dec 5, 2024
2 parents a87b6b2 + 9130d7e commit 06cc18e
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .cookiecutter/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"template": "https://github.com/hypothesis/cookiecutters",
"checkout": null,
"directory": "pypackage",
"ignore": [],
"ignore": ["tests/functional/sanity_test.py"],
"extra_context": {
"name": "slack-annotations",
"package_name": "slack_annotations",
Expand Down
2 changes: 2 additions & 0 deletions .cookiecutter/includes/tox/deps
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lint,functests: pytest-httpx
lint,functests: freezegun
6 changes: 5 additions & 1 deletion src/slack_annotations/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import httpx

SEARCH_HOURS = 1


def notify(
search_params=None, token=None, cache_path=None
Expand All @@ -14,7 +16,9 @@ def notify(
# values are needed for the algorithm below to work.
search_params["sort"] = "created"
search_params["order"] = "asc"
search_params["search_after"] = (datetime.now(UTC) - timedelta(hours=1)).isoformat()
search_params["search_after"] = (
datetime.now(UTC) - timedelta(hours=SEARCH_HOURS)
).isoformat()

if cache_path:
try:
Expand Down
170 changes: 170 additions & 0 deletions tests/functional/cli_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import json
from datetime import UTC, datetime, timedelta
from subprocess import run

import httpx
import pytest
from freezegun import freeze_time

from src.slack_annotations.cli import cli
from src.slack_annotations.core import SEARCH_HOURS


def test_help():
"""Test the slack-annotations --help command."""
Expand All @@ -9,3 +18,164 @@ def test_help():
def test_version():
"""Test the slack-annotations --version command."""
run(["slack-annotations", "--version"], check=True)


@freeze_time("2024-12-01T01:00:00+00:00")
def test_cli_default(search_annotations, slack_annotations, httpx_mock, capsys):
"""Test the slack-annotations with default options."""
search_after = (datetime.now(UTC) - timedelta(hours=SEARCH_HOURS)).isoformat()
params = {
"sort": "created",
"order": "asc",
"search_after": search_after,
}
httpx_mock.add_response(
url=httpx.URL("https://hypothes.is/api/search", params=params),
content=json.dumps(search_annotations),
)

cli([])

assert capsys.readouterr().out.strip() == json.dumps(slack_annotations)


@freeze_time("2024-12-01T01:00:00+00:00")
def test_cli_with_search_after_from_cache_file(
search_annotations, slack_annotations, httpx_mock, tmp_path, capsys
):
"""Test the slack-annotations with a search_after from the cache file."""
search_after = "2024-12-01T00:30:00+00:00"
params = {
"sort": "created",
"order": "asc",
"search_after": search_after,
}
httpx_mock.add_response(
url=httpx.URL("https://hypothes.is/api/search", params=params),
content=json.dumps(search_annotations),
)
cache_path = tmp_path / "cache.json"
cache_path.write_text(json.dumps({"search_after": search_after}))

cli(["--cache-path", str(cache_path)])

assert capsys.readouterr().out.strip() == json.dumps(slack_annotations)
assert json.loads(cache_path.read_text()) == {
"search_after": "2024-12-03T18:40:42.325652+00:00"
}


@freeze_time("2024-12-01T01:00:00+00:00")
def test_cli_token(search_annotations, slack_annotations, httpx_mock, capsys):
"""Test the slack-annotations with a token."""
search_after = (datetime.now(UTC) - timedelta(hours=SEARCH_HOURS)).isoformat()
params = {
"sort": "created",
"order": "asc",
"search_after": search_after,
}
token = "test-token"
httpx_mock.add_response(
url=httpx.URL("https://hypothes.is/api/search", params=params),
content=json.dumps(search_annotations),
match_headers={"Authorization": f"Bearer {token}"},
)

cli(["--token", token])

assert capsys.readouterr().out.strip() == json.dumps(slack_annotations)


@pytest.fixture
def slack_annotations():
return {
"text": "2 new annotations",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "`test_user_1` (md............................) annotated <https://example.com/|Annotating the law | Hypothes.is>:",
},
"fields": [
{"type": "mrkdwn", "text": "*Quote:*"},
{
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/test_annotation_id_1/example.com/|in-context link>):",
},
{"type": "plain_text", "text": "(None)"},
{"type": "plain_text", "text": "test_user_1 reply"},
],
},
{"type": "divider"},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "`test_user_2` (Test User 2) annotated <https://web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/|Annotating the law | Hypothes.is>:",
},
"fields": [
{"type": "mrkdwn", "text": "*Quote:*"},
{
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/test_annotation_id_2/web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/|in-context link>):",
},
{
"type": "plain_text",
"text": "The ability to collaborate effectively is invaluable in today\u2019s interconnected world. Whether in academic or professional settings, tools that streamline communication, enhance content sharing and promote open dialogue are important. Hypothesis is a standout tool in this regard, especially renowned for its capabilities in group annotations and as an online learning tool.",
},
{"type": "plain_text", "text": "A useful tool!"},
],
},
{"type": "divider"},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "These annotations are posted to Slack by a <https://github.com/hypothesis/slack-annotations/|GitHub Actions workflow>",
}
],
},
],
}


@pytest.fixture
def search_annotations():
return {
"rows": [
{
"created": "2024-12-02T18:34:42.333087+00:00",
"user": "acct:[email protected]",
"uri": "https://example.com/",
"text": "test_user_1 reply",
"document": {"title": ["Annotating the law | Hypothes.is"]},
"links": {
"incontext": "https://hyp.is/test_annotation_id_1/example.com/"
},
"user_info": {"display_name": "md............................"},
},
{
"created": "2024-12-03T18:40:42.325652+00:00",
"user": "acct:[email protected]",
"uri": "https://web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/",
"text": "A useful tool!",
"target": [
{
"source": "https://web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/",
"selector": [
{
"exact": "The ability to collaborate effectively is invaluable in today’s interconnected world. Whether in academic or professional settings, tools that streamline communication, enhance content sharing and promote open dialogue are important. Hypothesis is a standout tool in this regard, especially renowned for its capabilities in group annotations and as an online learning tool."
},
],
}
],
"document": {"title": ["Annotating the law | Hypothes.is"]},
"links": {
"incontext": "https://hyp.is/test_annotation_id_2/web.hypothes.is/blog/step-by-step-guide-to-using-hypothesis-for-collaborative-projects/"
},
"user_info": {"display_name": "Test User 2"},
},
]
}
2 changes: 0 additions & 2 deletions tests/functional/sanity_test.py

This file was deleted.

2 changes: 2 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ deps =
lint,tests,functests: h-matchers
lint,template: cookiecutter
typecheck: mypy
lint,functests: pytest-httpx
lint,functests: freezegun
depends =
coverage: tests,py{312,311}-tests
commands =
Expand Down

0 comments on commit 06cc18e

Please sign in to comment.