From b8a67b26b6559e69c2209d0779fb99b0fda51ed6 Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Mon, 14 Feb 2022 00:08:45 +0100 Subject: [PATCH] test enable commands (#77) --- tests/commands/__init__.py | 46 +++++++++++++++++++--- tests/commands/test_advanced_mode.py | 18 +++++++++ tests/commands/test_battery.py | 23 ++--------- tests/commands/test_carpet.py | 22 +++++++++++ tests/commands/test_continuous_cleaning.py | 22 +++++++++++ tests/commands/test_fan_speed.py | 21 ++-------- tests/helpers.py | 26 +++++++++++- 7 files changed, 134 insertions(+), 44 deletions(-) create mode 100644 tests/commands/test_advanced_mode.py create mode 100644 tests/commands/test_carpet.py create mode 100644 tests/commands/test_continuous_cleaning.py diff --git a/tests/commands/__init__.py b/tests/commands/__init__.py index 5ec924ba..fce55a65 100644 --- a/tests/commands/__init__.py +++ b/tests/commands/__init__.py @@ -1,18 +1,54 @@ -from typing import Any +from typing import Any, Optional, Union from unittest.mock import Mock -from deebot_client.commands import CommandWithHandling +from deebot_client.commands import CommandWithHandling, SetCommand from deebot_client.events import Event from deebot_client.events.event_bus import EventBus from deebot_client.message import HandlingState +from tests.helpers import get_message_json def assert_command_requested( - command: CommandWithHandling, data: dict[str, Any], expected_event: Event -): + command: CommandWithHandling, json: dict[str, Any], expected_event: Event +) -> None: event_bus = Mock(spec_set=EventBus) - result = command.handle_requested(event_bus, data) + assert command.name != "invalid" + + result = command.handle_requested(event_bus, json) assert result.state == HandlingState.SUCCESS event_bus.notify.assert_called_once_with(expected_event) + + +def assert_set_command( + command: SetCommand, + args: Union[dict, list, None], + expected_get_command_event: Event, +) -> None: + assert command.name != "invalid" + assert command.args == args + + event_bus = Mock(spec_set=EventBus) + + # Failed to set + json = { + "header": { + "pri": 1, + "tzm": 480, + "ts": "1304623069888", + "ver": "0.0.1", + "fwVer": "1.8.2", + "hwVer": "0.1.1", + }, + "body": { + "code": 500, + "msg": "fail", + }, + } + command.handle_mqtt_p2p(event_bus, json) + event_bus.notify.assert_not_called() + + # Success + command.handle_mqtt_p2p(event_bus, get_message_json(None)) + event_bus.notify.assert_called_once_with(expected_get_command_event) diff --git a/tests/commands/test_advanced_mode.py b/tests/commands/test_advanced_mode.py new file mode 100644 index 00000000..3d197600 --- /dev/null +++ b/tests/commands/test_advanced_mode.py @@ -0,0 +1,18 @@ +import pytest + +from deebot_client.commands import GetAdvancedMode, SetAdvancedMode +from deebot_client.events import AdvancedModeEvent +from tests.commands import assert_command_requested, assert_set_command +from tests.helpers import get_request_json + + +@pytest.mark.parametrize("value", [False, True]) +def test_get_advanced_mode_requested(value: bool): + json = get_request_json({"enable": 1 if value else 0}) + assert_command_requested(GetAdvancedMode(), json, AdvancedModeEvent(value)) + + +@pytest.mark.parametrize("value", [False, True]) +def test_set_advanced_mode(value: bool): + args = {"enable": 1 if value else 0} + assert_set_command(SetAdvancedMode(value), args, AdvancedModeEvent(value)) diff --git a/tests/commands/test_battery.py b/tests/commands/test_battery.py index 6e9929d2..67b9ab57 100644 --- a/tests/commands/test_battery.py +++ b/tests/commands/test_battery.py @@ -3,27 +3,10 @@ from deebot_client.commands import GetBattery from deebot_client.events import BatteryEvent from tests.commands import assert_command_requested +from tests.helpers import get_request_json @pytest.mark.parametrize("percentage", [0, 49, 100]) def test_get_battery_requested(percentage: int): - data = { - "id": "ALZf", - "ret": "ok", - "resp": { - "header": { - "pri": 1, - "tzm": 480, - "ts": "1304623069888", - "ver": "0.0.1", - "fwVer": "1.8.2", - "hwVer": "0.1.1", - }, - "body": { - "code": 0, - "msg": "ok", - "data": {"value": percentage, "isLow": 1 if percentage < 20 else 0}, - }, - }, - } - assert_command_requested(GetBattery(), data, BatteryEvent(percentage)) + json = get_request_json({"value": percentage, "isLow": 1 if percentage < 20 else 0}) + assert_command_requested(GetBattery(), json, BatteryEvent(percentage)) diff --git a/tests/commands/test_carpet.py b/tests/commands/test_carpet.py new file mode 100644 index 00000000..de857c81 --- /dev/null +++ b/tests/commands/test_carpet.py @@ -0,0 +1,22 @@ +import pytest + +from deebot_client.commands import GetCarpetAutoFanBoost, SetCarpetAutoFanBoost +from deebot_client.events import CarpetAutoFanBoostEvent +from tests.commands import assert_command_requested, assert_set_command +from tests.helpers import get_request_json + + +@pytest.mark.parametrize("value", [False, True]) +def test_get_carpet_auto_fan_boost_requested(value: bool): + json = get_request_json({"enable": 1 if value else 0}) + assert_command_requested( + GetCarpetAutoFanBoost(), json, CarpetAutoFanBoostEvent(value) + ) + + +@pytest.mark.parametrize("value", [False, True]) +def test_set_carpet_auto_fan_boost(value: bool): + args = {"enable": 1 if value else 0} + assert_set_command( + SetCarpetAutoFanBoost(value), args, CarpetAutoFanBoostEvent(value) + ) diff --git a/tests/commands/test_continuous_cleaning.py b/tests/commands/test_continuous_cleaning.py new file mode 100644 index 00000000..4d2f0ea3 --- /dev/null +++ b/tests/commands/test_continuous_cleaning.py @@ -0,0 +1,22 @@ +import pytest + +from deebot_client.commands import GetContinuousCleaning, SetContinuousCleaning +from deebot_client.events import ContinuousCleaningEvent +from tests.commands import assert_command_requested, assert_set_command +from tests.helpers import get_request_json + + +@pytest.mark.parametrize("value", [False, True]) +def test_get_continuous_cleaning_requested(value: bool): + json = get_request_json({"enable": 1 if value else 0}) + assert_command_requested( + GetContinuousCleaning(), json, ContinuousCleaningEvent(value) + ) + + +@pytest.mark.parametrize("value", [False, True]) +def test_set_continuous_cleaning(value: bool): + args = {"enable": 1 if value else 0} + assert_set_command( + SetContinuousCleaning(value), args, ContinuousCleaningEvent(value) + ) diff --git a/tests/commands/test_fan_speed.py b/tests/commands/test_fan_speed.py index 3d2f4ec5..8b4503ea 100644 --- a/tests/commands/test_fan_speed.py +++ b/tests/commands/test_fan_speed.py @@ -5,7 +5,7 @@ from deebot_client.commands import FanSpeedLevel, GetFanSpeed, SetFanSpeed from deebot_client.events import FanSpeedEvent from tests.commands import assert_command_requested -from tests.helpers import verify_DisplayNameEnum_unique +from tests.helpers import get_request_json, verify_DisplayNameEnum_unique def test_FanSpeedLevel_unique(): @@ -13,23 +13,8 @@ def test_FanSpeedLevel_unique(): def test_GetFanSpeed_requested(): - data = { - "id": "WiQO", - "ret": "ok", - "resp": { - "header": { - "pri": 1, - "tzm": 480, - "ts": "1305336289055", - "ver": "0.0.1", - "fwVer": "1.8.2", - "hwVer": "0.1.1", - }, - "body": {"code": 0, "msg": "ok", "data": {"speed": 2}}, - }, - } - - assert_command_requested(GetFanSpeed(), data, FanSpeedEvent("max+")) + json = get_request_json({"speed": 2}) + assert_command_requested(GetFanSpeed(), json, FanSpeedEvent("max+")) @pytest.mark.parametrize( diff --git a/tests/helpers.py b/tests/helpers.py index 50045e4e..9cafb1fb 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -1,4 +1,4 @@ -from typing import Set, Type +from typing import Any, Optional, Set, Type from deebot_client.util import DisplayNameIntEnum @@ -19,3 +19,27 @@ def verify_DisplayNameEnum_unique(enum: type[DisplayNameIntEnum]): if display_name != name: assert display_name not in names names.add(display_name) + + +def get_request_json(data: Optional[dict[str, Any]]) -> dict[str, Any]: + return {"id": "ALZf", "ret": "ok", "resp": get_message_json(data)} + + +def get_message_json(data: Optional[dict[str, Any]]) -> dict[str, Any]: + json = { + "header": { + "pri": 1, + "tzm": 480, + "ts": "1304623069888", + "ver": "0.0.1", + "fwVer": "1.8.2", + "hwVer": "0.1.1", + }, + "body": { + "code": 0, + "msg": "ok", + }, + } + if data: + json["body"]["data"] = data + return json