test_logging.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # test_logging.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
  2. # This copyright was auto-generated on Wed Mar 27 22:05:49 UTC 2024
  3. from typing import Generic, TypeVar
  4. import construct
  5. import pytest
  6. from open_gopro import GoProResp
  7. from open_gopro.api.builders import (
  8. BleProtoCommand,
  9. BleReadCommand,
  10. BleSettingFacade,
  11. BleStatusFacade,
  12. BleWriteCommand,
  13. HttpSetting,
  14. )
  15. from open_gopro.domain.communicator_interface import HttpMessage, Message
  16. from open_gopro.models.constants import (
  17. ActionId,
  18. CmdId,
  19. ErrorCode,
  20. FeatureId,
  21. GoProUUID,
  22. QueryCmdId,
  23. SettingId,
  24. StatusId,
  25. )
  26. dummy_kwargs = {"first": 1, "second": 2}
  27. def assert_kwargs(message: dict):
  28. assert message.pop("first") == 1
  29. assert message.pop("second") == 2
  30. async def test_ble_read_command():
  31. message = BleReadCommand(uuid=GoProUUID.ACC_APPEARANCE, parser=None)
  32. d = message._as_dict(**dummy_kwargs)
  33. assert d.pop("id") == GoProUUID.ACC_APPEARANCE
  34. assert d.pop("protocol") == GoProResp.Protocol.BLE
  35. assert d.pop("uuid") == GoProUUID.ACC_APPEARANCE
  36. assert_kwargs(d)
  37. assert not d
  38. async def test_ble_write_command():
  39. message = BleWriteCommand(uuid=GoProUUID.ACC_APPEARANCE, cmd=CmdId.GET_CAMERA_CAPABILITIES)
  40. d = message._as_dict(**dummy_kwargs)
  41. assert d.pop("id") == CmdId.GET_CAMERA_CAPABILITIES
  42. assert d.pop("protocol") == GoProResp.Protocol.BLE
  43. assert d.pop("uuid") == GoProUUID.ACC_APPEARANCE
  44. assert_kwargs(d)
  45. assert not d
  46. async def test_ble_proto_command():
  47. message = BleProtoCommand(
  48. uuid=GoProUUID.ACC_APPEARANCE,
  49. feature_id=FeatureId.COMMAND,
  50. action_id=ActionId.GET_AP_ENTRIES,
  51. response_action_id=ActionId.GET_AP_ENTRIES_RSP,
  52. request_proto=None,
  53. response_proto=None,
  54. parser=None,
  55. )
  56. d = message._as_dict(**dummy_kwargs)
  57. assert d.pop("id") == ActionId.GET_AP_ENTRIES
  58. assert d.pop("feature_id") == FeatureId.COMMAND
  59. assert d.pop("protocol") == GoProResp.Protocol.BLE
  60. assert d.pop("uuid") == GoProUUID.ACC_APPEARANCE
  61. assert_kwargs(d)
  62. assert not d
  63. T = TypeVar("T", bound=Message)
  64. class MockCommunicator(Generic[T]):
  65. def __init__(self) -> None:
  66. self.message: T
  67. async def _send_ble_message(self, message: T) -> GoProResp:
  68. self.message = message
  69. return GoProResp(
  70. protocol=GoProResp.Protocol.BLE, status=ErrorCode.SUCCESS, data=bytes(), identifier=message._identifier
  71. )
  72. async def _get_json(self, message, **kwargs) -> GoProResp:
  73. self.message = message
  74. return GoProResp(protocol=GoProResp.Protocol.BLE, status=ErrorCode.SUCCESS, data=bytes(), identifier="unknown")
  75. def register_update(self, *args, **kwargs): ...
  76. def unregister_update(self, *args, **kwargs): ...
  77. async def test_ble_setting():
  78. class Communicator(MockCommunicator[BleSettingFacade.BleSettingMessageBase]): ...
  79. communicator = Communicator()
  80. message = BleSettingFacade(communicator=communicator, identifier=SettingId.MAX_LENS, parser_builder=construct.Flag)
  81. # Set Setting Value
  82. await message.set(bytes())
  83. d = communicator.message._as_dict(**dummy_kwargs)
  84. assert d.pop("id") == SettingId.MAX_LENS
  85. assert d.pop("setting_id") == SettingId.MAX_LENS
  86. assert d.pop("protocol") == GoProResp.Protocol.BLE
  87. assert d.pop("uuid") == GoProUUID.CQ_SETTINGS
  88. assert_kwargs(d)
  89. assert not d
  90. # Get Setting Value
  91. await message.get_value()
  92. d = communicator.message._as_dict(**dummy_kwargs)
  93. assert d.pop("id") == QueryCmdId.GET_SETTING_VAL
  94. assert d.pop("setting_id") == SettingId.MAX_LENS
  95. assert d.pop("protocol") == GoProResp.Protocol.BLE
  96. assert d.pop("uuid") == GoProUUID.CQ_QUERY
  97. assert_kwargs(d)
  98. assert not d
  99. # Get Setting Capabilities Values
  100. await message.get_capabilities_values()
  101. d = communicator.message._as_dict(**dummy_kwargs)
  102. assert d.pop("id") == QueryCmdId.GET_CAPABILITIES_VAL
  103. assert d.pop("setting_id") == SettingId.MAX_LENS
  104. assert d.pop("protocol") == GoProResp.Protocol.BLE
  105. assert d.pop("uuid") == GoProUUID.CQ_QUERY
  106. assert_kwargs(d)
  107. assert not d
  108. async def test_ble_status():
  109. class Communicator(MockCommunicator[BleStatusFacade.BleStatusMessageBase]): ...
  110. communicator = Communicator()
  111. message = BleStatusFacade(
  112. communicator=communicator, identifier=StatusId.MICROPHONE_ACCESSORY, parser=construct.Flag
  113. )
  114. # Get Status Value
  115. await message.get_value()
  116. d = communicator.message._as_dict(**dummy_kwargs)
  117. assert d.pop("id") == QueryCmdId.GET_STATUS_VAL
  118. assert d.pop("status_id") == StatusId.MICROPHONE_ACCESSORY
  119. assert d.pop("protocol") == GoProResp.Protocol.BLE
  120. assert d.pop("uuid") == GoProUUID.CQ_QUERY
  121. assert_kwargs(d)
  122. assert not d
  123. async def test_http_command():
  124. message = HttpMessage("endpoint", identifier=None)
  125. d = message._as_dict(**dummy_kwargs)
  126. assert d.pop("id") == "Endpoint"
  127. assert d.pop("protocol") == GoProResp.Protocol.HTTP
  128. assert d.pop("endpoint") == "endpoint"
  129. assert_kwargs(d)
  130. assert not d
  131. async def test_http_setting():
  132. class Communicator(MockCommunicator[HttpMessage]): ...
  133. communicator = Communicator()
  134. message = HttpSetting(communicator=communicator, identifier=SettingId.MAX_LENS)
  135. await message.set(1)
  136. d = communicator.message._as_dict(**dummy_kwargs)
  137. assert d.pop("id") == SettingId.MAX_LENS
  138. assert d.pop("protocol") == GoProResp.Protocol.HTTP
  139. assert d.pop("endpoint") == r"gopro/camera/setting?setting={setting}&option={option}"
  140. assert_kwargs(d)
  141. assert not d