From 8d3a9c86e75249add75f17c32e748533892609f9 Mon Sep 17 00:00:00 2001 From: collerek Date: Mon, 29 Apr 2024 11:25:33 +0200 Subject: [PATCH] check for fields overwriting with email validator --- poetry.lock | 39 ++++++++++++++++++- pyproject.toml | 1 + .../test_overwriting_pydantic_field_type.py | 34 ++++++++++++---- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/poetry.lock b/poetry.lock index 76b66e3..494e2ab 100644 --- a/poetry.lock +++ b/poetry.lock @@ -658,6 +658,43 @@ files = [ {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]] name = "exceptiongroup" version = "1.2.0" @@ -2704,4 +2741,4 @@ sqlite = ["aiosqlite"] [metadata] lock-version = "2.0" python-versions = "^3.8.0" -content-hash = "69ac3f442f88e777aeb77154e45fdd3d000cf3eedcfaca1d6b82e2fd568ceb44" +content-hash = "5c654875bb524c072c614ecb0661400c5cf99b6980fdc53adeb392ad957d5cc3" diff --git a/pyproject.toml b/pyproject.toml index b958633..27d5a96 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -129,6 +129,7 @@ watchdog = "<4.0.0" pytest-codspeed = "^2.2.0" mike = "^2.0.0" faker = "^24.3.0" +email-validator = "^2.1.1" [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/tests/test_model_definition/test_overwriting_pydantic_field_type.py b/tests/test_model_definition/test_overwriting_pydantic_field_type.py index d2d4fb0..601b9ff 100644 --- a/tests/test_model_definition/test_overwriting_pydantic_field_type.py +++ b/tests/test_model_definition/test_overwriting_pydantic_field_type.py @@ -1,6 +1,7 @@ from typing import Dict, Optional import ormar +import pydantic import pytest from pydantic import Json, PositiveInt, ValidationError @@ -20,20 +21,35 @@ class OverwriteTest(ormar.Model): ) # 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) def test_constraints(): - with pytest.raises(ValidationError) as e: + with pytest.raises(ValidationError, match="Input should be greater than 0"): 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"}) - assert ( - "Input should be a valid integer, unable to parse string as an integer" - in str(e.value) - ) + + with pytest.raises( + ValidationError, + match="The email address is not valid. It must have exactly one @-sign", + ): + User(email="wrong") @pytest.mark.asyncio @@ -44,3 +60,7 @@ async def test_saving(): test = await OverwriteTest.objects.get() assert test.my_int == 5 assert test.constraint_dict == {"aa": 123} + + await User(email="test@as.eu").save() + test = await User.objects.get() + assert test.email == "test@as.eu"