test_pl_plan_requires_site_link.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """现场链接生成条件:装货/送货段含山峡或云栋客户"""
  2. from types import SimpleNamespace
  3. from app.services.pl_internal_customer import (
  4. INTERNAL_CUSTOMER_NAMES,
  5. plan_requires_site_link,
  6. )
  7. class _FakeQuery:
  8. def __init__(self, rows):
  9. self._rows = rows
  10. def filter(self, *args, **kwargs):
  11. return self
  12. def first(self):
  13. if isinstance(self._rows, list):
  14. return self._rows[0] if self._rows else None
  15. return self._rows
  16. def all(self):
  17. if isinstance(self._rows, list):
  18. return self._rows
  19. return [self._rows] if self._rows else []
  20. class _FakeSession:
  21. def __init__(self, load=None, deliveries=None):
  22. self.load = load
  23. self.deliveries = deliveries or []
  24. def query(self, model):
  25. name = getattr(model, "__name__", str(model))
  26. if name == "PlPlanLoad":
  27. return _FakeQuery(self.load)
  28. if name == "PlPlanDelivery":
  29. return _FakeQuery(self.deliveries)
  30. return _FakeQuery(None)
  31. def _plan(**kwargs):
  32. defaults = {
  33. "id": "plan-1",
  34. "supplier_name": None,
  35. "customer_name": None,
  36. "destination_name": None,
  37. }
  38. defaults.update(kwargs)
  39. return SimpleNamespace(**defaults)
  40. def test_internal_delivery_requires_site_link():
  41. plan = _plan()
  42. db = _FakeSession(
  43. load=SimpleNamespace(customer_name="巢湖市大自然化工有限公司"),
  44. deliveries=[SimpleNamespace(customer_name="安庆市山峡化工有限公司")],
  45. )
  46. assert plan_requires_site_link(db, tenant_no="shanxia2.0", plan=plan) is True
  47. def test_external_stops_do_not_require_site_link():
  48. plan = _plan()
  49. db = _FakeSession(
  50. load=SimpleNamespace(customer_name="巢湖市大自然化工有限公司"),
  51. deliveries=[SimpleNamespace(customer_name="外部客户有限公司")],
  52. )
  53. assert plan_requires_site_link(db, tenant_no="shanxia2.0", plan=plan) is False
  54. def test_legacy_plan_customer_fallback():
  55. plan = _plan(customer_name="安徽云栋化工有限公司")
  56. db = _FakeSession(load=None, deliveries=[])
  57. assert plan_requires_site_link(db, tenant_no="shanxia2.0", plan=plan) is True
  58. def test_internal_customer_names_constant():
  59. assert "安庆市山峡化工有限公司" in INTERNAL_CUSTOMER_NAMES
  60. assert "安徽云栋化工有限公司" in INTERNAL_CUSTOMER_NAMES