Aucune description

test_v201_charge_point.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import json
  2. import pytest
  3. from ocpp.routing import after, create_route_map, on
  4. from ocpp.v201 import call_result
  5. @pytest.mark.asyncio
  6. async def test_route_message_with_existing_route(
  7. base_central_system, boot_notification_call
  8. ):
  9. """Test if the correct handler is called when routing a message.
  10. Also test if payload of request is injected correctly in handler.
  11. """
  12. @on("BootNotification")
  13. def on_boot_notification(reason, charging_station, **kwargs):
  14. assert reason == "PowerUp"
  15. assert charging_station == {
  16. "vendor_name": "ICU Eve Mini",
  17. "firmware_version": "#1:3.4.0-2990#N:217H;1.0-223",
  18. "model": "ICU Eve Mini",
  19. }
  20. return call_result.BootNotificationPayload(
  21. current_time="2018-05-29T17:37:05.495259",
  22. interval=350,
  23. status="Accepted",
  24. )
  25. @after("BootNotification")
  26. def after_boot_notification(reason, charging_station, **kwargs):
  27. assert reason == "PowerUp"
  28. assert charging_station == {
  29. "vendor_name": "ICU Eve Mini",
  30. "firmware_version": "#1:3.4.0-2990#N:217H;1.0-223",
  31. "model": "ICU Eve Mini",
  32. }
  33. setattr(base_central_system, "on_boot_notification", on_boot_notification)
  34. setattr(base_central_system, "after_boot_notification", after_boot_notification)
  35. base_central_system.route_map = create_route_map(base_central_system)
  36. await base_central_system.route_message(boot_notification_call)
  37. base_central_system._connection.send.assert_called_once_with(
  38. json.dumps(
  39. [
  40. 3,
  41. "1",
  42. {
  43. "currentTime": "2018-05-29T17:37:05.495259",
  44. "interval": 350,
  45. "status": "Accepted",
  46. },
  47. ],
  48. separators=(",", ":"),
  49. )
  50. )
  51. @pytest.mark.asyncio
  52. async def test_route_message_with_no_route(base_central_system, heartbeat_call):
  53. """
  54. Test that a CALLERROR is sent back, reporting that no handler is
  55. registred for it.
  56. """
  57. # Empty the route map
  58. base_central_system.route_map = {}
  59. await base_central_system.route_message(heartbeat_call)
  60. base_central_system._connection.send.assert_called_once_with(
  61. json.dumps(
  62. [
  63. 4,
  64. 1,
  65. "NotImplemented",
  66. "Request Action is recognized but not supported by the receiver",
  67. {"cause": "No handler for Heartbeat registered."},
  68. ],
  69. separators=(",", ":"),
  70. )
  71. )
  72. @pytest.mark.asyncio
  73. async def test_call_with_unique_id_should_return_same_id(
  74. mock_boot_request, mock_base_central_system
  75. ):
  76. expected_unique_id = "12345"
  77. # Call the method being tested with a unique_id as a parameter
  78. await mock_base_central_system.call(mock_boot_request, unique_id=expected_unique_id)
  79. (
  80. actual_unique_id,
  81. _,
  82. ) = mock_base_central_system._get_specific_response.call_args_list[0][0]
  83. # Check the actual unique id is equals to the one passed to the call method
  84. assert actual_unique_id == expected_unique_id
  85. @pytest.mark.asyncio
  86. async def test_call_without_unique_id_should_return_a_random_value(
  87. mock_boot_request, mock_base_central_system
  88. ):
  89. expected_unique_id = str(mock_base_central_system._unique_id_generator())
  90. # Call the method being tested without passing a unique_id as a parameter
  91. await mock_base_central_system.call(mock_boot_request)
  92. (
  93. actual_unique_id,
  94. _,
  95. ) = mock_base_central_system._get_specific_response.call_args_list[0][0]
  96. # Check the actual unique id is equals to the one internally generated
  97. assert actual_unique_id == expected_unique_id