test_pl_plan_stop_service.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """运输计划多段:装送货产品与计划产品行匹配"""
  2. from decimal import Decimal
  3. from types import SimpleNamespace
  4. import pytest
  5. from fastapi import HTTPException
  6. from sqlalchemy.orm import Session
  7. from app.schemas.pl.plan import PlanStopProductBody
  8. from app.services.pl_plan_stop_service import (
  9. _resolve_plan_product_id,
  10. resolve_stop_address_from_library,
  11. )
  12. def _plan_product_row(
  13. row_id: str,
  14. product_id: str,
  15. product_no: str = "P001",
  16. ):
  17. return SimpleNamespace(id=row_id, product_id=product_id, product_no=product_no)
  18. def test_resolve_plan_product_id_by_explicit_id():
  19. rows = [_plan_product_row("pp-1", "crm-1")]
  20. prod = PlanStopProductBody(
  21. planProductId="pp-1",
  22. productId="crm-1",
  23. productNo="P001",
  24. productName="别名1111",
  25. )
  26. assert _resolve_plan_product_id(prod, rows) == "pp-1"
  27. def test_resolve_plan_product_id_falls_back_when_explicit_id_stale():
  28. rows = [_plan_product_row("pp-new", "crm-1")]
  29. prod = PlanStopProductBody(
  30. planProductId="pp-old",
  31. productId="crm-1",
  32. productNo="P001",
  33. productName="别名1111",
  34. )
  35. assert _resolve_plan_product_id(prod, rows) == "pp-new"
  36. def test_resolve_plan_product_id_falls_back_to_product_no():
  37. rows = [_plan_product_row("pp-new", "crm-1", "P001")]
  38. prod = PlanStopProductBody(
  39. planProductId="pp-old",
  40. productId="crm-1",
  41. productNo="P001",
  42. productName="别名1111",
  43. )
  44. assert _resolve_plan_product_id(prod, rows) == "pp-new"
  45. def test_resolve_plan_product_id_raises_when_unmatched():
  46. rows = [_plan_product_row("pp-new", "crm-2", "P002")]
  47. prod = PlanStopProductBody(
  48. planProductId="pp-old",
  49. productId="crm-1",
  50. productNo="P001",
  51. productName="别名1111",
  52. )
  53. with pytest.raises(HTTPException) as exc_info:
  54. _resolve_plan_product_id(prod, rows)
  55. assert exc_info.value.status_code == 422
  56. assert "别名1111" in exc_info.value.detail
  57. def test_resolve_stop_address_from_library_returns_existing_text():
  58. db = SimpleNamespace()
  59. assert (
  60. resolve_stop_address_from_library(db, "shanxia2.0", "已有地址", None, None)
  61. == "已有地址"
  62. )
  63. def test_resolve_stop_address_from_library_looks_up_by_lat():
  64. row = SimpleNamespace(
  65. full_address="高新区霞虹路5号安庆市山峡化工有限公司",
  66. lng=Decimal("116.990968"),
  67. lat=Decimal("30.549796"),
  68. )
  69. class FakeCol:
  70. @staticmethod
  71. def desc():
  72. return FakeCol()
  73. class FakeAddressModel:
  74. tenant_no = None
  75. del_flag = None
  76. status = None
  77. lat = None
  78. lng = None
  79. create_time = FakeCol()
  80. class FakeQuery:
  81. def filter(self, *args, **kwargs):
  82. return self
  83. def order_by(self, *args, **kwargs):
  84. return self
  85. def all(self):
  86. return [row]
  87. class FakeSession:
  88. def query(self, model):
  89. return FakeQuery()
  90. import app.services.pl_plan_stop_service as mod
  91. original = mod.PlTransportAddress
  92. mod.PlTransportAddress = FakeAddressModel
  93. try:
  94. resolved = resolve_stop_address_from_library(
  95. FakeSession(),
  96. "shanxia2.0",
  97. "",
  98. None,
  99. Decimal("30.549796"),
  100. )
  101. finally:
  102. mod.PlTransportAddress = original
  103. assert resolved == "高新区霞虹路5号安庆市山峡化工有限公司"