Example #1
0
 def __init__(self, name: str, controller: "Controller",
              mocker: MockerFixture):
     self.name = name
     self.controller = controller
     self.get_default_actions_mapping = MagicMock(
         name="get_default_actions_mapping", return_value={})
     self.listen_changes = mocker.stub(name="listen_changes")
async def test_get_entity_state(
    sut: MyTypeController,
    mocker: MockerFixture,
    monkeypatch: MonkeyPatch,
    entity_input: str,
    entities: List[str],
    expected_calls: int,
):
    stub_get_state = mocker.stub()

    async def fake_get_state(entity, attribute=None):
        stub_get_state(entity, attribute=attribute)
        return entities

    monkeypatch.setattr(sut, "get_state", fake_get_state)

    with wrap_exetuction(error_expected=expected_calls is None,
                         exception=ValueError):
        await sut.get_entity_state(entity_input, "attribute_test")

    if expected_calls is not None:
        if expected_calls == 1:
            stub_get_state.assert_called_once_with(entity_input,
                                                   attribute="attribute_test")
        elif expected_calls == 2:
            stub_get_state.call_count == 2
            stub_get_state.assert_any_call(entity_input, attribute="entity_id")
            stub_get_state.assert_any_call("entity.test",
                                           attribute="attribute_test")
Example #3
0
async def test_call_service_controller(
    monkeypatch: MonkeyPatch,
    mocker: MockerFixture,
    integration: str,
    services: List[Dict[str, Any]],
    expected_calls: List[Tuple[str, Dict[str, Any]]],
):
    sut = CallServiceController()  # type: ignore
    sut.args = {
        "controller": "test_controller",
        "integration": integration,
        "mapping": {"action": services},
    }
    call_service_stub = mocker.stub()

    async def fake_call_service(self, service, **data):
        call_service_stub(service, **data)

    monkeypatch.setattr(Controller, "call_service", fake_call_service)

    # SUT
    await sut.initialize()
    sut.action_delta = 0
    await sut.handle_action("action")

    # Checks
    assert call_service_stub.call_count == len(expected_calls)
    for expected_service, expected_data in expected_calls:
        call_service_stub.assert_any_call(expected_service, **expected_data)
def test_fetch_user_finally(mocker: MockerFixture):
    """ Should raise finally when code reaches finally """

    stub = mocker.stub(name="db_conn_handler.session.close")

    with pytest.raises(Exception):
        user_repository.fetch()

        stub.assert_called()
def test_fetch_user_except(mocker: MockerFixture):
    """ Should raise exception if fetch throws """

    stub = mocker.stub(name="db_conn_handler.session.rollback")

    with pytest.raises(Exception):
        user_repository.fetch()

        stub.assert_called()
def test_remove_user_except(mocker: MockerFixture):
    """ Should raise exception if remove throws """

    stub = mocker.stub(name="db_conn_handler.session.rollback")

    with pytest.raises(Exception):
        user_repository.remove(user_id="1")

        stub.assert_called()
def test_insert_user_finally(mocker: MockerFixture):
    """ Should call close when code reaches finally """

    engine = db_conn_handler.get_engine()
    stub = mocker.stub(name="db_conn_handler.session.close")

    with pytest.raises(Exception):
        user_repository.insert(username="******", password="******")

        engine.execute("DELETE FROM user WHERE id='{}';".format(1))

        stub.assert_called()
def test_insert_user_except(mocker: MockerFixture):
    """ Should raise exception if insert throws """

    engine = db_conn_handler.get_engine()
    stub = mocker.stub(name="db_conn_handler.session.rollback")

    with pytest.raises(Exception):
        user_repository.insert(username="******", password="******")

        engine.execute("DELETE FROM user WHERE id='{}';".format(1))

        stub.assert_called()
Example #9
0
async def test_after_render(app, mocker: MockerFixture):
    stub = mocker.stub("app")
    app._custom_effects = [stub]
    task = mocker.patch.object(App, "_render_task")
    mocker.patch.object(FilePane, "loading")
    app._rendered = False
    app._after_render(None)
    task.assert_called_once()
    task.reset_mock()
    app._after_render(None)
    task.assert_not_called()
    stub.assert_called_with(app)