Skip to content

Commit

Permalink
fix: provide CoroutineMock adapter
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Oct 27, 2023
1 parent 818ee7f commit cfc2b8c
Show file tree
Hide file tree
Showing 154 changed files with 3,495 additions and 3,270 deletions.
44 changes: 23 additions & 21 deletions aries_cloudagent/admin/tests/test_admin_server.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

import pytest
import mock
from aries_cloudagent.tests import mock
from unittest import IsolatedAsyncioTestCase
from aiohttp import ClientSession, DummyCookieJar, TCPConnector, web
from aiohttp.test_utils import unused_port
Expand Down Expand Up @@ -44,9 +44,9 @@ async def test_debug_middleware(self):
method="GET",
path_qs="/hello/world?a=1&b=2",
match_info={"match": "info"},
text=mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
handler = mock.AsyncMock()
handler = mock.CoroutineMock()

await test_module.debug_middleware(request, handler)
mock_logger.isEnabledFor.assert_called_once()
Expand All @@ -62,36 +62,36 @@ async def test_ready_middleware(self):
request = mock.MagicMock(
rel_url="/", app=mock.MagicMock(_state={"ready": False})
)
handler = mock.AsyncMock(return_value="OK")
handler = mock.CoroutineMock(return_value="OK")
with self.assertRaises(test_module.web.HTTPServiceUnavailable):
await test_module.ready_middleware(request, handler)

request.app._state["ready"] = True
assert await test_module.ready_middleware(request, handler) == "OK"

