test_pl_plan_stop_service.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_rejects_explicit_stale_id():
  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. with pytest.raises(HTTPException) as error:
  36. _resolve_plan_product_id(prod, rows)
  37. assert error.value.status_code == 422
  38. def test_resolve_plan_product_id_matches_unique_product_when_id_omitted():
  39. rows = [_plan_product_row("pp-new", "crm-1", "P001")]
  40. prod = PlanStopProductBody(
  41. productId="crm-1",
  42. productNo="P001",
  43. productName="别名1111",
  44. )
  45. assert _resolve_plan_product_id(prod, rows) == "pp-new"
  46. def test_resolve_plan_product_id_raises_when_unmatched():
  47. rows = [_plan_product_row("pp-new", "crm-2", "P002")]
  48. prod = PlanStopProductBody(
  49. planProductId="pp-old",
  50. productId="crm-1",
  51. productNo="P001",
  52. productName="别名1111",
  53. )
  54. with pytest.raises(HTTPException) as exc_info:
  55. _resolve_plan_product_id(prod, rows)
  56. assert exc_info.value.status_code == 422
  57. assert "别名1111" in exc_info.value.detail
  58. def test_resolve_stop_address_from_library_returns_existing_text():
  59. db = SimpleNamespace()
  60. assert (
  61. resolve_stop_address_from_library(db, "shanxia2.0", "已有地址", None, None)
  62. == "已有地址"
  63. )
  64. def test_resolve_stop_address_from_library_looks_up_by_lat():
  65. row = SimpleNamespace(
  66. full_address="高新区霞虹路5号安庆市山峡化工有限公司",
  67. lng=Decimal("116.990968"),
  68. lat=Decimal("30.549796"),
  69. )
  70. class FakeCol:
  71. @staticmethod
  72. def desc():
  73. return FakeCol()
  74. class FakeAddressModel:
  75. tenant_no = None
  76. del_flag = None
  77. status = None
  78. lat = None
  79. lng = None
  80. create_time = FakeCol()
  81. class FakeQuery:
  82. def filter(self, *args, **kwargs):
  83. return self
  84. def order_by(self, *args, **kwargs):
  85. return self
  86. def all(self):
  87. return [row]
  88. class FakeSession:
  89. def query(self, model):
  90. return FakeQuery()
  91. import app.services.pl_plan_stop_service as mod
  92. original = mod.PlTransportAddress
  93. mod.PlTransportAddress = FakeAddressModel
  94. try:
  95. resolved = resolve_stop_address_from_library(
  96. FakeSession(),
  97. "shanxia2.0",
  98. "",
  99. None,
  100. Decimal("30.549796"),
  101. )
  102. finally:
  103. mod.PlTransportAddress = original
  104. assert resolved == "高新区霞虹路5号安庆市山峡化工有限公司"