test_storage_slot_select_keyword.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. """储物格下拉关键词:须支持完整 fullSlotCode 模糊匹配。"""
  2. from types import SimpleNamespace
  3. from app.crud.storage_facility import (
  4. _build_node_path_cache,
  5. _collect_slot_host_node_ids,
  6. _exact_node_ids_for_path,
  7. _leaf_node_ids_for_path_prefix,
  8. _resolve_path_keyword,
  9. _slot_matches_select_keyword,
  10. _slot_to_dict,
  11. )
  12. from app.services.storage_facility_code import build_full_slot_code
  13. def _facility(node_id: str, node_code: str, parent_id: str = "0") -> SimpleNamespace:
  14. return SimpleNamespace(
  15. id=node_id,
  16. node_code=node_code,
  17. parent_id=parent_id,
  18. node_name=node_code,
  19. )
  20. def _slot(slot_id: str, facility_node_id: str, slot_code: str, remark: str = "") -> SimpleNamespace:
  21. return SimpleNamespace(
  22. id=slot_id,
  23. facility_node_id=facility_node_id,
  24. slot_code=slot_code,
  25. slot_status="空",
  26. occupied_count=0,
  27. tenant_no="zkxw",
  28. order_num=1,
  29. remark=remark,
  30. create_time=None,
  31. update_time=None,
  32. )
  33. def test_full_slot_code_substring_match():
  34. facilities = {
  35. "box": _facility("box", "BOX01", "shelf"),
  36. "shelf": _facility("shelf", "A1", "root"),
  37. "root": _facility("root", "FRZ", "0"),
  38. }
  39. slot = _slot("s1", "box", "03")
  40. full = build_full_slot_code("box", "03", facilities)
  41. assert full == "FRZ-A1-BOX01-03"
  42. row = _slot_to_dict(slot, facilities)
  43. assert row["fullSlotCode"] == full
  44. assert _slot_matches_select_keyword(slot, "FRZ-A1-BOX01-03", facilities, set())
  45. assert _slot_matches_select_keyword(slot, "BOX01-03", facilities, set())
  46. assert _slot_matches_select_keyword(slot, "FRZ-A1", facilities, set())
  47. def test_local_slot_code_and_remark_still_match():
  48. facilities = {"box": _facility("box", "BOX01", "0")}
  49. slot = _slot("s1", "box", "07", remark="备用格")
  50. assert _slot_matches_select_keyword(slot, "07", facilities, set())
  51. assert _slot_matches_select_keyword(slot, "备用", facilities, set())
  52. def test_facility_node_name_match():
  53. facilities = {
  54. "box": _facility("box", "BOX01", "0"),
  55. }
  56. facilities["box"].node_name = "液氮盒A"
  57. slot = _slot("s1", "box", "01")
  58. assert _slot_matches_select_keyword(slot, "液氮", facilities, {"box"})
  59. def test_leaf_node_ids_for_path_prefix():
  60. facilities = {
  61. "box": _facility("box", "BOX01", "shelf"),
  62. "shelf": _facility("shelf", "A1", "root"),
  63. "root": _facility("root", "FRZ", "0"),
  64. }
  65. ids = _leaf_node_ids_for_path_prefix(facilities, "FRZ-A1")
  66. assert ids == {"box", "shelf", "root"}
  67. assert "box" in _leaf_node_ids_for_path_prefix(facilities, "BOX01")
  68. def test_no_match_returns_false():
  69. facilities = {"box": _facility("box", "BOX01", "0")}
  70. slot = _slot("s1", "box", "01")
  71. assert not _slot_matches_select_keyword(slot, "NOT-EXIST", facilities, set())
  72. def test_resolve_path_keyword_for_box_prefix():
  73. facilities = {
  74. "box": _facility("box", "2", "shelf"),
  75. "shelf": _facility("shelf", "1", "root"),
  76. "root": _facility("root", "1", "0"),
  77. }
  78. path_cache = _build_node_path_cache(facilities)
  79. assert _resolve_path_keyword(path_cache, "1-1-2-") == ("1-1-2", None)
  80. assert _resolve_path_keyword(path_cache, "1-1-2") == ("1-1-2", None)
  81. assert _resolve_path_keyword(path_cache, "1-1-2-1") == ("1-1-2", "1")
  82. assert _exact_node_ids_for_path(path_cache, "1-1-2") == ["box"]
  83. def test_trailing_dash_matches_all_slots_in_box():
  84. facilities = {
  85. "box": _facility("box", "2", "shelf"),
  86. "shelf": _facility("shelf", "1", "root"),
  87. "root": _facility("root", "1", "0"),
  88. }
  89. slots = [_slot("s1", "box", "1"), _slot("s2", "box", "2")]
  90. kw = "1-1-2-"
  91. for slot in slots:
  92. assert _slot_matches_select_keyword(slot, kw, facilities, set())
  93. def test_non_leaf_path_expands_to_descendant_leaves():
  94. facilities = {
  95. "layer2": _facility("layer2", "2", "shelf"),
  96. "layer10": _facility("layer10", "10", "shelf"),
  97. "shelf": _facility("shelf", "1", "root"),
  98. "root": _facility("root", "1", "0"),
  99. }
  100. facilities["layer2"].is_leaf = 1
  101. facilities["layer10"].is_leaf = 1
  102. path_cache = _build_node_path_cache(facilities)
  103. host_ids = _collect_slot_host_node_ids(path_cache, facilities, "1-1")
  104. assert set(host_ids) == {"layer2", "layer10"}
  105. assert _collect_slot_host_node_ids(path_cache, facilities, "1-1-2") == ["layer2"]