test_gateway_api.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from __future__ import annotations
  2. import unittest
  3. from unittest.mock import patch
  4. from data_collector_mcp import gateway_api
  5. class GatewayApiTests(unittest.TestCase):
  6. def test_modbus_point_collect_test_posts_gateway_payload(self) -> None:
  7. response = {"code": 0, "msg": "success", "data": {"points": []}}
  8. with patch(
  9. "data_collector_mcp.gateway_api.find_project_config",
  10. return_value={"project_key": "dev-01", "base_url": "http://gateway.test"},
  11. ), patch(
  12. "data_collector_mcp.gateway_api.request_json",
  13. return_value=response,
  14. ) as request_json:
  15. result = gateway_api.modbus_point_collect_test(
  16. "dev-01",
  17. ip="192.168.1.10",
  18. port=502,
  19. slave_id=1,
  20. points=[{"function_code": 3, "address": 7, "type": "int16"}],
  21. )
  22. self.assertEqual(result, response)
  23. request_json.assert_called_once_with(
  24. "POST",
  25. "http://gateway.test/api/dc-gateway/modbus/read_points",
  26. json_payload={
  27. "device_type": "ModbusTCP",
  28. "ip": "192.168.1.10",
  29. "port": 502,
  30. "word_byte_order": "ABCD",
  31. "address_base": 0,
  32. "slave_id": 1,
  33. "points": [{"function_code": 3, "address": 7, "type": "int16"}],
  34. },
  35. )
  36. def test_modbus_point_collect_test_keeps_custom_word_byte_order(self) -> None:
  37. with patch(
  38. "data_collector_mcp.gateway_api.find_project_config",
  39. return_value={"project_key": "dev-01", "base_url": "http://gateway.test"},
  40. ), patch(
  41. "data_collector_mcp.gateway_api.request_json",
  42. return_value={"code": 0},
  43. ) as request_json:
  44. gateway_api.modbus_point_collect_test(
  45. "dev-01",
  46. ip="192.168.1.10",
  47. port=502,
  48. slave_id=1,
  49. word_byte_order="DCBA",
  50. address_base=1,
  51. points=[{"function_code": 4, "address": 0, "type": "float32"}],
  52. )
  53. payload = request_json.call_args.kwargs["json_payload"]
  54. self.assertEqual(payload["word_byte_order"], "DCBA")
  55. self.assertEqual(payload["address_base"], 1)
  56. def test_s7_raw_read_posts_gateway_payload(self) -> None:
  57. response = {"code": 0, "msg": "success", "data": {"communication": []}}
  58. with patch(
  59. "data_collector_mcp.gateway_api.find_project_config",
  60. return_value={"project_key": "dev-01", "base_url": "http://gateway.test"},
  61. ), patch(
  62. "data_collector_mcp.gateway_api.request_json",
  63. return_value=response,
  64. ) as request_json:
  65. result = gateway_api.s7_raw_read(
  66. "dev-01",
  67. ip="192.168.1.10",
  68. rock=0,
  69. slot=1,
  70. read={"area": "DB", "db": 1, "start": 0, "size": 4},
  71. )
  72. self.assertEqual(result, response)
  73. request_json.assert_called_once_with(
  74. "POST",
  75. "http://gateway.test/api/dc-gateway/s7/read",
  76. json_payload={
  77. "device_type": "S7TCP",
  78. "ip": "192.168.1.10",
  79. "port": 102,
  80. "rock": 0,
  81. "slot": 1,
  82. "tsap_conn_type": "PG",
  83. "read": {"area": "DB", "db": 1, "start": 0, "size": 4},
  84. },
  85. )
  86. def test_s7_point_collect_test_posts_gateway_payload(self) -> None:
  87. response = {"code": 0, "msg": "success", "data": {"points": []}}
  88. with patch(
  89. "data_collector_mcp.gateway_api.find_project_config",
  90. return_value={"project_key": "dev-01", "base_url": "http://gateway.test"},
  91. ), patch(
  92. "data_collector_mcp.gateway_api.request_json",
  93. return_value=response,
  94. ) as request_json:
  95. result = gateway_api.s7_point_collect_test(
  96. "dev-01",
  97. ip="192.168.1.10",
  98. port=1102,
  99. rock=0,
  100. slot=1,
  101. tsap_conn_type="OP",
  102. points=[{"area": "DB", "db": 1, "start": 0, "type": "float32"}],
  103. )
  104. self.assertEqual(result, response)
  105. request_json.assert_called_once_with(
  106. "POST",
  107. "http://gateway.test/api/dc-gateway/s7/read_points",
  108. json_payload={
  109. "device_type": "S7TCP",
  110. "ip": "192.168.1.10",
  111. "port": 1102,
  112. "rock": 0,
  113. "slot": 1,
  114. "tsap_conn_type": "OP",
  115. "points": [{"area": "DB", "db": 1, "start": 0, "type": "float32"}],
  116. },
  117. )
  118. def test_s7_connect_scan_posts_ip_only(self) -> None:
  119. response = {"code": 0, "msg": "success", "data": {"available": []}}
  120. with patch(
  121. "data_collector_mcp.gateway_api.find_project_config",
  122. return_value={"project_key": "dev-01", "base_url": "http://gateway.test"},
  123. ), patch(
  124. "data_collector_mcp.gateway_api.request_json",
  125. return_value=response,
  126. ) as request_json:
  127. result = gateway_api.s7_connect_scan("dev-01", ip="192.168.1.10")
  128. self.assertEqual(result, response)
  129. request_json.assert_called_once_with(
  130. "POST",
  131. "http://gateway.test/api/dc-gateway/s7/connect_scan",
  132. json_payload={"ip": "192.168.1.10"},
  133. )
  134. if __name__ == "__main__":
  135. unittest.main()