-
-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
134 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters