def test_to_dict(self): ''' ''' aircraft = Struct() aircraft.tail_number = 'G-ABCD' aircraft.frame.type = '737-3C' aircraft.model.dist_gear_to_tail = 200 self.assertEqual(aircraft.to_dict(), { 'tail_number': 'G-ABCD', 'frame': {'type': '737-3C'}, 'model': {'dist_gear_to_tail': 200}, })
def test_create_attrs_on_the_fly(self): ''' ''' # Create initial struct from a dictionary: aircraft = Struct({'model': {'dist_gear_to_tail': 200}}) # Add attribute at the top level: aircraft.tail_number = 'G-ABCD' self.assertEqual(aircraft.tail_number, 'G-ABCD') # Add attribute at level created from dictionary earlier: aircraft.model.name = '737-800' self.assertEqual(aircraft.model.name, '737-800') # Add attribute on a new attribute two levels deep: aircraft.frame.type = '737-3C' self.assertEqual(aircraft.frame.type, '737-3C') self.assertTrue(isinstance(aircraft.frame, Struct)) # Add attribute on non-existing attribute with lots of depth: aircraft.a.b.c.d.e = 'alphabet' # Check that a previously unreferenced attribute is created on the fly: self.assertEqual(aircraft.x.y.z, Struct()) # Ensure that a non-struct attribute cannot have an unknown attribute: self.assertRaises(AttributeError, getattr, aircraft.a.b.c.d.e, 'f')