48 lines
1.4 KiB
Python
48 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
import asyncio
|
|
from pathlib import Path
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.db.engine import create_engine
|
|
from app.db.models.base import Base
|
|
from app.db.models.request_logs import RequestLog
|
|
from app.db.session import create_sessionmaker
|
|
|
|
|
|
def test_models_request_logs_insert_select(tmp_path: Path) -> None:
|
|
db_path = tmp_path / "orm_request_logs.db"
|
|
engine = create_engine("sqlite+aiosqlite:///" + db_path.as_posix())
|
|
|
|
async def _run() -> None:
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(Base.metadata.create_all)
|
|
|
|
Session = create_sessionmaker(engine)
|
|
async with Session() as session:
|
|
row = RequestLog(
|
|
request_id="req_test",
|
|
method="GET",
|
|
route="/healthz",
|
|
status=200,
|
|
duration_ms=12,
|
|
ip="127.0.0.1",
|
|
user_agent="pytest",
|
|
sample_rate=1.0,
|
|
)
|
|
session.add(row)
|
|
await session.commit()
|
|
await session.refresh(row)
|
|
assert row.id == 1
|
|
|
|
async with Session() as session:
|
|
fetched = (await session.execute(select(RequestLog))).scalars().first()
|
|
assert fetched is not None
|
|
assert fetched.route == "/healthz"
|
|
|
|
await engine.dispose()
|
|
|
|
asyncio.run(_run())
|
|
|