Spaces:
Running on T4
Running on T4
Download backend/tests/integration/test_notifications.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 5.42 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_notifications.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/tests/integration/test_notifications.py
-
curl -L -o test_notifications.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/tests/integration/test_notifications.py
5.42 kB
| """N06 to N09.""" | |
| import uuid | |
| from datetime import UTC, datetime, timedelta | |
| from sqlalchemy import update | |
| from app.common.notify import notify | |
| from app.core.database import get_session_factory | |
| from app.modules.notifications.models import Notification | |
| from tests.factories import make_account | |
| async def _notify(recipient, n: int, *, kind="drill", target=None, age_s: int = 0) -> uuid.UUID: | |
| async with get_session_factory()() as db: | |
| id_ = await notify(db, None, recipient["id"], kind=kind, title=f"T{n}", body="b", target=target, | |
| event_key=f"e{n}-{uuid.uuid4().hex[:4]}") | |
| await db.execute(update(Notification).where(Notification.id == id_).values( | |
| created_at=datetime.now(UTC) - timedelta(seconds=age_s))) | |
| await db.commit() | |
| return id_ | |
| async def test_list_newest_first_with_paging_and_no_side_effects(client): | |
| me = await make_account() | |
| h = me["headers"] | |
| sid = uuid.uuid4() | |
| ids = [await _notify(me, i, age_s=100 - i, target={"session_id": sid} if i == 0 else None) for i in range(3)] | |
| first = (await client.get("/v1/notifications?limit=2", headers=h)).json() | |
| assert [i["id"] for i in first["items"]] == [str(ids[2]), str(ids[1])] | |
| assert first["next_cursor"] | |
| second = (await client.get(f"/v1/notifications?limit=2&cursor={first['next_cursor']}", headers=h)).json() | |
| assert [i["id"] for i in second["items"]] == [str(ids[0])] and second["next_cursor"] is None | |
| item = second["items"][0] | |
| assert item["target"] == {"session_id": str(sid)} and item["read"] is False and item["kind"] == "drill" | |
| assert (await client.get("/v1/notifications/unread-count", headers=h)).json() == {"count": 3} | |
| async def test_list_empty_and_scoped_to_recipient(client): | |
| me, other = await make_account(), await make_account() | |
| await _notify(other, 1) | |
| assert (await client.get("/v1/notifications", headers=me["headers"])).json() == {"items": [], "next_cursor": None} | |
| assert (await client.get("/v1/notifications/unread-count", headers=me["headers"])).json() == {"count": 0} | |
| async def test_list_errors(client): | |
| me = await make_account() | |
| assert (await client.get("/v1/notifications")).status_code == 401 | |
| assert (await client.get("/v1/notifications?limit=0", headers=me["headers"])).status_code == 400 | |
| assert (await client.get("/v1/notifications?cursor=junk", headers=me["headers"])).status_code == 400 | |
| assert (await client.get("/v1/notifications/unread-count")).status_code == 401 | |
| async def test_mark_one_read(client): | |
| me, other = await make_account(), await make_account() | |
| mine, theirs = await _notify(me, 1), await _notify(other, 2) | |
| h = me["headers"] | |
| assert (await client.post(f"/v1/notifications/{mine}/read", headers=h)).status_code == 204 | |
| assert (await client.post(f"/v1/notifications/{mine}/read", headers=h)).status_code == 204 # repeat | |
| items = (await client.get("/v1/notifications", headers=h)).json()["items"] | |
| assert items[0]["read"] is True | |
| assert (await client.get("/v1/notifications/unread-count", headers=h)).json() == {"count": 0} | |
| for missing in (theirs, uuid.uuid4()): | |
| response = await client.post(f"/v1/notifications/{missing}/read", headers=h) | |
| assert response.status_code == 404 and response.json()["error"]["code"] == "NOT_FOUND" | |
| assert (await client.post(f"/v1/notifications/{theirs}/read", headers=other["headers"])).status_code == 204 | |
| assert (await client.post("/v1/notifications/not-a-uuid/read", headers=h)).status_code == 400 | |
| assert (await client.post(f"/v1/notifications/{mine}/read")).status_code == 401 | |
| async def test_read_all_without_body_and_with_through(client): | |
| me, other = await make_account(), await make_account() | |
| h = me["headers"] | |
| await _notify(me, 1, age_s=300) | |
| await _notify(me, 2, age_s=200) | |
| late = await _notify(me, 3, age_s=10) | |
| await _notify(other, 4) | |
| through = (datetime.now(UTC) - timedelta(seconds=100)).isoformat() | |
| assert (await client.post("/v1/notifications/read-all", json={"through": through}, headers=h)).status_code == 204 | |
| assert (await client.get("/v1/notifications/unread-count", headers=h)).json() == {"count": 1} | |
| unread = [i for i in (await client.get("/v1/notifications", headers=h)).json()["items"] if not i["read"]] | |
| assert [i["id"] for i in unread] == [str(late)] | |
| assert (await client.post("/v1/notifications/read-all", headers=h)).status_code == 204 | |
| assert (await client.get("/v1/notifications/unread-count", headers=h)).json() == {"count": 0} | |
| assert (await client.get("/v1/notifications/unread-count", headers=other["headers"])).json() == {"count": 1} | |
| async def test_read_all_future_is_clamped_and_bad_body_rejected(client): | |
| me = await make_account() | |
| h = me["headers"] | |
| await _notify(me, 1) | |
| future = (datetime.now(UTC) + timedelta(days=1)).isoformat() | |
| assert (await client.post("/v1/notifications/read-all", json={"through": future}, headers=h)).status_code == 204 | |
| assert (await client.get("/v1/notifications/unread-count", headers=h)).json() == {"count": 0} | |
| bad = await client.post("/v1/notifications/read-all", json={"through": "yesterday"}, headers=h) | |
| assert bad.status_code == 400 | |
| assert (await client.post("/v1/notifications/read-all", json={"x": 1}, headers=h)).status_code == 400 | |
| assert (await client.post("/v1/notifications/read-all")).status_code == 401 | |