|
| 1 | +from test.test_json import PyTest |
| 2 | +import pickle |
| 3 | +import sys |
| 4 | +import unittest |
| 5 | + |
| 6 | +kepler_dict = { |
| 7 | + "orbital_period": { |
| 8 | + "mercury": 88, |
| 9 | + "venus": 225, |
| 10 | + "earth": 365, |
| 11 | + "mars": 687, |
| 12 | + "jupiter": 4331, |
| 13 | + "saturn": 10_756, |
| 14 | + "uranus": 30_687, |
| 15 | + "neptune": 60_190, |
| 16 | + }, |
| 17 | + "dist_from_sun": { |
| 18 | + "mercury": 58, |
| 19 | + "venus": 108, |
| 20 | + "earth": 150, |
| 21 | + "mars": 228, |
| 22 | + "jupiter": 778, |
| 23 | + "saturn": 1_400, |
| 24 | + "uranus": 2_900, |
| 25 | + "neptune": 4_500, |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class TestAttrDict(PyTest): |
| 30 | + |
| 31 | + def test_dict_subclass(self): |
| 32 | + self.assertTrue(issubclass(self.AttrDict, dict)) |
| 33 | + |
| 34 | + def test_slots(self): |
| 35 | + d = self.AttrDict(x=1, y=2) |
| 36 | + with self.assertRaises(TypeError): |
| 37 | + vars(d) |
| 38 | + |
| 39 | + def test_constructor_signatures(self): |
| 40 | + AttrDict = self.AttrDict |
| 41 | + target = dict(x=1, y=2) |
| 42 | + self.assertEqual(AttrDict(x=1, y=2), target) # kwargs |
| 43 | + self.assertEqual(AttrDict(dict(x=1, y=2)), target) # mapping |
| 44 | + self.assertEqual(AttrDict(dict(x=1, y=0), y=2), target) # mapping, kwargs |
| 45 | + self.assertEqual(AttrDict([('x', 1), ('y', 2)]), target) # iterable |
| 46 | + self.assertEqual(AttrDict([('x', 1), ('y', 0)], y=2), target) # iterable, kwargs |
| 47 | + |
| 48 | + def test_getattr(self): |
| 49 | + d = self.AttrDict(x=1, y=2) |
| 50 | + self.assertEqual(d.x, 1) |
| 51 | + with self.assertRaises(AttributeError): |
| 52 | + d.z |
| 53 | + |
| 54 | + def test_setattr(self): |
| 55 | + d = self.AttrDict(x=1, y=2) |
| 56 | + d.x = 3 |
| 57 | + d.z = 5 |
| 58 | + self.assertEqual(d, dict(x=3, y=2, z=5)) |
| 59 | + |
| 60 | + def test_delattr(self): |
| 61 | + d = self.AttrDict(x=1, y=2) |
| 62 | + del d.x |
| 63 | + self.assertEqual(d, dict(y=2)) |
| 64 | + with self.assertRaises(AttributeError): |
| 65 | + del d.z |
| 66 | + |
| 67 | + def test_dir(self): |
| 68 | + d = self.AttrDict(x=1, y=2) |
| 69 | + self.assertTrue(set(dir(d)), set(dir(dict)).union({'x', 'y'})) |
| 70 | + |
| 71 | + def test_repr(self): |
| 72 | + # This repr is doesn't round-trip. It matches a regular dict. |
| 73 | + # That seems to be the norm for AttrDict recipes being used |
| 74 | + # in the wild. Also it supports the design concept that an |
| 75 | + # AttrDict is just like a regular dict but has optional |
| 76 | + # attribute style lookup. |
| 77 | + self.assertEqual(repr(self.AttrDict(x=1, y=2)), |
| 78 | + repr(dict(x=1, y=2))) |
| 79 | + |
| 80 | + def test_overlapping_keys_and_methods(self): |
| 81 | + d = self.AttrDict(items=50) |
| 82 | + self.assertEqual(d['items'], 50) |
| 83 | + self.assertEqual(d.items(), dict(d).items()) |
| 84 | + |
| 85 | + def test_invalid_attribute_names(self): |
| 86 | + d = self.AttrDict({ |
| 87 | + 'control': 'normal case', |
| 88 | + 'class': 'keyword', |
| 89 | + 'two words': 'contains space', |
| 90 | + 'hypen-ate': 'contains a hyphen' |
| 91 | + }) |
| 92 | + self.assertEqual(d.control, dict(d)['control']) |
| 93 | + self.assertEqual(d['class'], dict(d)['class']) |
| 94 | + self.assertEqual(d['two words'], dict(d)['two words']) |
| 95 | + self.assertEqual(d['hypen-ate'], dict(d)['hypen-ate']) |
| 96 | + |
| 97 | + def test_object_hook_use_case(self): |
| 98 | + AttrDict = self.AttrDict |
| 99 | + json_string = self.dumps(kepler_dict) |
| 100 | + kepler_ad = self.loads(json_string, object_hook=AttrDict) |
| 101 | + |
| 102 | + self.assertEqual(kepler_ad, kepler_dict) # Match regular dict |
| 103 | + self.assertIsInstance(kepler_ad, AttrDict) # Verify conversion |
| 104 | + self.assertIsInstance(kepler_ad.orbital_period, AttrDict) # Nested |
| 105 | + |
| 106 | + # Exercise dotted lookups |
| 107 | + self.assertEqual(kepler_ad.orbital_period, kepler_dict['orbital_period']) |
| 108 | + self.assertEqual(kepler_ad.orbital_period.earth, |
| 109 | + kepler_dict['orbital_period']['earth']) |
| 110 | + self.assertEqual(kepler_ad['orbital_period'].earth, |
| 111 | + kepler_dict['orbital_period']['earth']) |
| 112 | + |
| 113 | + # Dict style error handling and Attribute style error handling |
| 114 | + with self.assertRaises(KeyError): |
| 115 | + kepler_ad.orbital_period['pluto'] |
| 116 | + with self.assertRaises(AttributeError): |
| 117 | + kepler_ad.orbital_period.Pluto |
| 118 | + |
| 119 | + # Order preservation |
| 120 | + self.assertEqual(list(kepler_ad.items()), list(kepler_dict.items())) |
| 121 | + self.assertEqual(list(kepler_ad.orbital_period.items()), |
| 122 | + list(kepler_dict['orbital_period'].items())) |
| 123 | + |
| 124 | + # Round trip |
| 125 | + self.assertEqual(self.dumps(kepler_ad), json_string) |
| 126 | + |
| 127 | + def test_pickle(self): |
| 128 | + AttrDict = self.AttrDict |
| 129 | + json_string = self.dumps(kepler_dict) |
| 130 | + kepler_ad = self.loads(json_string, object_hook=AttrDict) |
| 131 | + |
| 132 | + # Pickling requires the cached module to be the real module |
| 133 | + cached_module = sys.modules.get('json') |
| 134 | + sys.modules['json'] = self.json |
| 135 | + try: |
| 136 | + for protocol in range(6): |
| 137 | + kepler_ad2 = pickle.loads(pickle.dumps(kepler_ad, protocol)) |
| 138 | + self.assertEqual(kepler_ad2, kepler_ad) |
| 139 | + self.assertEqual(type(kepler_ad2), AttrDict) |
| 140 | + finally: |
| 141 | + sys.modules['json'] = cached_module |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + unittest.main() |
0 commit comments