test_pl_brand_archive.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_apply_stock_change_outbound_insufficient():
  61. from app.services.pl_inventory_service import assert_outbound_enough
  62. with pytest.raises(ValueError, match="库存不足"):
  63. assert_outbound_enough(stock_qty=Decimal("1"), outbound_qty=Decimal("2"), brand_label="君正")
  64. def test_delta_for_plan_type():
  65. from app.services.pl_inventory_service import qty_delta_for_plan
  66. assert qty_delta_for_plan("inbound", Decimal("3")) == Decimal("3")
  67. assert qty_delta_for_plan("outbound", Decimal("3")) == Decimal("-3")
  68. assert qty_delta_for_plan("direct", Decimal("3")) == Decimal("0")