返回教程正文

配套源码

test_gateway.py

can-gateway/tests/test_gateway.py
zdt_x57s_can Brick:把一台 ZDT X57S 封装成可复用对象can-gateway/tests/test_gateway.py
Python134 行
  1. import unittest
  2. from gateway import GatewayRequestError, MotorGateway
  3. class GatewayValidationTests(unittest.TestCase):
  4. """
  5. @description : 验证网关鉴权、方法白名单和运动安全限制
  6. @param : unittest自动创建
  7. @return : 无
  8. """
  9. def setUp(self):
  10. """
  11. @description : 为每个测试创建不访问真实CAN的网关实例
  12. @param : 无
  13. @return : 无
  14. """
  15. self.gateway = MotorGateway("can0", "secret", 60, 0.5)
  16. def request(self, method, params=None, token="secret"):
  17. """
  18. @description : 构造版本1网关请求字典
  19. @param method : 网关方法名
  20. @param params : 方法参数字典
  21. @param token : 鉴权令牌
  22. @return : 请求字典
  23. """
  24. return {
  25. "version": 1,
  26. "request_id": "test",
  27. "token": token,
  28. "method": method,
  29. "params": params or {},
  30. }
  31. def test_rejects_wrong_token(self):
  32. """
  33. @description : 校验错误令牌被拒绝
  34. @param : 无
  35. @return : 无
  36. """
  37. with self.assertRaises(GatewayRequestError) as caught:
  38. self.gateway.dispatch(self.request("status", token="wrong"))
  39. self.assertEqual(caught.exception.code, "unauthorized")
  40. def test_rejects_unknown_method(self):
  41. """
  42. @description : 校验未列入白名单的方法被拒绝
  43. @param : 无
  44. @return : 无
  45. """
  46. with self.assertRaises(GatewayRequestError) as caught:
  47. self.gateway.dispatch(self.request("raw_can_send"))
  48. self.assertEqual(caught.exception.code, "unknown_method")
  49. def test_rejects_removed_batch_methods(self):
  50. """
  51. @description : 校验批量探测和批量停车不属于单电机网关API
  52. @param : 无
  53. @return : 无
  54. """
  55. for method_name in ("probe", "stop_all"):
  56. with self.subTest(method=method_name):
  57. with self.assertRaises(GatewayRequestError) as caught:
  58. self.gateway.dispatch(self.request(method_name))
  59. self.assertEqual(caught.exception.code, "unknown_method")
  60. def test_motion_needs_confirmation(self):
  61. """
  62. @description : 校验使能命令缺少运动确认时不会访问CAN
  63. @param : 无
  64. @return : 无
  65. """
  66. with self.assertRaises(GatewayRequestError) as caught:
  67. self.gateway.dispatch(
  68. self.request("enable", {"motor_id": 1})
  69. )
  70. self.assertEqual(caught.exception.code, "confirmation_required")
  71. def test_rejects_unsafe_speed_before_can(self):
  72. """
  73. @description : 校验超过60RPM的命令在访问CAN前被拒绝
  74. @param : 无
  75. @return : 无
  76. """
  77. with self.assertRaises(GatewayRequestError) as caught:
  78. self.gateway.dispatch(
  79. self.request(
  80. "set_speed",
  81. {
  82. "motor_id": 1,
  83. "rpm": 61,
  84. "acceleration_level": 10,
  85. "confirmation": "RUN_ZDT_X57S_V1_0",
  86. },
  87. )
  88. )
  89. self.assertEqual(caught.exception.code, "invalid_params")
  90. def test_rejects_unsafe_watchdog_configuration(self):
  91. """
  92. @description : 校验速度命令看门狗不能被配置为过长时间
  93. @param : 无
  94. @return : 无
  95. """
  96. with self.assertRaises(ValueError):
  97. MotorGateway("can0", "secret", 60, 0.5, 5.1)
  98. def test_accepts_full_protocol_motor_id_range(self):
  99. """
  100. @description : 校验单电机网关支持第二代协议地址1至255
  101. @param : 无
  102. @return : 无
  103. """
  104. self.assertEqual(self.gateway._validate_motor_id(1), 1)
  105. self.assertEqual(self.gateway._validate_motor_id(255), 255)
  106. def test_rejects_motor_id_outside_protocol_range(self):
  107. """
  108. @description : 校验地址0、256和布尔值在访问CAN前被拒绝
  109. @param : 无
  110. @return : 无
  111. """
  112. for motor_id in (0, 256, True):
  113. with self.subTest(motor_id=motor_id):
  114. with self.assertRaises(GatewayRequestError) as caught:
  115. self.gateway._validate_motor_id(motor_id)
  116. self.assertEqual(caught.exception.code, "invalid_params")
  117. if __name__ == "__main__":
  118. unittest.main()