Final docs push

This commit is contained in:
Andrew Brookins 2021-11-24 18:12:27 -08:00
parent 321b356140
commit 269d44c26e
13 changed files with 442 additions and 48 deletions

View file

@ -1,8 +1,9 @@
import abc
import dataclasses
import datetime
import decimal
from collections import namedtuple
from typing import Optional
from typing import Optional, Dict, Set, List
from unittest import mock
import pytest
@ -49,7 +50,7 @@ async def m(key_prefix, redis):
model_key_prefix = "member"
primary_key_pattern = ""
await Migrator(redis).run()
await Migrator().run()
return namedtuple("Models", ["BaseHashModel", "Order", "Member"])(
BaseHashModel, Order, Member
@ -365,6 +366,34 @@ def test_raises_error_with_embedded_models(m):
address: Address
def test_raises_error_with_dataclasses(m):
@dataclasses.dataclass
class Address:
address_line_1: str
with pytest.raises(RedisModelError):
class InvalidMember(m.BaseHashModel):
address: Address
def test_raises_error_with_dicts(m):
with pytest.raises(RedisModelError):
class InvalidMember(m.BaseHashModel):
address: Dict[str, str]
def test_raises_error_with_sets(m):
with pytest.raises(RedisModelError):
class InvalidMember(m.BaseHashModel):
friend_ids: Set[str]
def test_raises_error_with_lists(m):
with pytest.raises(RedisModelError):
class InvalidMember(m.BaseHashModel):
friend_ids: List[str]
@pytest.mark.asyncio
async def test_saves_many(m):
member1 = m.Member(

View file

@ -1,8 +1,9 @@
import abc
import dataclasses
import datetime
import decimal
from collections import namedtuple
from typing import List, Optional
from typing import List, Optional, Set, Dict
from unittest import mock
import pytest
@ -73,7 +74,7 @@ async def m(key_prefix, redis):
# Creates an embedded list of models.
orders: Optional[List[Order]]
await Migrator(redis).run()
await Migrator().run()
return namedtuple(
"Models", ["BaseJsonModel", "Note", "Address", "Item", "Order", "Member"]
@ -166,7 +167,7 @@ async def test_validation_passes(address, m):
@pytest.mark.asyncio
async def test_saves_model_and_creates_pk(address, m, redis):
await Migrator(redis).run()
await Migrator().run()
member = m.Member(
first_name="Andrew",
@ -650,7 +651,7 @@ async def test_list_field_limitations(m, redis):
# We need to import and run this manually because we defined
# our model classes within a function that runs after the test
# suite's migrator has already looked for migrations to run.
await Migrator(redis).run()
await Migrator().run()
witch = TarotWitch(tarot_cards=["death"])
await witch.save()
@ -658,6 +659,63 @@ async def test_list_field_limitations(m, redis):
assert actual == [witch]
@pytest.mark.asyncio
async def test_allows_dataclasses(m):
@dataclasses.dataclass
class Address:
address_line_1: str
class ValidMember(m.BaseJsonModel):
address: Address
address = Address(address_line_1="hey")
member = ValidMember(address=address)
await member.save()
member2 = await ValidMember.get(member.pk)
assert member2 == member
assert member2.address.address_line_1 == "hey"
@pytest.mark.asyncio
async def test_allows_and_serializes_dicts(m):
class ValidMember(m.BaseJsonModel):
address: Dict[str, str]
member = ValidMember(address={"address_line_1": "hey"})
await member.save()
member2 = await ValidMember.get(member.pk)
assert member2 == member
assert member2.address['address_line_1'] == "hey"
@pytest.mark.asyncio
async def test_allows_and_serializes_sets(m):
class ValidMember(m.BaseJsonModel):
friend_ids: Set[int]
member = ValidMember(friend_ids={1, 2})
await member.save()
member2 = await ValidMember.get(member.pk)
assert member2 == member
assert member2.friend_ids == {1, 2}
@pytest.mark.asyncio
async def test_allows_and_serializes_lists(m):
class ValidMember(m.BaseJsonModel):
friend_ids: List[int]
member = ValidMember(friend_ids=[1, 2])
await member.save()
member2 = await ValidMember.get(member.pk)
assert member2 == member
assert member2.friend_ids == [1, 2]
@pytest.mark.asyncio
async def test_schema(m, key_prefix):
# We need to build the key prefix because it will differ based on whether

View file

@ -35,7 +35,7 @@ async def m(key_prefix, redis):
model_key_prefix = "member"
primary_key_pattern = ""
await Migrator(redis).run()
await Migrator().run()
return namedtuple("Models", ["BaseHashModel", "Order", "Member"])(
BaseHashModel, Order, Member

View file

@ -24,7 +24,7 @@ async def m(key_prefix, redis):
join_date: datetime.date
age: int
await Migrator(redis).run()
await Migrator().run()
return namedtuple("Models", ["Member"])(Member)