@@ -306,3 +306,71 @@ def test_repr(self, fake_manager):
306
306
307
307
FakeObject ._id_attr = None
308
308
assert repr (obj ) == "<FakeObject>"
309
+
310
+ def test_asdict (self , fake_manager ):
311
+ fake_object = FakeObject (fake_manager , {"attr1" : "foo" , "alist" : [1 , 2 , 3 ]})
312
+ assert fake_object .attr1 == "foo"
313
+ result = fake_object .asdict ()
314
+ assert result == {"attr1" : "foo" , "alist" : [1 , 2 , 3 ]}
315
+ # Demonstrate modifying the dictionary does not modify the object
316
+ result ["attr1" ] = "testing"
317
+ result ["alist" ].append (4 )
318
+ assert result == {"attr1" : "testing" , "alist" : [1 , 2 , 3 , 4 ]}
319
+ assert fake_object .attr1 == "foo"
320
+ assert fake_object .alist == [1 , 2 , 3 ]
321
+ # asdict() returns the updated value
322
+ fake_object .attr1 = "spam"
323
+ assert fake_object .asdict () == {"attr1" : "spam" , "alist" : [1 , 2 , 3 ]}
324
+ # Modify attribute and then ensure modifying a list in the returned dict won't
325
+ # modify the list in the object.
326
+ fake_object .attr1 = [9 , 7 , 8 ]
327
+ assert fake_object .asdict () == {
328
+ "attr1" : [9 , 7 , 8 ],
329
+ "alist" : [1 , 2 , 3 ],
330
+ }
331
+ result = fake_object .asdict ()
332
+ result ["attr1" ].append (1 )
333
+ assert fake_object .asdict () == {
334
+ "attr1" : [9 , 7 , 8 ],
335
+ "alist" : [1 , 2 , 3 ],
336
+ }
337
+
338
+ def test_attributes (self , fake_manager ):
339
+ fake_object = FakeObject (fake_manager , {"attr1" : [1 , 2 , 3 ]})
340
+ assert fake_object .attr1 == [1 , 2 , 3 ]
341
+ result = fake_object .attributes
342
+ assert result == {"attr1" : [1 , 2 , 3 ]}
343
+
344
+ # Updated attribute value is not reflected in `attributes`
345
+ fake_object .attr1 = "hello"
346
+ assert fake_object .attributes == {"attr1" : [1 , 2 , 3 ]}
347
+ assert fake_object .attr1 == "hello"
348
+ # New attribute is in `attributes`
349
+ fake_object .new_attrib = "spam"
350
+ assert fake_object .attributes == {"attr1" : [1 , 2 , 3 ], "new_attrib" : "spam" }
351
+
352
+ # Modifying the dictionary can cause modification to the object :(
353
+ result = fake_object .attributes
354
+ result ["attr1" ].append (10 )
355
+ assert result == {"attr1" : [1 , 2 , 3 , 10 ], "new_attrib" : "spam" }
356
+ assert fake_object .attributes == {"attr1" : [1 , 2 , 3 , 10 ], "new_attrib" : "spam" }
357
+ assert fake_object .attr1 == "hello"
358
+
359
+
360
+ def test_asdict_vs_attributes (self , fake_manager ):
361
+ fake_object = FakeObject (fake_manager , {"attr1" : "foo" })
362
+ assert fake_object .attr1 == "foo"
363
+ result = fake_object .asdict ()
364
+ assert result == {"attr1" : "foo" }
365
+
366
+ # New attribute added, return same result
367
+ assert fake_object .attributes == fake_object .asdict ()
368
+ fake_object .attr2 = "eggs"
369
+ assert fake_object .attributes == fake_object .asdict ()
370
+ # Update attribute, return different result
371
+ fake_object .attr1 = "hello"
372
+ assert fake_object .attributes != fake_object .asdict ()
373
+ # asdict() returns the updated value
374
+ assert fake_object .asdict () == {"attr1" : "hello" , "attr2" : "eggs" }
375
+ # `attributes` returns original value
376
+ assert fake_object .attributes == {"attr1" : "foo" , "attr2" : "eggs" }
0 commit comments