Skip to content

Commit

Permalink
⬆️ Upgrade aiohttp and 🎨 fix deprecation warnings (#5)
Browse files Browse the repository at this point in the history
* Rebase the pagination with ordering feature on latest release (#2)

* ✨ add `order_by` and `descending` options to query / scan and fetch_all methods

Signed-off-by: ff137 <[email protected]>

* ✨ add `order_by` and `descending` options to PaginatedQuerySchema

Signed-off-by: ff137 <[email protected]>

* ✨ modify `get_limit_offset` to `get_paginated_query_params`

Signed-off-by: ff137 <[email protected]>

* ✨ add ordering to InMemoryStorage scan and fetch_all methods

Signed-off-by: ff137 <[email protected]>

* 🚧 test in-progress aries-askar PR: openwallet-foundation/askar#291

Signed-off-by: ff137 <[email protected]>

* ⬆️ Update lock file

Signed-off-by: ff137 <[email protected]>

* 🎨 fix ruff warning

Signed-off-by: ff137 <[email protected]>

* ✅ fix assertions

Signed-off-by: ff137 <[email protected]>

* 🚧 test aries-askar with TestPyPI package

Signed-off-by: ff137 <[email protected]>

* 🚧 test latest askar testpypi package

Signed-off-by: ff137 <[email protected]>

* 🎨 Update order_by description and default value. Include in schema

Signed-off-by: ff137 <[email protected]>

* 🐛 fix deserialisation of descending: bool parameter

Signed-off-by: ff137 <[email protected]>

* ⏪ revert to testing 0.3.3b0 askar test package

Signed-off-by: ff137 <[email protected]>

* 🔥 remove unnecessary record sorts (now that askar sorts by id == sorting by created_at)

Signed-off-by: ff137 <[email protected]>

* ✅ fix test assertions -- wallet_list and connections_list no longer does additional sorting

Signed-off-by: ff137 <[email protected]>

* ⬆️ Update lock file

Signed-off-by: ff137 <[email protected]>

---------

Signed-off-by: ff137 <[email protected]>

* arrow_up: Upgrade python-dateutil

Resolves calling deprecated `datetime.utcfromtimestamp` method

Signed-off-by: ff137 <[email protected]>

* art: Rename class to resolve PytestCollectionWarning

Signed-off-by: ff137 <[email protected]>

* art: Replace deprecated `open_binary` method with `files`

Signed-off-by: ff137 <[email protected]>

* art: Replace deprecated `scope` keyword with `loop_scope`

Signed-off-by: ff137 <[email protected]>

* art: Filter Aries RFC 0160 DeprecationWarning in tests

Signed-off-by: ff137 <[email protected]>

* art: Filter Pydantic UserWarning in test

Signed-off-by: ff137 <[email protected]>

* arrow_up: Upgrade aiohttp to latest release and update lock file

Signed-off-by: ff137 <[email protected]>

* art: Remove unnecessary asyncio pytest markers

Resolves unclosed event loop warnings.

Signed-off-by: ff137 <[email protected]>

---------

Signed-off-by: ff137 <[email protected]>
  • Loading branch information
ff137 authored Nov 14, 2024
1 parent d425b2d commit 82ed69d
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 181 deletions.
1 change: 0 additions & 1 deletion acapy_agent/anoncreds/tests/test_revocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,6 @@ async def test_create_credential_w3c(self, mock_supports_revocation):
assert isinstance(result, tuple)
assert mock_supports_revocation.call_count == 1

@pytest.mark.asyncio
@mock.patch.object(AskarAnoncredsProfileSession, "handle")
async def test_create_credential_w3c_keyerror(self, mock_handle):
mock_handle.fetch = mock.CoroutineMock(side_effect=[MockEntry(), MockEntry()])
Expand Down
2 changes: 1 addition & 1 deletion acapy_agent/config/logging/configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def load_resource(path: str, encoding: Optional[str] = None):
else:
# Package resource
package, resource = components
bstream = resources.open_binary(package, resource)
bstream = resources.files(package).joinpath(resource).open("rb")
if encoding:
return io.TextIOWrapper(bstream, encoding=encoding)
return bstream
Expand Down
59 changes: 45 additions & 14 deletions acapy_agent/config/tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import contextlib
from io import StringIO
from io import BufferedReader, StringIO, TextIOWrapper
from tempfile import NamedTemporaryFile
from unittest import IsolatedAsyncioTestCase, mock

Expand Down Expand Up @@ -110,24 +110,55 @@ def test_banner_did(self):
def test_load_resource(self):
# Testing local file access
with mock.patch("builtins.open", mock.MagicMock()) as mock_open:
test_module.load_resource("abc", encoding="utf-8")
# First call succeeds
file_handle = mock.MagicMock(spec=TextIOWrapper)
mock_open.return_value = file_handle
result = test_module.load_resource("abc", encoding="utf-8")
mock_open.assert_called_once_with("abc", encoding="utf-8")
assert result == file_handle # Verify the returned file handle

mock_open.reset_mock()
# Simulate IOError on second call
mock_open.side_effect = IOError("insufficient privilege")
# load_resource should absorb IOError
test_module.load_resource("abc", encoding="utf-8")
result = test_module.load_resource("abc", encoding="utf-8")
mock_open.assert_called_once_with("abc", encoding="utf-8")
assert result is None

# Testing package resource access with encoding (text mode)
with mock.patch(
"importlib.resources.open_binary", mock.MagicMock()
) as mock_open_binary, mock.patch(
with mock.patch("importlib.resources.files") as mock_files, mock.patch(
"io.TextIOWrapper", mock.MagicMock()
) as mock_text_io_wrapper:
test_module.load_resource("abc:def", encoding="utf-8")
mock_open_binary.assert_called_once_with("abc", "def")
mock_text_io_wrapper.assert_called_once()
# Setup the mocks
mock_resource_path = mock.MagicMock()
mock_files.return_value.joinpath.return_value = mock_resource_path
mock_resource_handle = mock.MagicMock(spec=BufferedReader)
mock_resource_path.open.return_value = mock_resource_handle
mock_text_io_wrapper.return_value = mock.MagicMock(spec=TextIOWrapper)

result = test_module.load_resource("abc:def", encoding="utf-8")

# Assertions
mock_files.assert_called_once_with("abc")
mock_files.return_value.joinpath.assert_called_once_with("def")
mock_resource_path.open.assert_called_once_with("rb")
mock_text_io_wrapper.assert_called_once_with(
mock_resource_handle, encoding="utf-8"
)
assert result is mock_text_io_wrapper.return_value

# Testing package resource access without encoding (binary mode)
with mock.patch(
"importlib.resources.open_binary", mock.MagicMock()
) as mock_open_binary:
test_module.load_resource("abc:def", encoding=None)
mock_open_binary.assert_called_once_with("abc", "def")
with mock.patch("importlib.resources.files") as mock_files:
# Setup the mocks
mock_resource_path = mock.MagicMock()
mock_files.return_value.joinpath.return_value = mock_resource_path
mock_resource_handle = mock.MagicMock(spec=BufferedReader)
mock_resource_path.open.return_value = mock_resource_handle

result = test_module.load_resource("abc:def", encoding=None)

# Assertions
mock_files.assert_called_once_with("abc")
mock_files.return_value.joinpath.assert_called_once_with("def")
mock_resource_path.open.assert_called_once_with("rb")
assert result == mock_resource_handle # Verify the returned binary stream
6 changes: 3 additions & 3 deletions acapy_agent/connections/tests/test_base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import IsolatedAsyncioTestCase
from unittest.mock import call

import pytest
from pydid import DID, DIDDocument, DIDDocumentBuilder
from pydid.doc.builder import ServiceBuilder
from pydid.verification_method import (
Expand All @@ -27,9 +28,7 @@
from ...protocols.connections.v1_0.messages.connection_invitation import (
ConnectionInvitation,
)
from ...protocols.coordinate_mediation.v1_0.models.mediation_record import (
MediationRecord,
)
from ...protocols.coordinate_mediation.v1_0.models.mediation_record import MediationRecord
from ...protocols.coordinate_mediation.v1_0.route_manager import (
CoordinateMediationV1RouteManager,
RouteManager,
Expand Down Expand Up @@ -1065,6 +1064,7 @@ async def test_resolve_connection_targets_x_bad_key_material(self):
await self.manager.resolve_connection_targets(did)
assert "not supported" in str(cm.exception)

@pytest.mark.filterwarnings("ignore::UserWarning")
async def test_resolve_connection_targets_x_unsupported_key(self):
did = "did:sov:" + self.test_did
doc_builder = DIDDocumentBuilder(did)
Expand Down
1 change: 1 addition & 0 deletions acapy_agent/core/tests/test_conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,7 @@ async def test_dispatch_complete_fatal_x(self):
conductor.dispatch_complete(message, mock_task)
mock_notify.assert_called_once_with()

@pytest.mark.filterwarnings("ignore:Aries RFC 0160.*:DeprecationWarning")
async def test_print_invite_connection(self):
builder: ContextBuilder = StubContextBuilder(self.test_settings)
builder.update_settings(
Expand Down
6 changes: 3 additions & 3 deletions acapy_agent/didcomm_v2/tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from ..adapters import ResolverAdapter, SecretsAdapter, SecretsAdapterError


class TestDIDResolver(BaseDIDResolver):
class MockDIDResolver(BaseDIDResolver):
async def setup(self, context: InjectionContext):
return await super().setup(context)

Expand All @@ -26,7 +26,7 @@ async def resolve(
self,
profile,
did,
sercive_accept=None,
service_accept=None,
):
return {"did": did, "test": "didDoc"}

Expand All @@ -46,7 +46,7 @@ async def asyncSetUp(self):
self.test_did = "did:test:0"
self.invalid_did = "this shouldn't work"
resolver = DIDResolver()
resolver.register_resolver(TestDIDResolver())
resolver.register_resolver(MockDIDResolver())
self.res_adapter = ResolverAdapter(profile=self.profile, resolver=resolver)

async def test_resolver_adapter_resolve_did(self):
Expand Down
9 changes: 8 additions & 1 deletion acapy_agent/messaging/models/paginated_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class PaginatedQuerySchema(OpenAPISchema):
descending = fields.Bool(
required=False,
load_default=False,
truthy={"true", "1", "yes"},
falsy={"false", "0", "no"},
metadata={"description": "Order results in descending order if true"},
error_messages={"invalid": "Not a valid boolean."},
)


Expand All @@ -67,5 +70,9 @@ def get_paginated_query_params(request: BaseRequest) -> Tuple[int, int, str, boo
limit = int(request.query.get("limit", DEFAULT_PAGE_SIZE))
offset = int(request.query.get("offset", 0))
order_by = request.query.get("order_by", "id")
descending = bool(request.query.get("descending", False))

# Convert the 'descending' parameter to a boolean
descending_str = request.query.get("descending", "False").lower()
descending = descending_str in {"true", "1", "yes"}

return limit, offset, order_by, descending
2 changes: 1 addition & 1 deletion acapy_agent/messaging/models/tests/test_base_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ async def test_query(self):
result = await BaseRecordImpl.query(session, tag_filter)
mock_storage.find_all_records.assert_awaited_once_with(
type_filter=BaseRecordImpl.RECORD_TYPE,
tag_query=tag_filter,
order_by=None,
descending=False,
tag_query=tag_filter,
)
assert result and isinstance(result[0], BaseRecordImpl)
assert result[0]._id == record_id
Expand Down
1 change: 0 additions & 1 deletion acapy_agent/multitenant/admin/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ async def wallets_list(request: web.BaseRequest):
descending=descending,
)
results = [format_wallet_record(record) for record in records]
results.sort(key=lambda w: w["created_at"])
except (StorageError, BaseModelError) as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

Expand Down
5 changes: 1 addition & 4 deletions acapy_agent/multitenant/admin/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from unittest import IsolatedAsyncioTestCase

import pytest
from marshmallow.exceptions import ValidationError

from ....admin.request_context import AdminRequestContext
Expand Down Expand Up @@ -87,7 +86,7 @@ async def test_wallets_list(self):
),
]
mock_wallet_record.query = mock.CoroutineMock()
mock_wallet_record.query.return_value = [wallets[2], wallets[0], wallets[1]]
mock_wallet_record.query.return_value = wallets

await test_module.wallets_list(self.request)
mock_response.assert_called_once_with(
Expand Down Expand Up @@ -143,7 +142,6 @@ async def test_wallets_list_query(self):
}
)

@pytest.mark.asyncio(scope="function")
async def test_wallet_create_tenant_settings(self):
body = {
"wallet_name": "test",
Expand Down Expand Up @@ -796,7 +794,6 @@ async def test_wallet_create_token_x(self):
)
await test_module.wallet_create_token(self.request)

@pytest.mark.asyncio(scope="function")
async def test_wallet_remove_managed(self):
self.request.has_body = False
self.request.match_info = {"wallet_id": "dummy"}
Expand Down
15 changes: 0 additions & 15 deletions acapy_agent/protocols/connections/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,20 +414,6 @@ class EndpointsResultSchema(OpenAPISchema):
)


def connection_sort_key(conn):
"""Get the sorting key for a particular connection."""

conn_rec_state = ConnRecord.State.get(conn["state"])
if conn_rec_state is ConnRecord.State.ABANDONED:
pfx = "2"
elif conn_rec_state is ConnRecord.State.INVITATION:
pfx = "1"
else:
pfx = "0"

return pfx + conn["created_at"]


@docs(
tags=["connection"],
summary="Query agent-to-agent connections",
Expand Down Expand Up @@ -488,7 +474,6 @@ async def connections_list(request: web.BaseRequest):
alt=True,
)
results = [record.serialize() for record in records]
results.sort(key=connection_sort_key)
except (StorageError, BaseModelError) as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

Expand Down
3 changes: 3 additions & 0 deletions acapy_agent/protocols/connections/v1_0/tests/test_manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest import IsolatedAsyncioTestCase

import pytest

from .....cache.base import BaseCache
from .....cache.in_memory import InMemoryCache
from .....connections.models.conn_record import ConnRecord
Expand Down Expand Up @@ -29,6 +31,7 @@
from ..models.connection_detail import ConnectionDetail


@pytest.mark.filterwarnings("ignore:Aries RFC 0160.*:DeprecationWarning")
class TestConnectionManager(IsolatedAsyncioTestCase):
def make_did_doc(self, did, verkey):
doc = DIDDoc(did=did)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def test_connections_list(self):
)
),
]
mock_conn_rec.query.return_value = [conns[2], conns[0], conns[1]] # jumbled
mock_conn_rec.query.return_value = conns

