Skip to content

Commit

Permalink
Rebase the pagination with ordering feature on latest release (#2)
Browse files Browse the repository at this point in the history
* ✨ 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]>
  • Loading branch information
ff137 committed Nov 14, 2024
1 parent d425b2d commit 28b79dc
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 20 deletions.
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
2 changes: 1 addition & 1 deletion acapy_agent/multitenant/admin/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,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
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
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

0 comments on commit 28b79dc

Please sign in to comment.