dodil/erp-brick-tax

DODIL ERP brick: tax

tests/test_commit_conflict.py

"""On DataK3 the loser of a write race on a claim row can fail at COMMIT, after the use case returned: that is the retry-safe 409, never a 500."""
from contextlib import contextmanager

import pytest
from erp_platform.bricks import Refused
from sqlalchemy.exc import IntegrityError

from brick_tax.application import use_cases


class _Engine:
    def __init__(self, text):
        self.text = text

    @contextmanager
    def begin(self):
        yield object()
        raise IntegrityError("COMMIT", {}, Exception(self.text))


def test_a_unique_violation_at_commit_is_the_409_and_any_other_integrity_error_is_not_hidden():
    with pytest.raises(Refused) as lost:
        with use_cases._on_engine(_Engine("duplicate key value violates unique constraint (SQLSTATE 23505)")):
            pass
    assert lost.value.status == 409
    with pytest.raises(IntegrityError):
        with use_cases._on_engine(_Engine("null value in column violates not-null constraint")):
            pass