from decimal import Decimal import pytest from app.services.pl_brand_archive import validate_brand_qty_rows def test_required_allows_other_brand_when_sum_matches(): validate_brand_qty_rows( brand_mode="required", required_brand_id="b1", actual_qty=Decimal("10"), rows=[{"brandId": "b2", "brandName": "红四方", "qty": Decimal("10")}], ) def test_required_allows_multi_brand_when_sum_matches(): validate_brand_qty_rows( brand_mode="required", required_brand_id="b1", actual_qty=Decimal("10"), rows=[ {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")}, {"brandId": "b2", "brandName": "红四方", "qty": Decimal("4")}, ], ) def test_required_sum_must_match(): with pytest.raises(ValueError, match="合计"): validate_brand_qty_rows( brand_mode="required", required_brand_id="b1", actual_qty=Decimal("10"), rows=[ {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")}, {"brandId": "b2", "brandName": "红四方", "qty": Decimal("3")}, ], ) def test_any_sum_must_match(): with pytest.raises(ValueError, match="合计"): validate_brand_qty_rows( brand_mode="any", required_brand_id=None, actual_qty=Decimal("10"), rows=[ {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")}, {"brandId": "b2", "brandName": "红四方", "qty": Decimal("3")}, ], ) def test_any_ok(): validate_brand_qty_rows( brand_mode="any", required_brand_id=None, actual_qty=Decimal("10"), rows=[ {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")}, {"brandId": "b2", "brandName": "红四方", "qty": Decimal("4")}, ], ) def test_no_brand_ignores_rows(): validate_brand_qty_rows( brand_mode=None, required_brand_id=None, actual_qty=Decimal("5"), rows=[], ) def test_delta_for_plan_type(): from app.services.pl_inventory_service import qty_delta_for_plan assert qty_delta_for_plan("inbound", Decimal("3")) == Decimal("3") assert qty_delta_for_plan("outbound", Decimal("3")) == Decimal("-3") assert qty_delta_for_plan("direct", Decimal("3")) == Decimal("0")