2
2
module for testing functionality of serializable fields
3
3
"""
4
4
5
+ # lib
6
+ import pytest
7
+
5
8
# src
6
9
from objectfactory import Factory , Serializable , Field , Nested , List
7
10
from .test_serializable import MyBasicClass
@@ -21,7 +24,16 @@ class MyComplexClass( Serializable ):
21
24
"""
22
25
complex class to test hierarchical serialization
23
26
"""
24
- nested = Nested ( MyBasicClass )
27
+ nested = Nested ()
28
+ prop = Field ()
29
+
30
+
31
+ @Factory .register_class
32
+ class MyTypedComplexClass ( Serializable ):
33
+ """
34
+ complex class to test hierarchical serialization
35
+ """
36
+ nested = Nested ( field_type = MyBasicClass )
25
37
prop = Field ()
26
38
27
39
@@ -34,6 +46,15 @@ class MyOtherComplexClass( Serializable ):
34
46
nested_list_prop = List ()
35
47
36
48
49
+ @Factory .register_class
50
+ class MyOtherTypedComplexClass ( Serializable ):
51
+ """
52
+ complex class to test list serialization
53
+ """
54
+ str_prop = Field ()
55
+ nested_list_prop = List ( field_type = MyBasicClass )
56
+
57
+
37
58
@Factory .register_class
38
59
class MyClassWithFieldOptionals ( Serializable ):
39
60
"""
@@ -98,6 +119,56 @@ def test_deserialize( self ):
98
119
assert obj .nested .str_prop == 'random string'
99
120
assert obj .nested .int_prop == 4321
100
121
122
+ def test_deserialize_typed ( self ):
123
+ """
124
+ test deserialization without _type field
125
+
126
+ expect nested json to be deserialized into a MyBasicClass object that is
127
+ a member of MyTypedComplexClass, even without nested _type field
128
+
129
+ :return:
130
+ """
131
+ body = {
132
+ '_type' : 'MyComplexClass' ,
133
+ 'prop' : 'really cool property' ,
134
+ 'nested' : {
135
+ 'str_prop' : 'random string' ,
136
+ 'int_prop' : 4321
137
+ }
138
+ }
139
+
140
+ obj = MyTypedComplexClass ()
141
+ obj .deserialize ( body )
142
+
143
+ assert isinstance ( obj , MyTypedComplexClass )
144
+ assert obj .prop == 'really cool property'
145
+ assert isinstance ( obj .nested , MyBasicClass )
146
+ assert obj .nested .str_prop == 'random string'
147
+ assert obj .nested .int_prop == 4321
148
+
149
+ def test_deserialize_enforce ( self ):
150
+ """
151
+ test deserialization enforcing field type
152
+
153
+ expect an error to be thrown on deserialization because the nested
154
+ field is of the incorrect type
155
+
156
+ :return:
157
+ """
158
+ body = {
159
+ '_type' : 'MyComplexClass' ,
160
+ 'prop' : 'really cool property' ,
161
+ 'nested' : {
162
+ '_type' : 'MyBasicClassWithLists' ,
163
+ 'str_prop' : 'random string' ,
164
+ 'int_prop' : 4321
165
+ }
166
+ }
167
+
168
+ obj = MyTypedComplexClass ()
169
+ with pytest .raises ( ValueError ):
170
+ obj .deserialize ( body )
171
+
101
172
102
173
class TestPrimitiveList ( object ):
103
174
"""
@@ -267,6 +338,43 @@ def test_deserialize( self ):
267
338
assert nested_obj .str_prop == nested_strings [i ]
268
339
assert nested_obj .int_prop == nested_ints [i ]
269
340
341
+ def test_deserialize_typed ( self ):
342
+ """
343
+ test deserialization without _type field
344
+
345
+ expect list of nested json objects to be deserialized into a list
346
+ of MyBasicClass objects that is a member of MyComplexClass, even without
347
+ _type field specified
348
+
349
+ :return:
350
+ """
351
+ body = {
352
+ '_type' : 'MyOtherTypedComplexClass' ,
353
+ 'str_prop' : 'really great string property' ,
354
+ 'nested_list_prop' : []
355
+ }
356
+ nested_strings = ['some string' , 'another string' , 'one more string' ]
357
+ nested_ints = [101 , 102 , 103 ]
358
+
359
+ for s , n in zip ( nested_strings , nested_ints ):
360
+ body ['nested_list_prop' ].append (
361
+ {
362
+ 'str_prop' : s ,
363
+ 'int_prop' : n
364
+ }
365
+ )
366
+
367
+ obj = MyOtherTypedComplexClass ()
368
+ obj .deserialize ( body )
369
+
370
+ assert isinstance ( obj , MyOtherTypedComplexClass )
371
+ assert obj .str_prop == 'really great string property'
372
+ assert len ( obj .nested_list_prop ) == 3
373
+ for i , nested_obj in enumerate ( obj .nested_list_prop ):
374
+ assert isinstance ( nested_obj , MyBasicClass )
375
+ assert nested_obj .str_prop == nested_strings [i ]
376
+ assert nested_obj .int_prop == nested_ints [i ]
377
+
270
378
271
379
class TestFieldOptionals ( object ):
272
380
"""
0 commit comments