Basic testing infrastructure
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import TYPE_CHECKING, cast
|
||||
from uuid import UUID
|
||||
|
||||
@ -13,7 +14,12 @@ from litestar.contrib.sqlalchemy.plugins.init.config.common import (
|
||||
)
|
||||
from litestar.utils import delete_litestar_scope_state, get_litestar_scope_state
|
||||
from sqlalchemy import event
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
|
||||
from sqlalchemy.ext.asyncio import (
|
||||
AsyncEngine,
|
||||
AsyncSession,
|
||||
async_sessionmaker,
|
||||
create_async_engine,
|
||||
)
|
||||
from sqlalchemy.pool import NullPool
|
||||
|
||||
from app.lib import settings
|
||||
@ -37,25 +43,47 @@ def _default(val: Any) -> str:
|
||||
raise TypeError()
|
||||
|
||||
|
||||
db_connection_url = sqlalchemy.engine.URL.create(
|
||||
drivername="postgresql+asyncpg",
|
||||
username=settings.db.USER,
|
||||
password=settings.db.PASSWORD,
|
||||
host=settings.db.HOST,
|
||||
port=settings.db.PORT,
|
||||
database=settings.db.NAME,
|
||||
@dataclass
|
||||
class DBConnectionSettings:
|
||||
username: str
|
||||
password: str
|
||||
host: str
|
||||
port: int
|
||||
database: str
|
||||
|
||||
|
||||
def create_db_engine(connection_settings: DBConnectionSettings) -> AsyncEngine:
|
||||
db_connection_url = sqlalchemy.engine.URL.create(
|
||||
drivername="postgresql+asyncpg",
|
||||
username=connection_settings.username,
|
||||
password=connection_settings.password,
|
||||
host=connection_settings.host,
|
||||
port=connection_settings.port,
|
||||
database=connection_settings.database,
|
||||
)
|
||||
return create_async_engine(
|
||||
db_connection_url,
|
||||
echo=settings.db.ECHO,
|
||||
echo_pool=settings.db.ECHO_POOL,
|
||||
json_serializer=msgspec.json.Encoder(enc_hook=_default),
|
||||
max_overflow=settings.db.POOL_MAX_OVERFLOW,
|
||||
pool_size=settings.db.POOL_SIZE,
|
||||
pool_timeout=settings.db.POOL_TIMEOUT,
|
||||
poolclass=NullPool if settings.db.POOL_DISABLE else None,
|
||||
)
|
||||
|
||||
|
||||
engine = create_db_engine(
|
||||
connection_settings=DBConnectionSettings(
|
||||
username=settings.db.USER,
|
||||
password=settings.db.PASSWORD,
|
||||
host=settings.db.HOST,
|
||||
port=settings.db.PORT,
|
||||
database=settings.db.NAME,
|
||||
)
|
||||
)
|
||||
|
||||
engine = create_async_engine(
|
||||
db_connection_url,
|
||||
echo=settings.db.ECHO,
|
||||
echo_pool=settings.db.ECHO_POOL,
|
||||
json_serializer=msgspec.json.Encoder(enc_hook=_default),
|
||||
max_overflow=settings.db.POOL_MAX_OVERFLOW,
|
||||
pool_size=settings.db.POOL_SIZE,
|
||||
pool_timeout=settings.db.POOL_TIMEOUT,
|
||||
poolclass=NullPool if settings.db.POOL_DISABLE else None,
|
||||
)
|
||||
|
||||
"""Configure via DatabaseSettings.
|
||||
|
||||
Overrides default JSON
|
||||
|
||||
Reference in New Issue
Block a user