Forward-port lab3 SQLAlchemy and Alembic foundation

Carry source f591fa64e5 forward from 72a1e57. Preserve lab4 attachment transactions, reconciliation locks, quarantine, and existing schema data through verified legacy adoption. Keep report and evidence work in progress untouched.
This commit is contained in:
Artyom
2026-11-06 01:47:30 +03:00
parent dd4021b678
commit 8e4532f6f5
54 changed files with 3459 additions and 617 deletions
+23 -6
View File
@@ -4,8 +4,10 @@ from uuid import uuid4
import pytest
from httpx import AsyncClient, Response
from sqlalchemy import func, select
from conftest import actor, keyed, make_chat, register, running_app, scalar
from messenger.messaging.infrastructure.postgres import IdempotencyKeyRow
INVALID_KEYS = ["", "k" * 129, "два слова", "a b", "a/b", "ключ"]
@@ -93,12 +95,22 @@ async def test_key_is_scoped_by_user_and_chat(client: AsyncClient) -> None:
bob = await register(client, "bob")
chat = await make_chat(client, alice, bob)
other = await make_chat(client, alice)
ids = {
(await send(client, chat, alice, "алиса", "shared-1")).json()["id"],
(await send(client, chat, bob, "боб", "shared-1")).json()["id"],
(await send(client, other, alice, "другой чат", "shared-1")).json()["id"],
}
cases = [(chat, alice, "алиса"), (chat, bob, "боб"), (other, alice, "другой чат")]
messages = []
for target, user, text in cases:
response = await send(client, target, user, text, "shared-1")
assert response.status_code == 201
message = response.json()
assert (message["chat_id"], message["sender_id"], message["text"]) == (
target["id"],
user["id"],
text,
)
messages.append(message)
ids = {message["id"] for message in messages}
assert len(ids) == 3
for (target, user, text), message in zip(cases, messages, strict=True):
assert (await send(client, target, user, text, "shared-1")).json() == message
async def test_replay_checks_session_and_access_again(client: AsyncClient) -> None:
@@ -142,4 +154,9 @@ async def test_concurrent_retries_through_two_replicas_store_one_message() -> No
}
assert all(response.json() == responses[0].json() for response in responses)
assert [item["text"] for item in await history(second, chat, alice)] == [text]
assert await scalar("SELECT count(*) FROM idempotency_keys WHERE key = $1", key) == 1
assert (
await scalar(
select(func.count()).select_from(IdempotencyKeyRow).where(IdempotencyKeyRow.key == key)
)
== 1
)