Skip to content

Commit

Permalink
Add functests with sample data
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomilov committed Dec 4, 2024
1 parent 10a8960 commit 7380779
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/slack_annotations/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ 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()
if not search_params.get("search_after"):
search_params["search_after"] = (
datetime.now(UTC) - timedelta(hours=1)
).isoformat()

if cache_path:
try:
Expand Down
116 changes: 116 additions & 0 deletions tests/data/search_annotations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
{
"total": 1000,
"rows": [
{
"id": "gtu_pDTHEe-6-NN-lcCgBQ",
"created": "2024-06-27T20:54:56.605900+00:00",
"updated": "2024-11-29T15:12:47.777369+00:00",
"user": "acct:[email protected]",
"uri": "https://example.com/",
"text": " test",
"tags": [],
"group": "__world__",
"permissions": {
"read": [
"group:__world__"
],
"admin": [
"acct:[email protected]"
],
"update": [
"acct:[email protected]"
],
"delete": [
"acct:[email protected]"
]
},
"target": [
{
"source": "https://example.com/",
"selector": [
{
"type": "RangeSelector",
"endOffset": 4,
"startOffset": 0,
"endContainer": "/div[1]/h1[1]/hypothesis-highlight[2]/hypothesis-highlight[1]/hypothesis-highlight[1]/hypothesis-highlight[1]/hypothesis-highlight[1]",
"startContainer": "/div[1]/h1[1]/hypothesis-highlight[1]/hypothesis-highlight[1]"
},
{
"end": 20,
"type": "TextPositionSelector",
"start": 6
},
{
"type": "TextQuoteSelector",
"exact": "Example Domain",
"prefix": "\n\n ",
"suffix": "\n This domain is for use in i"
}
]
}
],
"document": {
"title": [
"Annotating the law | Hypothes.is"
]
},
"links": {
"html": "https://hypothes.is/a/gtu_pDTHEe-6-NN-lcCgBQ",
"incontext": "https://hyp.is/gtu_pDTHEe-6-NN-lcCgBQ/example.com/",
"json": "https://hypothes.is/api/annotations/gtu_pDTHEe-6-NN-lcCgBQ"
},
"user_info": {
"display_name": null
},
"flagged": false,
"hidden": false
},
{
"id": "wU0NQqjzEe-eIPcuiGN1Hg",
"created": "2024-11-22T17:03:54.248643+00:00",
"updated": "2024-11-22T17:03:54.248643+00:00",
"user": "acct:[email protected]",
"uri": "https://example.com",
"text": "This is a test annotation.",
"tags": [
"Test",
"API"
],
"group": "__world__",
"permissions": {
"read": [
"group:__world__"
],
"admin": [
"acct:[email protected]"
],
"update": [
"acct:[email protected]"
],
"delete": [
"acct:[email protected]"
]
},
"target": [
{
"source": "https://example.com"
}
],
"document": {
"title": [
"Annotating the law | Hypothes.is"
]
},
"links": {
"html": "https://hypothes.is/a/wU0NQqjzEe-eIPcuiGN1Hg",
"incontext": "https://hyp.is/wU0NQqjzEe-eIPcuiGN1Hg/example.com",
"json": "https://hypothes.is/api/annotations/wU0NQqjzEe-eIPcuiGN1Hg"
},
"user_info": {
"display_name": null
},
"flagged": false,
"hidden": false
}
]
}
70 changes: 70 additions & 0 deletions tests/data/slack_annotations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"text": "2 new annotations",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "`Janrae.Valencia` (None) annotated <https://example.com/|Annotating the law | Hypothes.is>:"
},
"fields": [
{
"type": "mrkdwn",
"text": "*Quote:*"
},
{
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/gtu_pDTHEe-6-NN-lcCgBQ/example.com/|in-context link>):"
},
{
"type": "plain_text",
"text": "Example Domain"
},
{
"type": "plain_text",
"text": " test"
}
]
},
{
"type": "divider"
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "`4egson` (None) annotated <https://example.com|Annotating the law | Hypothes.is>:"
},
"fields": [
{
"type": "mrkdwn",
"text": "*Quote:*"
},
{
"type": "mrkdwn",
"text": "*Annotation* (<https://hyp.is/wU0NQqjzEe-eIPcuiGN1Hg/example.com|in-context link>):"
},
{
"type": "plain_text",
"text": "(None)"
},
{
"type": "plain_text",
"text": "This is a test annotation."
}
]
},
{
"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>"
}
]
}
]
}
38 changes: 38 additions & 0 deletions tests/functional/core_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
import pytest

from slack_annotations.core import notify


@pytest.fixture
def slack_annotations():
with open("tests/data/slack_annotations.json") as f:
return json.load(f)


@pytest.fixture
def search_annotations():
with open("tests/data/search_annotations.json") as f:
return json.load(f)


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"
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)


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"
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
)
assert json.loads(cache_path.read_text()) == {
"search_after": "2024-11-22T17:03:54.248643+00:00"
}

0 comments on commit 7380779

Please sign in to comment.