def test_xml_to_json(self): """ test conversion from xml to json :return: """ original_xml = self.simple_xml xml_as_json = json.loads(xml_to_json(original_xml)) self.assertDictEqual(self.simple_json, xml_as_json[0])
def test_json_to_xml_to_json(self): """ test conversion from json to xml, and back from xml to json :return: """ original_json = deepcopy(self.simple_json) # convert json to xml json_as_xml = json_to_xml(json.dumps(original_json)) # convert xml back to json xml_as_json = json.loads(xml_to_json(json_as_xml)) # compare results self.assertDictEqual(original_json, xml_as_json[0])
def test_json_to_xml_to_json__with_multiple(self): """ test conversion from json to xml, and back from xml to json with multiple tags :return: """ original_json = deepcopy(self.complex_json) # convert json to xml json_as_xml = json_to_xml(json.dumps(original_json[0])) # convert xml back to json xml_as_json = json.loads(xml_to_json(json_as_xml)) # compare results self.assertEquals(len(original_json), len(xml_as_json)) for idx, d in enumerate(original_json): self.assertDictEqual(d, xml_as_json[idx])
def test_xml_to_json_to_xml__with_multiple(self): """ test conversion from xml to json, and back from json to xml with multiple tags :return: """ original_xml = deepcopy(self.complex_xml) # convert xml to json xml_as_json = json.loads(xml_to_json(deepcopy(original_xml))) # convert json back to xml json_as_xml = json_to_xml(json.dumps(xml_as_json[0])) # format result and expected result into dictionaries result = json.loads(json.dumps(xmltodict.parse(json_as_xml))) expects = json.loads(json.dumps(xmltodict.parse(original_xml))) # compare dictionary values self.assertDictEqual(result, expects)
def test_xml_to_json_to_xml(self): """ test conversion from xml to json, and back from json to xml :return: """ original_xml = deepcopy(self.simple_xml) # convert xml to json xml_as_json = json.loads(xml_to_json(deepcopy(original_xml))) # convert json back to xml json_as_xml = json_to_xml(json.dumps(xml_as_json[0])) # format result and expected result into dictionaries actual = json.loads(json.dumps(xmltodict.parse(json_as_xml))) expect = json.loads(json.dumps(xmltodict.parse(original_xml))) # compare dictionary values self.assertDictEqual(actual, expect)
def test_xml_to_json__with_multiple(self): """ test conversion from xml to json with multiple tags :return: """ original_xml = deepcopy(self.complex_xml) # convert xml to json result = json.loads(xml_to_json(original_xml)) self.assertTrue(isinstance(result, list)) # check is flattened for article in result: for v in article.values(): self.assertNotIsInstance(v, dict) self.assertNotIsInstance(v, list) # check values for idx, json_value in enumerate(self.complex_json): for k, v in json_value.iteritems(): self.assertEquals(v, result[idx][k])