ЛР2: PostgreSQL + Valkey, сессии Basic/Bearer, миграции, compose, тесты, отчёт

This commit is contained in:
2026-10-15 19:17:22 +03:00
parent 65003b6119
commit 6808ed8c98
44 changed files with 2370 additions and 19 deletions
+35
View File
@@ -0,0 +1,35 @@
"""Grade-4 integrity checks: registration race and composite-op rollback."""
from __future__ import annotations
import concurrent.futures
import os
def test_concurrent_duplicate_username_single_winner(client):
username = "race_dup_" + os.urandom(4).hex()
password = "T3st-" + os.urandom(8).hex()
def register(_):
return client.register(username, password).status
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as pool:
statuses = list(pool.map(register, range(5)))
assert statuses.count(201) == 1, statuses
assert statuses.count(409) == 4, statuses
def test_failed_chat_creation_rolls_back(client, alice):
def chats_count():
return len(client.request("GET", "/api/v1/chats?limit=100", token=alice["token"]).json["items"])
before = chats_count()
unknown = "00000000-0000-4000-8000-000000000000"
response = client.create_chat(alice["token"], [alice["id"], unknown], title="should-rollback")
assert response.status == 400, response.body
after = chats_count()
assert after == before, "failed composite operation left a partial chat"
# And nothing titled like the failed attempt is visible.
listed = client.request("GET", "/api/v1/chats?limit=100", token=alice["token"]).json["items"]
assert all(item["title"] != "should-rollback" for item in listed)