check for fields overwriting with email validator

This commit is contained in:
collerek
2024-04-29 11:25:33 +02:00
parent ae8bc85efe
commit 8d3a9c86e7
3 changed files with 66 additions and 8 deletions

39
poetry.lock generated
View File

@ -658,6 +658,43 @@ files = [
{file = "distlib-0.3.8.tar.gz", hash = "sha256:1530ea13e350031b6312d8580ddb6b27a104275a31106523b8f123787f494f64"}, {file = "distlib-0.3.8.tar.gz", hash = "sha256:1530ea13e350031b6312d8580ddb6b27a104275a31106523b8f123787f494f64"},
] ]
[[package]]
name = "dnspython"
version = "2.6.1"
description = "DNS toolkit"
category = "dev"
optional = false
python-versions = ">=3.8"
files = [
{file = "dnspython-2.6.1-py3-none-any.whl", hash = "sha256:5ef3b9680161f6fa89daf8ad451b5f1a33b18ae8a1c6778cdf4b43f08c0a6e50"},
{file = "dnspython-2.6.1.tar.gz", hash = "sha256:e8f0f9c23a7b7cb99ded64e6c3a6f3e701d78f50c55e002b839dea7225cff7cc"},
]
[package.extras]
dev = ["black (>=23.1.0)", "coverage (>=7.0)", "flake8 (>=7)", "mypy (>=1.8)", "pylint (>=3)", "pytest (>=7.4)", "pytest-cov (>=4.1.0)", "sphinx (>=7.2.0)", "twine (>=4.0.0)", "wheel (>=0.42.0)"]
dnssec = ["cryptography (>=41)"]
doh = ["h2 (>=4.1.0)", "httpcore (>=1.0.0)", "httpx (>=0.26.0)"]
doq = ["aioquic (>=0.9.25)"]
idna = ["idna (>=3.6)"]
trio = ["trio (>=0.23)"]
wmi = ["wmi (>=1.5.1)"]
[[package]]
name = "email-validator"
version = "2.1.1"
description = "A robust email address syntax and deliverability validation library."
category = "dev"
optional = false
python-versions = ">=3.8"
files = [
{file = "email_validator-2.1.1-py3-none-any.whl", hash = "sha256:97d882d174e2a65732fb43bfce81a3a834cbc1bde8bf419e30ef5ea976370a05"},
{file = "email_validator-2.1.1.tar.gz", hash = "sha256:200a70680ba08904be6d1eef729205cc0d687634399a5924d842533efb824b84"},
]
[package.dependencies]
dnspython = ">=2.0.0"
idna = ">=2.0.0"
[[package]] [[package]]
name = "exceptiongroup" name = "exceptiongroup"
version = "1.2.0" version = "1.2.0"
@ -2704,4 +2741,4 @@ sqlite = ["aiosqlite"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = "^3.8.0" python-versions = "^3.8.0"
content-hash = "69ac3f442f88e777aeb77154e45fdd3d000cf3eedcfaca1d6b82e2fd568ceb44" content-hash = "5c654875bb524c072c614ecb0661400c5cf99b6980fdc53adeb392ad957d5cc3"

View File

@ -129,6 +129,7 @@ watchdog = "<4.0.0"
pytest-codspeed = "^2.2.0" pytest-codspeed = "^2.2.0"
mike = "^2.0.0" mike = "^2.0.0"
faker = "^24.3.0" faker = "^24.3.0"
email-validator = "^2.1.1"
[build-system] [build-system]
requires = ["poetry-core>=1.0.0"] requires = ["poetry-core>=1.0.0"]

View File

@ -1,6 +1,7 @@
from typing import Dict, Optional from typing import Dict, Optional
import ormar import ormar
import pydantic
import pytest import pytest
from pydantic import Json, PositiveInt, ValidationError from pydantic import Json, PositiveInt, ValidationError
@ -20,20 +21,35 @@ class OverwriteTest(ormar.Model):
) # type: ignore ) # type: ignore
class User(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="users")
id: int = ormar.Integer(primary_key=True)
email: str = ormar.String(
max_length=255,
unique=True,
nullable=False,
overwrite_pydantic_type=pydantic.EmailStr,
)
create_test_database = init_tests(base_ormar_config) create_test_database = init_tests(base_ormar_config)
def test_constraints(): def test_constraints():
with pytest.raises(ValidationError) as e: with pytest.raises(ValidationError, match="Input should be greater than 0"):
OverwriteTest(my_int=-10) OverwriteTest(my_int=-10)
assert "Input should be greater than 0" in str(e.value)
with pytest.raises(ValidationError) as e: with pytest.raises(
ValidationError,
match="Input should be a valid integer, unable to parse string as an integer",
):
OverwriteTest(my_int=10, constraint_dict={"aa": "ab"}) OverwriteTest(my_int=10, constraint_dict={"aa": "ab"})
assert (
"Input should be a valid integer, unable to parse string as an integer" with pytest.raises(
in str(e.value) ValidationError,
) match="The email address is not valid. It must have exactly one @-sign",
):
User(email="wrong")
@pytest.mark.asyncio @pytest.mark.asyncio
@ -44,3 +60,7 @@ async def test_saving():
test = await OverwriteTest.objects.get() test = await OverwriteTest.objects.get()
assert test.my_int == 5 assert test.my_int == 5
assert test.constraint_dict == {"aa": 123} assert test.constraint_dict == {"aa": 123}
await User(email="test@as.eu").save()
test = await User.objects.get()
assert test.email == "test@as.eu"