Files
life-echo/api/tests/test_log_events.py

63 lines
1.6 KiB
Python
Raw Normal View History

"""log_eventsformat_log_event 与 celery_prerun_extras。"""
from __future__ import annotations
from app.core.log_events import (
celery_prerun_extras,
correlation_bind_kwargs,
format_log_event,
)
def test_format_log_event_msg_last() -> None:
s = format_log_event(
"demo",
z_last=1,
a_first="x",
msg="你好 世界",
)
assert s.startswith("event=demo ")
assert s.endswith(" msg=你好 世界")
assert "a_first=x" in s
assert "z_last=1" in s
def test_format_log_event_skips_empty() -> None:
s = format_log_event("x", empty="", none_val=None, ok=5)
assert "empty=" not in s
assert "none_val=" not in s
assert "ok=5" in s
def test_format_log_event_float() -> None:
s = format_log_event("t", duration_ms=12.3456)
assert "duration_ms=12.3" in s
def test_correlation_bind_kwargs() -> None:
d = correlation_bind_kwargs(
user_id="u1",
memoir_correlation_id="c1",
)
assert d == {"user_id": "u1", "correlation_id": "c1"}
def test_celery_prerun_extras_from_kwargs() -> None:
ex = celery_prerun_extras(
"app.tasks.memory_enrichment_tasks.enrich_memory_source",
("uid", "sid"),
{"memoir_correlation_id": "mc"},
)
assert ex["user_id"] == "uid"
assert ex["source_id"] == "sid"
assert ex["correlation_id"] == "mc"
def test_celery_prerun_extras_positional_only() -> None:
ex = celery_prerun_extras(
"app.tasks.chapter_compose_tasks.recompose_chapter",
("chap-1",),
{},
)
assert ex == {"chapter_id": "chap-1"}