How to use the asynctest.mock.patch.object function in asynctest

To help you get started, we’ve selected a few asynctest examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zigpy / bellows / tests / test_application.py View on Github external
ezsp_mock.return_value.setConfigurationValue = mockezsp
    ezsp_mock.return_value.networkInit = mockinit
    ezsp_mock.return_value.getNetworkParameters = mockezsp
    ezsp_mock.return_value.setPolicy = mockezsp
    ezsp_mock.return_value.getNodeId = mockezsp
    ezsp_mock.return_value.getEui64.side_effect = CoroutineMock(return_value=[ieee])
    ezsp_mock.return_value.leaveNetwork = mockezsp
    app.form_network = CoroutineMock()
    ezsp_mock.return_value.reset.side_effect = CoroutineMock()
    ezsp_mock.return_value.version.side_effect = CoroutineMock()
    ezsp_mock.return_value.getConfigurationValue.side_effect = CoroutineMock(
        return_value=(0, 1)
    )

    loop = asyncio.get_event_loop()
    p1 = mock.patch.object(bellows.ezsp, "EZSP", new=ezsp_mock)
    p2 = mock.patch.object(bellows.multicast.Multicast, "startup")

    with p1, p2 as multicast_mock:
        loop.run_until_complete(app.startup(auto_form=auto_form))
    assert multicast_mock.await_count == 1
github opsdroid / opsdroid / tests / test_connector_rocketchat.py View on Github external
async def test_listen(self):
        with amock.patch.object(
            self.connector.loop, "create_task"
        ) as mocked_task, amock.patch.object(
            self.connector._closing, "wait"
        ) as mocked_event, amock.patch.object(
            self.connector, "get_messages_loop"
        ):
            mocked_event.return_value = asyncio.Future()
            mocked_event.return_value.set_result(True)
            mocked_task.return_value = asyncio.Future()
            await self.connector.listen()

            self.assertTrue(mocked_event.called)
            self.assertTrue(mocked_task.called)
github zigpy / zigpy / tests / test_zcl.py View on Github external
async def test_handle_cluster_general_request_not_attr_report(cluster):
    hdr = foundation.ZCLHeader.general(1, foundation.Command.Write_Attributes)
    p1 = mock.patch.object(cluster, "_update_attribute")
    p2 = mock.patch.object(cluster, "create_catching_task")
    with p1 as attr_lst_mock, p2 as response_mock:
        cluster.handle_cluster_general_request(hdr, [1, 2, 3])
        await asyncio.sleep(0)
        assert attr_lst_mock.call_count == 0
        assert response_mock.call_count == 0
github opsdroid / opsdroid / tests / test_parser_dialogflow.py View on Github external
opsdroid.config["parsers"] = [{"name": "dialogflow", "project-id": "test"}]
            mock_skill = await self.getMockSkill()
            mock_skill.config = {"name": "greetings"}
            opsdroid.skills.append(
                match_dialogflow_action("smalltalk.greetings.whatsup")(mock_skill)
            )

            mock_connector = amock.CoroutineMock()
            message = Message(
                text="I want some good French food",
                user="user",
                target="default",
                connector=mock_connector,
            )

            with amock.patch.object(
                dialogflow, "call_dialogflow"
            ) as mocked_call_dialogflow:
                mocked_call_dialogflow.return_value = dialogflow_response

                skills = await dialogflow.parse_dialogflow(
                    opsdroid, opsdroid.skills, message, opsdroid.config["parsers"][0]
                )
                self.assertEqual(mock_skill, skills[0]["skill"])
                self.assertLogs("_LOGGERS", "debug")
github zigpy / bellows / tests / test_application.py View on Github external
ezsp_mock.return_value.networkInit = mockinit
    ezsp_mock.return_value.getNetworkParameters = mockezsp
    ezsp_mock.return_value.setPolicy = mockezsp
    ezsp_mock.return_value.getNodeId = mockezsp
    ezsp_mock.return_value.getEui64.side_effect = CoroutineMock(return_value=[ieee])
    ezsp_mock.return_value.leaveNetwork = mockezsp
    app.form_network = CoroutineMock()
    ezsp_mock.return_value.reset.side_effect = CoroutineMock()
    ezsp_mock.return_value.version.side_effect = CoroutineMock()
    ezsp_mock.return_value.getConfigurationValue.side_effect = CoroutineMock(
        return_value=(0, 1)
    )

    loop = asyncio.get_event_loop()
    p1 = mock.patch.object(bellows.ezsp, "EZSP", new=ezsp_mock)
    p2 = mock.patch.object(bellows.multicast.Multicast, "startup")

    with p1, p2 as multicast_mock:
        loop.run_until_complete(app.startup(auto_form=auto_form))
    assert multicast_mock.await_count == 1
github zigpy / zigpy / tests / test_zcl.py View on Github external
async def test_write_attributes_cache_default_response(cluster, status):
    write_mock = CoroutineMock(
        return_value=[foundation.Command.Write_Attributes, status]
    )
    with mock.patch.object(cluster, "_write_attributes", write_mock):
        attributes = {4: "manufacturer", 5: "model", 12: 12}
        await cluster.write_attributes(attributes)
        assert cluster._write_attributes.call_count == 1
        for attr_id in attributes:
            assert attr_id not in cluster._attr_cache
github opsdroid / opsdroid / tests / test_connector_rocketchat.py View on Github external
async def test_disconnect(self):
        with amock.patch.object(self.connector.session, "close") as mocked_close:
            mocked_close.return_value = asyncio.Future()
            mocked_close.return_value.set_result(True)

            await self.connector.disconnect()
            self.assertFalse(self.connector.listening)
            self.assertTrue(self.connector.session.closed())
            self.assertEqual(self.connector._closing.set(), None)
github opsdroid / opsdroid / tests / test_parser_rasanlu.py View on Github external
async def test_train_rasanlu_succeeded(self):
        result = amock.Mock()
        result.text = amock.CoroutineMock()
        result.json = amock.CoroutineMock()
        result.status = 200
        result.json.return_value = {"info": "new model trained: abc1234"}

        with amock.patch(
            "aiohttp.ClientSession.post"
        ) as patched_request, amock.patch.object(
            rasanlu, "_get_all_intents"
        ) as mock_gai, amock.patch.object(
            rasanlu, "_init_model"
        ), amock.patch.object(
            rasanlu, "_build_training_url"
        ) as mock_btu, amock.patch.object(
            rasanlu, "_get_existing_models"
        ) as mock_gem, amock.patch.object(
            rasanlu, "_get_intents_fingerprint"
        ) as mock_gif:

            mock_gem.return_value = ["abc123"]
            mock_btu.return_value = "http://example.com"
            mock_gai.return_value = "Hello World"
            mock_gif.return_value = "abc1234"
            result.content_type = "application/json"

            patched_request.side_effect = None
github zigpy / bellows / tests / test_ezsp.py View on Github external
@mock.patch.object(ezsp.EZSP, "reset", new_callable=CoroutineMock)
@mock.patch.object(uart, "connect")
async def test_probe_success(mock_connect, mock_reset):
    """Test device probing."""

    res = await ezsp.EZSP.probe(DEVICE_CONFIG)
    assert res is True
    assert mock_connect.call_count == 1
    assert mock_connect.await_count == 1
    assert mock_reset.call_count == 1
    assert mock_connect.return_value.close.call_count == 1

    mock_connect.reset_mock()
    mock_reset.reset_mock()
    mock_connect.reset_mock()
    res = await ezsp.EZSP.probe(DEVICE_CONFIG)
    assert res is True