@@ -109,3 +109,104 @@ def test_setattr_after_init(self):
109109 data = obj .to_dict ()
110110 self .assertEqual (data ['new_prop' ], 'abc' )
111111 self .assertEqual (data ['test_prop' ], 123 )
112+
113+ def test_update_with_dict (self ):
114+ """ Test we can update the JSONObject by passing a dict object """
115+ obj = JSONObject ({'test_prop' : 123 })
116+ self .assertIsInstance (obj , JSONObject )
117+
118+ obj = obj .update ({'test_prop' : 456 , 'new_prop' : 987 })
119+
120+ self .assertEqual (obj .new_prop , 987 )
121+ self .assertEqual (obj .test_prop , 456 )
122+
123+ data = obj .to_dict ()
124+
125+ self .assertEqual (data ['new_prop' ], 987 )
126+ self .assertEqual (data ['test_prop' ], 456 )
127+
128+ def test_update_with_keyword_args (self ):
129+ """ Test we can update the JSONObject by passing key word arguments """
130+ obj = JSONObject ({'test_prop' : 123 })
131+ self .assertIsInstance (obj , JSONObject )
132+
133+ obj = obj .update (test_prop = 456 , new_prop = 987 )
134+
135+ self .assertEqual (obj .new_prop , 987 )
136+ self .assertEqual (obj .test_prop , 456 )
137+
138+ data = obj .to_dict ()
139+
140+ self .assertEqual (data ['new_prop' ], 987 )
141+ self .assertEqual (data ['test_prop' ], 456 )
142+
143+ def test_update_with_iterable_pairs (self ):
144+ """ Test we can update the JSONObject by passing iterable pair arguments """
145+ obj = JSONObject ({'test_prop' : 123 })
146+ self .assertIsInstance (obj , JSONObject )
147+
148+ obj = obj .update ([('test_prop' , 456 ), ('new_prop' , 987 )])
149+
150+ self .assertEqual (obj .new_prop , 987 )
151+ self .assertEqual (obj .test_prop , 456 )
152+
153+ data = obj .to_dict ()
154+
155+ self .assertEqual (data ['new_prop' ], 987 )
156+ self .assertEqual (data ['test_prop' ], 456 )
157+
158+ def test_update_exceptions (self ):
159+ """ Test update using bad data to cause exceptions """
160+
161+ obj = JSONObject ({'test_prop' : 123 })
162+ self .assertIsInstance (obj , JSONObject )
163+
164+ # Test no args, should not raise an error.
165+ self .assertIsInstance (obj .update ({}), JSONObject )
166+
167+ # Test non-iterable value raises TypeError
168+ self .assertRaises (TypeError , obj .update , None )
169+ self .assertRaises (TypeError , obj .update , 123 )
170+
171+ # Test bad iterable pair raise ValueError
172+ self .assertRaises (ValueError , obj .update , [('test_prop' , 456 , 333 )])
173+
174+ def test_number_of_properties (self ):
175+ """ Test the number of properties by calling len() function """
176+
177+ obj = JSONObject ({'test_prop' : 123 , 'another_prop' : 'abc' })
178+ self .assertIsInstance (obj , JSONObject )
179+
180+ self .assertEqual (len (obj ), 2 )
181+
182+ def test_add_object (self ):
183+ """ Test add object """
184+ obj = JSONObject ({'test_prop' : 123 })
185+ self .assertIsInstance (obj , JSONObject )
186+
187+ other_obj = JSONObject ({'another_prop' : 'abc' })
188+ self .assertIsInstance (other_obj , JSONObject )
189+
190+ island_other_obj = JSONObject ({'island_prop' : 'sandy' })
191+ self .assertIsInstance (island_other_obj , JSONObject )
192+
193+ obj += other_obj
194+
195+ self .assertEqual (obj .test_prop , 123 )
196+ self .assertEqual (obj .another_prop , 'abc' )
197+
198+ obj = obj + island_other_obj
199+
200+ self .assertEqual (obj .island_prop , 'sandy' )
201+
202+ def test_add_invalid_operand (self ):
203+ """ Test adding an invalid operand """
204+ obj = JSONObject ({'test_prop' : 123 })
205+ self .assertIsInstance (obj , JSONObject )
206+
207+ try :
208+ obj += {'other_prop' : 'abc' }
209+ except TypeError :
210+ pass
211+
212+ self .assertFalse (hasattr (obj , 'other_prop' ))
0 commit comments