arm-tabular-parser/api/tests/unit/test_golden_helpers.py
Andrey Epifancev 3a08b990dd feat(contract): модели v1.2 — DISCLOSURE, StatementChecks, границы периода ОСВ; копия схемы
Refs #14

Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
Claude-Session: cedd7915-2efe-41e5-988c-a9117033598b
2026-10-06 14:23:27 +04:00

90 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Помощники golden-снимков: нормализация volatile-полей, путь снимка, формат записи (design D-10)."""
import json
from pathlib import Path
from golden_helpers import SNAPSHOT_DIR, golden_path, normalize_result, snapshot_text, unified_diff
def _result() -> dict:
return {
"schema_version": "1.2",
"job_id": "00000000-0000-0000-0000-000000000000",
"file_id": "00000000-0000-0000-0000-000000000000",
"service": {"name": "tabular-parser", "version": "0.1.0"},
"status": "done",
"metrics": {"duration_ms": 12, "rows": 7, "warnings": ["б", "а"]},
"documents": [{"z": 1, "a": 2}],
}
def test_normalize_removes_exactly_the_volatile_fields() -> None:
normalized = normalize_result(_result())
assert "job_id" not in normalized
assert "file_id" not in normalized
assert normalized["service"] == {"name": "tabular-parser"}
assert normalized["metrics"] == {"rows": 7, "warnings": ["б", "а"]}
assert normalized["documents"] == [{"z": 1, "a": 2}]
def test_normalize_keeps_key_and_warning_order_and_does_not_mutate_input() -> None:
source = _result()
normalized = normalize_result(source)
assert list(normalized) == ["schema_version", "service", "status", "metrics", "documents"]
assert list(normalized["documents"][0]) == ["z", "a"]
assert normalized["metrics"]["warnings"] == ["б", "а"]
assert source == _result()
def test_normalize_tolerates_missing_volatile_fields() -> None:
assert normalize_result({"status": "error"}) == {"status": "error"}
def test_golden_path_mirrors_osv_tree_without_extension(tmp_path: Path) -> None:
root = tmp_path
file = root / "osv" / "rb" / "2025-Q1" / "ОСВ сч 60.xlsx"
assert golden_path(root, file) == root / "golden" / SNAPSHOT_DIR / "rb" / "2025-Q1" / "ОСВ сч 60.json"
synthetic = root / "osv" / "synthetic" / "s01.xlsx"
assert golden_path(root, synthetic) == root / "golden" / SNAPSHOT_DIR / "synthetic" / "s01.json"
def test_golden_path_keeps_dots_inside_the_name(tmp_path: Path) -> None:
file = tmp_path / "osv" / "a" / "сч 62 за 3 квартал 2024 г..xls"
assert golden_path(tmp_path, file).name == "сч 62 за 3 квартал 2024 г..json"
def test_snapshot_text_is_utf8_json_with_indent_and_trailing_newline() -> None:
text = snapshot_text({"имя": "ОСВ", "n": ["1"]})
assert text.endswith("}\n")
assert "ОСВ" in text
assert json.loads(text) == {"имя": "ОСВ", "n": ["1"]}
assert text.startswith('{\n "имя"')
def test_unified_diff_names_files_and_shows_changed_lines() -> None:
diff = unified_diff("a\nb\n", "a\nc\n", name="снимок.json")
assert "--- снимок.json (в fixtures)" in diff
assert "+++ снимок.json (разбор сейчас)" in diff
assert "-b" in diff
assert "+c" in diff
assert unified_diff("a\n", "a\n", name="x") == ""
def test_dkb_snapshot_path_mirrors_osv_dkb_under_its_own_subdirectory(tmp_path: Path) -> None:
file = tmp_path / "osv-dkb" / "nm" / "2025-Q3" / "НМ ОСВ 63.xlsx"
assert golden_path(tmp_path, file) == (
tmp_path / "golden" / SNAPSHOT_DIR / "osv-dkb" / "nm" / "2025-Q3" / "НМ ОСВ 63.json"
)
assert (
golden_path(tmp_path, tmp_path / "osv" / "rb" / "a.xlsx")
== tmp_path / "golden" / SNAPSHOT_DIR / "rb" / "a.json"
)