-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: add a few tests for light command * chore: test all light commands * chore: test unkown light mac * chore: add battery:level tests * chore: add light:level tests * chore: add sensor:presence tests * chore: add sensor:temperature tests * chore: add system:* tests * chore: cover all off api_factory * chore: add tests for cached api
- Loading branch information
Showing
9 changed files
with
529 additions
and
20 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
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
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,31 @@ | ||
# Copyright (c) Ely Deckers. | ||
# | ||
# This source code is licensed under the MPL-2.0 license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import unittest | ||
|
||
from huemon.api.api import Api | ||
from huemon.api.api_factory import create_api, create_hue_hub_url | ||
from huemon.api.cached_api import CachedApi | ||
|
||
|
||
class TestApiConfiguration(unittest.TestCase): | ||
def test_when_no_api_configuration_available_throw(self): | ||
with self.assertRaises(Exception): | ||
create_hue_hub_url({}) | ||
|
||
def test_when_cache_enabled_return_cached(self): | ||
api = create_api( | ||
{"ip": "IRRELEVANT_IP", "key": "IRRELEVANT_KEY", "cache": {"enable": True}} | ||
) | ||
|
||
self.assertIsInstance(api, CachedApi) | ||
|
||
def test_when_cache_enabled_return_regular(self): | ||
api = create_api( | ||
{"ip": "IRRELEVANT_IP", "key": "IRRELEVANT_KEY", "cache": {"enable": False}} | ||
) | ||
|
||
self.assertNotIsInstance(api, CachedApi) | ||
self.assertIsInstance(api, Api) |
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,156 @@ | ||
# Copyright (c) Ely Deckers. | ||
# | ||
# This source code is licensed under the MPL-2.0 license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import unittest | ||
from unittest.mock import MagicMock, patch | ||
|
||
from huemon.commands.command_handler import ( | ||
CommandHandler, | ||
create_name_to_command_mapping, | ||
) | ||
from huemon.commands_available.light_command import LightCommand | ||
from tests.fixtures import MutableApi, read_result | ||
|
||
CACHE_VALIDITY_INFINITE_SECONDS = 1_000_000 | ||
CACHE_VALIDITY_ZERO_SECONDS = 0 | ||
|
||
|
||
SOME_LIGHT_MAC_0 = "SO:ME:LI:GH:TM:AC:00" | ||
SOME_LIGHT_MAC_1 = "SO:ME:LI:GH:TM:AC:01" | ||
|
||
|
||
class TestLightCommand(unittest.TestCase): | ||
def test_when_light_doesnt_exist(self): | ||
mutable_api = MutableApi() | ||
mutable_api.set_lights([]) | ||
|
||
command_handler = CommandHandler( | ||
create_name_to_command_mapping({}, mutable_api, [LightCommand]) | ||
) | ||
|
||
with self.assertRaises(Exception): | ||
command_handler.exec("light", [SOME_LIGHT_MAC_0, "status"]) | ||
|
||
@patch("builtins.print") | ||
def test_when_light_exists_return_status(self, mock_print: MagicMock): | ||
mutable_api = MutableApi() | ||
mutable_api.set_lights( | ||
[ | ||
{ | ||
"uniqueid": SOME_LIGHT_MAC_0, | ||
"state": { | ||
"on": 1, | ||
}, | ||
}, | ||
{ | ||
"uniqueid": SOME_LIGHT_MAC_1, | ||
"state": { | ||
"on": 0, | ||
}, | ||
}, | ||
] | ||
) | ||
|
||
command_handler = CommandHandler( | ||
create_name_to_command_mapping({}, mutable_api, [LightCommand]) | ||
) | ||
|
||
command_handler.exec("light", [SOME_LIGHT_MAC_0, "status"]) | ||
state_light_0 = read_result(mock_print) | ||
command_handler.exec("light", [SOME_LIGHT_MAC_1, "status"]) | ||
state_light_1 = read_result(mock_print) | ||
|
||
self.assertEqual(1, state_light_0) | ||
self.assertEqual(0, state_light_1) | ||
|
||
@patch("builtins.print") | ||
def test_when_light_exists_return_is_upgrade_available(self, mock_print: MagicMock): | ||
mutable_api = MutableApi() | ||
mutable_api.set_lights( | ||
[ | ||
{ | ||
"uniqueid": SOME_LIGHT_MAC_0, | ||
"swupdate": { | ||
"state": "noupdates", | ||
}, | ||
}, | ||
{ | ||
"uniqueid": SOME_LIGHT_MAC_1, | ||
"swupdate": { | ||
"state": "update-available", | ||
}, | ||
}, | ||
] | ||
) | ||
|
||
command_handler = CommandHandler( | ||
create_name_to_command_mapping({}, mutable_api, [LightCommand]) | ||
) | ||
|
||
command_handler.exec("light", [SOME_LIGHT_MAC_0, "is_upgrade_available"]) | ||
state_light_0 = read_result(mock_print) | ||
command_handler.exec("light", [SOME_LIGHT_MAC_1, "is_upgrade_available"]) | ||
state_light_1 = read_result(mock_print) | ||
|
||
self.assertEqual(0, state_light_0) | ||
self.assertEqual(1, state_light_1) | ||
|
||
@patch("builtins.print") | ||
def test_when_light_exists_return_is_reachable(self, mock_print: MagicMock): | ||
mutable_api = MutableApi() | ||
mutable_api.set_lights( | ||
[ | ||
{"uniqueid": SOME_LIGHT_MAC_0, "state": {"reachable": 0}}, | ||
{ | ||
"uniqueid": SOME_LIGHT_MAC_1, | ||
"state": { | ||
"reachable": 1, | ||
}, | ||
}, | ||
] | ||
) | ||
|
||
command_handler = CommandHandler( | ||
create_name_to_command_mapping({}, mutable_api, [LightCommand]) | ||
) | ||
|
||
command_handler.exec("light", [SOME_LIGHT_MAC_0, "reachable"]) | ||
state_light_0 = read_result(mock_print) | ||
command_handler.exec("light", [SOME_LIGHT_MAC_1, "reachable"]) | ||
state_light_1 = read_result(mock_print) | ||
|
||
self.assertEqual(0, state_light_0) | ||
self.assertEqual(1, state_light_1) | ||
|
||
@patch("builtins.print") | ||
def test_when_light_exists_return_version(self, mock_print: MagicMock): | ||
some_version_0 = "some_version_0" | ||
some_version_1 = "some_version_1" | ||
|
||
mutable_api = MutableApi() | ||
mutable_api.set_lights( | ||
[ | ||
{ | ||
"uniqueid": SOME_LIGHT_MAC_0, | ||
"swversion": some_version_0, | ||
}, | ||
{ | ||
"uniqueid": SOME_LIGHT_MAC_1, | ||
"swversion": some_version_1, | ||
}, | ||
] | ||
) | ||
|
||
command_handler = CommandHandler( | ||
create_name_to_command_mapping({}, mutable_api, [LightCommand]) | ||
) | ||
|
||
command_handler.exec("light", [SOME_LIGHT_MAC_0, "version"]) | ||
state_light_0 = read_result(mock_print) | ||
command_handler.exec("light", [SOME_LIGHT_MAC_1, "version"]) | ||
state_light_1 = read_result(mock_print) | ||
|
||
self.assertEqual(some_version_0, state_light_0) | ||
self.assertEqual(some_version_1, state_light_1) |
Oops, something went wrong.