Files
mtusi2026lab2/student-tests/test_pagination.py

49 lines
2.1 KiB
Python

"""Pure cursor checks: python -m unittest discover -s student-tests -p test_pagination.py."""
import importlib.util
import unittest
from datetime import datetime, timezone
from uuid import UUID
class PaginationTests(unittest.TestCase):
def setUp(self):
self.assertIsNotNone(
importlib.util.find_spec("src.pagination"),
"SQL pagination helper has not been implemented",
)
from src import pagination
from src.errors import ApiError
self.p = pagination
self.error = ApiError
def test_cursor_round_trip_and_scope(self):
item = {"id": "00000000-0000-4000-8000-000000000001",
"created_at": "2026-10-19T12:00:00.123456Z"}
cursor = self.p.encode_cursor("messages:chat-a", item)
limit, after = self.p.parse_page("17", cursor, "messages:chat-a")
self.assertEqual(limit, 17)
self.assertEqual(after, (datetime(2026, 10, 19, 12, 0, 0, 123456,
tzinfo=timezone.utc), UUID(item["id"])))
with self.assertRaises(self.error):
self.p.parse_page("17", cursor, "messages:chat-b")
def test_invalid_limits_and_cursors(self):
for value in ("0", "101", "1.5", "abc", "-1"):
with self.subTest(limit=value), self.assertRaises(self.error):
self.p.parse_page(value, None, "scope")
for value in ("", "garbage", "!!!!", "bnVsbA", "W10"):
with self.subTest(cursor=value), self.assertRaises(self.error):
self.p.parse_page("50", value, "scope")
self.assertEqual(self.p.parse_page("50", None, "scope"), (50, None))
def test_cursor_requires_complete_utc_datetime(self):
for timestamp in ("2026-01-01Z", "2026-01-01T00:00:00", "2026-01-01T00:00:00+03:00"):
item = {"id": "00000000-0000-4000-8000-000000000001", "created_at": timestamp}
cursor = self.p.encode_cursor("scope", item)
with self.subTest(timestamp=timestamp), self.assertRaises(self.error):
self.p.parse_page("50", cursor, "scope")
if __name__ == "__main__":
unittest.main(verbosity=2)