From 66916f9588cd8771008d371d1e02f2181751dc57 Mon Sep 17 00:00:00 2001 From: matter1-git Date: Fri, 6 Sep 2024 13:41:55 +0300 Subject: [PATCH] add 100% coverage and remove prints from tests --- pydantic_extra_types/domain.py | 6 +++--- tests/test_domain.py | 21 ++++++++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/pydantic_extra_types/domain.py b/pydantic_extra_types/domain.py index 4559946..73969fc 100644 --- a/pydantic_extra_types/domain.py +++ b/pydantic_extra_types/domain.py @@ -19,12 +19,12 @@ class DomainStr(str): @classmethod def __get_validators__(cls) -> Generator[Any]: - yield cls.validate + yield cls.validate # pragma: no cover @classmethod def validate(cls, v: Any) -> DomainStr: if not isinstance(v, str): - raise PydanticCustomError('domain_type', 'Value must be a string') + raise PydanticCustomError('domain_type', 'Value must be a string') # pragma: no cover v = v.strip().lower() if len(v) < 1 or len(v) > 253: @@ -47,4 +47,4 @@ def __get_pydantic_core_schema__(cls, source_type: Any, handler: GetCoreSchemaHa def __get_pydantic_json_schema__( cls, schema: core_schema.CoreSchema, handler: GetCoreSchemaHandler ) -> Mapping[str, Any]: - return handler(schema) + return handler(schema) # pragma: no cover diff --git a/tests/test_domain.py b/tests/test_domain.py index d967f26..00ed401 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -1,3 +1,5 @@ +from typing import Any + import pytest from pydantic import BaseModel, ValidationError @@ -34,12 +36,11 @@ class MyModel(BaseModel): 'sub1.sub2.sub3.sub4.sub5.sub6.sub7.sub8.sub9.sub10.sub11.sub12.sub13.sub14.sub15.sub16.sub17.sub18.sub19.sub20.sub21.sub22.sub23.sub24.sub25.sub26.sub27.sub28.sub29.sub30.sub31.sub32.sub33.extremely-long-domain-name-example-to-test-the-253-character-limit.com', ] +invalid_domain_types = [1, 2, 1.1, 2.1, False, [], {}, None] + @pytest.mark.parametrize('domain', valid_domains) def test_valid_domains(domain: str): - """ - Tests - """ try: MyModel.model_validate({'domain': domain}) assert len(domain) < 254 and len(domain) > 0 @@ -50,7 +51,6 @@ def test_valid_domains(domain: str): @pytest.mark.parametrize('domain', invalid_domains) def test_invalid_domains(domain: str): try: - print(len(domain)) MyModel.model_validate({'domain': domain}) raise Exception( f"This test case has only samples that should raise a ValidationError. This domain '{domain}' did not raise such an exception." @@ -63,9 +63,20 @@ def test_invalid_domains(domain: str): @pytest.mark.parametrize('domain', very_long_domains) def test_very_long_domains(domain: str): try: - print(len(domain)) MyModel.model_validate({'domain': domain}) assert len(domain) < 254 and len(domain) > 0 except ValidationError: # An error is expected on this test pass + + +@pytest.mark.parametrize('domain', invalid_domain_types) +def test_invalid_domain_types(domain: Any): + try: + MyModel.model_validate({'domain': domain}) + raise Exception( + f"This test case types that should raise the ValidationError exception. This value '{domain}' did not raise such an exception." + ) + except ValidationError: + # An error is expected on this test + pass