Files
contracts-flask/tests/test_classify.py
T

93 lines
2.8 KiB
Python

"""Unit-тесты services/classify.py — фильтры + JSON-парсинг + выжимка."""
import json
import pytest
from services.classify import (
_is_garbage_by_filename,
_is_garbage_by_header,
_safe_json_parse,
_smart_extract,
)
class TestGarbageFilename:
def test_invoice(self):
assert _is_garbage_by_filename("счет-фактура №123.docx") is True
def test_act(self):
assert _is_garbage_by_filename("Акт сверки.pdf") is True
def test_upd(self):
assert _is_garbage_by_filename("УПД-2025.docx") is True
def test_not_garbage(self):
assert _is_garbage_by_filename("договор.docx") is False
assert _is_garbage_by_filename("спецификация.pdf") is False
class TestGarbageHeader:
def test_invoice_header(self):
assert _is_garbage_by_header("СЧЕТ-ФАКТУРА № 123 от 01.01.2026") is True
def test_act_header(self):
assert _is_garbage_by_header("АКТ ОКАЗАННЫХ УСЛУГ за май") is True
def test_not_garbage(self):
assert _is_garbage_by_header("ДОГОВОР № 03700_1 об оказании услуг") is False
class TestSafeJsonParse:
def test_valid(self):
d, fix = _safe_json_parse('{"a":1}')
assert d == {"a": 1}
assert fix is False
def test_markdown(self):
d, fix = _safe_json_parse('```json\n{"a":1}\n```')
assert d == {"a": 1}
assert fix is False
def test_chatter(self):
d, _ = _safe_json_parse('Вот ответ: {"a":1}. Спасибо!')
assert d == {"a": 1}
def test_trailing_comma(self):
d, fix = _safe_json_parse('{"a":1,}')
assert d == {"a": 1}
assert fix is True
def test_trailing_comma_in_list(self):
d, fix = _safe_json_parse('{"a":[1,2,]}')
assert d == {"a": [1, 2]}
assert fix is True
def test_empty_raises(self):
with pytest.raises(ValueError):
_safe_json_parse("")
def test_none_raises(self):
with pytest.raises(ValueError):
_safe_json_parse(None)
class TestSmartExtract:
def test_paragraphs(self):
ej = [{"type": "paragraph", "text": "Договор № 03700"}]
out = _smart_extract(ej)
assert "Договор" in out
def test_table(self):
ej = [{"type": "table", "rows": [["Аренда", "50000"]]}]
out = _smart_extract(ej)
assert "Аренда" in out
def test_string_json(self):
ej = json.dumps([{"type": "paragraph", "text": "Спецификация"}])
out = _smart_extract(ej)
assert "Спецификация" in out
def test_empty(self):
assert _smart_extract(None) == ""
assert _smart_extract("") == ""