| """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 |
|
|
|
|
| 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 |
| |
| 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 |
|
|