2021-11-03 20:37:09 +01:00
|
|
|
import abc
|
|
|
|
import datetime
|
|
|
|
import decimal
|
|
|
|
from collections import namedtuple
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from pydantic import ValidationError
|
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
from aredis_om import HashModel, Migrator, NotFoundError, RedisModelError
|
2021-11-03 20:37:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
today = datetime.date.today()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2021-11-10 00:59:10 +01:00
|
|
|
async def m(key_prefix, redis):
|
2021-11-03 20:37:09 +01:00
|
|
|
class BaseHashModel(HashModel, abc.ABC):
|
|
|
|
class Meta:
|
|
|
|
global_key_prefix = key_prefix
|
|
|
|
|
|
|
|
class Order(BaseHashModel):
|
|
|
|
total: decimal.Decimal
|
|
|
|
currency: str
|
|
|
|
created_on: datetime.datetime
|
|
|
|
|
|
|
|
class Member(BaseHashModel):
|
|
|
|
first_name: str
|
|
|
|
last_name: str
|
|
|
|
email: str
|
|
|
|
join_date: datetime.date
|
|
|
|
age: int
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model_key_prefix = "member"
|
|
|
|
primary_key_pattern = ""
|
|
|
|
|
2021-11-25 03:12:27 +01:00
|
|
|
await Migrator().run()
|
2021-11-03 20:37:09 +01:00
|
|
|
|
|
|
|
return namedtuple("Models", ["BaseHashModel", "Order", "Member"])(
|
|
|
|
BaseHashModel, Order, Member
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2021-11-10 00:59:10 +01:00
|
|
|
async def members(m):
|
2021-11-03 20:37:09 +01:00
|
|
|
member1 = m.Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
age=38,
|
|
|
|
join_date=today,
|
|
|
|
)
|
|
|
|
|
|
|
|
member2 = m.Member(
|
|
|
|
first_name="Kim",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="k@example.com",
|
|
|
|
age=34,
|
|
|
|
join_date=today,
|
|
|
|
)
|
|
|
|
|
|
|
|
member3 = m.Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Smith",
|
|
|
|
email="as@example.com",
|
|
|
|
age=100,
|
|
|
|
join_date=today,
|
|
|
|
)
|
2021-11-10 00:59:10 +01:00
|
|
|
await member1.save()
|
|
|
|
await member2.save()
|
|
|
|
await member3.save()
|
2021-11-03 20:37:09 +01:00
|
|
|
|
|
|
|
yield member1, member2, member3
|
|
|
|
|
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_all_keys(members, m):
|
|
|
|
pks = sorted([pk async for pk in await m.Member.all_pks()])
|
|
|
|
assert len(pks) == 3
|
|
|
|
assert pks == sorted([m.pk for m in members])
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_not_found(m):
|
|
|
|
with pytest.raises(NotFoundError):
|
|
|
|
# This ID does not exist.
|
|
|
|
await m.Member.get(1000)
|
|
|
|
|
|
|
|
|
2021-11-03 20:37:09 +01:00
|
|
|
def test_validates_required_fields(m):
|
|
|
|
# Raises ValidationError: last_name is required
|
2021-11-10 00:59:10 +01:00
|
|
|
# TODO: Test the error value
|
2021-11-03 20:37:09 +01:00
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
m.Member(first_name="Andrew", zipcode="97086", join_date=today)
|
|
|
|
|
|
|
|
|
|
|
|
def test_validates_field(m):
|
|
|
|
# Raises ValidationError: join_date is not a date
|
2021-11-10 00:59:10 +01:00
|
|
|
# TODO: Test the error value
|
2021-11-03 20:37:09 +01:00
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
m.Member(first_name="Andrew", last_name="Brookins", join_date="yesterday")
|
|
|
|
|
|
|
|
|
|
|
|
def test_validation_passes(m):
|
|
|
|
member = m.Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
join_date=today,
|
|
|
|
age=38,
|
|
|
|
)
|
|
|
|
assert member.first_name == "Andrew"
|
|
|
|
|
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_saves_model_and_creates_pk(m):
|
2021-11-03 20:37:09 +01:00
|
|
|
member = m.Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
join_date=today,
|
|
|
|
age=38,
|
|
|
|
)
|
|
|
|
# Save a model instance to Redis
|
2021-11-10 00:59:10 +01:00
|
|
|
await member.save()
|
2021-11-03 20:37:09 +01:00
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
member2 = await m.Member.get(member.pk)
|
2021-11-03 20:37:09 +01:00
|
|
|
assert member2 == member
|
|
|
|
|
|
|
|
|
|
|
|
def test_raises_error_with_embedded_models(m):
|
|
|
|
class Address(m.BaseHashModel):
|
|
|
|
address_line_1: str
|
|
|
|
address_line_2: Optional[str]
|
|
|
|
city: str
|
|
|
|
country: str
|
|
|
|
postal_code: str
|
|
|
|
|
|
|
|
with pytest.raises(RedisModelError):
|
|
|
|
|
|
|
|
class InvalidMember(m.BaseHashModel):
|
|
|
|
address: Address
|
|
|
|
|
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_saves_many(m):
|
|
|
|
member1 = m.Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="a@example.com",
|
|
|
|
join_date=today,
|
|
|
|
age=38,
|
|
|
|
)
|
|
|
|
member2 = m.Member(
|
|
|
|
first_name="Kim",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="k@example.com",
|
|
|
|
join_date=today,
|
|
|
|
age=34,
|
|
|
|
)
|
|
|
|
members = [member1, member2]
|
|
|
|
result = await m.Member.add(members)
|
|
|
|
assert result == [member1, member2]
|
2021-11-03 20:37:09 +01:00
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
assert await m.Member.get(pk=member1.pk) == member1
|
|
|
|
assert await m.Member.get(pk=member2.pk) == member2
|
2021-11-03 20:37:09 +01:00
|
|
|
|
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_updates_a_model(members, m):
|
|
|
|
member1, member2, member3 = members
|
|
|
|
await member1.update(last_name="Smith")
|
|
|
|
member = await m.Member.get(member1.pk)
|
|
|
|
assert member.last_name == "Smith"
|