with mock.patch.object(test_module.web, "json_response") as mock_response:
await test_module.connections_list(self.request)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from unittest import IsolatedAsyncioTestCase

import pytest

from ......connections.models import connection_target
from ......connections.models.diddoc import DIDDoc, PublicKey, PublicKeyType, Service
from ......messaging.decorators.attach_decorator import AttachDecorator
Expand Down Expand Up @@ -75,7 +73,6 @@ async def asyncSetUp(self):
did_doc_attach=self.did_doc_attach,
)

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
async def test_called(self, mock_didx_mgr):
mock_didx_mgr.return_value.accept_response = mock.CoroutineMock()
Expand All @@ -89,7 +86,6 @@ async def test_called(self, mock_didx_mgr):
)
assert not responder.messages

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
async def test_called_auto_ping(self, mock_didx_mgr):
self.ctx.update_settings({"auto_ping_connection": True})
Expand All @@ -107,7 +103,6 @@ async def test_called_auto_ping(self, mock_didx_mgr):
result, _ = messages[0]
assert isinstance(result, Ping)

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
@mock.patch.object(connection_target, "ConnectionTarget")
async def test_problem_report(self, mock_conn_target, mock_didx_mgr):
Expand Down Expand Up @@ -144,7 +139,6 @@ async def test_problem_report(self, mock_conn_target, mock_didx_mgr):
)
assert target == {"target_list": [mock_conn_target]}

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
@mock.patch.object(connection_target, "ConnectionTarget")
async def test_problem_report_did_doc(
Expand Down Expand Up @@ -191,7 +185,6 @@ async def test_problem_report_did_doc(
)
assert target == {"target_list": [mock_conn_target]}

@pytest.mark.asyncio(scope="function")
@mock.patch.object(test_module, "DIDXManager")
@mock.patch.object(connection_target, "ConnectionTarget")
async def test_problem_report_did_doc_no_conn_target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,6 @@ async def test_issue_credential_non_revocable(self):
# assert data is encoded as base64
assert attachment.data.base64

@pytest.mark.asyncio
async def test_match_sent_cred_def_id_error(self):
tag_query = {"tag": "test_tag"}

Expand All @@ -737,7 +736,6 @@ async def test_match_sent_cred_def_id_error(self):
context.exception
)

@pytest.mark.asyncio
async def test_store_credential(self):
attr_values = {
"legalName": "value",
Expand Down
Loading

0 comments on commit 82ed69d

Please sign in to comment.