add tests
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""Миграции с нуля: «миграции с нуля и повторный запуск без дублирования данных»."""
|
||||
"""Миграции: применение с нуля, идемпотентный повтор, совпадение со схемой моделей."""
|
||||
|
||||
import pytest
|
||||
from alembic.autogenerate import compare_metadata
|
||||
@@ -8,7 +8,7 @@ from sqlalchemy import inspect, text
|
||||
from app.models import Base
|
||||
from tests.conftest import reset_schema, upgrade_head
|
||||
|
||||
EXPECTED_TABLES = {"users", "chats", "chat_members", "messages", "sessions"}
|
||||
EXPECTED_TABLES = {"users", "chats", "chat_members", "messages", "counters"}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -24,11 +24,15 @@ async def migrated_engine(database_url, db_engine):
|
||||
|
||||
async def test_upgrade_creates_all_tables(migrated_engine):
|
||||
async with migrated_engine.connect() as connection:
|
||||
tables = await connection.run_sync(lambda sync: set(inspect(sync).get_table_names()))
|
||||
tables = await connection.run_sync(
|
||||
lambda sync: set(inspect(sync).get_table_names())
|
||||
)
|
||||
assert EXPECTED_TABLES <= tables
|
||||
|
||||
|
||||
async def test_upgrade_twice_is_idempotent_and_keeps_data(migrated_engine, database_url):
|
||||
async def test_upgrade_twice_is_idempotent_and_keeps_data(
|
||||
migrated_engine, database_url
|
||||
):
|
||||
async with migrated_engine.begin() as connection:
|
||||
await connection.execute(
|
||||
text(
|
||||
@@ -41,17 +45,23 @@ async def test_upgrade_twice_is_idempotent_and_keeps_data(migrated_engine, datab
|
||||
|
||||
async with migrated_engine.connect() as connection:
|
||||
count = (
|
||||
await connection.execute(text("SELECT count(*) FROM users WHERE username = 'keep_me'"))
|
||||
await connection.execute(
|
||||
text("SELECT count(*) FROM users WHERE username = 'keep_me'")
|
||||
)
|
||||
).scalar_one()
|
||||
assert count == 1
|
||||
|
||||
|
||||
async def test_models_and_migrations_are_in_sync(migrated_engine):
|
||||
def compare(sync_connection):
|
||||
context = MigrationContext.configure(sync_connection, opts={"compare_type": True})
|
||||
context = MigrationContext.configure(
|
||||
sync_connection, opts={"compare_type": True}
|
||||
)
|
||||
return compare_metadata(context, Base.metadata)
|
||||
|
||||
async with migrated_engine.connect() as connection:
|
||||
diff = await connection.run_sync(compare)
|
||||
|
||||
assert diff == [], f"Создайте миграцию: alembic revision --autogenerate. Diff: {diff}"
|
||||
assert diff == [], (
|
||||
f"Создайте миграцию: alembic revision --autogenerate. Diff: {diff}"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user