2021-09-16 02:41:45 +02:00
|
|
|
import abc
|
2021-09-01 21:56:06 +02:00
|
|
|
import datetime
|
2021-10-20 22:01:46 +02:00
|
|
|
import decimal
|
2021-10-21 08:24:31 +02:00
|
|
|
from collections import namedtuple
|
2021-09-16 02:41:45 +02:00
|
|
|
from typing import Optional
|
2021-09-30 05:23:39 +02:00
|
|
|
from unittest import mock
|
2021-09-01 21:56:06 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from pydantic import ValidationError
|
|
|
|
|
2021-11-03 20:37:09 +01:00
|
|
|
from redis_om.checks import has_redisearch
|
2021-10-22 15:36:15 +02:00
|
|
|
from redis_om.model import Field, HashModel
|
|
|
|
from redis_om.model.migrations.migrator import Migrator
|
2021-10-22 15:53:39 +02:00
|
|
|
from redis_om.model.model import NotFoundError, QueryNotSupportedError, RedisModelError
|
2021-10-20 22:01:46 +02:00
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
|
2021-11-03 20:37:09 +01:00
|
|
|
if not has_redisearch():
|
|
|
|
pytestmark = pytest.mark.skip
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
today = datetime.date.today()
|
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def m(key_prefix):
|
|
|
|
class BaseHashModel(HashModel, abc.ABC):
|
|
|
|
class Meta:
|
|
|
|
global_key_prefix = key_prefix
|
2021-09-01 21:56:06 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
class Order(BaseHashModel):
|
|
|
|
total: decimal.Decimal
|
|
|
|
currency: str
|
|
|
|
created_on: datetime.datetime
|
2021-09-01 21:56:06 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
class Member(BaseHashModel):
|
|
|
|
first_name: str = Field(index=True)
|
|
|
|
last_name: str = Field(index=True)
|
|
|
|
email: str = Field(index=True)
|
|
|
|
join_date: datetime.date
|
|
|
|
age: int = Field(index=True)
|
2021-09-01 21:56:06 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
class Meta:
|
|
|
|
model_key_prefix = "member"
|
|
|
|
primary_key_pattern = ""
|
2021-09-01 21:56:06 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
Migrator().run()
|
2021-09-01 21:56:06 +02:00
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
return namedtuple("Models", ["BaseHashModel", "Order", "Member"])(
|
|
|
|
BaseHashModel, Order, Member
|
|
|
|
)
|
2021-10-21 08:24:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def members(m):
|
|
|
|
member1 = m.Member(
|
2021-09-17 18:27:11 +02:00
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
age=38,
|
2021-10-20 22:01:46 +02:00
|
|
|
join_date=today,
|
2021-09-17 18:27:11 +02:00
|
|
|
)
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
member2 = m.Member(
|
2021-09-17 18:27:11 +02:00
|
|
|
first_name="Kim",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="k@example.com",
|
|
|
|
age=34,
|
2021-10-20 22:01:46 +02:00
|
|
|
join_date=today,
|
2021-09-17 18:27:11 +02:00
|
|
|
)
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
member3 = m.Member(
|
2021-09-17 18:27:11 +02:00
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Smith",
|
|
|
|
email="as@example.com",
|
|
|
|
age=100,
|
2021-10-20 22:01:46 +02:00
|
|
|
join_date=today,
|
2021-09-17 18:27:11 +02:00
|
|
|
)
|
|
|
|
member1.save()
|
|
|
|
member2.save()
|
|
|
|
member3.save()
|
|
|
|
|
|
|
|
yield member1, member2, member3
|
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_validates_required_fields(m):
|
2021-10-04 22:55:33 +02:00
|
|
|
# Raises ValidationError: last_name is required
|
2021-09-01 21:56:06 +02:00
|
|
|
with pytest.raises(ValidationError):
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member(first_name="Andrew", zipcode="97086", join_date=today)
|
2021-09-01 21:56:06 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_validates_field(m):
|
2021-09-01 21:56:06 +02:00
|
|
|
# Raises ValidationError: join_date is not a date
|
|
|
|
with pytest.raises(ValidationError):
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member(first_name="Andrew", last_name="Brookins", join_date="yesterday")
|
2021-09-01 21:56:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Passes validation
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_validation_passes(m):
|
|
|
|
member = m.Member(
|
2021-09-01 21:56:06 +02:00
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
2021-09-17 18:27:11 +02:00
|
|
|
join_date=today,
|
2021-10-20 22:01:46 +02:00
|
|
|
age=38,
|
2021-09-01 21:56:06 +02:00
|
|
|
)
|
|
|
|
assert member.first_name == "Andrew"
|
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_saves_model_and_creates_pk(m):
|
|
|
|
member = m.Member(
|
2021-09-01 21:56:06 +02:00
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
2021-09-17 18:27:11 +02:00
|
|
|
join_date=today,
|
2021-10-20 22:01:46 +02:00
|
|
|
age=38,
|
2021-09-01 21:56:06 +02:00
|
|
|
)
|
|
|
|
# Save a model instance to Redis
|
|
|
|
member.save()
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
member2 = m.Member.get(member.pk)
|
2021-09-01 21:56:06 +02:00
|
|
|
assert member2 == member
|
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_raises_error_with_embedded_models(m):
|
|
|
|
class Address(m.BaseHashModel):
|
2021-09-16 02:41:45 +02:00
|
|
|
address_line_1: str
|
|
|
|
address_line_2: Optional[str]
|
|
|
|
city: str
|
|
|
|
country: str
|
|
|
|
postal_code: str
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
with pytest.raises(RedisModelError):
|
2021-10-21 08:31:11 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
class InvalidMember(m.BaseHashModel):
|
2021-09-01 21:56:06 +02:00
|
|
|
address: Address
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip("Not implemented yet")
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_saves_many(m):
|
2021-09-01 21:56:06 +02:00
|
|
|
members = [
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member(
|
2021-09-01 21:56:06 +02:00
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
2021-10-20 22:01:46 +02:00
|
|
|
join_date=today,
|
2021-09-01 21:56:06 +02:00
|
|
|
),
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member(
|
2021-09-01 21:56:06 +02:00
|
|
|
first_name="Kim",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="k@example.com",
|
2021-10-20 22:01:46 +02:00
|
|
|
join_date=today,
|
|
|
|
),
|
2021-09-01 21:56:06 +02:00
|
|
|
]
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member.add(members)
|
2021-09-01 21:56:06 +02:00
|
|
|
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
@pytest.mark.skip("Not ready yet")
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_updates_a_model(members, m):
|
2021-09-26 05:38:02 +02:00
|
|
|
member1, member2, member3 = members
|
2021-09-01 21:56:06 +02:00
|
|
|
|
|
|
|
# Or, with an implicit save:
|
2021-09-26 05:38:02 +02:00
|
|
|
member1.update(last_name="Smith")
|
2021-10-21 08:24:31 +02:00
|
|
|
assert m.Member.find(m.Member.pk == member1.pk).first() == member1
|
2021-09-01 21:56:06 +02:00
|
|
|
|
|
|
|
# Or, affecting multiple model instances with an implicit save:
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member.find(m.Member.last_name == "Brookins").update(last_name="Smith")
|
|
|
|
results = m.Member.find(m.Member.last_name == "Smith")
|
2021-10-06 01:40:02 +02:00
|
|
|
assert results == members
|
2021-09-26 05:38:02 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_paginate_query(members, m):
|
2021-09-26 05:38:02 +02:00
|
|
|
member1, member2, member3 = members
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = m.Member.find().sort_by("age").all(batch_size=1)
|
2021-10-21 08:24:31 +02:00
|
|
|
assert actual == [member2, member1, member3]
|
2021-09-26 05:38:02 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_access_result_by_index_cached(members, m):
|
2021-09-30 05:23:39 +02:00
|
|
|
member1, member2, member3 = members
|
2021-10-21 08:24:31 +02:00
|
|
|
query = m.Member.find().sort_by("age")
|
2021-09-26 05:38:02 +02:00
|
|
|
# Load the cache, throw away the result.
|
2021-09-30 05:23:39 +02:00
|
|
|
assert query._model_cache == []
|
2021-09-26 05:38:02 +02:00
|
|
|
query.execute()
|
2021-09-30 05:23:39 +02:00
|
|
|
assert query._model_cache == [member2, member1, member3]
|
2021-09-26 05:38:02 +02:00
|
|
|
|
|
|
|
# Access an item that should be in the cache.
|
2021-10-20 22:01:46 +02:00
|
|
|
with mock.patch.object(query.model, "db") as mock_db:
|
2021-09-30 05:23:39 +02:00
|
|
|
assert query[0] == member2
|
|
|
|
assert not mock_db.called
|
2021-09-26 05:38:02 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_access_result_by_index_not_cached(members, m):
|
2021-09-26 05:38:02 +02:00
|
|
|
member1, member2, member3 = members
|
2021-10-21 08:24:31 +02:00
|
|
|
query = m.Member.find().sort_by("age")
|
2021-09-26 05:38:02 +02:00
|
|
|
|
|
|
|
# Assert that we don't have any models in the cache yet -- we
|
|
|
|
# haven't made any requests of Redis.
|
|
|
|
assert query._model_cache == []
|
|
|
|
assert query[0] == member2
|
|
|
|
assert query[1] == member1
|
|
|
|
assert query[2] == member3
|
2021-09-16 02:41:45 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_exact_match_queries(members, m):
|
2021-09-17 18:27:11 +02:00
|
|
|
member1, member2, member3 = members
|
2021-09-16 21:03:03 +02:00
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = m.Member.find(m.Member.last_name == "Brookins").sort_by("age").all()
|
2021-10-21 08:24:31 +02:00
|
|
|
assert actual == [member2, member1]
|
2021-09-16 02:41:45 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(
|
|
|
|
(m.Member.last_name == "Brookins") & ~(m.Member.first_name == "Andrew")
|
2021-10-20 22:01:46 +02:00
|
|
|
).all()
|
2021-09-16 21:03:03 +02:00
|
|
|
assert actual == [member2]
|
2021-09-16 02:41:45 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(~(m.Member.last_name == "Brookins")).all()
|
2021-09-16 21:03:03 +02:00
|
|
|
assert actual == [member3]
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(m.Member.last_name != "Brookins").all()
|
2021-09-16 21:03:03 +02:00
|
|
|
assert actual == [member3]
|
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = (
|
|
|
|
m.Member.find(
|
|
|
|
(m.Member.last_name == "Brookins") & (m.Member.first_name == "Andrew")
|
|
|
|
| (m.Member.first_name == "Kim")
|
|
|
|
)
|
|
|
|
.sort_by("age")
|
|
|
|
.all()
|
|
|
|
)
|
2021-10-21 08:24:31 +02:00
|
|
|
assert actual == [member2, member1]
|
2021-09-16 21:03:03 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(
|
|
|
|
m.Member.first_name == "Kim", m.Member.last_name == "Brookins"
|
2021-10-20 22:01:46 +02:00
|
|
|
).all()
|
2021-09-26 05:38:02 +02:00
|
|
|
assert actual == [member2]
|
2021-09-16 02:41:45 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_recursive_query_resolution(members, m):
|
2021-09-21 01:06:04 +02:00
|
|
|
member1, member2, member3 = members
|
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = (
|
|
|
|
m.Member.find(
|
|
|
|
(m.Member.last_name == "Brookins")
|
|
|
|
| (m.Member.age == 100) & (m.Member.last_name == "Smith")
|
|
|
|
)
|
|
|
|
.sort_by("age")
|
|
|
|
.all()
|
|
|
|
)
|
2021-10-21 08:24:31 +02:00
|
|
|
assert actual == [member2, member1, member3]
|
2021-09-21 01:06:04 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_tag_queries_boolean_logic(members, m):
|
2021-09-21 01:06:04 +02:00
|
|
|
member1, member2, member3 = members
|
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = (
|
|
|
|
m.Member.find(
|
|
|
|
(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
|
|
|
|
| (m.Member.last_name == "Smith")
|
|
|
|
)
|
|
|
|
.sort_by("age")
|
|
|
|
.all()
|
|
|
|
)
|
2021-10-06 01:40:02 +02:00
|
|
|
assert actual == [member1, member3]
|
2021-09-21 01:06:04 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_tag_queries_punctuation(m):
|
|
|
|
member1 = m.Member(
|
2021-10-06 01:40:02 +02:00
|
|
|
first_name="Andrew, the Michael",
|
2021-09-26 05:38:02 +02:00
|
|
|
last_name="St. Brookins-on-Pier",
|
2021-10-06 01:40:02 +02:00
|
|
|
email="a|b@example.com", # NOTE: This string uses the TAG field separator.
|
2021-09-26 05:38:02 +02:00
|
|
|
age=38,
|
2021-10-06 01:40:02 +02:00
|
|
|
join_date=today,
|
2021-09-26 05:38:02 +02:00
|
|
|
)
|
2021-10-06 01:40:02 +02:00
|
|
|
member1.save()
|
2021-09-26 05:38:02 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
member2 = m.Member(
|
2021-10-06 01:40:02 +02:00
|
|
|
first_name="Bob",
|
|
|
|
last_name="the Villain",
|
|
|
|
email="a|villain@example.com", # NOTE: This string uses the TAG field separator.
|
|
|
|
age=38,
|
|
|
|
join_date=today,
|
|
|
|
)
|
|
|
|
member2.save()
|
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
assert (
|
|
|
|
m.Member.find(m.Member.first_name == "Andrew, the Michael").first() == member1
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
m.Member.find(m.Member.last_name == "St. Brookins-on-Pier").first() == member1
|
|
|
|
)
|
2021-10-06 01:40:02 +02:00
|
|
|
|
|
|
|
# Notice that when we index and query multiple values that use the internal
|
|
|
|
# TAG separator for single-value exact-match fields, like an indexed string,
|
|
|
|
# the queries will succeed. We apply a workaround that queries for the union
|
|
|
|
# of the two values separated by the tag separator.
|
2021-10-21 08:24:31 +02:00
|
|
|
assert m.Member.find(m.Member.email == "a|b@example.com").all() == [member1]
|
|
|
|
assert m.Member.find(m.Member.email == "a|villain@example.com").all() == [member2]
|
2021-09-26 05:38:02 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_tag_queries_negation(members, m):
|
2021-09-21 01:06:04 +02:00
|
|
|
member1, member2, member3 = members
|
|
|
|
|
2021-10-06 01:40:02 +02:00
|
|
|
"""
|
|
|
|
┌first_name
|
|
|
|
NOT EQ┤
|
|
|
|
└Andrew
|
|
|
|
|
|
|
|
"""
|
2021-10-21 08:24:31 +02:00
|
|
|
query = m.Member.find(~(m.Member.first_name == "Andrew"))
|
2021-10-06 01:40:02 +02:00
|
|
|
assert query.all() == [member2]
|
|
|
|
|
|
|
|
"""
|
|
|
|
┌first_name
|
|
|
|
┌NOT EQ┤
|
|
|
|
| └Andrew
|
|
|
|
AND┤
|
|
|
|
| ┌last_name
|
|
|
|
└EQ┤
|
|
|
|
└Brookins
|
|
|
|
|
|
|
|
"""
|
2021-10-21 08:24:31 +02:00
|
|
|
query = m.Member.find(
|
|
|
|
~(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
|
2021-10-06 01:40:02 +02:00
|
|
|
)
|
|
|
|
assert query.all() == [member2]
|
|
|
|
|
|
|
|
"""
|
|
|
|
┌first_name
|
|
|
|
┌NOT EQ┤
|
|
|
|
| └Andrew
|
|
|
|
AND┤
|
|
|
|
| ┌last_name
|
|
|
|
| ┌EQ┤
|
|
|
|
| | └Brookins
|
|
|
|
└OR┤
|
|
|
|
| ┌last_name
|
|
|
|
└EQ┤
|
|
|
|
└Smith
|
|
|
|
"""
|
2021-10-21 08:24:31 +02:00
|
|
|
query = m.Member.find(
|
|
|
|
~(m.Member.first_name == "Andrew")
|
|
|
|
& ((m.Member.last_name == "Brookins") | (m.Member.last_name == "Smith"))
|
2021-10-20 22:01:46 +02:00
|
|
|
)
|
2021-10-06 01:40:02 +02:00
|
|
|
assert query.all() == [member2]
|
|
|
|
|
|
|
|
"""
|
|
|
|
┌first_name
|
|
|
|
┌NOT EQ┤
|
|
|
|
| └Andrew
|
|
|
|
┌AND┤
|
|
|
|
| | ┌last_name
|
|
|
|
| └EQ┤
|
|
|
|
| └Brookins
|
|
|
|
OR┤
|
|
|
|
| ┌last_name
|
|
|
|
└EQ┤
|
|
|
|
└Smith
|
|
|
|
"""
|
2021-10-21 08:24:31 +02:00
|
|
|
query = m.Member.find(
|
|
|
|
~(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins")
|
|
|
|
| (m.Member.last_name == "Smith")
|
2021-10-20 22:01:46 +02:00
|
|
|
)
|
2021-10-21 08:31:11 +02:00
|
|
|
assert query.sort_by("age").all() == [member2, member3]
|
2021-09-21 01:06:04 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(
|
|
|
|
(m.Member.first_name == "Andrew") & ~(m.Member.last_name == "Brookins")
|
2021-10-20 22:01:46 +02:00
|
|
|
).all()
|
2021-10-06 01:40:02 +02:00
|
|
|
assert actual == [member3]
|
2021-09-21 01:06:04 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_numeric_queries(members, m):
|
2021-09-17 18:27:11 +02:00
|
|
|
member1, member2, member3 = members
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(m.Member.age == 34).all()
|
2021-09-26 05:38:02 +02:00
|
|
|
assert actual == [member2]
|
2021-09-17 18:27:11 +02:00
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = m.Member.find(m.Member.age > 34).sort_by("age").all()
|
2021-10-06 01:40:02 +02:00
|
|
|
assert actual == [member1, member3]
|
2021-09-17 18:27:11 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(m.Member.age < 35).all()
|
2021-09-17 18:27:11 +02:00
|
|
|
assert actual == [member2]
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(m.Member.age <= 34).all()
|
2021-09-17 18:27:11 +02:00
|
|
|
assert actual == [member2]
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(m.Member.age >= 100).all()
|
2021-09-17 18:27:11 +02:00
|
|
|
assert actual == [member3]
|
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = m.Member.find(m.Member.age != 34).sort_by("age").all()
|
2021-10-14 02:16:20 +02:00
|
|
|
assert actual == [member1, member3]
|
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = m.Member.find(~(m.Member.age == 100)).sort_by("age").all()
|
2021-10-21 08:24:31 +02:00
|
|
|
assert actual == [member2, member1]
|
2021-09-17 18:27:11 +02:00
|
|
|
|
2021-10-21 08:31:11 +02:00
|
|
|
actual = m.Member.find(m.Member.age > 30, m.Member.age < 40).sort_by("age").all()
|
2021-10-21 08:24:31 +02:00
|
|
|
assert actual == [member2, member1]
|
2021-10-14 02:16:20 +02:00
|
|
|
|
2021-09-17 18:27:11 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_sorting(members, m):
|
2021-09-30 05:23:39 +02:00
|
|
|
member1, member2, member3 = members
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(m.Member.age > 34).sort_by("age").all()
|
2021-10-06 01:40:02 +02:00
|
|
|
assert actual == [member1, member3]
|
2021-09-30 05:23:39 +02:00
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
actual = m.Member.find(m.Member.age > 34).sort_by("-age").all()
|
2021-10-06 01:40:02 +02:00
|
|
|
assert actual == [member3, member1]
|
2021-09-30 05:23:39 +02:00
|
|
|
|
|
|
|
with pytest.raises(QueryNotSupportedError):
|
|
|
|
# This field does not exist.
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member.find().sort_by("not-a-real-field").all()
|
2021-09-30 05:23:39 +02:00
|
|
|
|
|
|
|
with pytest.raises(QueryNotSupportedError):
|
|
|
|
# This field is not sortable.
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member.find().sort_by("join_date").all()
|
2021-09-30 05:23:39 +02:00
|
|
|
|
|
|
|
|
2021-11-09 01:28:58 +01:00
|
|
|
def test_all_keys(members, m):
|
|
|
|
pks = sorted(list(m.Member.all_pks()))
|
|
|
|
assert len(pks) == 3
|
|
|
|
assert pks == sorted([m.pk for m in members])
|
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_not_found(m):
|
2021-09-30 05:42:02 +02:00
|
|
|
with pytest.raises(NotFoundError):
|
2021-09-30 05:32:44 +02:00
|
|
|
# This ID does not exist.
|
2021-10-21 08:24:31 +02:00
|
|
|
m.Member.get(1000)
|
2021-09-30 05:32:44 +02:00
|
|
|
|
|
|
|
|
2021-10-21 08:24:31 +02:00
|
|
|
def test_schema(m, key_prefix):
|
|
|
|
class Address(m.BaseHashModel):
|
2021-09-17 18:27:11 +02:00
|
|
|
a_string: str = Field(index=True)
|
|
|
|
a_full_text_string: str = Field(index=True, full_text_search=True)
|
|
|
|
an_integer: int = Field(index=True, sortable=True)
|
|
|
|
a_float: float = Field(index=True)
|
|
|
|
another_integer: int
|
|
|
|
another_float: float
|
2021-09-16 02:41:45 +02:00
|
|
|
|
2021-10-20 22:01:46 +02:00
|
|
|
assert (
|
|
|
|
Address.redisearch_schema()
|
2021-10-21 08:24:31 +02:00
|
|
|
== f"ON HASH PREFIX 1 {key_prefix}:tests.test_hash_model.Address: SCHEMA pk TAG SEPARATOR | a_string TAG SEPARATOR | a_full_text_string TAG SEPARATOR | a_full_text_string_fts TEXT an_integer NUMERIC SORTABLE a_float NUMERIC"
|
2021-10-20 22:01:46 +02:00
|
|
|
)
|