Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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())
def test_awaited_wait(self):
mock = asynctest.mock.CoroutineFunctionMock()
t = asyncio.ensure_future(mock.awaited.wait())
yield from mock()
yield from mock()
yield from t
mock.reset_mock()
t = asyncio.ensure_future(mock.awaited.wait(skip=1))
yield from mock()
self.assertFalse(t.done())
yield from mock()
yield from t
@asynctest.skipIf(small_test, "No enviroment variables for testing set up")
async def test_page_edit(self):
wiki = aiowiki.Wiki(AIOWIKI_TEST_URL)
page = wiki.get_page("Spam")
try:
await page.edit("Eggs & Ham")
except aiowiki.EditError as e:
if not "action you have requested is limited to users in the group" in str(
e
):
raise
else:
await wiki.login(AIOWIKI_TEST_USERNAME, AIOWIKI_TEST_PASSWORD)
await page.edit("Eggs & Ham")
await wiki.close()
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()
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
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"
)
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"))
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",
}
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
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