Пример #1
0
async def test_handle_action(
    sut: Controller,
    mocker: MockerFixture,
    actions_input: List[ActionEvent],
    action_called: str,
    action_called_times: int,
    action_delta: int,
    expected_calls: int,
    fake_action_type: ActionType,
):
    sut.action_delta = action_delta
    sut.action_times = defaultdict(lambda: 0)

    actions_mapping: ActionsMapping = {
        action: [fake_action_type]
        for action in actions_input
    }
    sut.actions_mapping = actions_mapping
    call_action_patch = mocker.patch.object(sut, "call_action")

    # SUT
    for _ in range(action_called_times):
        await sut.handle_action(action_called)

    # Checks
    assert call_action_patch.call_count == expected_calls
Пример #2
0
async def test_merge_mapping(
    sut_before_init: Controller,
    mocker: MockerFixture,
    mapping: List[str],
    merge_mapping: List[str],
    actions_output: List[str],
    error_expected: bool,
):
    actions_input = ["action1", "action2", "action3"]
    actions = {action: action for action in actions_input}
    predefined_actions = {action: lambda: None for action in actions_input}
    if mapping:
        sut_before_init.args["mapping"] = {item: item for item in mapping}
    if merge_mapping:
        sut_before_init.args["merge_mapping"] = {
            item: item
            for item in merge_mapping
        }

    mocker.patch.object(sut_before_init,
                        "get_default_actions_mapping",
                        return_value=actions)
    mocker.patch.object(
        sut_before_init,
        "get_predefined_actions_mapping",
        return_value=predefined_actions,
    )

    # SUT
    with wrap_exetuction(error_expected=error_expected, exception=ValueError):
        await sut_before_init.initialize()

    # Checks
    if not error_expected:
        assert list(sut_before_init.actions_mapping.keys()) == actions_output
Пример #3
0
def test_get_default_actions_mapping_throwing_error(sut: Controller,
                                                    mocker: MockerFixture):
    integration_mock = IntegrationMock("integration-test", sut, mocker)
    mocker.patch.object(integration_mock,
                        "get_default_actions_mapping",
                        return_value=None)

    with pytest.raises(ValueError) as e:
        sut.get_default_actions_mapping(integration_mock)  # type: ignore

    assert str(e.value) == "This controller does not support integration-test."
Пример #4
0
async def test_initialize(
    sut_before_init: Controller,
    mocker: MockerFixture,
    controller_input: Union[str, List[str]],
    actions_input: List[str],
    included_actions: Optional[List[str]],
    excluded_actions: Optional[List[str]],
    actions_output: List[str],
    error_expected: bool,
):
    actions = {action: action for action in actions_input}
    predefined_actions = {action: lambda: None for action in actions_input}
    sut_before_init.args["controller"] = controller_input
    integration_mock = IntegrationMock(INTEGRATION_TEST_NAME, sut_before_init,
                                       mocker)
    mocker.patch.object(sut_before_init,
                        "get_integration",
                        return_value=integration_mock)
    if included_actions:
        sut_before_init.args["actions"] = included_actions
    if excluded_actions:
        sut_before_init.args["excluded_actions"] = excluded_actions
    mocker.patch.object(sut_before_init,
                        "get_default_actions_mapping",
                        return_value=actions)
    mocker.patch.object(
        sut_before_init,
        "get_predefined_actions_mapping",
        return_value=predefined_actions,
    )
    get_default_actions_mapping = mocker.spy(sut_before_init,
                                             "get_default_actions_mapping")

    # SUT
    with wrap_exetuction(error_expected=error_expected, exception=ValueError):
        await sut_before_init.initialize()

    # Checks
    if not error_expected:
        get_default_actions_mapping.assert_called_once()
        for controller_id in controller_input:
            integration_mock.listen_changes.assert_any_call(controller_id)
        assert integration_mock.listen_changes.call_count == len(
            controller_input)
        assert list(sut_before_init.actions_mapping.keys()) == actions_output
Пример #5
0
def test_get_multiple_click_actions(
    fake_action_type: ActionType,
    sut: Controller,
    mapping: List[ActionEvent],
    expected: List[str],
):
    actions_mapping: ActionsMapping = {key: [fake_action_type] for key in mapping}
    output = sut.get_multiple_click_actions(actions_mapping)
    assert output == set(expected)
Пример #6
0
def test_get_mapping_per_action(
    sut: Controller,
    actions: Set[ActionEvent],
    custom: Optional[Dict[ActionEvent, Any]],
    default: Any,
    expected: Dict[ActionEvent, Any],
) -> None:
    actions_mapping: ActionsMapping = {action: [] for action in actions}
    output = sut.get_mapping_per_action(actions_mapping, custom=custom, default=default)
    assert output == expected
