Files
mtusi2026lab4/student-tests/helpers.py

43 lines
1.6 KiB
Python

from pathlib import Path
import sys
import unittest
from urllib.parse import urlencode
from uuid import uuid4
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client
class ApiTestCase(unittest.TestCase):
def setUp(self):
self.client = Client()
self.alice, self.bob, self.eve, self.chat = self.client.bootstrap()
self.path = "/api/v1/chats/" + self.chat["id"]
def assert_error(self, method, path, status, code, **kwargs):
if method == "POST" and path.endswith("/messages"):
kwargs["headers"] = {"Idempotency-Key": uuid4().hex, **kwargs.get("headers", {})}
result = self.client.call(method, path, status, **kwargs)
self.assertEqual(result["json"]["error"]["code"], code)
return result["json"]["error"]
def read_all(self, path, actor, schema, limit=3):
items = []
cursor = None
seen_cursors = set()
while True:
query = {"limit": limit}
if cursor is not None:
query["cursor"] = cursor
page = self.client.call("GET", path + "?" + urlencode(query), actor=actor, schema=schema)["json"]
self.assertLessEqual(len(page["items"]), limit)
items.extend(page["items"])
cursor = page["next_cursor"]
if cursor is None:
return items
self.assertTrue(page["items"], "Пустая страница не должна вести к следующей")
self.assertNotIn(cursor, seen_cursors, "Зацикливание пагинации")
seen_cursors.add(cursor)