Files
mtusi2026lab2/tests/student-tests/test_pagination.py
T
Yuliiiaaaa 7255878240
Skeleton contracts / contracts (pull_request) Canceled after 0s
ЛР2 — БВТ2302 — Никитина — на 5 (трек 2)
2026-10-20 00:04:12 +03:00

69 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Walk multi-page history, verify no loss/dup/reorder, and limit change."""
import uuid
from _bootstrap import TESTS_DIR # noqa: F401
from client import Client, arguments, check, run, timestamp
def main():
args = arguments('Student pagination tests')
c = Client(args.base_url, args.ca_file)
alice, bob, eve, chat = c.bootstrap()
path = '/api/v1/chats/' + chat['id']
# Create 25 messages with different Unicode payloads.
created = []
for i in range(25):
created.append(c.message(alice, chat,
'строка-%02d-😀' % i)['json'])
created.sort(key=lambda m: (timestamp(m['created_at']), uuid.UUID(m['id']).int))
# Walk with limit=7
items = []
cursor = None
pages = 0
while True:
q = path + '/messages?limit=7'
if cursor:
from urllib.parse import quote
q += '&cursor=' + quote(cursor, safe='')
page = c.call('GET', q, actor=bob, schema='MessagePage')['json']
items.extend(page['items'])
pages += 1
if page['next_cursor'] is None:
break
cursor = page['next_cursor']
check(pages < 20, 'too many pages')
check(items == created,
'pagination lost/duplicated/reordered messages')
check(pages == 4, 'expected 4 pages, got %d' % pages)
# Change limit on continuation
first = c.call('GET', path + '/messages?limit=3', actor=alice,
schema='MessagePage')['json']
from urllib.parse import quote
second = c.call('GET',
path + '/messages?limit=10&cursor=' + quote(first['next_cursor'], safe=''),
actor=alice, schema='MessagePage')['json']
combined = first['items'] + second['items']
check(combined == created[:13], 'limit change broke the cursor')
# Cursor from another chat must be rejected with 400
other = c.call('POST', '/api/v1/chats', 201, actor=alice,
body={'title': 'Second', 'member_ids': [alice['id']]},
schema='Chat')['json']
c.call('GET', '/api/v1/chats/%s/messages?limit=1' % other['id'],
actor=alice, schema='MessagePage')
# Take a cursor from the original chat and try it on `other`
fake = c.call('GET', path + '/messages?limit=1', actor=alice,
schema='MessagePage')['json']['next_cursor']
c.call('GET',
'/api/v1/chats/%s/messages?limit=1&cursor=%s' % (other['id'], fake),
400, actor=alice)
print('PASS: student pagination tests (4 pages, no loss, cursor scoped)')
if __name__ == '__main__':
run(main)