test_pl_brand_archive.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from decimal import Decimal
  2. import pytest
  3. from app.services.pl_brand_archive import validate_brand_qty_rows
  4. def test_required_allows_other_brand_when_sum_matches():
  5. validate_brand_qty_rows(
  6. brand_mode="required",
  7. required_brand_id="b1",
  8. actual_qty=Decimal("10"),
  9. rows=[{"brandId": "b2", "brandName": "红四方", "qty": Decimal("10")}],
  10. )
  11. def test_required_allows_multi_brand_when_sum_matches():
  12. validate_brand_qty_rows(
  13. brand_mode="required",
  14. required_brand_id="b1",
  15. actual_qty=Decimal("10"),
  16. rows=[
  17. {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")},
  18. {"brandId": "b2", "brandName": "红四方", "qty": Decimal("4")},
  19. ],
  20. )
  21. def test_required_sum_must_match():
  22. with pytest.raises(ValueError, match="合计"):
  23. validate_brand_qty_rows(
  24. brand_mode="required",
  25. required_brand_id="b1",
  26. actual_qty=Decimal("10"),
  27. rows=[
  28. {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")},
  29. {"brandId": "b2", "brandName": "红四方", "qty": Decimal("3")},
  30. ],
  31. )
  32. def test_any_sum_must_match():
  33. with pytest.raises(ValueError, match="合计"):
  34. validate_brand_qty_rows(
  35. brand_mode="any",
  36. required_brand_id=None,
  37. actual_qty=Decimal("10"),
  38. rows=[
  39. {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")},
  40. {"brandId": "b2", "brandName": "红四方", "qty": Decimal("3")},
  41. ],
  42. )
  43. def test_any_ok():
  44. validate_brand_qty_rows(
  45. brand_mode="any",
  46. required_brand_id=None,
  47. actual_qty=Decimal("10"),
  48. rows=[
  49. {"brandId": "b1", "brandName": "君正", "qty": Decimal("6")},
  50. {"brandId": "b2", "brandName": "红四方", "qty": Decimal("4")},
  51. ],
  52. )
  53. def test_no_brand_ignores_rows():
  54. validate_brand_qty_rows(
  55. brand_mode=None,
  56. required_brand_id=None,
  57. actual_qty=Decimal("5"),
  58. rows=[],
  59. )
  60. def test_delta_for_plan_type():
  61. from app.services.pl_inventory_service import qty_delta_for_plan
  62. assert qty_delta_for_plan("inbound", Decimal("3")) == Decimal("3")
  63. assert qty_delta_for_plan("outbound", Decimal("3")) == Decimal("-3")
  64. assert qty_delta_for_plan("direct", Decimal("3")) == Decimal("0")