Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jag-k committed Mar 25, 2024
1 parent 376b406 commit ba6a845
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/test_types.py
Expand Up @@ -3936,6 +3936,39 @@ class Foobar(BaseModel):
}


@pytest.mark.parametrize(
'use_compiled_pattern',
[True, False],
)
def test_pattern_field(use_compiled_pattern):
pattern_value = r'^whatev.r\d$'

field_pattern = pattern_value
if use_compiled_pattern:
field_pattern = re.compile(pattern_value)

class Foobar(BaseModel):
a: str = Field(..., pattern=field_pattern)

matching_value = 'whatever1'
f = Foobar(a=matching_value)
assert f.a == matching_value

# Check that pre-compiled patterns are accepted unchanged
with pytest.raises(
ValidationError,
match=re.escape(r"String should match pattern '^whatev.r\d$'."),
):
Foobar(a=' whatever1')

assert Foobar.model_json_schema() == {
'type': 'object',
'title': 'Foobar',
'properties': {'a': {'pattern': pattern_value, 'title': 'A', 'type': 'string'}},
'required': ['a'],
}


def test_pattern_with_invalid_param():
with pytest.raises(
PydanticSchemaGenerationError,
Expand Down

0 comments on commit ba6a845

Please sign in to comment.