Spaces:
Runtime error
Runtime error
Greg
Claude Opus 4.7 (1M context)
feat(db): add detect_db_state helper for entrypoint orchestration
54aa65c Download backend/tests/test_db_state.py from Talosware/talos-access: direct link, hf CLI and curl.
- Browser
- Download file 2.3 kB
-
https://huggingface.co/spaces/Talosware/talos-access/resolve/main/backend/tests/test_db_state.py
- Command line
-
hf download hf://spaces/Talosware/talos-access/backend/tests/test_db_state.py
-
curl -L -o test_db_state.py https://huggingface.co/spaces/Talosware/talos-access/resolve/main/backend/tests/test_db_state.py
2.3 kB
| """Unit tests for db_state.detect_db_state. | |
| Tests run against the local docker-compose Postgres because the function uses | |
| SQLAlchemy's inspector and we want to verify it works against the same engine | |
| (postgresql+psycopg2) that production uses. Each test creates a dedicated | |
| schema, asserts, and cleans up. | |
| """ | |
| import pytest | |
| from sqlalchemy import text | |
| from app.core.database import engine | |
| from app.core.db_state import detect_db_state | |
| TEST_SCHEMA = "pytest_db_state" | |
| def clean_test_schema(): | |
| """Create a fresh empty schema before each test, drop it after.""" | |
| with engine.begin() as conn: | |
| conn.execute(text(f"DROP SCHEMA IF EXISTS {TEST_SCHEMA} CASCADE")) | |
| conn.execute(text(f"CREATE SCHEMA {TEST_SCHEMA}")) | |
| yield | |
| with engine.begin() as conn: | |
| conn.execute(text(f"DROP SCHEMA IF EXISTS {TEST_SCHEMA} CASCADE")) | |
| def test_empty_schema_returns_init(): | |
| """An empty schema with no tables must return INIT.""" | |
| assert detect_db_state(engine, schema=TEST_SCHEMA) == "INIT" | |
| def test_schema_with_alembic_version_returns_upgrade(): | |
| """Presence of alembic_version table marks the schema as alembic-managed.""" | |
| with engine.begin() as conn: | |
| conn.execute(text( | |
| f"CREATE TABLE {TEST_SCHEMA}.alembic_version " | |
| f"(version_num VARCHAR(32) PRIMARY KEY)" | |
| )) | |
| assert detect_db_state(engine, schema=TEST_SCHEMA) == "UPGRADE" | |
| def test_schema_with_tables_but_no_alembic_returns_stamp(): | |
| """Existing tables without alembic_version -> STAMP (adopt as head).""" | |
| with engine.begin() as conn: | |
| conn.execute(text( | |
| f"CREATE TABLE {TEST_SCHEMA}.accounts (id SERIAL PRIMARY KEY)" | |
| )) | |
| assert detect_db_state(engine, schema=TEST_SCHEMA) == "STAMP" | |
| def test_schema_with_alembic_and_other_tables_returns_upgrade(): | |
| """If alembic_version exists, UPGRADE regardless of other tables present.""" | |
| with engine.begin() as conn: | |
| conn.execute(text( | |
| f"CREATE TABLE {TEST_SCHEMA}.alembic_version " | |
| f"(version_num VARCHAR(32) PRIMARY KEY)" | |
| )) | |
| conn.execute(text( | |
| f"CREATE TABLE {TEST_SCHEMA}.users (id SERIAL PRIMARY KEY)" | |
| )) | |
| assert detect_db_state(engine, schema=TEST_SCHEMA) == "UPGRADE" | |