|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from types import MappingProxyType |
| 4 | +from typing import Any |
| 5 | +from unittest.mock import Mock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from deebot_client.command import CommandResult, InitParam |
| 10 | +from deebot_client.commands.xml.common import XmlCommandMqttP2P |
| 11 | +from deebot_client.event_bus import EventBus |
| 12 | +from deebot_client.events import LifeSpan |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + ("payload", "decoded_payload"), |
| 17 | + [ |
| 18 | + (bytearray(bytes("test", "utf-8")), "test"), |
| 19 | + (bytes("test", "utf-8"), "test"), |
| 20 | + ("test", "test"), |
| 21 | + ], |
| 22 | + ids=["bytearray", "bytes", "str"], |
| 23 | +) |
| 24 | +@patch.multiple(XmlCommandMqttP2P, __abstractmethods__=set()) |
| 25 | +def test_XmlCommandMqttP2P_decoding( |
| 26 | + payload: bytearray | bytes | str, decoded_payload: str |
| 27 | +) -> None: |
| 28 | + command = XmlCommandMqttP2P() # type: ignore[abstract] |
| 29 | + event_bus = Mock(spec_set=EventBus) |
| 30 | + with patch.object(command, "_handle_mqtt_p2p", return_value=None) as mqtt_handler: |
| 31 | + command.handle_mqtt_p2p(event_bus, payload) |
| 32 | + |
| 33 | + mqtt_handler.assert_called_once_with(event_bus, decoded_payload) |
| 34 | + |
| 35 | + |
| 36 | +@patch.multiple(XmlCommandMqttP2P, __abstractmethods__=set()) |
| 37 | +def test_XmlCommandMqttP2P_invalid_decoding() -> None: |
| 38 | + command = XmlCommandMqttP2P() # type: ignore[abstract] |
| 39 | + event_bus = Mock(spec_set=EventBus) |
| 40 | + with ( |
| 41 | + patch.object(command, "_handle_mqtt_p2p", return_value=None), |
| 42 | + pytest.raises(TypeError), |
| 43 | + ): |
| 44 | + command.handle_mqtt_p2p(event_bus, {}) # type: ignore[arg-type] |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.parametrize( |
| 48 | + ("command_payload", "payload_type", "expected_argument"), |
| 49 | + [ |
| 50 | + ("test", str, "test"), |
| 51 | + (LifeSpan.BRUSH.xml_value, LifeSpan, LifeSpan.BRUSH), |
| 52 | + ], |
| 53 | + ids=["native", "with xml_value"], |
| 54 | +) |
| 55 | +def test_XmlCommandMqttP2P_create_from_mqtt( |
| 56 | + command_payload: str, payload_type: type, expected_argument: type |
| 57 | +) -> None: |
| 58 | + class Sut(XmlCommandMqttP2P): |
| 59 | + NAME = "Sut" |
| 60 | + _mqtt_params = MappingProxyType( |
| 61 | + {"payload": InitParam(payload_type), "remove": None} |
| 62 | + ) |
| 63 | + |
| 64 | + def __init__(self, payload: Any) -> None: |
| 65 | + super().__init__(args={"payload": payload}) |
| 66 | + |
| 67 | + def _handle_mqtt_p2p( |
| 68 | + self, _event_bus: EventBus, _response: dict[str, Any] | str |
| 69 | + ) -> None: |
| 70 | + pass |
| 71 | + |
| 72 | + def _handle_response( |
| 73 | + self, _event_bus: EventBus, _response: dict[str, Any] |
| 74 | + ) -> CommandResult: |
| 75 | + return CommandResult.analyse() |
| 76 | + |
| 77 | + xml_message = f"<ctl ret='ok' payload='{command_payload}' remove='42' />" |
| 78 | + |
| 79 | + sut = Sut.create_from_mqtt(xml_message) |
| 80 | + |
| 81 | + assert sut._args == {"payload": expected_argument} |
0 commit comments