test: набор pytest-тестов под site/ (unit + интеграционные + безопасность)
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
"""Unit-тесты services/grouping.py — нормализация + группировка (mock db)."""
|
||||
from services import grouping
|
||||
|
||||
|
||||
class TestNormalizeNumber:
|
||||
def test_basic(self):
|
||||
assert grouping.normalize_number("МЭС-123-2024") == "МЭС1232024"
|
||||
|
||||
def test_spaces_slashes(self):
|
||||
assert grouping.normalize_number("МЭС 123/2024") == "МЭС1232024"
|
||||
|
||||
def test_case(self):
|
||||
assert grouping.normalize_number("мэс-123") == "МЭС123"
|
||||
|
||||
def test_empty(self):
|
||||
assert grouping.normalize_number("") == ""
|
||||
assert grouping.normalize_number(None) == ""
|
||||
|
||||
|
||||
class _FakeDocs:
|
||||
def __init__(self, docs):
|
||||
self.docs = docs
|
||||
|
||||
def list_by_batch(self, batch_id):
|
||||
return self.docs
|
||||
|
||||
|
||||
class TestGroupDocuments:
|
||||
def _doc(self, id, doc_type, own, parent, date, cp):
|
||||
return {
|
||||
"id": id, "filename": f"{id}.docx", "doc_type": doc_type,
|
||||
"own_number": own, "parent_number": parent, "doc_date": date,
|
||||
"counterparty": cp, "classify_status": "classified",
|
||||
}
|
||||
|
||||
def test_contract_plus_supplement(self, monkeypatch):
|
||||
docs = [
|
||||
self._doc("d1", "contract", "03700_1", None, "2025-01-01", "ЗАО X"),
|
||||
self._doc("d2", "supplement", "1", "03700_1", "2025-02-01", "ЗАО X"),
|
||||
]
|
||||
monkeypatch.setattr(grouping, "db_docs", _FakeDocs(docs))
|
||||
r = grouping.group_documents("b1")
|
||||
assert r["ok"] is True
|
||||
assert r["total_docs"] == 2
|
||||
groups = [g for g in r["groups"] if g["contract_number"] != "__unresolved__"]
|
||||
assert len(groups) == 1
|
||||
assert groups[0]["contract_number"] == "03700_1"
|
||||
assert len(groups[0]["documents"]) == 2
|
||||
|
||||
def test_unmatched_goes_unresolved(self, monkeypatch):
|
||||
docs = [self._doc("d1", "other", None, None, None, "")]
|
||||
monkeypatch.setattr(grouping, "db_docs", _FakeDocs(docs))
|
||||
r = grouping.group_documents("b1")
|
||||
unresolved = [g for g in r["groups"] if g["contract_number"] == "__unresolved__"]
|
||||
assert len(unresolved) == 1
|
||||
|
||||
|
||||
class TestApplyGroups:
|
||||
def test_apply(self, monkeypatch):
|
||||
created = []
|
||||
|
||||
class FakeContracts:
|
||||
def insert(self, number, client=""):
|
||||
return {"id": f"c_{number}"}
|
||||
|
||||
class FakeSupps:
|
||||
def insert(self, cid, did, stype):
|
||||
created.append((cid, did, stype))
|
||||
return {"id": "s"}
|
||||
|
||||
monkeypatch.setattr(grouping, "db_contracts", FakeContracts())
|
||||
monkeypatch.setattr(grouping, "db_supplements", FakeSupps())
|
||||
|
||||
groups = [
|
||||
{"contract_number": "03700_1", "counterparty": "X",
|
||||
"documents": [{"id": "d1"}, {"id": "d2"}]},
|
||||
{"contract_number": "__unresolved__", "counterparty": "",
|
||||
"documents": [{"id": "d3"}]},
|
||||
]
|
||||
r = grouping.apply_groups("b1", groups)
|
||||
assert r["ok"] is True
|
||||
assert r["created"] == 2
|
||||
assert created == [("c_03700_1", "d1", "initial"), ("c_03700_1", "d2", "additional")]
|
||||
Reference in New Issue
Block a user