Пример #7
0
def sut_before_init(fake_controller: Controller, mocker: MockerFixture) -> Controller:
    fake_controller.args = {
        "controller": CONTROLLER_NAME,
        "integration": INTEGRATION_TEST_NAME,
    }
    integration_mock = IntegrationMock("test", fake_controller, mocker)
    mocker.patch.object(
        fake_controller, "get_integration", return_value=integration_mock
    )
    return fake_controller
Пример #8
0
 def __init__(
     self,
     name: str,
     entities: Optional[List[str]] = None,
     mode: Mode = DEFAULT_MODE,
     topic_prefix: str = DEFAULT_TOPIC_PREFIX,
 ) -> None:
     super().__init__(name, entities)
     mode = Controller.get_option(mode, ["ha", "mqtt"])
     self.mode = mode
     self.topic_prefix = topic_prefix
Пример #9
0
async def test_call_action(
    sut: Controller,
    monkeypatch,
    mocker: MockerFixture,
    delay: int,
    handle: Optional[int],
    cancel_timer_called: bool,
    run_in_called: bool,
    action_timer_callback_called: bool,
):
    action_key = "test"
    sut.action_delay = {action_key: delay}
    action_delay_handles: Dict[ActionEvent, Optional[float]] = {
        action_key: handle
    }
    sut.action_delay_handles = action_delay_handles

    monkeypatch.setattr(sut, "cancel_timer", fake_fn(async_=True))
    monkeypatch.setattr(sut, "run_in", fake_fn(async_=True))
    monkeypatch.setattr(sut, "action_timer_callback", fake_fn(async_=True))
    cancel_timer_patch = mocker.patch.object(sut, "cancel_timer")
    run_in_patch = mocker.patch.object(sut, "run_in")
    action_timer_callback_patch = mocker.patch.object(sut,
                                                      "action_timer_callback")

    # SUT
    await sut.call_action(action_key)

    # Checks
    if cancel_timer_called:
        cancel_timer_patch.assert_called_once_with(handle)
    if run_in_called:
        run_in_patch.assert_called_once_with(sut.action_timer_callback,
                                             delay,
                                             action_key=action_key,
                                             extra=None)
    if action_timer_callback_called:
        action_timer_callback_patch.assert_called_once_with({
            "action_key": action_key,
            "extra": None
        })
Пример #10
0
def test_get_integration(
    fake_controller: Controller,
    mocker: MockerFixture,
    integration_input: Union[str, Dict[str, Any]],
    integration_name_expected: str,
    args_expected: Dict[str, Any],
    error_expected: bool,
):
    get_integrations_spy = mocker.spy(integration_module, "get_integrations")

    with wrap_exetuction(error_expected=error_expected, exception=ValueError):
        integration = fake_controller.get_integration(integration_input)

    if not error_expected:
        get_integrations_spy.assert_called_once_with(fake_controller, args_expected)
        assert integration.name == integration_name_expected
Пример #11
0
def test_get_action(
    sut: Controller,
    test_input: TypeAction,
    expected: ActionFunction,
    error_expected: bool,
):
    with wrap_exetuction(error_expected=error_expected,
                         exception=ValueError) as err_info:
        output = sut.get_action(test_input)

    if err_info is not None:
        assert (
            str(err_info.value) ==
            "The action value from the action mapping should be a list or a function"
        )
    else:
        assert output == expected
Пример #12
0
def test_get_multiple_click_actions(sut: Controller,
                                    mapping: List[ActionEvent],
                                    expected: List[str]):
    output = sut.get_multiple_click_actions({key: "action" for key in mapping})
    assert output == set(expected)
Пример #13
0
def test_render_value(sut: Controller, template: str, expected: bool) -> None:
    output = sut.contains_templating(template)
    assert output == expected
Пример #14
0
def test_get_option(sut: Controller, option: str, options: List[str],
                    error_expected: bool):
    with wrap_exetuction(error_expected=error_expected, exception=ValueError):
        sut.get_option(option, options)
Пример #15
0
def test_get_list(sut: Controller, test_input: Union[List[str], str],
                  expected: List[str]):
    output = sut.get_list(test_input)
    assert output == expected
Пример #16
0
def fake_controller(hass_mock):
    c = Controller()
    c.args = {}
    return c
Пример #17
0
def fake_controller() -> Controller:
    c = Controller()  # type: ignore
    c.args = {}
    return c
Пример #18
0
def test_check_ad_version_throwing_error(sut: Controller,
                                         mocker: MockerFixture):
    mocker.patch.object(sut, "get_ad_version", return_value="3.0.0")
    with pytest.raises(ValueError) as e:
        sut.check_ad_version()
    assert str(e.value) == "Please upgrade to AppDaemon 4.x"
Пример #19
0
def fake_controller(hass_mock):
    c = Controller()  # type: ignore
    c.args = {}
    return c