Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: drop asynctest #2566

Merged
merged 33 commits into from
Oct 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
e81fc79
fix: drop asynctest
dbluhm Oct 25, 2023
fd41c62
fix: mt admin test routes mock issues
dbluhm Oct 25, 2023
8b77dfa
fix: conn v1 manager mock issues
dbluhm Oct 25, 2023
6d102c0
chore: normalize references to mock, replace async_mock
dbluhm Oct 27, 2023
818ee7f
chore: split unittest mock imports
dbluhm Oct 27, 2023
cfc2b8c
fix: provide CoroutineMock adapter
dbluhm Oct 27, 2023
2b8cf85
fix: xfail 3 tests that never worked
dbluhm Oct 27, 2023
ce2bb16
fix: never awaited error in PPv1 test routes
dbluhm Oct 27, 2023
d920e02
fix: never awaited warnings in wallet test routes
dbluhm Oct 27, 2023
0447250
fix: never awaited error in default verkey strat tests
dbluhm Oct 27, 2023
0a6746e
fix: test validation result async issue
dbluhm Oct 27, 2023
5005c84
fix: unawaited task in test basic message q
dbluhm Oct 27, 2023
0b581a5
fix: bad assertion in inbound test session
dbluhm Oct 27, 2023
b43f174
fix: mock logger.exception isn't a coroutine
dbluhm Oct 27, 2023
956a129
fix: introductiont tests bad assertion
dbluhm Oct 27, 2023
eee5e67
fix: bad assertions for called once with
dbluhm Oct 27, 2023
e77e8fe
fix: the "fixed" tests
dbluhm Oct 27, 2023
f257101
fix: test trie should be asyncio test case
dbluhm Oct 27, 2023
698808b
fix: inbound test message should be async test case
dbluhm Oct 27, 2023
9ca6b5e
fix: warnings on vc holder tests
dbluhm Oct 27, 2023
1998cab
fix: inbound ws transport test warning
dbluhm Oct 27, 2023
b69ff45
fix: in mem storage test warning
dbluhm Oct 27, 2023
13a65cc
fix: bad called once assertions on mocks
dbluhm Oct 27, 2023
1b5f923
fix: storage tests missing fixtures
dbluhm Oct 27, 2023
f15bdd9
fix: bad log assertions
dbluhm Oct 27, 2023
f754f95
fix: inbound message router not coroutine mock
dbluhm Oct 27, 2023
eff84bc
fix: bad mock in discovery tests
dbluhm Oct 27, 2023
3e61029
fix: bad mock in multiledger tests
dbluhm Oct 27, 2023
5b64fc1
fix: bad mocks in test dispatcher
dbluhm Oct 27, 2023
89e87c1
fix: unawaited coroutines in test conductor
dbluhm Oct 27, 2023
6ecd912
fix: unawaited coroutines in test start
dbluhm Oct 27, 2023
baa72c0
fix: unawaited coroutines in test_upgrade
dbluhm Oct 27, 2023
0da450c
chore: drop asynctest dep
dbluhm Oct 27, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion UnitTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class TestDidExchangeManager(AsyncTestCase, TestConfig):
self.responder = MockResponder()

self.oob_mock = async_mock.MagicMock(
clean_finished_oob_record=async_mock.CoroutineMock(return_value=None)
clean_finished_oob_record=async_mock.AsyncMock(return_value=None)
)

self.route_manager = async_mock.MagicMock(RouteManager)
Expand Down
108 changes: 51 additions & 57 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 as async_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 @@ -36,66 +36,62 @@ async def asyncTearDown(self):
self.client_session = None

async def test_debug_middleware(self):
with async_mock.patch.object(
test_module, "LOGGER", async_mock.MagicMock()
) as mock_logger:
mock_logger.isEnabledFor = async_mock.MagicMock(return_value=True)
mock_logger.debug = async_mock.MagicMock()
with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger:
mock_logger.isEnabledFor = mock.MagicMock(return_value=True)
mock_logger.debug = mock.MagicMock()

