-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
Copy pathtest_model.py
609 lines (514 loc) · 26.3 KB
/
test_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
# coding: utf-8
# flake8: noqa
import json
import os
import time
import unittest
import petstore_api
class ModelTests(unittest.TestCase):
def setUp(self):
self.pet = petstore_api.Pet(name="test name", photo_urls=["string"])
self.pet.id = 1
self.pet.status = "available"
cate = petstore_api.Category(name="dog")
cate.id = 1
cate.name = "dog"
self.pet.category = cate
tag = petstore_api.Tag()
tag.id = 1
self.pet.tags = [tag]
def test_cat(self):
self.cat = petstore_api.Cat(class_name="cat")
self.assertEqual("cat", self.cat.class_name)
self.assertEqual("red", self.cat.color)
cat_str = ("{'additional_properties': {},\n"
" 'className': 'cat',\n"
" 'color': 'red',\n"
" 'declawed': None}")
self.assertEqual(cat_str, self.cat.to_str())
def test_to_str(self):
data = ("{'additional_properties': {},\n"
" 'category': {'additional_properties': {}, 'id': 1, 'name': 'dog'},\n"
" 'id': 1,\n"
" 'name': 'test name',\n"
" 'photoUrls': ['string'],\n"
" 'status': 'available',\n"
" 'tags': [{'additional_properties': {}, 'id': 1, 'name': None}]}")
self.assertEqual(data, self.pet.to_str())
def test_equal(self):
self.pet1 = petstore_api.Pet(name="test name", photo_urls=["string"])
self.pet1.id = 1
self.pet1.status = "available"
cate1 = petstore_api.Category(name="dog")
cate1.id = 1
# cate1.name = "dog"
self.pet.category = cate1
tag1 = petstore_api.Tag()
tag1.id = 1
self.pet1.tags = [tag1]
self.pet2 = petstore_api.Pet(name="test name", photo_urls=["string"])
self.pet2.id = 1
self.pet2.status = "available"
cate2 = petstore_api.Category(name="dog")
cate2.id = 1
cate2.name = "dog"
self.pet.category = cate2
tag2 = petstore_api.Tag()
tag2.id = 1
self.pet2.tags = [tag2]
self.assertTrue(self.pet1 == self.pet2)
# reset pet1 tags to empty array so that object comparison returns false
self.pet1.tags = []
self.assertFalse(self.pet1 == self.pet2)
def test_oneOf_array_of_integers(self):
# test new Color
new_color = petstore_api.Color()
self.assertEqual("null", new_color.to_json())
self.assertEqual(None, new_color.actual_instance)
# test the oneof schema validator
json_str = '[12,34,56]'
array_of_integers = json.loads(json_str)
# no error should be thrown
new_color.oneof_schema_1_validator = array_of_integers
new_color.actual_instance = array_of_integers
new_color.actual_instance = None
# test the oneof schema validator with invalid input
json_str = '[12,34,56120938]'
array_of_integers = json.loads(json_str)
try:
new_color.oneof_schema_1_validator = array_of_integers
except ValueError as e:
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
try:
new_color.actual_instance = array_of_integers
except ValueError as e:
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
# test from_josn
json_str = '[12,34,56]'
p = petstore_api.Color.from_json(json_str)
self.assertEqual(p.actual_instance, [12, 34, 56])
try:
p = petstore_api.Color.from_json('[2342112,0,0,0]')
except ValueError as e:
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
# test to_json, to_dict method
json_str = '[12,34,56]'
p = petstore_api.Color.from_json(json_str)
self.assertEqual(p.to_json(), "[12, 34, 56]")
self.assertEqual(p.to_dict(), [12, 34, 56])
# test nullable
p = petstore_api.Color.from_json(None)
self.assertEqual(p.actual_instance, None)
def test_oneof_enum_string(self):
enum_string1 = petstore_api.EnumString1('a')
# test from_json
oneof_enum = petstore_api.OneOfEnumString.from_json('"a"')
# test from_dict
oneof_enum = petstore_api.OneOfEnumString.from_dict("a")
nested = petstore_api.WithNestedOneOf(size = 1, nested_oneof_enum_string = oneof_enum)
# test to_json
self.assertEqual(nested.to_json(), '{"size": 1, "nested_oneof_enum_string": "a"}')
# test from_json
nested = petstore_api.WithNestedOneOf.from_json('{"size": 1, "nested_oneof_enum_string": "c"}')
self.assertEqual(nested.to_json(), '{"size": 1, "nested_oneof_enum_string": "c"}')
# test from_dict
nested = petstore_api.WithNestedOneOf.from_dict({"size": 1, "nested_oneof_enum_string": "c"})
# test to_dict
self.assertEqual(nested.to_dict(), {"size": 1, "nested_oneof_enum_string": "c"})
# invalid enum value
try:
nested2 = petstore_api.WithNestedOneOf.from_json('{"size": 1, "nested_oneof_enum_string": "e"}')
except ValueError as e:
self.assertTrue("'e' is not a valid EnumString1, 'e' is not a valid EnumString" in str(e))
# test the constructor
enum_string1 = petstore_api.EnumString1('a')
constructor1 = petstore_api.OneOfEnumString(actual_instance=enum_string1)
self.assertEqual(constructor1.actual_instance, enum_string1)
constructor2 = petstore_api.OneOfEnumString(enum_string1)
self.assertEqual(constructor2.actual_instance, enum_string1)
constructor3 = petstore_api.OneOfEnumString()
self.assertEqual(constructor3.actual_instance, None)
def test_anyOf_array_of_integers(self):
# test new Color
new_color = petstore_api.AnyOfColor()
self.assertEqual("null", new_color.to_json())
self.assertEqual(None, new_color.actual_instance)
# test the oneof schema validator
json_str = '[12,34,56]'
array_of_integers = json.loads(json_str)
# no error should be thrown
new_color.anyof_schema_1_validator = array_of_integers
new_color.actual_instance = array_of_integers
# test the oneof schema validator with invalid input
json_str = '[12,34,56120938]'
array_of_integers = json.loads(json_str)
try:
new_color.anyof_schema_1_validator = array_of_integers
except ValueError as e:
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
try:
new_color.actual_instance = array_of_integers
except ValueError as e:
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
# test from_json
json_str = '[12,34,56]'
p = petstore_api.AnyOfColor.from_json(json_str)
self.assertEqual(p.actual_instance, [12, 34,56])
try:
p = petstore_api.AnyOfColor.from_json('[2342112,0,0,0]')
except ValueError as e:
self.assertTrue("ensure this value is less than or equal to 255" in str(e))
# test from_json, schema 3
json_str = '"#123456"'
p = petstore_api.AnyOfColor.from_json(json_str)
self.assertIsInstance(p.actual_instance, str)
self.assertEqual(p.actual_instance, '#123456')
# test to_json, schema 3
p = petstore_api.AnyOfColor(actual_instance='#123456')
self.assertEqual(p.to_json(), '"#123456"')
# test from_dict, schema 3
obj = '#123456'
p = petstore_api.AnyOfColor.from_dict(obj)
self.assertIsInstance(p.actual_instance, str)
self.assertEqual(p.actual_instance, '#123456')
# test to_dict, schema 3
p = petstore_api.AnyOfColor(actual_instance='#123456')
self.assertEqual(p.to_dict(), '#123456')
p = petstore_api.AnyOfColor.from_dict(p.to_dict())
self.assertEqual(p.actual_instance, '#123456')
def test_oneOf(self):
# test new Pig
bp = petstore_api.BasquePig.from_dict({"className": "BasquePig", "color": "red"})
new_pig = petstore_api.Pig()
self.assertEqual("null", new_pig.to_json())
self.assertEqual(None, new_pig.actual_instance)
new_pig2 = petstore_api.Pig(actual_instance=bp)
self.assertEqual('{"className": "BasquePig", "color": "red"}', new_pig2.to_json())
new_pig3 = petstore_api.Pig(bp)
self.assertEqual('{"className": "BasquePig", "color": "red"}', new_pig3.to_json())
try:
new_pig4 = petstore_api.Pig(bp, actual_instance=bp)
except ValueError as e:
self.assertTrue("If position argument is used, keyword argument cannot be used.", str(e))
try:
new_pig5 = petstore_api.Pig(bp, bp)
except ValueError as e:
self.assertTrue("If position argument is used, only 1 is allowed to set `actual_instance`", str(e))
# test from_json
json_str = '{"className": "BasquePig", "color": "red"}'
p = petstore_api.Pig.from_json(json_str)
self.assertIsInstance(p.actual_instance, petstore_api.BasquePig)
# test from_dict
json_dict = {"className": "BasquePig", "color": "red"}
p = petstore_api.Pig.from_dict(json_dict)
self.assertIsInstance(p.actual_instance, petstore_api.BasquePig)
# test init
basque_pig = p.actual_instance
pig2 = petstore_api.Pig(actual_instance=basque_pig)
self.assertIsInstance(pig2.actual_instance, petstore_api.BasquePig)
# test failed init
try:
pig3 = petstore_api.Pig(actual_instance="123")
self.assertTrue(False) # this line shouldn't execute
except ValueError as e:
self.assertTrue("No match found when setting `actual_instance` in Pig with oneOf schemas: BasquePig, DanishPig" in str(e))
# failure
try:
p2 = petstore_api.Pig.from_json("1")
self.assertTrue(False) # this line shouldn't execute
except AttributeError as e:
self.assertEqual(str(e), "'int' object has no attribute 'get'")
# comment out below as the error message is different using oneOf discriminator lookup option
#except ValueError as e:
# error_message = (
# "No match found when deserializing the JSON string into Pig with oneOf schemas: BasquePig, DanishPig. "
# "Details: 1 validation error for BasquePig\n"
# "__root__\n"
# " BasquePig expected dict not int (type=type_error), 1 validation error for DanishPig\n"
# "__root__\n"
# " DanishPig expected dict not int (type=type_error)")
# self.assertEqual(str(e), error_message)
# test to_json
self.assertEqual(p.to_json(), '{"className": "BasquePig", "color": "red"}')
# test nested property
nested = petstore_api.WithNestedOneOf(size = 1, nested_pig = p)
self.assertEqual(nested.to_json(), '{"size": 1, "nested_pig": {"className": "BasquePig", "color": "red"}}')
nested_json = nested.to_json()
nested2 = petstore_api.WithNestedOneOf.from_json(nested_json)
self.assertEqual(nested2.to_json(), nested_json)
def test_anyOf(self):
# test new AnyOfPig
new_anypig = petstore_api.AnyOfPig()
self.assertEqual("null", new_anypig.to_json())
self.assertEqual(None, new_anypig.actual_instance)
# test from_json
json_str = '{"className": "BasquePig", "color": "red"}'
p = petstore_api.AnyOfPig.from_json(json_str)
self.assertIsInstance(p.actual_instance, petstore_api.BasquePig)
# test from_dict
json_dict = {"className": "BasquePig", "color": "red"}
p = petstore_api.AnyOfPig.from_dict(json_dict)
self.assertIsInstance(p.actual_instance, petstore_api.BasquePig)
# test init
basque_pig = p.actual_instance
pig2 = petstore_api.AnyOfPig(actual_instance=basque_pig)
self.assertIsInstance(pig2.actual_instance, petstore_api.BasquePig)
# test failed init
try:
pig3 = petstore_api.AnyOfPig(actual_instance="123")
self.assertTrue(False) # this line shouldn't execute
except ValueError as e:
self.assertTrue(
"No match found when setting the actual_instance in AnyOfPig with anyOf schemas: BasquePig, "
"DanishPig" in str(e))
# failure
try:
p2 = petstore_api.AnyOfPig.from_json("1")
self.assertTrue(False) # this line shouldn't execute
except ValueError as e:
error_message = (
"No match found when deserializing the JSON string into AnyOfPig with anyOf schemas: BasquePig, "
"DanishPig. Details: 1 validation error for BasquePig\n"
"__root__\n"
" BasquePig expected dict not int (type=type_error), 1 validation error for DanishPig\n"
"__root__\n"
" DanishPig expected dict not int (type=type_error)")
self.assertEqual(str(e), error_message)
# test to_dict
self.assertEqual(p.to_dict(), {'className': 'BasquePig', 'color': 'red'})
# test to_json
self.assertEqual(p.to_json(), '{"className": "BasquePig", "color": "red"}')
def test_inheritance(self):
dog = petstore_api.Dog(breed="bulldog", className="dog", color="white")
self.assertEqual(dog.to_json(), '{"className": "dog", "color": "white", "breed": "bulldog"}')
self.assertEqual(dog.to_dict(), {'breed': 'bulldog', 'className':
'dog', 'color': 'white'})
dog2 = petstore_api.Dog.from_json(dog.to_json())
self.assertEqual(dog2.breed, 'bulldog')
self.assertEqual(dog2.class_name, "dog")
self.assertEqual(dog2.color, 'white')
def test_list(self):
# should throw exception as var_123_list should be string
try:
l3 = petstore_api.ListClass(var_123_list=123)
self.assertTrue(False) # this line shouldn't execute
except ValueError as e:
#error_message = (
# "1 validation error for List\n"
# "123-list\n"
# " str type expected (type=type_error.str)\n")
self.assertTrue("str type expected" in str(e))
l = petstore_api.ListClass(var_123_list="bulldog")
self.assertEqual(l.to_json(), '{"123-list": "bulldog"}')
self.assertEqual(l.to_dict(), {'123-list': 'bulldog'})
l2 = petstore_api.ListClass.from_json(l.to_json())
self.assertEqual(l2.var_123_list, 'bulldog')
self.assertTrue(isinstance(l2, petstore_api.ListClass))
def test_enum_ref_property(self):
# test enum ref property
# test to_json
d = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1)
self.assertEqual(d.to_json(), '{"value": 1}')
d2 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1, str_value=petstore_api.OuterEnum.DELIVERED)
self.assertEqual(d2.to_json(), '{"str_value": "delivered", "value": 1}')
# test from_json (round trip)
d3 = petstore_api.OuterObjectWithEnumProperty.from_json(d2.to_json())
self.assertEqual(d3.str_value, petstore_api.OuterEnum.DELIVERED)
self.assertEqual(d3.value, petstore_api.OuterEnumInteger.NUMBER_1)
self.assertEqual(d3.to_json(), '{"str_value": "delivered", "value": 1}')
d4 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1, str_value=None)
self.assertEqual(d4.to_json(), '{"value": 1, "str_value": null}')
d5 = petstore_api.OuterObjectWithEnumProperty(value=petstore_api.OuterEnumInteger.NUMBER_1)
self.assertEqual(d5.__fields_set__, {'value'})
d5.str_value = None # set None explicitly
self.assertEqual(d5.__fields_set__, {'value', 'str_value'})
self.assertEqual(d5.to_json(), '{"value": 1, "str_value": null}')
def test_enum_single_members(self):
enum_test = petstore_api.EnumTest(
enum_string_required="lower",
enum_string_single_member="abc",
enum_integer_single_member=100,
)
self.assertEqual(enum_test.enum_string_single_member, "abc")
self.assertEqual(enum_test.enum_integer_single_member, 100)
with self.assertRaises(ValueError) as e:
enum_test = petstore_api.EnumTest(
enum_string_required="lower",
enum_string_single_member="ab",
)
self.assertTrue("must be one of enum values ('abc')" in str(e))
with self.assertRaises(ValueError) as e:
enum_test = petstore_api.EnumTest(
enum_string_required="lower",
enum_integer_single_member=10,
)
self.assertTrue("must be one of enum values (100)" in str(e))
def test_valdiator(self):
# test regular expression
a = petstore_api.FormatTest(number=123.45, byte=bytes("string", 'utf-8'), date="2013-09-17", password="testing09876")
try:
a.pattern_with_digits_and_delimiter = "123"
self.assertTrue(False) # this line shouldn't execute
except ValueError as e:
self.assertTrue(r"must validate the regular expression /^image_\d{1,3}$/i" in str(e))
# test None with optional string (with regualr expression)
a = petstore_api.FormatTest(number=123.45, byte=bytes("string", 'utf-8'), date="2013-09-17", password="testing09876")
a.string = None # shouldn't throw an exception
a.pattern_with_digits_and_delimiter = "IMAGE_123"
self.assertEqual(a.pattern_with_digits_and_delimiter, "IMAGE_123")
a.pattern_with_digits_and_delimiter = "image_123"
self.assertEqual(a.pattern_with_digits_and_delimiter, "image_123")
def test_inline_enum_validator(self):
self.pet = petstore_api.Pet(name="test name", photo_urls=["string"])
self.pet.id = 1
try:
self.pet.status = "error"
self.assertTrue(False) # this line shouldn't execute
except ValueError as e:
self.assertTrue("must be one of enum values ('available', 'pending', 'sold')" in str(e))
def test_object_id(self):
pet_ap = petstore_api.Pet(name="test name", photo_urls=["string"])
pet_ap2 = petstore_api.Pet(name="test name", photo_urls=["string"])
self.assertNotEqual(id(pet_ap), id(pet_ap2))
pet_ap3 = petstore_api.Pet.from_dict(pet_ap.to_dict())
pet_ap4 = petstore_api.Pet.from_dict(pet_ap.to_dict())
self.assertNotEqual(id(pet_ap3), id(pet_ap4))
def test_additional_properties(self):
pet_ap = petstore_api.Pet(name="test name", photo_urls=["string"])
pet_ap.id = 1
pet_ap.status = "available"
pet_ap2 = petstore_api.Pet(name="test name", photo_urls=["string"])
pet_ap2.id = 1
pet_ap2.status = "available"
self.assertNotEqual(id(pet_ap.additional_properties), id(pet_ap2.additional_properties))
pet_ap.additional_properties["something-new"] = "haha"
self.assertEqual(pet_ap.to_json(), '{"id": 1, "name": "test name", "photoUrls": ["string"], "status": "available", "something-new": "haha"}')
self.assertEqual(type(pet_ap2.additional_properties), dict)
self.assertNotEqual(id(pet_ap.additional_properties), id(pet_ap2.additional_properties))
self.assertEqual(pet_ap.additional_properties["something-new"], "haha")
try:
_tmp = pet_ap2.additional_properties["something-new"]
self.assertTrue(False) # this line shouldn't execute
except KeyError as e:
self.assertEqual("'something-new'", str(e))
pet_ap_dict = pet_ap.to_dict()
pet_ap_dict["something-new"] = 123
pet_ap_dict["array"] = ["a", "b"]
pet_ap_dict["dict"] = {"key999": "value999"}
pet_ap2 = petstore_api.Pet.from_dict(pet_ap_dict)
self.assertEqual(pet_ap2.additional_properties["array"], ["a", "b"])
self.assertEqual(pet_ap2.additional_properties["something-new"], 123)
self.assertEqual(pet_ap2.additional_properties["dict"], {"key999": "value999"})
def test_nullable(self):
h = petstore_api.HealthCheckResult(nullable_message="Not none")
self.assertEqual(h.to_json(), '{"NullableMessage": "Not none"}')
h.nullable_message = None
self.assertEqual(h.to_json(), '{"NullableMessage": null}')
#import json
#dictionary ={
# "id": "04",
# "name": "sunil",
# "department": None
#}
#
## Serializing json
#json_object = json.dumps(dictionary)
#self.assertEqual(json_object, "")
def test_inline_enum_default(self):
enum_test = petstore_api.EnumTest(enum_string_required="lower")
self.assertEqual(enum_test.enum_integer_default, 5)
def test_object_with_optional_dict(self):
# for https://github.com/OpenAPITools/openapi-generator/issues/14913
# shouldn't throw exception by the optional dict property
a = petstore_api.ParentWithOptionalDict.from_dict({})
self.assertFalse(a is None)
b = petstore_api.ParentWithOptionalDict.from_dict({"optionalDict": {"key": {"aProperty": {"a": "b"}}}})
self.assertFalse(b is None)
self.assertEqual(b.optional_dict["key"].a_property["a"], "b")
def test_object_with_dict_of_dict_of_object(self):
# for https://github.com/OpenAPITools/openapi-generator/issues/15135
d = {"optionalDict": {"a": {"b": {"aProperty": "value"}}}}
a = petstore_api.Parent.from_dict(d)
self.assertEqual(a.to_dict(), d)
def test_eum_class(self):
a = petstore_api.EnumClass("-efg")
self.assertEqual(a.value, "-efg")
self.assertEqual(a.name, "MINUS_EFG")
self.assertEqual(a, "-efg")
def test_nullable_property_pattern(self):
a = petstore_api.NullableProperty(id=12, name=None)
self.assertEqual(a.id, 12)
self.assertEqual(a.name, None)
def test_int_or_string_oneof(self):
a = petstore_api.IntOrString("-efg")
self.assertEqual(a.actual_instance, "-efg")
a = petstore_api.IntOrString(100)
self.assertEqual(a.actual_instance, 100)
try:
a = petstore_api.IntOrString(1)
except ValueError as e:
self.assertTrue("ensure this value is greater than or equal to 10" in str(e))
def test_map_of_array_of_model(self):
a = petstore_api.MapOfArrayOfModel()
t = petstore_api.Tag(id=123, name="tag name")
a.shop_id_to_org_online_lip_map = {"somekey": [t]}
self.assertEqual(a.to_dict(), {'shopIdToOrgOnlineLipMap': {'somekey': [{'id': 123, 'name': 'tag name'}]}})
self.assertEqual(a.to_json(), '{"shopIdToOrgOnlineLipMap": {"somekey": [{"id": 123, "name": "tag name"}]}}')
a2 = petstore_api.MapOfArrayOfModel.from_dict(a.to_dict())
self.assertEqual(a.to_dict(), a2.to_dict())
def test_array_of_array_of_model(self):
a = petstore_api.ArrayOfArrayOfModel()
t = petstore_api.Tag(id=123, name="tag name")
a.another_property = [[t]]
self.assertEqual(a.to_dict(), {'another_property': [[ {'id': 123, 'name': 'tag name'} ]]})
self.assertEqual(a.to_json(), '{"another_property": [[{"id": 123, "name": "tag name"}]]}')
a2 = petstore_api.ArrayOfArrayOfModel.from_dict(a.to_dict())
self.assertEqual(a.to_dict(), a2.to_dict())
def test_object_with_additional_properties(self):
a = petstore_api.ObjectToTestAdditionalProperties()
a.additional_properties = { "abc": 123 }
# should not throw the following errors:
# pydantic.errors.ConfigError: field "additional_properties" not yet prepared so type is still a ForwardRef, you might need to call ObjectToTestAdditionalProperties.update_forward_refs().
def test_first_ref(self):
# shouldn't throw "still a ForwardRef" error
a = petstore_api.FirstRef.from_dict({})
self.assertEqual(a.to_json(), "{}")
def test_allof(self):
# for issue 16104
model = petstore_api.Tiger.from_json('{"skill": "none", "type": "tiger", "info": {"name": "creature info"}}')
# shouldn't throw NameError
self.assertEqual(model.to_json(), '{"skill": "none", "type": "tiger", "info": {"name": "creature info"}}')
def test_additional_properties(self):
a1 = petstore_api.AdditionalPropertiesAnyType()
a1.additional_properties = { "abc": 123 }
self.assertEqual(a1.to_dict(), {"abc": 123})
self.assertEqual(a1.to_json(), "{\"abc\": 123}")
a2 = petstore_api.AdditionalPropertiesObject()
a2.additional_properties = { "efg": 45.6 }
self.assertEqual(a2.to_dict(), {"efg": 45.6})
self.assertEqual(a2.to_json(), "{\"efg\": 45.6}")
a3 = petstore_api.AdditionalPropertiesWithDescriptionOnly()
a3.additional_properties = { "xyz": 45.6 }
self.assertEqual(a3.to_dict(), {"xyz": 45.6})
self.assertEqual(a3.to_json(), "{\"xyz\": 45.6}")
class TestUnnamedDictWithAdditionalStringListProperties:
def test_empty_dict(self):
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={})
assert a.to_dict() == {"dictProperty": {}}
def test_empty_list(self):
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={"b": []})
assert a.to_dict() == {"dictProperty": {"b": []}}
def test_single_string_item(self):
a = petstore_api.UnnamedDictWithAdditionalStringListProperties(dict_property={"b": ["c"]})
assert a.to_dict() == {"dictProperty": {"b": ["c"]}}
class TestUnnamedDictWithAdditionalModelListProperties:
def test_empty_dict(self):
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property={})
assert a.to_dict() == {"dictProperty": {}}
def test_empty_list(self):
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property={"b": []})
assert a.to_dict() == {"dictProperty": {"b": []}}
def test_single_string_item(self):
value = {"b": [petstore_api.CreatureInfo(name="creature_name")]}
a = petstore_api.UnnamedDictWithAdditionalModelListProperties(dict_property=value)
assert a.to_dict() == {"dictProperty": {"b": [{"name": "creature_name"}]}}