2021-09-16 02:41:45 +02:00
|
|
|
import abc
|
2021-09-01 21:56:06 +02:00
|
|
|
import decimal
|
|
|
|
import datetime
|
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
|
|
|
|
import redis
|
|
|
|
from pydantic import ValidationError
|
|
|
|
|
|
|
|
from redis_developer.orm import (
|
|
|
|
HashModel,
|
|
|
|
Field,
|
|
|
|
)
|
2021-09-30 05:23:39 +02:00
|
|
|
from redis_developer.orm.model import RedisModelError, QueryNotSupportedError
|
2021-09-01 21:56:06 +02:00
|
|
|
|
|
|
|
r = redis.Redis()
|
|
|
|
today = datetime.date.today()
|
|
|
|
|
|
|
|
|
2021-09-16 02:41:45 +02:00
|
|
|
class BaseHashModel(HashModel, abc.ABC):
|
|
|
|
class Meta:
|
2021-09-01 21:56:06 +02:00
|
|
|
global_key_prefix = "redis-developer"
|
|
|
|
|
|
|
|
|
|
|
|
class Order(BaseHashModel):
|
|
|
|
total: decimal.Decimal
|
|
|
|
currency: str
|
|
|
|
created_on: datetime.datetime
|
|
|
|
|
|
|
|
|
|
|
|
class Member(BaseHashModel):
|
2021-09-21 01:06:04 +02:00
|
|
|
first_name: str = Field(index=True)
|
|
|
|
last_name: str = Field(index=True)
|
2021-09-17 18:27:11 +02:00
|
|
|
email: str = Field(index=True)
|
2021-09-01 21:56:06 +02:00
|
|
|
join_date: datetime.date
|
2021-09-21 01:06:04 +02:00
|
|
|
age: int = Field(index=True)
|
2021-09-01 21:56:06 +02:00
|
|
|
|
2021-09-16 02:41:45 +02:00
|
|
|
class Meta:
|
2021-09-01 21:56:06 +02:00
|
|
|
model_key_prefix = "member"
|
|
|
|
primary_key_pattern = ""
|
|
|
|
|
|
|
|
|
2021-09-17 18:27:11 +02:00
|
|
|
@pytest.fixture()
|
|
|
|
def members():
|
|
|
|
member1 = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
age=38,
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
|
|
|
|
member2 = Member(
|
|
|
|
first_name="Kim",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="k@example.com",
|
|
|
|
age=34,
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
|
|
|
|
member3 = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Smith",
|
|
|
|
email="as@example.com",
|
|
|
|
age=100,
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
member1.save()
|
|
|
|
member2.save()
|
|
|
|
member3.save()
|
|
|
|
|
|
|
|
yield member1, member2, member3
|
|
|
|
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
def test_validates_required_fields():
|
|
|
|
# Raises ValidationError: last_name, address are required
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
zipcode="97086",
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validates_field():
|
|
|
|
# Raises ValidationError: join_date is not a date
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
join_date="yesterday"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
# Passes validation
|
|
|
|
def test_validation_passes():
|
|
|
|
member = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
2021-09-17 18:27:11 +02:00
|
|
|
join_date=today,
|
|
|
|
age=38
|
2021-09-01 21:56:06 +02:00
|
|
|
)
|
|
|
|
assert member.first_name == "Andrew"
|
|
|
|
|
|
|
|
|
|
|
|
def test_saves_model_and_creates_pk():
|
|
|
|
member = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
2021-09-17 18:27:11 +02:00
|
|
|
join_date=today,
|
|
|
|
age=38
|
2021-09-01 21:56:06 +02:00
|
|
|
)
|
|
|
|
# Save a model instance to Redis
|
|
|
|
member.save()
|
|
|
|
|
|
|
|
member2 = Member.get(member.pk)
|
|
|
|
assert member2 == member
|
|
|
|
|
|
|
|
|
|
|
|
def test_raises_error_with_embedded_models():
|
2021-09-16 02:41:45 +02:00
|
|
|
class Address(BaseHashModel):
|
|
|
|
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):
|
|
|
|
class InvalidMember(BaseHashModel):
|
|
|
|
address: Address
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip("Not implemented yet")
|
|
|
|
def test_saves_many():
|
|
|
|
members = [
|
|
|
|
Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
join_date=today
|
|
|
|
),
|
|
|
|
Member(
|
|
|
|
first_name="Kim",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="k@example.com",
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
]
|
|
|
|
Member.add(members)
|
|
|
|
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
@pytest.mark.skip("Not ready yet")
|
|
|
|
def test_updates_a_model(members):
|
|
|
|
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")
|
|
|
|
assert Member.find(Member.pk == member1.pk).first() == member1
|
2021-09-01 21:56:06 +02:00
|
|
|
|
|
|
|
# Or, affecting multiple model instances with an implicit save:
|
2021-09-16 21:03:03 +02:00
|
|
|
Member.find(Member.last_name == "Brookins").update(last_name="Smith")
|
2021-09-26 05:38:02 +02:00
|
|
|
results = Member.find(Member.last_name == "Smith")
|
|
|
|
assert sorted(results) == members
|
|
|
|
|
|
|
|
|
|
|
|
def test_paginate_query(members):
|
|
|
|
member1, member2, member3 = members
|
|
|
|
actual = Member.find().all(batch_size=1)
|
|
|
|
assert sorted(actual) == [member1, member2, member3]
|
|
|
|
|
|
|
|
|
|
|
|
def test_access_result_by_index_cached(members):
|
2021-09-30 05:23:39 +02:00
|
|
|
member1, member2, member3 = members
|
2021-09-26 05:38:02 +02:00
|
|
|
query = Member.find().sort_by('age')
|
|
|
|
# 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-09-30 05:23:39 +02:00
|
|
|
with mock.patch.object(query.model, 'db') as mock_db:
|
|
|
|
assert query[0] == member2
|
|
|
|
assert not mock_db.called
|
2021-09-26 05:38:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_access_result_by_index_not_cached(members):
|
|
|
|
member1, member2, member3 = members
|
|
|
|
query = Member.find().sort_by('age')
|
|
|
|
|
|
|
|
# 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-09-17 18:27:11 +02:00
|
|
|
def test_exact_match_queries(members):
|
|
|
|
member1, member2, member3 = members
|
2021-09-16 21:03:03 +02:00
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(Member.last_name == "Brookins").all()
|
2021-09-21 01:06:04 +02:00
|
|
|
assert sorted(actual) == [member1, member2]
|
2021-09-16 02:41:45 +02:00
|
|
|
|
2021-09-16 21:03:03 +02:00
|
|
|
actual = Member.find(
|
2021-09-26 05:38:02 +02:00
|
|
|
(Member.last_name == "Brookins") & ~(Member.first_name == "Andrew")).all()
|
2021-09-16 21:03:03 +02:00
|
|
|
assert actual == [member2]
|
2021-09-16 02:41:45 +02:00
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(~(Member.last_name == "Brookins")).all()
|
2021-09-16 21:03:03 +02:00
|
|
|
assert actual == [member3]
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(Member.last_name != "Brookins").all()
|
2021-09-16 21:03:03 +02:00
|
|
|
assert actual == [member3]
|
|
|
|
|
2021-09-16 23:35:25 +02:00
|
|
|
actual = Member.find(
|
|
|
|
(Member.last_name == "Brookins") & (Member.first_name == "Andrew")
|
|
|
|
| (Member.first_name == "Kim")
|
2021-09-26 05:38:02 +02:00
|
|
|
).all()
|
2021-09-16 23:35:25 +02:00
|
|
|
assert actual == [member2, member1]
|
2021-09-16 21:03:03 +02:00
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(Member.first_name == "Kim", Member.last_name == "Brookins").all()
|
|
|
|
assert actual == [member2]
|
2021-09-16 02:41:45 +02:00
|
|
|
|
|
|
|
|
2021-09-21 01:06:04 +02:00
|
|
|
def test_recursive_query_resolution(members):
|
|
|
|
member1, member2, member3 = members
|
|
|
|
|
|
|
|
actual = Member.find((Member.last_name == "Brookins") | (
|
|
|
|
Member.age == 100
|
2021-09-26 05:38:02 +02:00
|
|
|
) & (Member.last_name == "Smith")).all()
|
2021-09-21 01:06:04 +02:00
|
|
|
assert sorted(actual) == [member1, member2, member3]
|
|
|
|
|
|
|
|
|
|
|
|
def test_tag_queries_boolean_logic(members):
|
|
|
|
member1, member2, member3 = members
|
|
|
|
|
|
|
|
actual = Member.find(
|
|
|
|
(Member.first_name == "Andrew") &
|
2021-09-26 05:38:02 +02:00
|
|
|
(Member.last_name == "Brookins") | (Member.last_name == "Smith")).all()
|
2021-09-21 01:06:04 +02:00
|
|
|
assert sorted(actual) == [member1, member3]
|
|
|
|
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
def test_tag_queries_punctuation():
|
|
|
|
member = Member(
|
|
|
|
first_name="Andrew the Michael",
|
|
|
|
last_name="St. Brookins-on-Pier",
|
|
|
|
email="a@example.com",
|
|
|
|
age=38,
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
member.save()
|
|
|
|
|
|
|
|
assert Member.find(Member.first_name == "Andrew the Michael").first() == member
|
|
|
|
assert Member.find(Member.last_name == "St. Brookins-on-Pier").first() == member
|
|
|
|
assert Member.find(Member.email == "a@example.com").first() == member
|
|
|
|
|
|
|
|
|
2021-09-21 01:06:04 +02:00
|
|
|
def test_tag_queries_negation(members):
|
|
|
|
member1, member2, member3 = members
|
|
|
|
|
|
|
|
actual = Member.find(
|
|
|
|
~(Member.first_name == "Andrew") &
|
2021-09-26 05:38:02 +02:00
|
|
|
(Member.last_name == "Brookins") | (Member.last_name == "Smith")).all()
|
2021-09-21 01:06:04 +02:00
|
|
|
assert sorted(actual) == [member2, member3]
|
|
|
|
|
|
|
|
actual = Member.find(
|
2021-09-26 05:38:02 +02:00
|
|
|
(Member.first_name == "Andrew") & ~(Member.last_name == "Brookins")).all()
|
2021-09-21 01:06:04 +02:00
|
|
|
assert sorted(actual) == [member3]
|
|
|
|
|
|
|
|
|
2021-09-17 18:27:11 +02:00
|
|
|
def test_numeric_queries(members):
|
|
|
|
member1, member2, member3 = members
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(Member.age == 34).all()
|
|
|
|
assert actual == [member2]
|
2021-09-17 18:27:11 +02:00
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(Member.age > 34).all()
|
2021-09-17 18:27:11 +02:00
|
|
|
assert sorted(actual) == [member1, member3]
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(Member.age < 35).all()
|
2021-09-17 18:27:11 +02:00
|
|
|
assert actual == [member2]
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(Member.age <= 34).all()
|
2021-09-17 18:27:11 +02:00
|
|
|
assert actual == [member2]
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(Member.age >= 100).all()
|
2021-09-17 18:27:11 +02:00
|
|
|
assert actual == [member3]
|
|
|
|
|
2021-09-26 05:38:02 +02:00
|
|
|
actual = Member.find(~(Member.age == 100)).all()
|
2021-09-17 18:27:11 +02:00
|
|
|
assert sorted(actual) == [member1, member2]
|
|
|
|
|
|
|
|
|
2021-09-30 05:23:39 +02:00
|
|
|
def test_sorting(members):
|
|
|
|
member1, member2, member3 = members
|
|
|
|
|
|
|
|
actual = Member.find(Member.age > 34).sort_by('age').all()
|
|
|
|
assert sorted(actual) == [member3, member1]
|
|
|
|
|
|
|
|
actual = Member.find(Member.age > 34).sort_by('-age').all()
|
|
|
|
assert sorted(actual) == [member1, member3]
|
|
|
|
|
|
|
|
with pytest.raises(QueryNotSupportedError):
|
|
|
|
# This field does not exist.
|
|
|
|
Member.find().sort_by('not-a-real-field').all()
|
|
|
|
|
|
|
|
with pytest.raises(QueryNotSupportedError):
|
|
|
|
# This field is not sortable.
|
|
|
|
Member.find().sort_by('join_date').all()
|
|
|
|
|
|
|
|
|
2021-09-16 02:41:45 +02:00
|
|
|
def test_schema():
|
|
|
|
class Address(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-09-30 05:23:39 +02:00
|
|
|
assert Address.redisearch_schema() == "ON HASH PREFIX 1 redis-developer:tests.test_hash_model.Address: " \
|
|
|
|
"SCHEMA pk TAG a_string TAG a_full_text_string TAG " \
|
|
|
|
"a_full_text_string_fts TEXT an_integer NUMERIC SORTABLE " \
|
|
|
|
"a_float NUMERIC"
|