128 lines
4.3 KiB
Python
128 lines
4.3 KiB
Python
"""Чистые снимки payload фактов качества (§14.1): форма для ОБ/ОФР и для ОСВ."""
|
||
|
||
import uuid
|
||
from decimal import Decimal
|
||
|
||
from src.domain.quality_facts import build_intake_payload, build_outcome_payload
|
||
from src.models.enums import (
|
||
DocumentIssueKind,
|
||
DocumentStatus,
|
||
DocumentType,
|
||
IssueSeverity,
|
||
IssueSource,
|
||
StatementLineStatus,
|
||
)
|
||
from src.models.files import Document
|
||
from src.models.statements import DocumentIssue, FinancialStatementLine, FinancialStatementValue
|
||
|
||
|
||
def make_document(document_type: DocumentType = DocumentType.BALANCE) -> Document:
|
||
return Document(
|
||
id=uuid.uuid4(),
|
||
document_type=document_type,
|
||
status=DocumentStatus.RECOGNIZED,
|
||
)
|
||
|
||
|
||
def test_build_intake_payload_statement_has_lines_and_values():
|
||
document = make_document(document_type=DocumentType.BALANCE)
|
||
payload = build_intake_payload(document, lines=[...], values=[...], issues=[])
|
||
assert "lines" in payload and "values" in payload
|
||
assert "osv_warnings" not in payload
|
||
|
||
|
||
def test_build_intake_payload_osv_has_no_lines_or_values():
|
||
document = make_document(document_type=DocumentType.OSV)
|
||
payload = build_intake_payload(document, osv_warnings=["OSV_SOMETHING"])
|
||
assert "lines" not in payload and "values" not in payload
|
||
assert payload["osv_warnings"] == ["OSV_SOMETHING"]
|
||
|
||
|
||
def test_build_intake_payload_statement_serializes_line_value_and_issue_fields():
|
||
document = make_document(document_type=DocumentType.INCOME)
|
||
line_id = uuid.uuid4()
|
||
column_id = uuid.uuid4()
|
||
line = FinancialStatementLine(
|
||
id=line_id,
|
||
source_ref="row:3",
|
||
row_index=3,
|
||
line_code="2110",
|
||
line_code_text="2110",
|
||
line_status=StatementLineStatus.ok,
|
||
needs_review=True,
|
||
)
|
||
value = FinancialStatementValue(
|
||
line_id=line_id,
|
||
column_id=column_id,
|
||
amount=Decimal("1500.00"),
|
||
string_value=None,
|
||
source_text="1 500",
|
||
confidence=Decimal("95.00"),
|
||
is_recognized=True,
|
||
needs_review=False,
|
||
)
|
||
issue = DocumentIssue(
|
||
document_id=document.id,
|
||
kind=DocumentIssueKind.relation,
|
||
code="RELATION_BROKEN",
|
||
severity=IssueSeverity.error,
|
||
message="нарушено",
|
||
source=IssueSource.core,
|
||
relation="2110 = 2120",
|
||
codes=["2110"],
|
||
)
|
||
payload = build_intake_payload(document, lines=[line], values=[value], issues=[issue])
|
||
assert payload["lines"] == [
|
||
{
|
||
"id": str(line_id),
|
||
"ref": "row:3",
|
||
"row_index": 3,
|
||
"line_code": "2110",
|
||
"line_code_text": "2110",
|
||
"line_status": "ok",
|
||
"needs_review": True,
|
||
}
|
||
]
|
||
assert payload["values"] == [
|
||
{
|
||
"line_id": str(line_id),
|
||
"column_id": str(column_id),
|
||
"amount": "1500.00",
|
||
"string_value": None,
|
||
"source_text": "1 500",
|
||
"confidence": "95.00",
|
||
"is_recognized": True,
|
||
"needs_review": False,
|
||
}
|
||
]
|
||
assert payload["issues"] == [
|
||
{
|
||
"kind": "relation",
|
||
"code": "RELATION_BROKEN",
|
||
"severity": "error",
|
||
"source": "core",
|
||
"field": None,
|
||
"relation": "2110 = 2120",
|
||
"codes": ["2110"],
|
||
"line_ids": None,
|
||
}
|
||
]
|
||
|
||
|
||
def test_build_outcome_payload_statement_has_fixes_and_no_osv_warnings():
|
||
document = make_document(document_type=DocumentType.BALANCE)
|
||
fix = {"what": "line_code", "was": "2110", "became": "2120", "source": "service", "rule": "§5"}
|
||
payload = build_outcome_payload(document, lines=[], values=[], issues=[], fixes=[fix])
|
||
assert payload["fixes"] == [fix]
|
||
assert "lines" in payload and "values" in payload
|
||
assert "osv_warnings" not in payload
|
||
|
||
|
||
def test_build_outcome_payload_osv_has_fixes_and_no_lines():
|
||
document = make_document(document_type=DocumentType.OSV)
|
||
fix = {"what": "period", "was": "2024 Q1", "became": "2024 Q2", "source": "ours", "rule": "§5"}
|
||
payload = build_outcome_payload(document, osv_warnings=["OSV_SOMETHING"], fixes=[fix])
|
||
assert payload["osv_warnings"] == ["OSV_SOMETHING"]
|
||
assert payload["fixes"] == [fix]
|
||
assert "lines" not in payload and "values" not in payload
|