@@ -167,3 +167,94 @@ def test_decode_optional_simple_class() -> None:
167
167
)
168
168
def test_decode_complex_class_with_encoding (expr : Any , type : Any , expected : str ) -> None :
169
169
assert from_json (expr , type ) == expected
170
+
171
+
172
+ @dataclass
173
+ class SimpleItemWithOptionalFields :
174
+ first : int
175
+ second : bool = True
176
+ third : Optional [str ] = None
177
+ forth : Optional [float ] = None
178
+
179
+
180
+ @pytest .mark .parametrize (
181
+ ("expr" , "type" , "expected" ),
182
+ [
183
+ ('{"first": 1}' , SimpleItemWithOptionalFields , SimpleItemWithOptionalFields (first = 1 )),
184
+ (
185
+ '{"first": 1, "third": "Hello"}' ,
186
+ SimpleItemWithOptionalFields ,
187
+ SimpleItemWithOptionalFields (first = 1 , third = "Hello" ),
188
+ ),
189
+ ('{"first": 1, "forth": 1.0}' , SimpleItemWithOptionalFields , SimpleItemWithOptionalFields (first = 1 , forth = 1.0 )),
190
+ ],
191
+ )
192
+ def test_decode_simple_item_with_optional_field (expr : Any , type : Any , expected : str ) -> None :
193
+ assert from_json (expr , type ) == expected
194
+
195
+
196
+ @dataclass
197
+ class SimpleItem1 :
198
+ d : int
199
+ e : int
200
+ f : int = 1
201
+
202
+
203
+ @dataclass
204
+ class ComplexItemWithUnionType :
205
+ a_union_field : Union [SimpleItem , SimpleItem1 ]
206
+
207
+
208
+ @pytest .mark .parametrize (
209
+ ("expr" , "type" , "expected" ),
210
+ [
211
+ ('{"a_union_field":{"a":1, "b":2}}' , ComplexItemWithUnionType , ComplexItemWithUnionType (SimpleItem (1 , 2 ))),
212
+ ('{"a_union_field":{"d":1, "e":2}}' , ComplexItemWithUnionType , ComplexItemWithUnionType (SimpleItem1 (1 , 2 ))),
213
+ (
214
+ '{"a_union_field":{"d":1, "e":2, "f": 3}}' ,
215
+ ComplexItemWithUnionType ,
216
+ ComplexItemWithUnionType (SimpleItem1 (1 , 2 , 3 )),
217
+ ),
218
+ ],
219
+ )
220
+ def test_decode_with_union_and_different_keys (expr : Any , type : Any , expected : str ) -> None :
221
+ assert from_json (expr , type ) == expected
222
+
223
+
224
+ @dataclass
225
+ class SimpleItem2 :
226
+ a : int
227
+ b : int
228
+ c : int = 1
229
+
230
+
231
+ @dataclass
232
+ class ComplexItemWithUnionTypeWithSameProperties :
233
+ a_union_field : Union [SimpleItem , SimpleItem2 ]
234
+
235
+
236
+ def test_decode_with_union_and_some_same_keys () -> None :
237
+ assert from_json (
238
+ '{"a_union_field": {"a": 1, "b":2, "c":3}}' , ComplexItemWithUnionTypeWithSameProperties
239
+ ) == ComplexItemWithUnionTypeWithSameProperties (SimpleItem2 (1 , 2 , 3 ))
240
+
241
+
242
+ def test_decode_with_union_and_same_keys_should_raise_typeerror () -> None :
243
+ with pytest .raises (TypeError ):
244
+ from_json (
245
+ '{"a_union_field": {"a": 1, "b":2}}' , ComplexItemWithUnionTypeWithSameProperties
246
+ ) == ComplexItemWithUnionTypeWithSameProperties (SimpleItem2 (1 , 2 , 3 ))
247
+
248
+
249
+ def test_decode_with_union_and_no_keys_should_raise_typeerror () -> None :
250
+ with pytest .raises (TypeError ):
251
+ from_json (
252
+ '{"a_union_field": {}}' , ComplexItemWithUnionTypeWithSameProperties
253
+ ) == ComplexItemWithUnionTypeWithSameProperties (SimpleItem2 (1 , 2 , 3 ))
254
+
255
+
256
+ def test_decode_with_union_and_no_match_should_raise_typeerror () -> None :
257
+ with pytest .raises (TypeError ):
258
+ from_json (
259
+ '{"a_union_field": {"x": 1, "y":2}}' , ComplexItemWithUnionTypeWithSameProperties
260
+ ) == ComplexItemWithUnionTypeWithSameProperties (SimpleItem2 (1 , 2 , 3 ))
0 commit comments