diff --git a/api/src/services/corrections_view.py b/api/src/services/corrections_view.py index d3eb719..c301d26 100644 --- a/api/src/services/corrections_view.py +++ b/api/src/services/corrections_view.py @@ -31,7 +31,7 @@ def attach_corrections( for key, corrections in grouped.items(): aggregate = by_key.get(key) if aggregate is None: - continue + raise ConflictError("CORRECTION_REVIEW_REQUIRED", "Корректировка ссылается на агрегат, которого больше нет") ordered = sorted(corrections, key=lambda c: (c.parent_correction_id is not None, c.id)) roots = [c for c in ordered if c.parent_correction_id is None] root_ids = {c.id for c in roots} diff --git a/api/tests/test_corrections_view.py b/api/tests/test_corrections_view.py new file mode 100644 index 0000000..5e30fc9 --- /dev/null +++ b/api/tests/test_corrections_view.py @@ -0,0 +1,56 @@ +import uuid +from decimal import Decimal + +import pytest + +from src.common.exceptions import ConflictError +from src.models.kfo import Correction +from src.schemas.consolidations import RefOut +from src.schemas.corrections import AggregateSideOut, CorrectionAggregateOut +from src.services.corrections_view import attach_corrections + +ZERO = Decimal("0") + + +def _side() -> AggregateSideOut: + return AggregateSideOut(article_line="1230", accounts=[], amount=ZERO, entries=[]) + + +def _aggregate(rule_id: uuid.UUID, company_1: uuid.UUID, company_2: uuid.UUID) -> CorrectionAggregateOut: + return CorrectionAggregateOut( + rule_id=rule_id, + rule_no=1, + company_1=RefOut(id=company_1, name="А"), + company_2=RefOut(id=company_2, name="Б"), + side_1=_side(), + side_2=_side(), + deviation_amount=ZERO, + deviation_percent=None, + exceeds_threshold=False, + status="AUTO", + eliminated_amount=ZERO, + correction=None, + ) + + +def _correction(rule_id: uuid.UUID, legal_entity_id: uuid.UUID, counterparty_id: uuid.UUID) -> Correction: + return Correction( + id=uuid.uuid4(), + rule_id=rule_id, + legal_entity_id=legal_entity_id, + counterparty_id=counterparty_id, + parent_correction_id=None, + comment="комментарий", + version_no=1, + ) + + +async def test_attach_corrections_rejects_correction_for_aggregate_that_no_longer_exists() -> None: + rule_id, company_1, company_2 = uuid.uuid4(), uuid.uuid4(), uuid.uuid4() + aggregates = [_aggregate(uuid.uuid4(), company_1, company_2)] + corrections = [_correction(rule_id, company_1, company_2)] + + with pytest.raises(ConflictError) as excinfo: + attach_corrections(aggregates, corrections, {}) + + assert excinfo.value.code == "CORRECTION_REVIEW_REQUIRED"