2021-10-22 17:31:08 +02:00
|
|
|
import abc
|
|
|
|
import datetime
|
|
|
|
from collections import namedtuple
|
|
|
|
|
|
|
|
import pytest
|
2022-05-01 16:03:05 +02:00
|
|
|
import pytest_asyncio
|
2021-10-22 17:31:08 +02:00
|
|
|
from pydantic import EmailStr, ValidationError
|
|
|
|
|
2021-11-10 00:59:10 +01:00
|
|
|
from aredis_om import Field, HashModel, Migrator
|
2021-10-22 17:31:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
today = datetime.date.today()
|
|
|
|
|
|
|
|
|
2022-05-01 16:03:05 +02:00
|
|
|
@pytest_asyncio.fixture
|
2021-11-10 00:59:10 +01:00
|
|
|
async def m(key_prefix, redis):
|
2021-10-22 17:31:08 +02:00
|
|
|
class BaseHashModel(HashModel, abc.ABC):
|
|
|
|
class Meta:
|
|
|
|
global_key_prefix = key_prefix
|
|
|
|
|
|
|
|
class Member(BaseHashModel):
|
|
|
|
first_name: str
|
|
|
|
last_name: str
|
|
|
|
email: EmailStr = Field(index=True)
|
|
|
|
join_date: datetime.date
|
|
|
|
age: int
|
|
|
|
|
2021-11-25 03:12:27 +01:00
|
|
|
await Migrator().run()
|
2021-10-22 17:31:08 +02:00
|
|
|
|
|
|
|
return namedtuple("Models", ["Member"])(Member)
|
|
|
|
|
|
|
|
|
|
|
|
def test_email_str(m):
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
m.Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="not an email!",
|
|
|
|
age=38,
|
|
|
|
join_date=today,
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValidationError):
|
|
|
|
m.Member(
|
|
|
|
first_name="Andrew",
|
|
|
|
last_name="Brookins",
|
|
|
|
email="andrew@bad-domain",
|
|
|
|
age=38,
|
|
|
|
join_date=today,
|
2021-10-22 18:19:06 +02:00
|
|
|
)
|