From e125d18d7ed159bfddfe3c1b651d14f3b71f86e9 Mon Sep 17 00:00:00 2001 From: andyinno Date: Sat, 23 May 2020 19:39:38 +0200 Subject: [PATCH 1/5] Added AppDaemon command. select_option is a AppDaemon command that was not mocked. --- appdaemontestframework/hass_mocks.py | 1 + 1 file changed, 1 insertion(+) diff --git a/appdaemontestframework/hass_mocks.py b/appdaemontestframework/hass_mocks.py index 0f11071..7aeacf1 100644 --- a/appdaemontestframework/hass_mocks.py +++ b/appdaemontestframework/hass_mocks.py @@ -94,6 +94,7 @@ def _hass_init_mock(self, _ad, name, *_args): MockHandler(Hass, 'turn_on'), MockHandler(Hass, 'turn_off'), MockHandler(Hass, 'fire_event'), + MockHandler(Hass, 'select_option'), ### Custom callback functions MockHandler(Hass, 'register_constraint'), From 5648ae58c4481c9948239f1bc8ad60d707e70bb6 Mon Sep 17 00:00:00 2001 From: Andrea Innocenti Date: Sun, 24 May 2020 15:59:38 +0200 Subject: [PATCH 2/5] Added test for select_option --- test/test_extra_hass_functions.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/test_extra_hass_functions.py b/test/test_extra_hass_functions.py index 7f4ddde..d015a2c 100644 --- a/test/test_extra_hass_functions.py +++ b/test/test_extra_hass_functions.py @@ -13,6 +13,9 @@ def call_notify(self): def call_now_is_between(self): self.now_is_between("sunset - 00:45:00", "sunrise + 00:45:00") + def call_select_option(self): + self.select_option("select_item", "value") + @automation_fixture(WithExtraHassFunctions) def with_extra_hass_functions(): @@ -27,3 +30,7 @@ def test_now_is_between(given_that, with_extra_hass_functions, hass_mocks): def test_notify(given_that, with_extra_hass_functions, hass_mocks): with_extra_hass_functions.call_notify() hass_mocks.hass_functions['notify'].assert_called_with(message="test", name="html5") + +def test_select_option(given_that, with_extra_hass_functions, hass_mocks): + with_extra_hass_functions.call_select_option() + hass_mocks.hass_functions['select_option'].assert_called_with("select_item", "value") \ No newline at end of file From 9d45e1d2692f1f41eae309d56ba7e308e7ae04c2 Mon Sep 17 00:00:00 2001 From: Andrea Innocenti Date: Sun, 24 May 2020 18:11:15 +0200 Subject: [PATCH 3/5] Initial addition of assert_that set_option --- appdaemontestframework/assert_that.py | 31 +++++++++++++++++++++++++++ test/test_assert_that.py | 8 +++++++ 2 files changed, 39 insertions(+) diff --git a/appdaemontestframework/assert_that.py b/appdaemontestframework/assert_that.py index 0d210fe..5e8fd38 100644 --- a/appdaemontestframework/assert_that.py +++ b/appdaemontestframework/assert_that.py @@ -68,6 +68,10 @@ def turned_off(self): def called_with(self, **kwargs): pass + @abstractmethod + def set_to_option(self, **kwargs): + pass + def called(self): self.called_with() @@ -120,6 +124,23 @@ def called_with(self, **kwargs): self.hass_functions['call_service'].assert_any_call( service_full_name, **kwargs) + def set_to_option(self, newvalue, **service_specific_parameters): + """ Assert that a given entity_id has been set to the choosen value """ + entity_id = self.thing_to_check + + service_not_called = _capture_assert_failure_exception( + lambda: self.hass_functions['call_service'].assert_any_call( + ServiceOnAnyDomain('select_option'), + **{'entity_id': entity_id, 'option': newvalue, **service_specific_parameters})) + + set_option_helper_not_called = _capture_assert_failure_exception( + lambda: self.hass_functions['select_option'].assert_any_call( + entity_id, newvalue + **service_specific_parameters)) + + if service_not_called and set_option_helper_not_called: + raise EitherOrAssertionError( + service_not_called, set_option_helper_not_called) class WasNotWrapper(Was): def __init__(self, was_wrapper): @@ -154,6 +175,16 @@ def called_with(self, **kwargs): raise AssertionError( "Service shoud NOT have been called with the given args: " + str(kwargs)) + def set_to_option(self, newvalue, **service_specific_parameters): + """ Assert that a given entity_id has NOT the value newvalue """ + val = _capture_assert_failure_exception( + lambda: self.was_wrapper.set_to_option(newvalue, **service_specific_parameters) + ) + if not val: + raise AssertionError( + "Should NOT have the value: " + + str(self.was_wrapper.thing_to_check)) + class ListensToWrapper: def __init__(self, automation_thing_to_check, hass_functions): diff --git a/test/test_assert_that.py b/test/test_assert_that.py index ce23b19..d4d13e0 100644 --- a/test/test_assert_that.py +++ b/test/test_assert_that.py @@ -22,6 +22,7 @@ LIGHT = 'light.some_light' SWITCH = 'switch.some_switch' +INPUT_SELECT = 'input_select.some_input_select' TRANSITION_DURATION = 2 @@ -141,3 +142,10 @@ def test_with_kwargs(self, assert_that, automation): assert_that(LIGHT).was_not.turned_off() automation.turn_off_light_with_transition(via_helper=True) assert_that(LIGHT).was.turned_off(transition=TRANSITION_DURATION) + +class TestSelectOption: + class TestViaService: + def test_option_is_set(self, assert_that, automation): + assert_that(INPUT_SELECT).was_not.set_to_option('new_option') + automation.select_option('new_option') + assert_that(INPUT_SELECT).was.set_to_option('new_option') \ No newline at end of file From fd0b8c7365c651cf4c225d630cd65718a824deb6 Mon Sep 17 00:00:00 2001 From: Florian Kempenich Date: Tue, 2 Jun 2020 18:15:27 +0100 Subject: [PATCH 4/5] Fix minor bug --- appdaemontestframework/assert_that.py | 9 +++++---- test/test_assert_that.py | 3 ++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/appdaemontestframework/assert_that.py b/appdaemontestframework/assert_that.py index 5e8fd38..1c71b9f 100644 --- a/appdaemontestframework/assert_that.py +++ b/appdaemontestframework/assert_that.py @@ -124,18 +124,19 @@ def called_with(self, **kwargs): self.hass_functions['call_service'].assert_any_call( service_full_name, **kwargs) - def set_to_option(self, newvalue, **service_specific_parameters): - """ Assert that a given entity_id has been set to the choosen value """ + def set_to_option(self, option_selected, **service_specific_parameters): + """ Assert that a given entity_id has been set to the chosen value """ entity_id = self.thing_to_check service_not_called = _capture_assert_failure_exception( lambda: self.hass_functions['call_service'].assert_any_call( ServiceOnAnyDomain('select_option'), - **{'entity_id': entity_id, 'option': newvalue, **service_specific_parameters})) + **{'entity_id': entity_id, 'option': option_selected, **service_specific_parameters})) set_option_helper_not_called = _capture_assert_failure_exception( lambda: self.hass_functions['select_option'].assert_any_call( - entity_id, newvalue + entity_id, + option_selected, **service_specific_parameters)) if service_not_called and set_option_helper_not_called: diff --git a/test/test_assert_that.py b/test/test_assert_that.py index d4d13e0..8808ad1 100644 --- a/test/test_assert_that.py +++ b/test/test_assert_that.py @@ -143,9 +143,10 @@ def test_with_kwargs(self, assert_that, automation): automation.turn_off_light_with_transition(via_helper=True) assert_that(LIGHT).was.turned_off(transition=TRANSITION_DURATION) + class TestSelectOption: class TestViaService: def test_option_is_set(self, assert_that, automation): assert_that(INPUT_SELECT).was_not.set_to_option('new_option') - automation.select_option('new_option') + automation.select_option(INPUT_SELECT, 'new_option') assert_that(INPUT_SELECT).was.set_to_option('new_option') \ No newline at end of file From 2b922e342c0c9bf7790a3a852d0447ec5a5410cd Mon Sep 17 00:00:00 2001 From: Andrea Innocenti Date: Sun, 7 Jun 2020 09:39:56 +0200 Subject: [PATCH 5/5] Added test via service call --- test/test_assert_that.py | 10 ++++++++-- test/test_extra_hass_functions.py | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/test_assert_that.py b/test/test_assert_that.py index 8808ad1..e226235 100644 --- a/test/test_assert_that.py +++ b/test/test_assert_that.py @@ -145,8 +145,14 @@ def test_with_kwargs(self, assert_that, automation): class TestSelectOption: - class TestViaService: + class TestViaHelper: def test_option_is_set(self, assert_that, automation): assert_that(INPUT_SELECT).was_not.set_to_option('new_option') automation.select_option(INPUT_SELECT, 'new_option') - assert_that(INPUT_SELECT).was.set_to_option('new_option') \ No newline at end of file + assert_that(INPUT_SELECT).was.set_to_option('new_option') + + class TestViaService: + def test_option_is_set(self, assert_that, automation): + assert_that(INPUT_SELECT).was_not.set_to_option('new_service_option') + automation.call_service("input_select/select_option", entity_id=INPUT_SELECT, option='new_service_option') + assert_that(INPUT_SELECT).was.set_to_option('new_service_option') \ No newline at end of file diff --git a/test/test_extra_hass_functions.py b/test/test_extra_hass_functions.py index d015a2c..2167f1a 100644 --- a/test/test_extra_hass_functions.py +++ b/test/test_extra_hass_functions.py @@ -33,4 +33,4 @@ def test_notify(given_that, with_extra_hass_functions, hass_mocks): def test_select_option(given_that, with_extra_hass_functions, hass_mocks): with_extra_hass_functions.call_select_option() - hass_mocks.hass_functions['select_option'].assert_called_with("select_item", "value") \ No newline at end of file + hass_mocks.hass_functions['select_option'].assert_called_with("select_item", "value")