From 129ef956b8cf6fe71223630b5cb0d2f980506a05 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Mon, 25 Apr 2022 13:05:11 +0100 Subject: [PATCH 1/2] Performance improvement for first - avoids unnecessary pagination. --- aredis_om/model/model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aredis_om/model/model.py b/aredis_om/model/model.py index 4b71eec..92bb6f9 100644 --- a/aredis_om/model/model.py +++ b/aredis_om/model/model.py @@ -749,7 +749,7 @@ class FindQuery: async def first(self): query = self.copy(offset=0, limit=1, sort_fields=self.sort_fields) - results = await query.execute() + results = await query.execute(exhaust_results=False) if not results: raise NotFoundError() return results[0] From e5c6c0494a69437b58a3f379dd98195e0637eeb9 Mon Sep 17 00:00:00 2001 From: Simon Prickett Date: Mon, 25 Apr 2022 13:26:27 +0100 Subject: [PATCH 2/2] Adds test for first() on hashes. --- tests/test_hash_model.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/test_hash_model.py b/tests/test_hash_model.py index e8f80e0..a35409d 100644 --- a/tests/test_hash_model.py +++ b/tests/test_hash_model.py @@ -46,7 +46,7 @@ async def m(key_prefix, redis): last_name: str = Field(index=True) email: str = Field(index=True) join_date: datetime.date - age: int = Field(index=True) + age: int = Field(index=True, sortable=True) bio: str = Field(index=True, full_text_search=True) class Meta: @@ -357,6 +357,43 @@ def test_validation_passes(m): ) assert member.first_name == "Andrew" +@pytest.mark.asyncio +async def test_retrieve_first(m): + member = m.Member( + first_name="Simon", + last_name="Prickett", + email="s@example.com", + join_date=today, + age=99, + bio="This is the bio field for this user.", + ) + + await member.save() + + member2 = m.Member( + first_name="Another", + last_name="Member", + email="m@example.com", + join_date=today, + age=98, + bio="This is the bio field for this user.", + ) + + await member2.save() + + member3 = m.Member( + first_name="Third", + last_name="Member", + email="t@example.com", + join_date=today, + age=97, + bio="This is the bio field for this user.", + ) + + await member3.save() + + first_one = await m.Member.find().sort_by("age").first() + assert first_one == member3 @pytest.mark.asyncio async def test_saves_model_and_creates_pk(m):