def test_download_should_cache_downloaded_file_and_download_only_once(
        active_artifact_with_loaded_artifact: ActiveArtifact,
        download_artifact_mock, mocker: MockerFixture):
    # arrange
    zip_mock = mocker.patch('mlaide.active_artifact.ZipFile')

    zip_bytes = io.BytesIO(initial_bytes=bytes('abc', 'utf-8'))
    download_artifact_mock.return_value = (zip_bytes, 'artifact.zip')

    # act
    active_artifact_with_loaded_artifact.download('./download-target')
    active_artifact_with_loaded_artifact.download('./download-target-2')

    # assert
    download_artifact_mock.assert_called_once()
    assert zip_mock.call_count == 2
    assert zip_mock.call_args_list == [
        mocker.call(zip_bytes), mocker.call(zip_bytes)
    ]
示例#2
0
async def test_integ_configs(mocker: MockerFixture, config_file: str,
                             data: Dict[str, Any]):
    entity_state_attributes = data.get("entity_state_attributes", {})
    entity_state = data.get("entity_state", None)
    fired_actions = data.get("fired_actions", [])
    render_template_response = data.get("render_template_response")
    extra = data.get("extra")
    expected_calls = data.get("expected_calls", [])
    expected_calls_count = data.get("expected_calls_count",
                                    len(expected_calls))

    config = read_config_yaml(config_file)
    controller = get_controller(config["module"], config["class"])
    if controller is None:
        raise ValueError(
            f"`{config['class']}` class controller does not exist")
    controller.args = config

    if render_template_response is not None:
        mocker.patch.object(controller,
                            "_render_template",
                            return_value=render_template_response)

    if isinstance(controller, TypeController):
        fake_entity_states = get_fake_entity_states(entity_state,
                                                    entity_state_attributes)
        mocker.patch.object(controller, "get_entity_state", fake_entity_states)
    call_service_stub = mocker.patch.object(Hass, "call_service")

    await controller.initialize()
    for idx, action in enumerate(fired_actions):
        if any(isinstance(action, type_) for type_ in (str, int)):
            coroutine = controller.handle_action(action, extra=extra)
            if idx == len(fired_actions) - 1:
                await coroutine
            else:
                asyncio.ensure_future(coroutine)
        elif isinstance(action, float):
            await asyncio.sleep(action)

    pending = asyncio.Task.all_tasks()
    # We exclude the current function we are executing
    pending = {
        task
        for task in pending
        if task._coro.__name__ != "test_integ_configs"  # type: ignore
    }
    if pending:  # Finish pending tasks if any
        await asyncio.wait(pending)
    assert call_service_stub.call_count == expected_calls_count
    calls = [
        mocker.call(controller, call["service"], **call.get("data", {}))
        for call in expected_calls
    ]
    call_service_stub.assert_has_calls(calls)