Skip to content
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

implement count function #336

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions aredis_om/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,9 @@ async def all(self, batch_size=DEFAULT_PAGE_SIZE):
async def page(self, offset=0, limit=10):
return await self.copy(offset=offset, limit=limit).execute()

async def count(self, batch_size=10):
return len(await self.all(batch_size))

def sort_by(self, *fields: str):
if not fields:
return self
Expand Down
11 changes: 11 additions & 0 deletions tests/test_hash_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ async def members(m):
yield member1, member2, member3


@py_test_mark_asyncio
async def test_count_query(members, m):

count = await m.Member.find((m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")).count()
assert count == 1
count = await m.Member.find(m.Member.first_name == "Andrew").count()
assert count == 2
count = await m.Member.find().count()
assert count == 3


@py_test_mark_asyncio
async def test_exact_match_queries(members, m):
member1, member2, member3 = members
Expand Down