diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 377eded..f95d4ce 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -948,7 +948,9 @@ async def all(self, batch_size=DEFAULT_PAGE_SIZE): return await self.execute() async def page(self, offset=0, limit=10): - return await self.copy(offset=offset, limit=limit).execute() + return await self.copy(offset=offset, limit=limit).execute( + exhaust_results=False + ) def sort_by(self, *fields: str): if not fields: diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index c967358..209c8a5 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -389,8 +389,10 @@ async def test_sorting(members, m): async def test_case_sensitive(members, m): member1, member2, member3 = members - actual = await m.Member.find(m.Member.first_name == "Andrew").all() - assert actual == [member1, member3] + actual = await m.Member.find( + m.Member.first_name == "Andrew" and m.Member.pk == member1.pk + ).all() + assert actual == [member1] actual = await m.Member.find(m.Member.first_name == "andrew").all() assert actual == [] @@ -859,13 +861,13 @@ async def test_xfix_queries(members, m): member1, member2, member3 = members result = await m.Member.find(m.Member.first_name.startswith("And")).first() - assert result.first_name == "Andrew" + assert result.last_name == "Brookins" result = await m.Member.find(m.Member.last_name.endswith("ins")).first() - assert result.first_name == "Andrew" + assert result.last_name == "Brookins" result = await m.Member.find(m.Member.last_name.contains("ook")).first() - assert result.first_name == "Andrew" + assert result.last_name == "Brookins" result = await m.Member.find(m.Member.bio % "great*").first() assert result.first_name == "Andrew" diff --git a/tests/test_json_model.py b/tests/test_json_model.py index 8464920..267185a 100644 --- a/tests/test_json_model.py +++ b/tests/test_json_model.py @@ -1069,3 +1069,28 @@ class Example(JsonModel): res = await Example.find(Example.d == ex.d and Example.b == True).first() assert res.name == ex.name + + +@py_test_mark_asyncio +async def test_pagination(): + class Test(JsonModel): + id: str = Field(primary_key=True, index=True) + num: int = Field(sortable=True, index=True) + + @classmethod + async def get_page(cls, offset, limit): + return await cls.find().sort_by("num").page(limit=limit, offset=offset) + + await Migrator().run() + + pipe = Test.Meta.database.pipeline() + for i in range(0, 1000): + await Test(num=i, id=str(i)).save(pipeline=pipe) + + await pipe.execute() + res = await Test.get_page(100, 100) + assert len(res) == 100 + assert res[0].num == 100 + res = await Test.get_page(10, 30) + assert len(res) == 30 + assert res[0].num == 10