Files
yawan-workbench/server/tests/test_system_log.py
T

48 lines
1.6 KiB
Python

"""Bounded ordinary system-log writer tests."""
from pathlib import Path
import tempfile
from sqlmodel import Session, SQLModel, create_engine, select
from app import system_log
from app.models import SystemLog
def _engine():
path = Path(tempfile.mktemp(suffix=".db"))
return create_engine(f"sqlite:///{path}"), path
def test_emit_batches_ordinary_logs_and_redacts():
engine, path = _engine()
try:
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
assert system_log.emit(session, level="INFO", source="test",
message="token=secret-value") is True
# emit must not commit the request session: the row is only visible
# after the writer creates its own Session and flushes the batch.
assert session.exec(select(SystemLog)).all() == []
assert system_log.flush(3) is True
with Session(engine) as session:
rows = session.exec(select(SystemLog)).all()
assert len(rows) == 1
assert "[REDACTED]" in rows[0].message
finally:
system_log.shutdown()
path.unlink(missing_ok=True)
def test_critical_emit_remains_synchronous_and_returns_id():
engine, path = _engine()
try:
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
assert system_log.emit(session, level="ERROR", source="test",
message="failure") is True
row = session.exec(select(SystemLog)).one()
assert row.id and row.level == "ERROR"
finally:
system_log.shutdown()
path.unlink(missing_ok=True)