43 lines
1.8 KiB
Python
43 lines
1.8 KiB
Python
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")
|