"""Ingest guards: the checks that refuse bad data rather than publishing it."""

import pytest

from app.services.ingest import _sizes_disagree


class TestSizeVeto:
    """A barcode is identity -- unless the sizes flatly disagree, in which case
    the retailer mislabelled it. Trusting one put a 70cl liqueur's price on a
    1L whisky's page as its 'cheapest'."""

    @pytest.mark.parametrize(("a", "b"), [(1000, 700), (250, 750), (700, 1750)])
    def test_materially_different_sizes_are_vetoed(self, a, b):
        assert _sizes_disagree(a, b)

    @pytest.mark.parametrize(("a", "b"), [(700, 750), (750, 700), (1000, 1000)])
    def test_the_700_750_sloppiness_is_tolerated(self, a, b):
        """Feeds are casually inconsistent between 70cl and 750ml for the same
        bottle; vetoing those would fragment the catalogue instead of protecting it."""
        assert not _sizes_disagree(a, b)

    @pytest.mark.parametrize(("a", "b"), [(None, 700), (700, None), (None, None)])
    def test_an_unknown_size_never_vetoes(self, a, b):
        """Absence of a fact is not evidence of a mismatch."""
        assert not _sizes_disagree(a, b)


class TestQuantityVeto:
    """Identity rules v4 (Stream L): the barcode veto compares like with like. A 75 g stick
    and a 75 ml bottle once shared a millilitre figure; now a different dimension is a
    disagreement, the same dimension keeps the 12 percent tolerance, and an unknown on
    either side never vetoes."""

    def test_a_different_dimension_is_a_disagreement(self):
        from app.services.ingest import _quantities_disagree
        from app.services.quantity import from_stored
        assert _quantities_disagree(from_stored(75, "ml"), from_stored(75, "g"))
        assert _quantities_disagree(from_stored(100, "g"), from_stored(100, "pcs"))

    def test_the_same_dimension_keeps_the_tolerance(self):
        from app.services.ingest import _quantities_disagree
        from app.services.quantity import from_stored
        assert not _quantities_disagree(from_stored(700, "ml"), from_stored(750, "ml"))
        assert _quantities_disagree(from_stored(1000, "ml"), from_stored(700, "ml"))
        assert not _quantities_disagree(from_stored(100, "g"), from_stored(110, "g"))
        assert _quantities_disagree(from_stored(100, "g"), from_stored(200, "g"))

    def test_an_unknown_never_vetoes(self):
        from app.services.ingest import _quantities_disagree
        from app.services.quantity import Quantity, from_stored
        assert not _quantities_disagree(None, from_stored(700, "ml"))
        assert not _quantities_disagree(Quantity(), from_stored(700, "ml"))
        assert not _quantities_disagree(from_stored(700, "ml"), Quantity(state="unparsed"))
