def test_config(pytestconfig: Config) -> Configuration: config = load_configuration("testing") if config.tests.testcontainers: config.database.dsn = ( pytestconfig.database_container.get_connection_url().replace( "psycopg2", "asyncpg")) redis_exposed_port = pytestconfig.redis_container.get_exposed_port( pytestconfig.redis_container.port_to_expose) config.cache.dsn = f"redis://localhost:{redis_exposed_port}" return config
def pytest_configure(config: Config) -> None: app_config = load_configuration("testing") if app_config.tests.testcontainers: config.database_container = PostgresContainer( image="postgres:12", user="******", password="******", dbname="backend", ) config.redis_container = RedisContainer(image="redis:alpine") config.database_container.start() config.redis_container.start()
from logging.config import fileConfig from alembic import context from sqlalchemy import engine_from_config, pool from backend.config import load_configuration from backend.db import models app_config = load_configuration() SQLALCHEMY_URI = app_config.database.dsn.replace("asyncpg", "psycopg2") # this is the Alembic Config object, which provides # access to the values within the .ini file in use. config = context.config config.set_main_option("sqlalchemy.url", SQLALCHEMY_URI) # Interpret the config file for Python logging. # This line sets up loggers basically. fileConfig(config.config_file_name) # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata target_metadata = models.Base.metadata # other values from the config, defined by the needs of env.py, # can be acquired: # my_important_option = config.get_main_option("my_important_option") # ... etc.
def pytest_unconfigure(config: Config) -> None: app_config = load_configuration("testing") if app_config.tests.testcontainers: config.database_container.stop() config.redis_container.stop()
async def _create_user(username: str, password: str) -> None: config = load_configuration() engine = get_engine(config.database) async with AsyncSession(engine) as session: await session.run_sync(create_user, username, password)
async def _init_database() -> None: config = load_configuration() engine = get_engine(config.database) async with engine.begin() as conn: await conn.run_sync(Base.metadata.drop_all) await conn.run_sync(Base.metadata.create_all)