request.app._state["ready"] = True
handler = mock.AsyncMock(
handler = mock.CoroutineMock(
side_effect=test_module.LedgerConfigError("Bad config")
)
with self.assertRaises(test_module.LedgerConfigError):
await test_module.ready_middleware(request, handler)

request.app._state["ready"] = True
handler = mock.AsyncMock(
handler = mock.CoroutineMock(
side_effect=test_module.web.HTTPFound(location="/api/doc")
)
with self.assertRaises(test_module.web.HTTPFound):
await test_module.ready_middleware(request, handler)

request.app._state["ready"] = True
handler = mock.AsyncMock(
handler = mock.CoroutineMock(
side_effect=test_module.asyncio.CancelledError("Cancelled")
)
with self.assertRaises(test_module.asyncio.CancelledError):
await test_module.ready_middleware(request, handler)

request.app._state["ready"] = True
handler = mock.AsyncMock(side_effect=KeyError("No such thing"))
handler = mock.CoroutineMock(side_effect=KeyError("No such thing"))
with self.assertRaises(KeyError):
await test_module.ready_middleware(request, handler)

Expand Down Expand Up @@ -123,10 +123,10 @@ def get_admin_server(
profile,
self.outbound_message_router,
self.webhook_router,
conductor_stop=mock.AsyncMock(),
conductor_stop=mock.CoroutineMock(),
task_queue=TaskQueue(max_active=4) if task_queue else None,
conductor_stats=(
None if task_queue else mock.AsyncMock(return_value={"a": 1})
None if task_queue else mock.CoroutineMock(return_value={"a": 1})
),
)

Expand Down Expand Up @@ -167,7 +167,9 @@ async def test_start_stop(self):
)
await server.stop()

with mock.patch.object(web.TCPSite, "start", mock.AsyncMock()) as mock_start:
with mock.patch.object(
web.TCPSite, "start", mock.CoroutineMock()
) as mock_start:
mock_start.side_effect = OSError("Failure to launch")
with self.assertRaises(AdminSetupError):
await self.get_admin_server(settings).start()
Expand Down Expand Up @@ -216,7 +218,7 @@ async def test_import_routes_multitenant_middleware(self):
method="GET",
headers={"Authorization": "Bearer ..."},
path="/multitenancy/etc",
text=mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
with self.assertRaises(test_module.web.HTTPUnauthorized):
await mt_authz_middle(mock_request, None)
Expand All @@ -225,7 +227,7 @@ async def test_import_routes_multitenant_middleware(self):
method="GET",
headers={},
path="/protected/non-multitenancy/non-server",
text=mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
with self.assertRaises(test_module.web.HTTPUnauthorized):
await mt_authz_middle(mock_request, None)
Expand All @@ -234,19 +236,19 @@ async def test_import_routes_multitenant_middleware(self):
method="GET",
headers={"Authorization": "Bearer ..."},
path="/protected/non-multitenancy/non-server",
text=mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
mock_handler = mock.AsyncMock()
mock_handler = mock.CoroutineMock()
await mt_authz_middle(mock_request, mock_handler)
assert mock_handler.called_once_with(mock_request)

mock_request = mock.MagicMock(
method="GET",
headers={"Authorization": "Non-bearer ..."},
path="/test",
text=mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
mock_handler = mock.AsyncMock()
mock_handler = mock.CoroutineMock()
await mt_authz_middle(mock_request, mock_handler)
assert mock_handler.called_once_with(mock_request)

Expand All @@ -257,7 +259,7 @@ async def test_import_routes_multitenant_middleware(self):
method="GET",
headers={"Authorization": "Non-bearer ..."},
path="/protected/non-multitenancy/non-server",
text=mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
with self.assertRaises(test_module.web.HTTPUnauthorized):
await setup_ctx_middle(mock_request, None)
Expand All @@ -266,12 +268,12 @@ async def test_import_routes_multitenant_middleware(self):
method="GET",
headers={"Authorization": "Bearer ..."},
path="/protected/non-multitenancy/non-server",
text=mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
with mock.patch.object(
server.multitenant_manager,
"get_profile_for_token",
mock.AsyncMock(),
mock.CoroutineMock(),
) as mock_get_profile:
mock_get_profile.side_effect = [
test_module.MultitenantManagerError("corrupt token"),
Expand Down Expand Up @@ -486,7 +488,7 @@ async def server():
async def test_on_record_event(server, event_topic, webhook_topic):
profile = InMemoryProfile.test_profile()
with mock.patch.object(
server, "send_webhook", mock.AsyncMock()
server, "send_webhook", mock.CoroutineMock()
) as mock_send_webhook:
await server._on_record_event(profile, Event(event_topic, None))
mock_send_webhook.assert_called_once_with(profile, webhook_topic, None)
Expand Down
12 changes: 6 additions & 6 deletions aries_cloudagent/commands/tests/test_provision.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from unittest import mock
from aries_cloudagent.tests import mock
from unittest import IsolatedAsyncioTestCase

from ...config.base import ConfigError
Expand Down Expand Up @@ -40,24 +40,24 @@ def test_provision_wallet(self):
)

async def test_provision_ledger_configured(self):
profile = mock.MagicMock(close=mock.AsyncMock())
profile = mock.MagicMock(close=mock.CoroutineMock())
with mock.patch.object(
test_module,
"wallet_config",
mock.AsyncMock(
mock.CoroutineMock(
return_value=(
profile,
mock.AsyncMock(did="public DID", verkey="verkey"),
mock.CoroutineMock(did="public DID", verkey="verkey"),
)
),
) as mock_wallet_config, mock.patch.object(
test_module, "ledger_config", mock.AsyncMock(return_value=True)
test_module, "ledger_config", mock.CoroutineMock(return_value=True)
) as mock_ledger_config:
await test_module.provision({})

async def test_provision_config_x(self):
with mock.patch.object(
test_module, "wallet_config", mock.AsyncMock()
test_module, "wallet_config", mock.CoroutineMock()
) as mock_wallet_config:
mock_wallet_config.side_effect = ConfigError("oops")
with self.assertRaises(test_module.ProvisionError):
Expand Down
20 changes: 10 additions & 10 deletions aries_cloudagent/commands/tests/test_start.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys

from unittest import mock
from aries_cloudagent.tests import mock
from unittest import IsolatedAsyncioTestCase

from ...config.error import ArgsParseError
Expand All @@ -18,9 +18,9 @@ def test_bad_args(self):

async def test_start_shutdown_app(self):
mock_conductor = mock.MagicMock(
setup=mock.AsyncMock(),
start=mock.AsyncMock(),
stop=mock.AsyncMock(),
setup=mock.CoroutineMock(),
start=mock.CoroutineMock(),
stop=mock.CoroutineMock(),
)
await test_module.start_app(mock_conductor)
await test_module.shutdown_app(mock_conductor)
Expand Down Expand Up @@ -57,9 +57,9 @@ def test_exec_start(self):
run_loop.assert_called_once()

async def test_run_loop(self):
startup = mock.AsyncMock()
startup = mock.CoroutineMock()
startup_call = startup()
shutdown = mock.AsyncMock()
shutdown = mock.CoroutineMock()
shutdown_call = shutdown()
with mock.patch.object(test_module, "asyncio", autospec=True) as mock_asyncio:
test_module.run_loop(startup_call, shutdown_call)
Expand All @@ -79,7 +79,7 @@ async def test_run_loop(self):
mock.MagicMock(),
mock.MagicMock(cancel=mock.MagicMock()),
]
mock_asyncio.gather = mock.AsyncMock()
mock_asyncio.gather = mock.CoroutineMock()

if sys.version_info.major == 3 and sys.version_info.minor > 6:
mock_asyncio.all_tasks.return_value = tasks
Expand All @@ -92,9 +92,9 @@ async def test_run_loop(self):
shutdown.assert_awaited_once()

async def test_run_loop_init_x(self):
startup = mock.AsyncMock(side_effect=KeyError("the front fell off"))
startup = mock.CoroutineMock(side_effect=KeyError("the front fell off"))
startup_call = startup()
shutdown = mock.AsyncMock()
shutdown = mock.CoroutineMock()
shutdown_call = shutdown()
with mock.patch.object(
test_module, "asyncio", autospec=True
Expand All @@ -115,7 +115,7 @@ async def test_run_loop_init_x(self):
done_calls[0][1]() # exec partial
done_coro = mock_asyncio.ensure_future.call_args[0][0]
task = mock.MagicMock()
mock_asyncio.gather = mock.AsyncMock()
mock_asyncio.gather = mock.CoroutineMock()

if sys.version_info.major == 3 and sys.version_info.minor > 6:
mock_asyncio.all_tasks.return_value = [task]
Expand Down
Loading

0 comments on commit cfc2b8c

Please sign in to comment.