def test_json(self): som_json = self._test_json some_json = json.dumps(som_json) ad_json = AdJson() ad_json.a.b.c = self._test_list prop_json = json.dumps(ad_json.to_dict()) self.assertEqual(some_json, prop_json)
def test_to_dict_with_tuple(self): nested = {'a': ({'a': 0}, {2: 0})} ad_json = AdJson(nested) regular = ad_json.to_dict() self.assert_ad_json_equal(regular, ad_json) self.assert_ad_json_equal(regular, nested) self.assertIsInstance(regular['a'], tuple) self.assertNotIsInstance(regular['a'][0], AdJson)
def test_to_dict(self): nested = {'a': [{'a': 0}, 2], 'b': {}, 'c': 2} ad_json = AdJson(nested) regular = ad_json.to_dict() self.assert_ad_json_equal(regular, ad_json) self.assert_ad_json_equal(regular, nested) self.assertNotIsInstance(regular, AdJson) def get_attr(): regular.a = 2 self.assertRaises(AttributeError, get_attr) def get_attr_deep(): regular['a'][0].a = 1 self.assertRaises(AttributeError, get_attr_deep)
def test_json_dump_load(self): origin = {'a': [{'a': 0}, 2], 'b': {}, 'c': 2} json_file = "test.json" with open(json_file, "w", encoding="utf-8") as fp: ad_json = AdJson(origin) ad_json.dump(fp) target = None with open(json_file, "r", encoding="utf-8") as fp: ad_json = AdJson() ad_json.load(fp) target = ad_json.to_dict() self.assert_ad_json_equal(origin, target) os.remove(json_file)