forked from mtusi2026/mtusi2026lab5
feat:report
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
"""Shared helpers for student lab5 tests; no tokens are logged."""
|
||||
import base64, json, os, subprocess, sys, time, uuid
|
||||
from pathlib import Path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
from tests.client import Client, Failure, check # noqa: E402
|
||||
|
||||
def fail(msg):
|
||||
print('FAIL:', msg)
|
||||
raise SystemExit(1)
|
||||
|
||||
def ok(msg='PASS'):
|
||||
print(msg)
|
||||
|
||||
def docker(*args, check_rc=True):
|
||||
"""Run docker compose <args>; return stdout. Raises if rc!=0 and check_rc."""
|
||||
rc = subprocess.run(['docker', 'compose', *args], capture_output=True, text=True)
|
||||
if check_rc and rc.returncode != 0:
|
||||
raise Failure('docker compose %s: %s' % (' '.join(args), rc.stderr.strip()))
|
||||
return rc.stdout
|
||||
|
||||
def psql(sql):
|
||||
return docker('exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger',
|
||||
'-tAc', sql).strip()
|
||||
|
||||
def upload(c, actor, chat, content, filename='sample.png', ct='image/png'):
|
||||
return c.call('POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename,
|
||||
202, actor=actor, raw=content, headers={'Content-Type': ct},
|
||||
schema='Attachment')['json']
|
||||
|
||||
def wait_status(c, actor, att_id, want, seconds=60):
|
||||
deadline = time.monotonic() + seconds
|
||||
while time.monotonic() < deadline:
|
||||
att = c.call('GET', '/api/v1/attachments/' + att_id, actor=actor, schema='Attachment')['json']
|
||||
if att['status'] == want:
|
||||
return att
|
||||
if att['status'] == 'failed' and want != 'failed':
|
||||
fail('attachment failed: ' + str(att.get('error_code')))
|
||||
time.sleep(0.25)
|
||||
fail('attachment did not reach %s in %ss' % (want, seconds))
|
||||
|
||||
def png_bytes(w, h, seed=0):
|
||||
"""Generate a valid PNG without external libs.
|
||||
|
||||
Быстро и с малой памятью: одна строка пикселей переиспользуется.
|
||||
Для 7000x7000 занимает ~1-2 секунды и ~150 MiB RAM.
|
||||
"""
|
||||
import struct, zlib
|
||||
|
||||
# одна строка: filter byte 0 + w * 3 байта RGB
|
||||
row = bytearray(1 + w * 3)
|
||||
for x in range(w):
|
||||
row[1 + x*3] = (x * 7 + seed) & 0xFF
|
||||
row[2 + x*3] = (x * 11) & 0xFF
|
||||
row[3 + x*3] = (x * 13) & 0xFF
|
||||
row = bytes(row)
|
||||
|
||||
# zlib потоково, без накопления всего raw
|
||||
co = zlib.compressobj(level=6)
|
||||
idat_parts = []
|
||||
for y in range(h):
|
||||
idat_parts.append(co.compress(row))
|
||||
idat_parts.append(co.flush())
|
||||
idat = b''.join(idat_parts)
|
||||
|
||||
def chunk(t, d):
|
||||
return struct.pack('>I', len(d)) + t + d + struct.pack('>I', zlib.crc32(t + d) & 0xffffffff)
|
||||
|
||||
ihdr = struct.pack('>IIBBBBB', w, h, 8, 2, 0, 0, 0)
|
||||
return (b'\x89PNG\r\n\x1a\n'
|
||||
+ chunk(b'IHDR', ihdr)
|
||||
+ chunk(b'IDAT', idat)
|
||||
+ chunk(b'IEND', b''))
|
||||
Reference in New Issue
Block a user