request = async_mock.MagicMock(
request = mock.MagicMock(
method="GET",
path_qs="/hello/world?a=1&b=2",
match_info={"match": "info"},
text=async_mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
handler = async_mock.AsyncMock()
handler = mock.CoroutineMock()

await test_module.debug_middleware(request, handler)
mock_logger.isEnabledFor.assert_called_once()
assert mock_logger.debug.call_count == 3

async def test_ready_middleware(self):
with async_mock.patch.object(
test_module, "LOGGER", async_mock.MagicMock()
) as mock_logger:
mock_logger.isEnabledFor = async_mock.MagicMock(return_value=True)
mock_logger.debug = async_mock.MagicMock()
mock_logger.info = async_mock.MagicMock()
mock_logger.error = async_mock.MagicMock()

request = async_mock.MagicMock(
rel_url="/", app=async_mock.MagicMock(_state={"ready": False})
with mock.patch.object(test_module, "LOGGER", mock.MagicMock()) as mock_logger:
mock_logger.isEnabledFor = mock.MagicMock(return_value=True)
mock_logger.debug = mock.MagicMock()
mock_logger.info = mock.MagicMock()
mock_logger.error = mock.MagicMock()

request = mock.MagicMock(
rel_url="/", app=mock.MagicMock(_state={"ready": False})
)
handler = async_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 = async_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 = async_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 = async_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 = async_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 All @@ -110,10 +106,8 @@ def get_admin_server(
# middleware is task queue xor collector: cover both over test suite
task_queue = (settings or {}).pop("task_queue", None)

plugin_registry = async_mock.MagicMock(
test_module.PluginRegistry, autospec=True
)
plugin_registry.post_process_routes = async_mock.MagicMock()
plugin_registry = mock.MagicMock(test_module.PluginRegistry, autospec=True)
plugin_registry.post_process_routes = mock.MagicMock()
context.injector.bind_instance(test_module.PluginRegistry, plugin_registry)

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

Expand Down Expand Up @@ -165,16 +159,16 @@ async def test_start_stop(self):
server = self.get_admin_server(settings)
await server.start()
assert server.app._client_max_size == 4 * 1024 * 1024
with async_mock.patch.object(
server, "websocket_queues", async_mock.MagicMock()
with mock.patch.object(
server, "websocket_queues", mock.MagicMock()
) as mock_wsq:
mock_wsq.values = async_mock.MagicMock(
return_value=[async_mock.MagicMock(stop=async_mock.MagicMock())]
mock_wsq.values = mock.MagicMock(
return_value=[mock.MagicMock(stop=mock.MagicMock())]
)
await server.stop()

with async_mock.patch.object(
web.TCPSite, "start", async_mock.AsyncMock()
with mock.patch.object(
web.TCPSite, "start", mock.CoroutineMock()
) as mock_start:
mock_start.side_effect = OSError("Failure to launch")
with self.assertRaises(AdminSetupError):
Expand All @@ -199,7 +193,7 @@ async def test_import_routes_multitenant_middleware(self):
context.injector.bind_instance(GoalCodeRegistry, GoalCodeRegistry())
context.injector.bind_instance(
test_module.BaseMultitenantManager,
async_mock.MagicMock(spec=test_module.BaseMultitenantManager),
mock.MagicMock(spec=test_module.BaseMultitenantManager),
)
await DefaultContextBuilder().load_plugins(context)
server = self.get_admin_server(
Expand All @@ -220,66 +214,66 @@ async def test_import_routes_multitenant_middleware(self):
m for m in app.middlewares if ".check_multitenant_authorization" in str(m)
]

mock_request = async_mock.MagicMock(
mock_request = mock.MagicMock(
method="GET",
headers={"Authorization": "Bearer ..."},
path="/multitenancy/etc",
text=async_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)

mock_request = async_mock.MagicMock(
mock_request = mock.MagicMock(
method="GET",
headers={},
path="/protected/non-multitenancy/non-server",
text=async_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)

mock_request = async_mock.MagicMock(
mock_request = mock.MagicMock(
method="GET",
headers={"Authorization": "Bearer ..."},
path="/protected/non-multitenancy/non-server",
text=async_mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
mock_handler = async_mock.AsyncMock()
mock_handler = mock.CoroutineMock()
await mt_authz_middle(mock_request, mock_handler)
assert mock_handler.called_once_with(mock_request)
mock_handler.assert_called_once_with(mock_request)

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

# multitenant setup context exception paths
[setup_ctx_middle] = [m for m in app.middlewares if ".setup_context" in str(m)]

mock_request = async_mock.MagicMock(
mock_request = mock.MagicMock(
method="GET",
headers={"Authorization": "Non-bearer ..."},
path="/protected/non-multitenancy/non-server",
text=async_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)

mock_request = async_mock.MagicMock(
mock_request = mock.MagicMock(
method="GET",
headers={"Authorization": "Bearer ..."},
path="/protected/non-multitenancy/non-server",
text=async_mock.AsyncMock(return_value="abc123"),
text=mock.CoroutineMock(return_value="abc123"),
)
with async_mock.patch.object(
with mock.patch.object(
server.multitenant_manager,
"get_profile_for_token",
async_mock.AsyncMock(),
mock.CoroutineMock(),
) as mock_get_profile:
mock_get_profile.side_effect = [
test_module.MultitenantManagerError("corrupt token"),
Expand Down Expand Up @@ -493,8 +487,8 @@ async def server():
)
async def test_on_record_event(server, event_topic, webhook_topic):
profile = InMemoryProfile.test_profile()
with async_mock.patch.object(
server, "send_webhook", async_mock.AsyncMock()
with mock.patch.object(
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
4 changes: 2 additions & 2 deletions aries_cloudagent/admin/tests/test_request_context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from asynctest import TestCase as AsyncTestCase
from unittest import IsolatedAsyncioTestCase

from ...core.in_memory import InMemoryProfile
from ...core.profile import ProfileSession
Expand All @@ -7,7 +7,7 @@
from .. import request_context as test_module


class TestAdminRequestContext(AsyncTestCase):
class TestAdminRequestContext(IsolatedAsyncioTestCase):
def setUp(self):
self.ctx = test_module.AdminRequestContext(InMemoryProfile.test_profile())
assert self.ctx.__class__.__name__ in str(self.ctx)
Expand Down
18 changes: 9 additions & 9 deletions aries_cloudagent/askar/didcomm/tests/test_v2.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

from asynctest import mock as async_mock
from unittest import mock
import pytest

from aries_askar import AskarError, Key, KeyAlg, Session
Expand Down Expand Up @@ -70,19 +70,19 @@ async def test_es_encrypt_x(self, session: Session):
):
_ = test_module.ecdh_es_encrypt({}, MESSAGE)

with async_mock.patch(
with mock.patch(
"aries_askar.Key.generate",
async_mock.MagicMock(side_effect=AskarError(99, "")),
mock.MagicMock(side_effect=AskarError(99, "")),
):
with pytest.raises(
test_module.DidcommEnvelopeError,
match="Error creating content encryption key",
):
_ = test_module.ecdh_es_encrypt({BOB_KID: bob_pk}, MESSAGE)

with async_mock.patch(
with mock.patch(
"aries_askar.Key.aead_encrypt",
async_mock.MagicMock(side_effect=AskarError(99, "")),
mock.MagicMock(side_effect=AskarError(99, "")),
):
with pytest.raises(
test_module.DidcommEnvelopeError,
Expand Down Expand Up @@ -188,9 +188,9 @@ async def test_1pu_encrypt_x(self, session: Session):
{BOB_KID: bob_pk, "alt": alt_pk}, ALICE_KID, alice_sk, MESSAGE
)

with async_mock.patch(
with mock.patch(
"aries_askar.Key.generate",
async_mock.MagicMock(side_effect=AskarError(99, "")),
mock.MagicMock(side_effect=AskarError(99, "")),
):
with pytest.raises(
test_module.DidcommEnvelopeError,
Expand All @@ -200,9 +200,9 @@ async def test_1pu_encrypt_x(self, session: Session):
{BOB_KID: bob_pk}, ALICE_KID, alice_sk, MESSAGE
)

with async_mock.patch(
with mock.patch(
"aries_askar.Key.aead_encrypt",
async_mock.MagicMock(side_effect=AskarError(99, "")),
mock.MagicMock(side_effect=AskarError(99, "")),
):
with pytest.raises(
test_module.DidcommEnvelopeError,
Expand Down
2 changes: 1 addition & 1 deletion aries_cloudagent/askar/tests/test_profile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import pytest

from asynctest import mock
from unittest import mock

from ...askar.profile import AskarProfile
from ...config.injection_context import InjectionContext
Expand Down
4 changes: 2 additions & 2 deletions aries_cloudagent/askar/tests/test_store.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from asynctest import TestCase as AsyncTestCase
from unittest import IsolatedAsyncioTestCase

from ...core.error import ProfileError

from ..store import AskarStoreConfig


class TestStoreConfig(AsyncTestCase):
class TestStoreConfig(IsolatedAsyncioTestCase):
key_derivation_method = "Raw"
key = "key"
storage_type = "postgres"
Expand Down
17 changes: 9 additions & 8 deletions aries_cloudagent/commands/tests/test_help.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from asynctest import mock as async_mock, TestCase as AsyncTestCase
from unittest import mock
from unittest import IsolatedAsyncioTestCase

from .. import help as command


class TestHelp(AsyncTestCase):
class TestHelp(IsolatedAsyncioTestCase):
def test_exec_help(self):
with async_mock.patch.object(
with mock.patch.object(
command.ArgumentParser, "print_help"
) as mock_print_help, async_mock.patch(
"builtins.print", async_mock.MagicMock()
) as mock_print_help, mock.patch(
"builtins.print", mock.MagicMock()
) as mock_print:
command.execute([])
mock_print_help.assert_called_once()
Expand All @@ -17,10 +18,10 @@ def test_exec_help(self):
mock_print.assert_called_once_with(command.__version__)

def test_main(self):
with async_mock.patch.object(
with mock.patch.object(
command, "__name__", "__main__"
) as mock_name, async_mock.patch.object(
command, "execute", async_mock.MagicMock()
) as mock_name, mock.patch.object(
command, "execute", mock.MagicMock()
) as mock_execute:
command.main()
mock_execute.assert_called_once
Loading