Пример #1
0
async def test_merge_mapping(
    sut,
    monkeypatch,
    mocker,
    mapping,
    merge_mapping,
    actions_output,
    expected_error,
):
    actions_input = ["action1", "action2", "action3"]
    actions = {action: action for action in actions_input}
    type_actions = {action: lambda: None for action in actions_input}
    sut.args["controller"] = "my_controller"
    sut.args["integration"] = "test"
    if mapping:
        sut.args["mapping"] = {item: item for item in mapping}
    if merge_mapping:
        sut.args["merge_mapping"] = {item: item for item in merge_mapping}
    integration_mock = IntegrationMock("test", sut, mocker)
    monkeypatch.setattr(sut, "get_integration",
                        lambda integration: integration_mock)
    monkeypatch.setattr(sut, "get_actions_mapping",
                        lambda integration: actions)
    monkeypatch.setattr(sut, "get_type_actions_mapping", lambda: type_actions)

    # SUT
    if expected_error:
        with pytest.raises(ValueError):
            await sut.initialize()
    else:
        await sut.initialize()

        # Checks
        assert list(sut.actions_mapping.keys()) == actions_output
Пример #2
0
def test_get_default_actions_mapping_happyflow(sut, monkeypatch, mocker):
    integration_mock = IntegrationMock("integration-test", sut, mocker)
    monkeypatch.setattr(integration_mock, "get_default_actions_mapping",
                        lambda: "this_is_a_mapping")

    mapping = sut.get_default_actions_mapping(integration_mock)

    assert mapping == "this_is_a_mapping"
Пример #3
0
def test_get_actions_mapping_throwing_error(sut, monkeypatch, mocker):
    integration_mock = IntegrationMock("integration-test", sut, mocker)
    monkeypatch.setattr(integration_mock, "get_actions_mapping", lambda: None)

    with pytest.raises(ValueError) as e:
        sut.get_actions_mapping(integration_mock)

    assert str(e.value) == "This controller does not support integration-test."
Пример #4
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
Пример #5
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."
Пример #6
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
Пример #7
0
async def test_initialize(
    sut,
    mocker,
    monkeypatch,
    controller_input,
    actions_input,
    included_actions,
    excluded_actions,
    actions_ouput,
    expect_an_error,
):
    actions = {action: action for action in actions_input}
    type_actions = {action: lambda: None for action in actions_input}
    sut.args["controller"] = controller_input
    sut.args["integration"] = "test"
    if included_actions:
        sut.args["actions"] = included_actions
    if excluded_actions:
        sut.args["excluded_actions"] = excluded_actions
    integration_mock = IntegrationMock("test", sut, mocker)
    monkeypatch.setattr(sut, "get_integration",
                        lambda integration: integration_mock)
    monkeypatch.setattr(sut, "get_actions_mapping",
                        lambda integration: actions)
    monkeypatch.setattr(sut, "get_type_actions_mapping", lambda: type_actions)
    check_ad_version = mocker.patch.object(sut, "check_ad_version")
    get_actions_mapping = mocker.spy(sut, "get_actions_mapping")

    # SUT
    if expect_an_error:
        with pytest.raises(ValueError):
            await sut.initialize()
    else:
        await sut.initialize()

        # Checks
        check_ad_version.assert_called_once()
        get_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.actions_mapping.keys()) == actions_ouput