Skip to content

Performance improvement for first - avoids unnecessary pagination. #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ async def execute(self, exhaust_results=True):

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]
Expand Down
39 changes: 38 additions & 1 deletion tests/test_hash_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Member(BaseHashModel):
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:
Expand Down Expand Up @@ -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="[email protected]",
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="[email protected]",
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="[email protected]",
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):
Expand Down