|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from pydantic_core import ArgsKwargs, SchemaValidator, ValidationError |
| 8 | +from pydantic_core import core_schema as cs |
| 9 | + |
| 10 | +from ...conftest import Err, PyAndJson |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.parametrize( |
| 14 | + ['input_value', 'expected'], |
| 15 | + ( |
| 16 | + [ArgsKwargs((1,)), ((1,), {})], |
| 17 | + [ArgsKwargs((), {'Foo': 1}), ((), {'a': 1})], |
| 18 | + [ArgsKwargs((), {'a': 1}), Err('Foo\n Missing required argument [type=missing_argument,')], |
| 19 | + [{'Foo': 1}, ((1,), {})], |
| 20 | + [{'a': 1}, Err('Foo\n Missing required argument [type=missing_argument,')], |
| 21 | + ), |
| 22 | + ids=repr, |
| 23 | +) |
| 24 | +def test_alias(py_and_json: PyAndJson, input_value, expected) -> None: |
| 25 | + v = py_and_json( |
| 26 | + cs.arguments_v3_schema( |
| 27 | + [ |
| 28 | + cs.arguments_v3_parameter(name='a', schema=cs.int_schema(), alias='Foo', mode='positional_or_keyword'), |
| 29 | + ] |
| 30 | + ) |
| 31 | + ) |
| 32 | + if isinstance(expected, Err): |
| 33 | + with pytest.raises(ValidationError, match=re.escape(expected.message)): |
| 34 | + v.validate_test(input_value) |
| 35 | + else: |
| 36 | + assert v.validate_test(input_value) == expected |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + ['input_value', 'expected'], |
| 41 | + ( |
| 42 | + [ArgsKwargs((1,)), ((1,), {})], |
| 43 | + [ArgsKwargs((), {'Foo': 1}), ((), {'a': 1})], |
| 44 | + [ArgsKwargs((), {'a': 1}), ((), {'a': 1})], |
| 45 | + [ArgsKwargs((), {'a': 1, 'b': 2}), Err('b\n Unexpected keyword argument [type=unexpected_keyword_argument,')], |
| 46 | + [ |
| 47 | + ArgsKwargs((), {'a': 1, 'Foo': 2}), |
| 48 | + Err('a\n Unexpected keyword argument [type=unexpected_keyword_argument,'), |
| 49 | + ], |
| 50 | + [{'Foo': 1}, ((1,), {})], |
| 51 | + [{'a': 1}, ((1,), {})], |
| 52 | + ), |
| 53 | + ids=repr, |
| 54 | +) |
| 55 | +def test_alias_validate_by_name(py_and_json: PyAndJson, input_value, expected): |
| 56 | + v = py_and_json( |
| 57 | + cs.arguments_v3_schema( |
| 58 | + [ |
| 59 | + cs.arguments_v3_parameter(name='a', schema=cs.int_schema(), alias='Foo', mode='positional_or_keyword'), |
| 60 | + ], |
| 61 | + validate_by_name=True, |
| 62 | + ) |
| 63 | + ) |
| 64 | + if isinstance(expected, Err): |
| 65 | + with pytest.raises(ValidationError, match=re.escape(expected.message)): |
| 66 | + v.validate_test(input_value) |
| 67 | + else: |
| 68 | + assert v.validate_test(input_value) == expected |
| 69 | + |
| 70 | + |
| 71 | +def test_only_validate_by_name(py_and_json) -> None: |
| 72 | + v = py_and_json( |
| 73 | + cs.arguments_v3_schema( |
| 74 | + [ |
| 75 | + cs.arguments_v3_parameter( |
| 76 | + name='a', schema=cs.str_schema(), alias='FieldA', mode='positional_or_keyword' |
| 77 | + ), |
| 78 | + ], |
| 79 | + validate_by_name=True, |
| 80 | + validate_by_alias=False, |
| 81 | + ) |
| 82 | + ) |
| 83 | + |
| 84 | + assert v.validate_test(ArgsKwargs((), {'a': 'hello'})) == ((), {'a': 'hello'}) |
| 85 | + assert v.validate_test({'a': 'hello'}) == (('hello',), {}) |
| 86 | + |
| 87 | + with pytest.raises(ValidationError, match=r'a\n +Missing required argument \[type=missing_argument,'): |
| 88 | + assert v.validate_test(ArgsKwargs((), {'FieldA': 'hello'})) |
| 89 | + with pytest.raises(ValidationError, match=r'a\n +Missing required argument \[type=missing_argument,'): |
| 90 | + assert v.validate_test({'FieldA': 'hello'}) |
| 91 | + |
| 92 | + |
| 93 | +def test_only_allow_alias(py_and_json) -> None: |
| 94 | + v = py_and_json( |
| 95 | + cs.arguments_v3_schema( |
| 96 | + [ |
| 97 | + cs.arguments_v3_parameter( |
| 98 | + name='a', schema=cs.str_schema(), alias='FieldA', mode='positional_or_keyword' |
| 99 | + ), |
| 100 | + ], |
| 101 | + validate_by_name=False, |
| 102 | + validate_by_alias=True, |
| 103 | + ) |
| 104 | + ) |
| 105 | + assert v.validate_test(ArgsKwargs((), {'FieldA': 'hello'})) == ((), {'a': 'hello'}) |
| 106 | + assert v.validate_test({'FieldA': 'hello'}) == (('hello',), {}) |
| 107 | + |
| 108 | + with pytest.raises(ValidationError, match=r'FieldA\n +Missing required argument \[type=missing_argument,'): |
| 109 | + assert v.validate_test(ArgsKwargs((), {'a': 'hello'})) |
| 110 | + with pytest.raises(ValidationError, match=r'FieldA\n +Missing required argument \[type=missing_argument,'): |
| 111 | + assert v.validate_test({'a': 'hello'}) |
| 112 | + |
| 113 | + |
| 114 | +@pytest.mark.parametrize('config_by_alias', [None, True, False]) |
| 115 | +@pytest.mark.parametrize('config_by_name', [None, True, False]) |
| 116 | +@pytest.mark.parametrize('runtime_by_alias', [None, True, False]) |
| 117 | +@pytest.mark.parametrize('runtime_by_name', [None, True, False]) |
| 118 | +def test_by_alias_and_name_config_interaction( |
| 119 | + config_by_alias: bool | None, |
| 120 | + config_by_name: bool | None, |
| 121 | + runtime_by_alias: bool | None, |
| 122 | + runtime_by_name: bool | None, |
| 123 | +) -> None: |
| 124 | + """This test reflects the priority that applies for config vs runtime validation alias configuration. |
| 125 | +
|
| 126 | + Runtime values take precedence over config values, when set. |
| 127 | + By default, by_alias is True and by_name is False. |
| 128 | + """ |
| 129 | + |
| 130 | + if config_by_alias is False and config_by_name is False and runtime_by_alias is False and runtime_by_name is False: |
| 131 | + pytest.skip("Can't have both by_alias and by_name as effectively False") |
| 132 | + |
| 133 | + schema = cs.arguments_v3_schema( |
| 134 | + arguments=[ |
| 135 | + cs.arguments_v3_parameter(name='my_field', schema=cs.int_schema(), alias='my_alias'), |
| 136 | + ], |
| 137 | + **({'validate_by_alias': config_by_alias} if config_by_alias is not None else {}), |
| 138 | + **({'validate_by_name': config_by_name} if config_by_name is not None else {}), |
| 139 | + ) |
| 140 | + s = SchemaValidator(schema) |
| 141 | + |
| 142 | + alias_allowed = next(x for x in (runtime_by_alias, config_by_alias, True) if x is not None) |
| 143 | + name_allowed = next(x for x in (runtime_by_name, config_by_name, False) if x is not None) |
| 144 | + |
| 145 | + if alias_allowed: |
| 146 | + assert s.validate_python( |
| 147 | + ArgsKwargs((), {'my_alias': 1}), by_alias=runtime_by_alias, by_name=runtime_by_name |
| 148 | + ) == ( |
| 149 | + (), |
| 150 | + {'my_field': 1}, |
| 151 | + ) |
| 152 | + if name_allowed: |
| 153 | + assert s.validate_python( |
| 154 | + ArgsKwargs((), {'my_field': 1}), by_alias=runtime_by_alias, by_name=runtime_by_name |
| 155 | + ) == ( |
| 156 | + (), |
| 157 | + {'my_field': 1}, |
| 158 | + ) |
0 commit comments