Skip to content

Commit

Permalink
Add test for RootModel
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Sep 22, 2023
1 parent e0a4fbf commit 5a5d690
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tests/test_main.py
Expand Up @@ -2577,7 +2577,7 @@ class Model(BaseModel):
with pytest.raises(expected, match=raises_match):
Model.model_validate_strings({'x': input_value}, strict=strict)
else:
Model.model_validate_strings({'x': input_value}, strict=strict).x == expected
assert Model.model_validate_strings({'x': input_value}, strict=strict).x == expected


@pytest.mark.parametrize('strict', [True, False])
Expand Down
36 changes: 36 additions & 0 deletions tests/test_root_model.py
@@ -1,4 +1,5 @@
import pickle
from datetime import date, datetime
from typing import Any, Dict, List, Optional, Union

import pytest
Expand Down Expand Up @@ -616,3 +617,38 @@ def test_copy_preserves_equality():

deepcopied = model.__deepcopy__()
assert model == deepcopied


@pytest.mark.parametrize(
'root_type,input_value,expected,raises_match,strict',
[
(bool, 'true', True, None, False),
(bool, 'true', True, None, True),
(bool, 'false', False, None, False),
(bool, 'e', ValidationError, 'type=bool_parsing', False),
(int, '1', 1, None, False),
(int, '1', 1, None, True),
(int, 'xxx', ValidationError, 'type=int_parsing', True),
(float, '1.1', 1.1, None, False),
(float, '1.10', 1.1, None, False),
(float, '1.1', 1.1, None, True),
(float, '1.10', 1.1, None, True),
(date, '2017-01-01', date(2017, 1, 1), None, False),
(date, '2017-01-01', date(2017, 1, 1), None, True),
(date, '2017-01-01T12:13:14.567', ValidationError, 'type=date_from_datetime_inexact', False),
(date, '2017-01-01T12:13:14.567', ValidationError, 'type=date_parsing', True),
(date, '2017-01-01T00:00:00', date(2017, 1, 1), None, False),
(date, '2017-01-01T00:00:00', ValidationError, 'type=date_parsing', True),
(datetime, '2017-01-01T12:13:14.567', datetime(2017, 1, 1, 12, 13, 14, 567_000), None, False),
(datetime, '2017-01-01T12:13:14.567', datetime(2017, 1, 1, 12, 13, 14, 567_000), None, True),
],
ids=repr,
)
def test_model_validate_strings(root_type, input_value, expected, raises_match, strict):
Model = RootModel[root_type]

if raises_match is not None:
with pytest.raises(expected, match=raises_match):
Model.model_validate_strings(input_value, strict=strict)
else:
assert Model.model_validate_strings(input_value, strict=strict).root == expected
6 changes: 3 additions & 3 deletions tests/test_type_adapter.py
Expand Up @@ -295,12 +295,12 @@ class UnrelatedClass:
ids=repr,
)
def test_validate_strings(field_type, input_value, expected, raises_match, strict):
ta = TypeAdapter(field_type)
if raises_match is not None:
print(TypeAdapter(field_type).core_schema)
with pytest.raises(expected, match=raises_match):
TypeAdapter(field_type).validate_strings(input_value, strict=strict)
ta.validate_strings(input_value, strict=strict)
else:
TypeAdapter(field_type).validate_strings(input_value, strict=strict) == expected
assert ta.validate_strings(input_value, strict=strict) == expected


@pytest.mark.parametrize('strict', [True, False])
Expand Down

0 comments on commit 5a5d690

Please sign in to comment.