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

➖ remove asynctest dependency and replace with unittest #449

Merged
merged 8 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 12 additions & 12 deletions basicmessage_storage/basicmessage_storage/v1_0/tests/test_init.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asynctest
from unittest import IsolatedAsyncioTestCase
from unittest.mock import MagicMock, Mock, patch

from aries_cloudagent.core.event_bus import Event
from aries_cloudagent.core.in_memory import InMemoryProfile
from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock
from pydantic import BaseModel

import basicmessage_storage.v1_0 as test_module
Expand All @@ -15,14 +15,14 @@ class MockConfig(BaseModel):
wallet_enabled: bool


class TestInit(AsyncTestCase):
async def setUp(self) -> None:
class TestInit(IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
self.session_inject = {}
self.context = async_mock.MagicMock()
self.context = MagicMock()
self.profile = InMemoryProfile.test_profile()

async def test_setup_injects_and_finishes_by_subscribing_to_event_bus(self):
self.context.inject = async_mock.Mock()
self.context.inject = Mock()
await setup(self.context)

last_call_as_string = self.context.mock_calls[-1].__str__()
Expand All @@ -34,25 +34,25 @@ async def test_setup_injects_and_finishes_by_subscribing_to_event_bus(self):
assert self.context.inject.call_count == 2

async def test_setup_throws_error_when_injecting_protocol_registry_fails(self):
self.context.inject = async_mock.Mock(side_effect=[None, None])
self.context.inject = Mock(side_effect=[None, None])
with self.assertRaises(AssertionError):
await setup(self.context)

async def test_setup_throws_error_when_injecting_event_bus_fails(self):
self.context.inject = async_mock.Mock(side_effect=["test", None])
self.context.inject = Mock(side_effect=["test", None])
with self.assertRaises(ValueError):
await setup(self.context)

@asynctest.patch.object(BasicMessageRecord, "save")
@patch.object(BasicMessageRecord, "save")
async def test_basic_message_event_handler_saves_record(self, mock_save):
event = Event(topic="test", payload={})
with asynctest.patch.object(test_module, "get_config") as mock_config:
with patch.object(test_module, "get_config") as mock_config:
mock_config.return_value = MockConfig(wallet_enabled=True)
await basic_message_event_handler(self.profile, event)

assert mock_save.called

@asynctest.patch.object(BasicMessageRecord, "save")
@patch.object(BasicMessageRecord, "save")
async def test_basic_message_event_handler_does_not_save_if_not_enabled(
self, mock_save
):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import asynctest
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch

from aries_cloudagent.core.in_memory import InMemoryProfile
from asynctest import TestCase as AsyncTestCase

from ..models import BasicMessageRecord

_id = "mytestid"


class TestBasicMessageRecord(AsyncTestCase):
async def setUp(self) -> None:
class TestBasicMessageRecord(IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
self.session = InMemoryProfile.test_session()

async def test_init_creates_record_with_default_parameters(self):
Expand All @@ -32,7 +33,7 @@ async def test_record_value_property_returns_set_attributes(self):
for x in [_locale, _content, _sent_time, BasicMessageRecord.STATE_SENT]
)

@asynctest.patch.object(BasicMessageRecord, "retrieve_by_tag_filter")
@patch.object(BasicMessageRecord, "retrieve_by_tag_filter")
async def test_retrieve_by_message_id_calls_retrieve_by_tag_filter_with_correct_args(
self, mock_retrieve
):
Expand Down
59 changes: 29 additions & 30 deletions basicmessage_storage/basicmessage_storage/v1_0/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import json
from unittest import IsolatedAsyncioTestCase
from unittest.mock import AsyncMock, MagicMock, Mock, patch

import asynctest
from aries_cloudagent.admin.request_context import AdminRequestContext
from aries_cloudagent.protocols.basicmessage.v1_0 import routes as base_module
from asynctest import TestCase as AsyncTestCase
from asynctest import mock as async_mock

from basicmessage_storage.v1_0.models import BasicMessageRecord

Expand All @@ -13,66 +12,66 @@
from .test_init import MockConfig


class TestRoutes(AsyncTestCase):
async def setUp(self) -> None:
class TestRoutes(IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
self.session_inject = {}
self.context = AdminRequestContext.test_context(self.session_inject)
self.request_dict = {
"context": self.context,
"outbound_message_router": async_mock.CoroutineMock(),
"outbound_message_router": AsyncMock(),
}
self.request = async_mock.MagicMock(
self.request = MagicMock(
app={},
match_info={},
query={},
__getitem__=lambda _, k: self.request_dict[k],
)
self.test_conn_id = "connection-id"

@asynctest.patch.object(base_module, "ConnRecord", autospec=True)
@asynctest.patch.object(test_module, "BasicMessageRecord", autospec=True)
@patch.object(base_module, "ConnRecord", autospec=True)
@patch.object(test_module, "BasicMessageRecord", autospec=True)
async def test_plugin_connections_send_message_saves_record(
self, mock_basic_message_rec_class, _
):
self.request.json = async_mock.CoroutineMock()
self.request.json = AsyncMock()
self.request.json.return_value = {"content": "content"}
self.request.match_info = {"conn_id": self.test_conn_id}
mock_basic_message_rec = async_mock.MagicMock(save=async_mock.CoroutineMock())
mock_basic_message_rec = MagicMock(save=AsyncMock())
mock_basic_message_rec_class.deserialize.return_value = mock_basic_message_rec
with asynctest.patch.object(test_module, "get_config") as mock_config:
with patch.object(test_module, "get_config") as mock_config:
mock_config.return_value = MockConfig(wallet_enabled=True)

res = await plugin_connections_send_message(self.request)

mock_basic_message_rec.save.assert_called()
assert res is not None

@asynctest.patch.object(base_module, "ConnRecord", autospec=True)
@asynctest.patch.object(test_module, "BasicMessageRecord", autospec=True)
@patch.object(base_module, "ConnRecord", autospec=True)
@patch.object(test_module, "BasicMessageRecord", autospec=True)
async def test_plugin_connections_send_message_raises_exception_when_save_fails(
self, mock_basic_message_rec_class, _
):
self.request.json = async_mock.CoroutineMock()
self.request.json = AsyncMock()
self.request.json.return_value = {"content": "content"}
self.request.match_info = {"conn_id": self.test_conn_id}

# Mock an exception during save
mock_basic_message_rec = async_mock.MagicMock(
mock_basic_message_rec = MagicMock(
save=lambda: (_ for _ in ()).throw(Exception("test"))
)
mock_basic_message_rec_class.deserialize.return_value = mock_basic_message_rec
with asynctest.patch.object(test_module, "get_config") as mock_config:
with patch.object(test_module, "get_config") as mock_config:
mock_config.return_value = MockConfig(wallet_enabled=True)

with self.assertRaises(Exception):
await plugin_connections_send_message(self.request)

@asynctest.patch.object(base_module, "ConnRecord", autospec=True)
@asynctest.patch.object(test_module, "BasicMessageRecord", autospec=True)
@patch.object(base_module, "ConnRecord", autospec=True)
@patch.object(test_module, "BasicMessageRecord", autospec=True)
async def test_all_messages_list_succeeds_and_sorts(
self, mock_basic_message_rec_class, _
):
mock_basic_message_rec_class.query = async_mock.CoroutineMock()
mock_basic_message_rec_class.query = AsyncMock()
mock_basic_message_rec_class.query.return_value = [
BasicMessageRecord(record_id="2", created_at="2023-10-13T21:49:14Z"),
BasicMessageRecord(record_id="1", created_at="2023-10-13T20:49:14Z"),
Expand All @@ -86,31 +85,31 @@ async def test_all_messages_list_succeeds_and_sorts(
assert results[2]["created_at"] == "2023-10-13T20:49:14Z"

async def test_register(self):
mock_app = async_mock.MagicMock()
mock_app.add_routes = async_mock.MagicMock()
mock_app = MagicMock()
mock_app.add_routes = MagicMock()

await test_module.register(mock_app)
mock_app.add_routes.assert_called()

async def test_post_process_routes(self):
mock_app = async_mock.MagicMock(_state={"swagger_dict": {}})
mock_app = MagicMock(_state={"swagger_dict": {}})
test_module.post_process_routes(mock_app)
assert "tags" in mock_app._state["swagger_dict"]

@asynctest.patch.object(BasicMessageRecord, "save")
@asynctest.patch.object(base_module, "ConnRecord", autospec=True)
@asynctest.patch.object(test_module, "BasicMessageRecord", autospec=True)
@patch.object(BasicMessageRecord, "save")
@patch.object(base_module, "ConnRecord", autospec=True)
@patch.object(test_module, "BasicMessageRecord", autospec=True)
async def test_basic_message_send_does_not_save_if_disabled(
self, mock_basic_message_rec_class, _, mock_save
):
self.request.json = async_mock.CoroutineMock()
self.request.json = AsyncMock()
self.request.json.return_value = {"content": "content"}
self.request.match_info = {"conn_id": self.test_conn_id}

mock_basic_message_rec = async_mock.MagicMock(save=async_mock.CoroutineMock())
mock_basic_message_rec = MagicMock(save=AsyncMock())
mock_basic_message_rec_class.deserialize.return_value = mock_basic_message_rec

with asynctest.patch.object(test_module, "get_config") as mock_config:
mock_config.return_value = asynctest.Mock(wallet_enabled=True)
with patch.object(test_module, "get_config") as mock_config:
mock_config.return_value = Mock(wallet_enabled=True)
await plugin_connections_send_message(self.request)
assert not mock_basic_message_rec.save.assert_called()
13 changes: 1 addition & 12 deletions basicmessage_storage/integration/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion basicmessage_storage/integration/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ authors = []
python = "^3.9"
pytest = "^7.4.0"
pytest-asyncio = "^0.21.0"
asynctest = "^0.13.0"
requests = "^2.31.0"

[tool.poetry.dev-dependencies]
Expand Down
Loading
Loading