@@ -68,130 +68,6 @@ def test_field_priority_model():
68
68
assert v .isinstance_python ({'f' : 'test long' }) is False
69
69
70
70
71
- def test_parent_priority ():
72
- v = SchemaValidator (
73
- {
74
- 'type' : 'model' ,
75
- 'cls' : MyModel ,
76
- 'schema' : {'type' : 'model-fields' , 'fields' : {'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' }}}},
77
- 'config' : {'str_min_length' : 2 , 'str_max_length' : 10 },
78
- },
79
- {'str_max_length' : 5 , 'config_choose_priority' : 1 },
80
- )
81
- r = plain_repr (v )
82
- assert 'min_length:Some(2)' not in r # child is ignored
83
- assert 'max_length:Some(5)' in r
84
- assert v .isinstance_python ({'f' : 'test' }) is True
85
- assert v .isinstance_python ({'f' : 't' }) is True
86
- assert v .isinstance_python ({'f' : 'test long' }) is False
87
-
88
-
89
- def test_child_priority ():
90
- v = SchemaValidator (
91
- {
92
- 'type' : 'model' ,
93
- 'cls' : MyModel ,
94
- 'schema' : {'type' : 'model-fields' , 'fields' : {'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' }}}},
95
- 'config' : {'str_max_length' : 5 , 'config_choose_priority' : 1 },
96
- },
97
- {'str_min_length' : 2 , 'str_max_length' : 10 },
98
- )
99
- r = plain_repr (v )
100
- assert 'min_length:Some(2)' not in r # parent is ignored
101
- assert 'max_length:Some(5)' in r
102
- assert v .isinstance_python ({'f' : 'test' }) is True
103
- assert v .isinstance_python ({'f' : 't' }) is True
104
- assert v .isinstance_python ({'f' : 'test long' }) is False
105
-
106
-
107
- def test_merge_child_wins ():
108
- v = SchemaValidator (
109
- {
110
- 'type' : 'model' ,
111
- 'cls' : MyModel ,
112
- 'schema' : {'type' : 'model-fields' , 'fields' : {'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' }}}},
113
- 'config' : {'str_max_length' : 5 },
114
- },
115
- {'str_min_length' : 2 , 'str_max_length' : 10 },
116
- )
117
- r = plain_repr (v )
118
- assert 'min_length:Some(2)' in r
119
- assert 'max_length:Some(5)' in r
120
- assert v .isinstance_python ({'f' : 'test' }) is True
121
- assert v .isinstance_python ({'f' : 't' }) is False
122
- assert v .isinstance_python ({'f' : 'test long' }) is False
123
-
124
-
125
- def test_merge_parent_wins ():
126
- v = SchemaValidator (
127
- {
128
- 'type' : 'model' ,
129
- 'cls' : MyModel ,
130
- 'schema' : {'type' : 'model-fields' , 'fields' : {'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' }}}},
131
- 'config' : {'str_max_length' : 5 },
132
- },
133
- {'str_min_length' : 2 , 'str_max_length' : 10 , 'config_merge_priority' : 1 },
134
- )
135
- r = plain_repr (v )
136
- assert 'min_length:Some(2)' in r
137
- assert 'max_length:Some(10)' in r
138
- assert 'max_length:Some(5)' not in r
139
- assert v .isinstance_python ({'f' : 'test' }) is True
140
- assert v .isinstance_python ({'f' : 't' }) is False
141
- assert v .isinstance_python ({'f' : 'test long' }) is True
142
- assert v .isinstance_python ({'f' : 'test very long' }) is False
143
-
144
-
145
- def test_sub_model_merge ():
146
- v = SchemaValidator (
147
- {
148
- 'type' : 'model' ,
149
- 'cls' : MyModel ,
150
- 'schema' : {
151
- 'type' : 'model-fields' ,
152
- 'fields' : {
153
- 'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' }},
154
- 'sub_model' : {
155
- 'type' : 'model-field' ,
156
- 'schema' : {
157
- 'type' : 'model' ,
158
- 'cls' : MyModel ,
159
- 'schema' : {
160
- 'type' : 'model-fields' ,
161
- 'fields' : {'f' : {'type' : 'model-field' , 'schema' : {'type' : 'str' }}},
162
- },
163
- 'config' : {'str_max_length' : 6 , 'str_to_upper' : True },
164
- },
165
- },
166
- },
167
- },
168
- 'config' : {'str_min_length' : 1 , 'str_max_length' : 4 },
169
- }
170
- )
171
- # f should have bounds 1-4
172
- # sub_model.f should have bounds 1-6, and should be upper-cased
173
- output = v .validate_python ({'f' : 'test' , 'sub_model' : {'f' : 'tests' }})
174
- assert output == IsInstance (MyModel ) & HasAttributes (f = 'test' , sub_model = HasAttributes (f = 'TESTS' ))
175
- with pytest .raises (ValidationError ) as exc_info :
176
- v .validate_python ({'f' : 'tests' , 'sub_model' : {'f' : '' }})
177
- assert exc_info .value .errors (include_url = False ) == [
178
- {
179
- 'type' : 'string_too_long' ,
180
- 'loc' : ('f' ,),
181
- 'msg' : 'String should have at most 4 characters' ,
182
- 'input' : 'tests' ,
183
- 'ctx' : {'max_length' : 4 },
184
- },
185
- {
186
- 'type' : 'string_too_short' ,
187
- 'loc' : ('sub_model' , 'f' ),
188
- 'msg' : 'String should have at least 1 characters' ,
189
- 'input' : '' ,
190
- 'ctx' : {'min_length' : 1 },
191
- },
192
- ]
193
-
194
-
195
71
@pytest .mark .parametrize (
196
72
'config,float_field_schema,input_value,expected' ,
197
73
[
0 commit comments