How to use the asynctest.CoroutineMock 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 ekonda / kutana / tests / test_vkontakte.py View on Github external
def test_raw_request(mock_post):
    mock_post.return_value.__aenter__.return_value.json = CoroutineMock(
        side_effect=[{"response": 1}, {"error": 1}]
    )

    async def test():
        vkontakte = Vkontakte(token="token", session=aiohttp.ClientSession())

        assert await vkontakte.raw_request("method1", {"arg": "val1"}) == 1

        with pytest.raises(RequestException):
            await vkontakte.raw_request("method2", {"arg": "val2"})

    asyncio.get_event_loop().run_until_complete(test())
github FAForever / server / tests / unit_tests / test_async_functions.py View on Github external
async def test_gather_without_exceptions_subclass():
    completes_correctly = CoroutineMock()

    await gather_without_exceptions([
        raises_connection_error(),
        raises_connection_reset_error(),
        completes_correctly()
    ], ConnectionError)

    completes_correctly.assert_called_once()
github argaen / aiocache / tests / ut / backends / test_redis.py View on Github external
def redis_connection():
    conn = MagicMock()
    conn.__enter__ = MagicMock(return_value=conn)
    conn.__exit__ = MagicMock()
    conn.get = CoroutineMock()
    conn.mget = CoroutineMock()
    conn.set = CoroutineMock()
    conn.setex = CoroutineMock()
    conn.mset = CoroutineMock()
    conn.incrby = CoroutineMock()
    conn.exists = CoroutineMock()
    conn.persist = CoroutineMock()
    conn.expire = CoroutineMock()
    conn.delete = CoroutineMock()
    conn.flushdb = CoroutineMock()
    conn.eval = CoroutineMock()
    conn.keys = CoroutineMock()
    conn.multi_exec = MagicMock(return_value=conn)
    conn.execute = CoroutineMock()
    return conn
github home-assistant / home-assistant / tests / components / pi_hole / test_init.py View on Github external
async def test_setup_custom_config(hass):
    """Tests component setup with custom config."""
    with patch.object(
        Hole, "get_data", new=CoroutineMock(side_effect=mock_pihole_data_call(Hole))
    ):
        assert await async_setup_component(
            hass, pi_hole.DOMAIN, {pi_hole.DOMAIN: {"name": "Custom"}}
        )

    await hass.async_block_till_done()

    assert (
        hass.states.get("sensor.custom_ads_blocked_today").name
        == "Custom Ads Blocked Today"
    )
github Martiusweb / asynctest / doc / examples / tutorial / mocking.py View on Github external
async def test_result_with_side_effect_function(self):
        def uppercase_all(*args):
            return tuple(arg.upper() for arg in args)

        coroutine_mock = asynctest.CoroutineMock()
        coroutine_mock.side_effect = uppercase_all

        self.assertEqual(("FIRST", "CALL"),
                         await coroutine_mock("first", "call"))
        self.assertEqual(("A", "SECOND", "CALL"),
                         await coroutine_mock("a", "second", "call"))
github Peter-Slump / python-keycloak-client / tests / keycloak / aio / test_uma.py View on Github external
async def setUp(self):
        self.realm = asynctest.MagicMock(spec_set=KeycloakRealm)
        self.realm.client.get = asynctest.CoroutineMock()
        self.realm.client.post = asynctest.CoroutineMock()
        self.realm.client.put = asynctest.CoroutineMock()
        self.realm.client.delete = asynctest.CoroutineMock()

        self.uma_client = await KeycloakUMA(realm=self.realm)
        self.uma_client.well_known.contents = {
            "resource_registration_endpoint": "https://resource_registration",
            "permission_endpoint": "https://permission",
            "policy_endpoint": "https://policy",
        }
github vapor-ware / synse-server / tests / unit / commands / test_transaction_command.py View on Github external
def mock_get_transaction(monkeypatch):
    """Fixture to monkeypatch the cache transaction lookup."""
    mock = asynctest.CoroutineMock(
        synse_server.cache.get_transaction,
        side_effect=mockgettransaction
    )
    monkeypatch.setattr(synse_server.cache, 'get_transaction', mock)
    return mock_get_transaction
github home-assistant / home-assistant / tests / components / zha / test_config_flow.py View on Github external
async def test_check_zigpy_connection():
    """Test config flow validator."""

    mock_radio = asynctest.MagicMock()
    mock_radio.connect = asynctest.CoroutineMock()
    radio_cls = asynctest.MagicMock(return_value=mock_radio)

    bad_radio = asynctest.MagicMock()
    bad_radio.connect = asynctest.CoroutineMock(side_effect=Exception)
    bad_radio_cls = asynctest.MagicMock(return_value=bad_radio)

    mock_ctrl = asynctest.MagicMock()
    mock_ctrl.startup = asynctest.CoroutineMock()
    mock_ctrl.shutdown = asynctest.CoroutineMock()
    ctrl_cls = asynctest.MagicMock(return_value=mock_ctrl)
    new_radios = {
        mock.sentinel.radio: {ZHA_GW_RADIO: radio_cls, CONTROLLER: ctrl_cls},
        mock.sentinel.bad_radio: {ZHA_GW_RADIO: bad_radio_cls, CONTROLLER: ctrl_cls},
    }

    with mock.patch.dict(
        homeassistant.components.zha.core.registries.RADIO_TYPES, new_radios, clear=True
    ):
        assert not await config_flow.check_zigpy_connection(
            mock.sentinel.usb_path, mock.sentinel.unk_radio, mock.sentinel.zigbee_db
        )
        assert mock_radio.connect.call_count == 0
        assert bad_radio.connect.call_count == 0
        assert mock_ctrl.startup.call_count == 0
github mgurdal / aegis / tests / test_auth_middleware.py View on Github external
async def test_auth_middleware_checks_aiohttp_auth_initialization():
    # make a mock request
    stub_request = CoroutineMock()
    stub_request.app = {}

    # make a mock view
    stub_view = CoroutineMock()

    with pytest.raises(AttributeError) as error:
        # noinspection PyTypeChecker
        await middlewares.auth_middleware(stub_request, stub_view)

    assert str(error.value) == (
        "Please initialize the authenticator with " "Authenticator.setup(app) first."
    )
github ekonda / kutana / tests / test_telegram.py View on Github external
def make_mock(exc):
        cm = CoroutineMock()
        cm.__aenter__ = CoroutineMock(side_effect=exc)
        cm.__aexit__ = CoroutineMock()
        return cm