2021-08-31 21:03:53 +02:00
|
|
|
import decimal
|
|
|
|
import datetime
|
|
|
|
from typing import Optional, List
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import redis
|
|
|
|
from pydantic import ValidationError
|
|
|
|
|
|
|
|
from redis_developer.orm import (
|
2021-09-01 21:56:06 +02:00
|
|
|
JsonModel,
|
2021-08-31 21:03:53 +02:00
|
|
|
Field,
|
|
|
|
)
|
|
|
|
|
|
|
|
r = redis.Redis()
|
|
|
|
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
class BaseJsonModel(JsonModel):
|
|
|
|
class Meta(JsonModel.Meta):
|
|
|
|
global_key_prefix = "redis-developer"
|
2021-08-31 21:03:53 +02:00
|
|
|
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
class Address(BaseJsonModel):
|
2021-08-31 21:03:53 +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
|
|
|
class Order(BaseJsonModel):
|
2021-08-31 21:03:53 +02:00
|
|
|
total: decimal.Decimal
|
|
|
|
currency: str
|
|
|
|
created_on: datetime.datetime
|
|
|
|
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
class Member(BaseJsonModel):
|
2021-08-31 21:03:53 +02:00
|
|
|
first_name: str
|
|
|
|
last_name: str
|
|
|
|
email: str = Field(unique=True, index=True)
|
|
|
|
join_date: datetime.date
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
# Creates an embedded model: stored as hash fields or JSON document.
|
2021-08-31 21:03:53 +02:00
|
|
|
address: Address
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
# Creates an embedded list of models.
|
|
|
|
orders: Optional[List[Order]]
|
2021-08-31 21:03:53 +02:00
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
class Meta(BaseJsonModel.Meta):
|
2021-08-31 21:03:53 +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=datetime.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():
|
|
|
|
address = Address(
|
|
|
|
address_line_1="1 Main St.",
|
|
|
|
city="Happy Town",
|
|
|
|
state="WY",
|
|
|
|
postal_code=11111,
|
|
|
|
country="USA"
|
|
|
|
)
|
|
|
|
member = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
address=address,
|
|
|
|
join_date=datetime.date.today()
|
|
|
|
)
|
|
|
|
assert member.first_name == "Andrew"
|
|
|
|
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
def test_gets_pk():
|
|
|
|
address = Address(
|
|
|
|
address_line_1="1 Main St.",
|
|
|
|
city="Happy Town",
|
|
|
|
state="WY",
|
|
|
|
postal_code=11111,
|
|
|
|
country="USA"
|
|
|
|
)
|
|
|
|
assert address.pk is not None
|
|
|
|
|
|
|
|
|
2021-08-31 21:03:53 +02:00
|
|
|
def test_saves_model():
|
|
|
|
address = Address(
|
|
|
|
address_line_1="1 Main St.",
|
|
|
|
city="Happy Town",
|
|
|
|
state="WY",
|
|
|
|
postal_code=11111,
|
|
|
|
country="USA"
|
|
|
|
)
|
|
|
|
# Save a model instance to Redis
|
|
|
|
address.save()
|
|
|
|
|
|
|
|
address2 = Address.get(address.pk)
|
|
|
|
assert address2 == address
|
|
|
|
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
# Saves a model with embedded models
|
2021-08-31 21:03:53 +02:00
|
|
|
def test_saves_with_relationships():
|
|
|
|
address = Address(
|
|
|
|
address_line_1="1 Main St.",
|
|
|
|
city="Happy Town",
|
|
|
|
state="WY",
|
|
|
|
postal_code=11111,
|
|
|
|
country="USA"
|
|
|
|
)
|
|
|
|
member = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
address=address,
|
|
|
|
join_date=datetime.date.today()
|
|
|
|
)
|
|
|
|
member.save()
|
|
|
|
|
2021-09-01 21:56:06 +02:00
|
|
|
member2 = Member.get(member.pk)
|
|
|
|
assert member2.address == address
|
|
|
|
|
2021-08-31 21:03:53 +02:00
|
|
|
|
|
|
|
# Save many model instances to Redis
|
2021-09-01 00:52:21 +02:00
|
|
|
@pytest.mark.skip("Not implemented yet")
|
2021-08-31 21:03:53 +02:00
|
|
|
def test_saves_many():
|
2021-08-31 22:31:14 +02:00
|
|
|
address = Address(
|
|
|
|
address_line_1="1 Main St.",
|
|
|
|
city="Happy Town",
|
|
|
|
state="WY",
|
|
|
|
postal_code=11111,
|
|
|
|
country="USA"
|
|
|
|
)
|
2021-08-31 21:03:53 +02:00
|
|
|
today = datetime.date.today()
|
|
|
|
members = [
|
|
|
|
Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
address=address,
|
|
|
|
join_date=today
|
|
|
|
),
|
|
|
|
Member(
|
|
|
|
first_name="Kim",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="k@example.com",
|
|
|
|
address=address,
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
]
|
|
|
|
Member.add(members)
|
|
|
|
|
|
|
|
|
2021-09-01 00:52:21 +02:00
|
|
|
@pytest.mark.skip("No implemented yet")
|
2021-08-31 21:03:53 +02:00
|
|
|
def test_updates_a_model():
|
2021-08-31 22:31:14 +02:00
|
|
|
address = Address(
|
|
|
|
address_line_1="1 Main St.",
|
|
|
|
city="Happy Town",
|
|
|
|
state="WY",
|
|
|
|
postal_code=11111,
|
|
|
|
country="USA"
|
|
|
|
)
|
2021-09-01 00:52:21 +02:00
|
|
|
today = datetime.date.today()
|
2021-08-31 22:31:14 +02:00
|
|
|
member = Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
address=address,
|
|
|
|
join_date=today
|
|
|
|
)
|
|
|
|
|
2021-08-31 21:03:53 +02:00
|
|
|
# Update a model instance in Redis
|
|
|
|
member.first_name = "Brian"
|
|
|
|
member.last_name = "Sam-Bodden"
|
|
|
|
member.save()
|
|
|
|
|
|
|
|
# Or, with an implicit save:
|
|
|
|
member.update(first_name="Brian", last_name="Sam-Bodden")
|
|
|
|
|
|
|
|
# Or, affecting multiple model instances with an implicit save:
|
|
|
|
Member.filter(Member.last_name == "Brookins").update(last_name="Sam-Bodden")
|
|
|
|
|
|
|
|
|
2021-09-01 00:52:21 +02:00
|
|
|
@pytest.mark.skip("Not implemented yet")
|
2021-08-31 21:03:53 +02:00
|
|
|
def test_exact_match_queries():
|
|
|
|
# What if the field wasn't unique and there were two "a@example.com"
|
|
|
|
# entries? This would raise a MultipleObjectsReturned error:
|
|
|
|
member = Member.get(Member.email == "a.m.brookins@gmail.com")
|
|
|
|
|
|
|
|
# What if you know there might be multiple results? Use filter():
|
|
|
|
members = Member.filter(Member.last_name == "Brookins")
|
|
|
|
|
|
|
|
# What if you want to only return values that don't match a query?
|
|
|
|
members = Member.exclude(Member.last_name == "Brookins")
|
|
|
|
|
|
|
|
# You can combine filer() and exclude():
|
|
|
|
members = Member.filter(Member.last_name == "Brookins").exclude(
|
|
|
|
Member.first_name == "Andrew")
|