redis-om-python/tests/test_pydantic_integrations.py
Manabu Niseki 3a0fa0c7be
Allow using Pydantic v2 (#533)
Co-authored-by: Chayim <chayim@users.noreply.github.com>
2023-07-12 11:48:08 +03:00

51 lines
1.1 KiB
Python

import abc
import datetime
from collections import namedtuple
import pytest
import pytest_asyncio
from aredis_om import Field, HashModel, Migrator
from tests._compat import EmailStr, ValidationError
today = datetime.date.today()
@pytest_asyncio.fixture
async def m(key_prefix, redis):
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
await Migrator().run()
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,
)