Add support for count (#397)

* add count

* update poetry
This commit is contained in:
dvora-h 2022-10-18 15:17:00 +03:00 committed by GitHub
parent 1221efd2c3
commit f77c21a059
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 97 additions and 56 deletions

View file

@ -706,7 +706,6 @@ def test_schema(m):
@py_test_mark_asyncio
async def test_primary_key_model_error(m):
class Customer(m.BaseHashModel):
id: int = Field(primary_key=True, index=True)
first_name: str = Field(primary_key=True, index=True)
@ -715,18 +714,19 @@ async def test_primary_key_model_error(m):
await Migrator().run()
with pytest.raises(RedisModelError, match="You must define only one primary key for a model"):
with pytest.raises(
RedisModelError, match="You must define only one primary key for a model"
):
_ = Customer(
id=0,
first_name="Mahmoud",
last_name="Harmouch",
bio="Python developer, wanna work at Redis, Inc."
bio="Python developer, wanna work at Redis, Inc.",
)
@py_test_mark_asyncio
async def test_primary_pk_exists(m):
class Customer1(m.BaseHashModel):
id: int
first_name: str
@ -745,10 +745,10 @@ async def test_primary_pk_exists(m):
id=0,
first_name="Mahmoud",
last_name="Harmouch",
bio="Python developer, wanna work at Redis, Inc."
bio="Python developer, wanna work at Redis, Inc.",
)
assert 'pk' in customer.__fields__
assert "pk" in customer.__fields__
customer = Customer2(
id=1,
@ -757,4 +757,19 @@ async def test_primary_pk_exists(m):
bio="This is member 2 who can be quite anxious until you get to know them.",
)
assert 'pk' not in customer.__fields__
assert "pk" not in customer.__fields__
@py_test_mark_asyncio
async def test_count(members, m):
# member1, member2, member3 = members
actual_count = await m.Member.find(
(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
| (m.Member.last_name == "Smith")
).count()
assert actual_count == 2
actual_count = await m.Member.find(
m.Member.first_name == "Kim", m.Member.last_name == "Brookins"
).count()
assert actual_count == 1

View file

@ -804,3 +804,18 @@ async def test_schema(m, key_prefix):
m.Member.redisearch_schema()
== f"ON JSON PREFIX 1 {key_prefix} SCHEMA $.pk AS pk TAG SEPARATOR | $.first_name AS first_name TAG SEPARATOR | $.last_name AS last_name TAG SEPARATOR | $.email AS email TAG SEPARATOR | $.age AS age NUMERIC $.bio AS bio TAG SEPARATOR | $.bio AS bio_fts TEXT $.address.pk AS address_pk TAG SEPARATOR | $.address.city AS address_city TAG SEPARATOR | $.address.postal_code AS address_postal_code TAG SEPARATOR | $.address.note.pk AS address_note_pk TAG SEPARATOR | $.address.note.description AS address_note_description TAG SEPARATOR | $.orders[*].pk AS orders_pk TAG SEPARATOR | $.orders[*].items[*].pk AS orders_items_pk TAG SEPARATOR | $.orders[*].items[*].name AS orders_items_name TAG SEPARATOR |"
)
@py_test_mark_asyncio
async def test_count(members, m):
# member1, member2, member3 = members
actual_count = await m.Member.find(
(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
| (m.Member.last_name == "Smith")
).count()
assert actual_count == 2
actual_count = await m.Member.find(
m.Member.first_name == "Kim", m.Member.last_name == "Brookins"
).count()
assert actual_count == 1