|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +from types import MappingProxyType |
| 4 | +from typing import Any |
3 | 5 | from unittest.mock import Mock, patch |
4 | 6 |
|
5 | 7 | import pytest |
6 | 8 |
|
| 9 | +from deebot_client.command import CommandResult, InitParam |
7 | 10 | from deebot_client.commands.xml.common import XmlCommandMqttP2P |
8 | 11 | from deebot_client.event_bus import EventBus |
| 12 | +from deebot_client.events import LifeSpan |
9 | 13 |
|
10 | 14 |
|
11 | 15 | @pytest.mark.parametrize( |
@@ -38,3 +42,38 @@ def test_XmlCommandMqttP2P_invalid_decoding() -> None: |
38 | 42 | pytest.raises(TypeError), |
39 | 43 | ): |
40 | 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 | + _mqtt_params = MappingProxyType({"payload": InitParam(payload_type)}) |
| 60 | + NAME = "Sut" |
| 61 | + |
| 62 | + def __init__(self, payload: Any) -> None: |
| 63 | + super().__init__(args={"payload": payload}) |
| 64 | + |
| 65 | + def _handle_mqtt_p2p( |
| 66 | + self, _event_bus: EventBus, _response: dict[str, Any] | str |
| 67 | + ) -> None: |
| 68 | + pass |
| 69 | + |
| 70 | + def _handle_response( |
| 71 | + self, _event_bus: EventBus, _response: dict[str, Any] |
| 72 | + ) -> CommandResult: |
| 73 | + return CommandResult.analyse() |
| 74 | + |
| 75 | + xml_message = f"<ctl ret='ok' payload='{command_payload}'/>" |
| 76 | + |
| 77 | + sut = Sut.create_from_mqtt(xml_message) |
| 78 | + |
| 79 | + assert sut._args == {"payload": expected_argument} |
0 commit comments