Bring the application, own tests, Dockerfile, toolchain, compose stack, HAProxy and k6 configuration, migrations, hooks, evidence and level 4 scripts and the CI job from the lab 3 solution commit 61c0c28, merge the lab 3 Makefile targets into the lab 4 skeleton Makefile without touching its targets, and append the lab 3 solution variables to the skeleton .env.example. The package and the application follow the contract version 1.4.0, and the image and the throwaway test containers are tagged lab4 so they do not collide with a lab 3 stand on the same host. This is the starting point of the lab 4 cycle: lint, own tests and the offline skeleton check pass before any lab 4 change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
78 lines
2.7 KiB
Python
78 lines
2.7 KiB
Python
import asyncpg
|
|
import pytest
|
|
|
|
from conftest import DATABASE, scalar
|
|
from messenger.migrate import MigrationChangedError, migrate, migrations
|
|
|
|
COUNTS = """
|
|
SELECT (SELECT count(*) FROM users), (SELECT count(*) FROM sessions),
|
|
(SELECT count(*) FROM chats), (SELECT count(*) FROM chat_members),
|
|
(SELECT count(*) FROM messages), (SELECT count(*) FROM idempotency_keys)
|
|
"""
|
|
|
|
|
|
async def counts() -> list[int]:
|
|
connection = await asyncpg.connect(DATABASE["url"])
|
|
try:
|
|
return list(await connection.fetchrow(COUNTS))
|
|
finally:
|
|
await connection.close()
|
|
|
|
|
|
async def test_migrations_are_ordered_and_versioned() -> None:
|
|
versions = [version for version, _ in migrations()]
|
|
assert versions == sorted(versions)
|
|
assert versions[:2] == ["0001_init", "0002_idempotency_keys"]
|
|
applied = await scalar("SELECT array_agg(version ORDER BY version) FROM schema_migrations")
|
|
assert applied == versions
|
|
|
|
|
|
async def test_rerun_changes_nothing() -> None:
|
|
before = await counts()
|
|
assert await migrate(DATABASE["url"]) == []
|
|
assert await counts() == before
|
|
|
|
|
|
async def test_changed_migration_is_refused() -> None:
|
|
connection = await asyncpg.connect(DATABASE["url"])
|
|
try:
|
|
original = await connection.fetchval(
|
|
"SELECT checksum FROM schema_migrations WHERE version = '0001_init'"
|
|
)
|
|
await connection.execute(
|
|
"UPDATE schema_migrations SET checksum = 'tampered' WHERE version = '0001_init'"
|
|
)
|
|
with pytest.raises(MigrationChangedError):
|
|
await migrate(DATABASE["url"])
|
|
await connection.execute(
|
|
"UPDATE schema_migrations SET checksum = $1 WHERE version = '0001_init'", original
|
|
)
|
|
finally:
|
|
await connection.close()
|
|
|
|
|
|
async def test_sender_must_be_member_even_bypassing_api() -> None:
|
|
connection = await asyncpg.connect(DATABASE["url"])
|
|
try:
|
|
with pytest.raises(asyncpg.ForeignKeyViolationError):
|
|
await connection.execute(
|
|
"INSERT INTO messages (id, chat_id, sender_id, text, created_at) "
|
|
"VALUES (gen_random_uuid(), gen_random_uuid(), gen_random_uuid(), 'x', now())"
|
|
)
|
|
finally:
|
|
await connection.close()
|
|
|
|
|
|
async def test_key_needs_a_member_and_a_message() -> None:
|
|
connection = await asyncpg.connect(DATABASE["url"])
|
|
try:
|
|
with pytest.raises(asyncpg.ForeignKeyViolationError):
|
|
await connection.execute(
|
|
"INSERT INTO idempotency_keys "
|
|
"(chat_id, user_id, key, fingerprint, message_id, created_at) "
|
|
"VALUES (gen_random_uuid(), gen_random_uuid(), 'k-1', "
|
|
"sha256('x'), gen_random_uuid(), now())"
|
|
)
|
|
finally:
|
|
await connection.close()
|