From e5e887229a142ff39dc8922087b697a26dbb0eb9 Mon Sep 17 00:00:00 2001 From: Mahmoud Harmouch Date: Wed, 10 Aug 2022 14:04:27 +0300 Subject: [PATCH] Implement page function (#339) * implement page function Signed-off-by: wiseaidev * add unit tests Signed-off-by: wiseaidev * fix linter issues Signed-off-by: wiseaidev --- aredis_om/model/model.py | 3 +++ tests/test_hash_model.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 2a1083d..6794045 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -760,6 +760,9 @@ class FindQuery: return await query.execute() return await self.execute() + async def page(self, offset=0, limit=10): + return await self.copy(offset=offset, limit=limit).execute() + def sort_by(self, *fields: str): if not fields: return self diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index 77418ba..c601277 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -150,6 +150,23 @@ async def test_full_text_search_queries(members, m): assert actual == [member1, member3] +@py_test_mark_asyncio +async def test_pagination_queries(members, m): + member1, member2, member3 = members + + actual = await m.Member.find(m.Member.last_name == "Brookins").page() + + assert actual == [member1, member2] + + actual = await m.Member.find().page(1, 1) + + assert actual == [member2] + + actual = await m.Member.find().page(0, 1) + + assert actual == [member1] + + @py_test_mark_asyncio async def test_recursive_query_resolution(members, m): member1, member2, member3 = members