Files
miem_workers/tests/test_extract_k2_journals.py
codex 24d07706e1
CI / test (pull_request) Successful in 9m24s
CI / Fingerprint diagnostics (production deployment temporarily disabled) (pull_request) Has been skipped
feat: implement sourced council publication assessment
2026-10-08 18:05:32 +03:00

43 lines
1.8 KiB
Python
Raw Permalink 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.
import hashlib
import importlib.util
import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
def extractor(monkeypatch, rows):
class Document:
pages = [SimpleNamespace(extract_tables=lambda: [rows])]
def __enter__(self):
return self
def __exit__(self, *_):
pass
monkeypatch.setitem(sys.modules, "pdfplumber", SimpleNamespace(open=lambda _: Document()))
spec = importlib.util.spec_from_file_location("category_extractor_test", Path("scripts/extract_k1_journals.py"))
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def test_k2_extraction_preserves_provenance_history_and_alias(monkeypatch, tmp_path):
source = tmp_path / "categories.pdf"
source.write_bytes(b"synthetic source")
module = extractor(monkeypatch, [["1", "First", "К1", "01.01.2024"],
["2", "Second (до 01.01.2024 г. наименование в Перечне «Previous»)", "К3\nК2", "01.01.2023\n01.01.2024"]])
result = module.extract(source, "К2")
assert result["source_rows"] == 2 and result["category_counts"] == {"К1": 1, "К2": 1}
assert result["journals"][0]["name"] == "Second"
assert result["journals"][0]["aliases"] == ["Previous"]
assert result["journals"][0]["categorized_on"] == "01.01.2024"
assert result["source_sha256"] == hashlib.sha256(source.read_bytes()).hexdigest()
assert module.extract(source)["journals"][0]["name"] == "First"
@pytest.mark.parametrize("rows", [[["2", "Second", "К2", "01.01.2024"]], [["1", "Second", "К2", "invalid"]]])
def test_k2_rejects_incomplete_numbering_or_date(monkeypatch, tmp_path, rows):
module = extractor(monkeypatch, rows)
with pytest.raises(ValueError):
module.extract(tmp_path / "categories.pdf", "К2")