AHD-CMA / tests /unit /test_logging.py
Akiyue's picture
Add files using upload-large-folder tool
b2077dd verified
Raw
History Blame Contribute Delete
1.46 kB
"""Tests for ahdcma.utils.logging."""
from __future__ import annotations
import logging
from pathlib import Path
from ahdcma.utils.logging import make_run_id, setup_logging
def test_make_run_id_format() -> None:
rid = make_run_id("ahdcma", "cifar100_vit", 7)
parts = rid.split("_")
assert parts[0] == "ahdcma"
assert parts[-2].startswith("seed")
assert parts[-2] == "seed7"
timestamp = parts[-1]
assert len(timestamp) == 15 # YYYYMMDD-HHMMSS
def test_setup_logging_writes_file(tmp_path: Path) -> None:
rid = "test_run_42"
logger = setup_logging(rid, log_dir=tmp_path)
logger.info("hello %s", "world")
for handler in logger.handlers:
handler.flush()
log_file = tmp_path / f"{rid}.log"
assert log_file.exists()
content = log_file.read_text()
assert "hello world" in content
assert "run_id=test_run_42" in content
def test_setup_logging_idempotent(tmp_path: Path) -> None:
rid = "rid_x"
a = setup_logging(rid, log_dir=tmp_path)
b = setup_logging(rid, log_dir=tmp_path)
assert a is b
# Each call resets handlers, so we still have exactly the two we set up.
assert len(a.handlers) == 2
def test_setup_logging_verbose(tmp_path: Path) -> None:
rid = "verb"
logger = setup_logging(rid, log_dir=tmp_path, verbose=True)
console = next(h for h in logger.handlers if h.__class__.__name__ == "RichHandler")
assert console.level == logging.DEBUG