| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- # test_logging.py/Open GoPro, Version 2.0 (C) Copyright 2021 GoPro, Inc. (http://gopro.com/OpenGoPro).
- # This copyright was auto-generated on Wed Mar 27 22:05:49 UTC 2024
- from typing import Generic, TypeVar
- import construct
- import pytest
- from open_gopro import GoProResp
- from open_gopro.api.builders import (
- BleProtoCommand,
- BleReadCommand,
- BleSettingFacade,
- BleStatusFacade,
- BleWriteCommand,
- HttpSetting,
- )
- from open_gopro.domain.communicator_interface import HttpMessage, Message
- from open_gopro.models.constants import (
- ActionId,
- CmdId,
- ErrorCode,
- FeatureId,
- GoProUUID,
- QueryCmdId,
- SettingId,
- StatusId,
- )
- dummy_kwargs = {"first": 1, "second": 2}
- def assert_kwargs(message: dict):
- assert message.pop("first") == 1
- assert message.pop("second") == 2
- async def test_ble_read_command():
- message = BleReadCommand(uuid=GoProUUID.ACC_APPEARANCE, parser=None)
- d = message._as_dict(**dummy_kwargs)
- assert d.pop("id") == GoProUUID.ACC_APPEARANCE
- assert d.pop("protocol") == GoProResp.Protocol.BLE
- assert d.pop("uuid") == GoProUUID.ACC_APPEARANCE
- assert_kwargs(d)
- assert not d
- async def test_ble_write_command():
- message = BleWriteCommand(uuid=GoProUUID.ACC_APPEARANCE, cmd=CmdId.GET_CAMERA_CAPABILITIES)
- d = message._as_dict(**dummy_kwargs)
- assert d.pop("id") == CmdId.GET_CAMERA_CAPABILITIES
- assert d.pop("protocol") == GoProResp.Protocol.BLE
- assert d.pop("uuid") == GoProUUID.ACC_APPEARANCE
- assert_kwargs(d)
- assert not d
- async def test_ble_proto_command():
- message = BleProtoCommand(
- uuid=GoProUUID.ACC_APPEARANCE,
- feature_id=FeatureId.COMMAND,
- action_id=ActionId.GET_AP_ENTRIES,
- response_action_id=ActionId.GET_AP_ENTRIES_RSP,
- request_proto=None,
- response_proto=None,
- parser=None,
- )
- d = message._as_dict(**dummy_kwargs)
- assert d.pop("id") == ActionId.GET_AP_ENTRIES
- assert d.pop("feature_id") == FeatureId.COMMAND
- assert d.pop("protocol") == GoProResp.Protocol.BLE
- assert d.pop("uuid") == GoProUUID.ACC_APPEARANCE
- assert_kwargs(d)
- assert not d
- T = TypeVar("T", bound=Message)
- class MockCommunicator(Generic[T]):
- def __init__(self) -> None:
- self.message: T
- async def _send_ble_message(self, message: T) -> GoProResp:
- self.message = message
- return GoProResp(
- protocol=GoProResp.Protocol.BLE, status=ErrorCode.SUCCESS, data=bytes(), identifier=message._identifier
- )
- async def _get_json(self, message, **kwargs) -> GoProResp:
- self.message = message
- return GoProResp(protocol=GoProResp.Protocol.BLE, status=ErrorCode.SUCCESS, data=bytes(), identifier="unknown")
- def register_update(self, *args, **kwargs): ...
- def unregister_update(self, *args, **kwargs): ...
- async def test_ble_setting():
- class Communicator(MockCommunicator[BleSettingFacade.BleSettingMessageBase]): ...
- communicator = Communicator()
- message = BleSettingFacade(communicator=communicator, identifier=SettingId.MAX_LENS, parser_builder=construct.Flag)
- # Set Setting Value
- await message.set(bytes())
- d = communicator.message._as_dict(**dummy_kwargs)
- assert d.pop("id") == SettingId.MAX_LENS
- assert d.pop("setting_id") == SettingId.MAX_LENS
- assert d.pop("protocol") == GoProResp.Protocol.BLE
- assert d.pop("uuid") == GoProUUID.CQ_SETTINGS
- assert_kwargs(d)
- assert not d
- # Get Setting Value
- await message.get_value()
- d = communicator.message._as_dict(**dummy_kwargs)
- assert d.pop("id") == QueryCmdId.GET_SETTING_VAL
- assert d.pop("setting_id") == SettingId.MAX_LENS
- assert d.pop("protocol") == GoProResp.Protocol.BLE
- assert d.pop("uuid") == GoProUUID.CQ_QUERY
- assert_kwargs(d)
- assert not d
- # Get Setting Capabilities Values
- await message.get_capabilities_values()
- d = communicator.message._as_dict(**dummy_kwargs)
- assert d.pop("id") == QueryCmdId.GET_CAPABILITIES_VAL
- assert d.pop("setting_id") == SettingId.MAX_LENS
- assert d.pop("protocol") == GoProResp.Protocol.BLE
- assert d.pop("uuid") == GoProUUID.CQ_QUERY
- assert_kwargs(d)
- assert not d
- async def test_ble_status():
- class Communicator(MockCommunicator[BleStatusFacade.BleStatusMessageBase]): ...
- communicator = Communicator()
- message = BleStatusFacade(
- communicator=communicator, identifier=StatusId.MICROPHONE_ACCESSORY, parser=construct.Flag
- )
- # Get Status Value
- await message.get_value()
- d = communicator.message._as_dict(**dummy_kwargs)
- assert d.pop("id") == QueryCmdId.GET_STATUS_VAL
- assert d.pop("status_id") == StatusId.MICROPHONE_ACCESSORY
- assert d.pop("protocol") == GoProResp.Protocol.BLE
- assert d.pop("uuid") == GoProUUID.CQ_QUERY
- assert_kwargs(d)
- assert not d
- async def test_http_command():
- message = HttpMessage("endpoint", identifier=None)
- d = message._as_dict(**dummy_kwargs)
- assert d.pop("id") == "Endpoint"
- assert d.pop("protocol") == GoProResp.Protocol.HTTP
- assert d.pop("endpoint") == "endpoint"
- assert_kwargs(d)
- assert not d
- async def test_http_setting():
- class Communicator(MockCommunicator[HttpMessage]): ...
- communicator = Communicator()
- message = HttpSetting(communicator=communicator, identifier=SettingId.MAX_LENS)
- await message.set(1)
- d = communicator.message._as_dict(**dummy_kwargs)
- assert d.pop("id") == SettingId.MAX_LENS
- assert d.pop("protocol") == GoProResp.Protocol.HTTP
- assert d.pop("endpoint") == r"gopro/camera/setting?setting={setting}&option={option}"
- assert_kwargs(d)
- assert not d
|