Skip to content

Commit

Permalink
test enable commands (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
edenhaus authored Feb 13, 2022
1 parent 5d6312b commit b8a67b2
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 44 deletions.
46 changes: 41 additions & 5 deletions tests/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions tests/commands/test_advanced_mode.py
Original file line number Diff line number Diff line change
@@ -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))
23 changes: 3 additions & 20 deletions tests/commands/test_battery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
22 changes: 22 additions & 0 deletions tests/commands/test_carpet.py
Original file line number Diff line number Diff line change
@@ -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)
)
22 changes: 22 additions & 0 deletions tests/commands/test_continuous_cleaning.py
Original file line number Diff line number Diff line change
@@ -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)
)
21 changes: 3 additions & 18 deletions tests/commands/test_fan_speed.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,16 @@
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():
verify_DisplayNameEnum_unique(FanSpeedLevel)


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(
Expand Down
26 changes: 25 additions & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Set, Type
from typing import Any, Optional, Set, Type

from deebot_client.util import DisplayNameIntEnum

Expand All @@ -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

0 comments on commit b8a67b2

Please sign in to comment.