Skip to content
Snippets Groups Projects
Verified Commit a7abb1bf authored by Daniel Schultz's avatar Daniel Schultz :tm:
Browse files

Support database interaction in tests

We want our integration tests to run against a test database.  This
allows us to create more specific tests (e.g. predict ID values) in
addition to making sure we don't interact with real data.

I decided to use the PostgreSQL schema support instead of requiring
a separate `TEST_DATABASE_URL`.  This means there is less setup for
a dev environment but also makes it easier for us to drop / create the
schema as part of the tests.
parent fd97d4b2
No related branches found
No related tags found
1 merge request!3Add `POST /conversions` endpoint
......@@ -27,8 +27,13 @@ test:
S3_ENDPOINT: "https://example.com"
S3_REGION: "us-east-1"
S3_BUCKET: "example"
DATABASE_URL: "postgresql://test_user:password@localhost/test_database"
# Used by gitlab CI to create the database
POSTGRES_DB: "test_database"
POSTGRES_USER: "test_user"
POSTGRES_PASSWORD: "password"
POSTGRES_HOST_AUTH_METHOD: trust
# Used by our code to connect to the database
PGHOST: 'localhost'
PGDATABASE: 'test_database'
PGUSER: 'test_user'
PGPASSWORD: 'password'
......@@ -16,7 +16,7 @@ install:
pip install -r requirements-dev.txt
test:
pytest tests/
APP_SCHEMA=test pytest tests/
migrate:
alembic upgrade head
import pytest
from sqlalchemy import text
from alembic import command
from alembic.config import Config
from service.core.database import engine
from service.core.settings import settings
def assert_app_schema_is_not_public():
if settings.APP_SCHEMA == "public":
pytest.exit(
"You must override APP_SCHEMA to a value other than `public` when running tests. These tests are data-destructive."
)
def drop_and_create_configured_schema():
assert_app_schema_is_not_public() # This is intentionally / defensively redundant since we're about to drop the schema.
with engine.connect() as connection:
connection.execute(text(f"DROP SCHEMA IF EXISTS {settings.APP_SCHEMA} CASCADE"))
connection.execute(text(f"CREATE SCHEMA IF NOT EXISTS {settings.APP_SCHEMA}"))
connection.commit()
def drop_configured_schema():
with engine.connect() as connection:
connection.execute(text(f"DROP SCHEMA IF EXISTS {settings.APP_SCHEMA} CASCADE"))
connection.commit()
def run_migrations():
alembic_cfg = Config("alembic.ini")
command.upgrade(alembic_cfg, "head")
@pytest.hookimpl(tryfirst=True)
def pytest_sessionstart():
assert_app_schema_is_not_public()
@pytest.fixture(scope="function", autouse=True)
def setup_and_teardown_db():
drop_and_create_configured_schema()
run_migrations()
yield
drop_configured_schema()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment