ken_nogi/PythonProj/ScanOCR/スクリプト/tests/test_logger.py
Kenichiro NOGI 1582960fd4 feat: 実行ログ出力モジュールを追加
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-02 10:10:14 +09:00

66 lines
1.8 KiB
Python

from datetime import datetime
from logger import LOG_ENCODING, append_log, log_path_for_today
def fixed_now():
return datetime(2026, 8, 2, 10, 30, 15)
def test_log_path_for_today_uses_now_func(tmp_path):
result = log_path_for_today(tmp_path, now_func=fixed_now)
assert result == tmp_path / "2026-08-02.log"
def test_append_log_creates_dir_and_file(tmp_path):
log_dir = tmp_path / "ログ"
append_log(
log_dir,
result="成功",
original_filename="scan001.pdf",
name="山田太郎",
date="20260802",
note="",
now_func=fixed_now,
)
log_path = log_dir / "2026-08-02.log"
assert log_path.exists()
content = log_path.read_text(encoding=LOG_ENCODING)
assert "[2026-08-02 10:30:15]" in content
assert "結果=成功" in content
assert "元ファイル=scan001.pdf" in content
assert "氏名=山田太郎" in content
assert "日付=20260802" in content
def test_append_log_handles_none_name_and_date(tmp_path):
log_dir = tmp_path / "ログ"
append_log(
log_dir,
result="失敗",
original_filename="scan002.pdf",
name=None,
date=None,
note="抽出不十分",
now_func=fixed_now,
)
content = (log_dir / "2026-08-02.log").read_text(encoding=LOG_ENCODING)
assert "氏名=" in content
assert "日付=" in content
assert "備考=抽出不十分" in content
def test_append_log_appends_multiple_lines(tmp_path):
log_dir = tmp_path / "ログ"
append_log(log_dir, "成功", "a.pdf", "氏名A", "20260801", "", now_func=fixed_now)
append_log(log_dir, "成功", "b.pdf", "氏名B", "20260802", "", now_func=fixed_now)
content = (log_dir / "2026-08-02.log").read_text(encoding=LOG_ENCODING)
assert content.count("結果=成功") == 2