Skip to content

Commit

Permalink
add 100% coverage and remove prints from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matter1-git committed Sep 6, 2024
1 parent 7274819 commit 66916f9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
6 changes: 3 additions & 3 deletions pydantic_extra_types/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
21 changes: 16 additions & 5 deletions tests/test_domain.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

import pytest
from pydantic import BaseModel, ValidationError

Expand Down Expand Up @@ -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
Expand All @@ -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."
Expand All @@ -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

0 comments on commit 66916f9

Please sign in to comment.