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-01 21:56:06 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
import redis
|
|
|
|
from pydantic import ValidationError
|
|
|
|
|
|
|
|
from redis_developer.orm import (
|
|
|
|
HashModel,
|
|
|
|
Field,
|
|
|
|
)
|
|
|
|
from redis_developer.orm.model import RedisModelError
|
|
|
|
|
|
|
|
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):
|
|
|
|
first_name: str
|
|
|
|
last_name: str
|
|
|
|
email: str = Field(unique=True, index=True)
|
|
|
|
join_date: datetime.date
|
|
|
|
|
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 = ""
|
|
|
|
|
|
|
|
|
|
|
|
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",
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
assert member.first_name == "Andrew"
|
|
|
|
|
|
|
|
|
|
|
|
def test_saves_model_and_creates_pk():
|
|
|
|
member = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.skip("No implemented yet")
|
|
|
|
def test_updates_a_model():
|
|
|
|
member = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
|
|
|
|
# Update a model instance in Redis
|
2021-09-16 21:03:03 +02:00
|
|
|
member.first_name = "Andrew"
|
|
|
|
member.last_name = "Brookins"
|
2021-09-01 21:56:06 +02:00
|
|
|
member.save()
|
|
|
|
|
|
|
|
# Or, with an implicit save:
|
2021-09-16 21:03:03 +02:00
|
|
|
member.update(last_name="Smith")
|
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-16 02:41:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_exact_match_queries():
|
|
|
|
member1 = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
|
|
|
|
member2 = Member(
|
|
|
|
first_name="Kim",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="k@example.com",
|
|
|
|
join_date=today
|
|
|
|
)
|
2021-09-16 21:03:03 +02:00
|
|
|
|
|
|
|
member3 = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Smith",
|
|
|
|
email="as@example.com",
|
|
|
|
join_date=today
|
|
|
|
)
|
2021-09-16 02:41:45 +02:00
|
|
|
member1.save()
|
|
|
|
member2.save()
|
2021-09-16 21:03:03 +02:00
|
|
|
member3.save()
|
|
|
|
|
|
|
|
# # TODO: How to help IDEs know that last_name is not a str, but a wrapped expression?
|
2021-09-16 02:41:45 +02:00
|
|
|
actual = Member.find(Member.last_name == "Brookins")
|
|
|
|
assert actual == [member2, member1]
|
|
|
|
|
2021-09-16 21:03:03 +02:00
|
|
|
actual = Member.find(
|
|
|
|
(Member.last_name == "Brookins") & ~(Member.first_name == "Andrew"))
|
2021-09-16 02:41:45 +02:00
|
|
|
|
2021-09-16 21:03:03 +02:00
|
|
|
assert actual == [member2]
|
2021-09-16 02:41:45 +02:00
|
|
|
|
2021-09-16 21:03:03 +02:00
|
|
|
actual = Member.find(~(Member.last_name == "Brookins"))
|
|
|
|
assert actual == [member3]
|
|
|
|
|
|
|
|
actual = Member.find(Member.last_name != "Brookins")
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
assert actual == [member2, member1]
|
2021-09-16 21:03:03 +02:00
|
|
|
|
2021-09-16 23:35:25 +02:00
|
|
|
actual = Member.find_one(Member.last_name == "Brookins")
|
|
|
|
assert actual == member2
|
2021-09-16 02:41:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_schema():
|
|
|
|
class Address(BaseHashModel):
|
|
|
|
a_string: str
|
|
|
|
an_integer: int
|
|
|
|
a_float: float
|
|
|
|
|
2021-09-16 23:35:25 +02:00
|
|
|
# TODO: Fix
|
|
|
|
assert Address.schema() == "ON HASH PREFIX 1 redis-developer:basehashmodel: SCHEMA pk TAG SORTABLE a_string TEXT an_integer NUMERIC " \
|
2021-09-16 02:41:45 +02:00
|
|
|
"a_float NUMERIC"
|