def test_create(self): serializer = common.MetadataXMLSerializer() fixture = { 'metadata': { 'key9': 'value9', 'key2': 'value2', 'key1': 'value1', }, } output = serializer.serialize(fixture, 'create') print output root = etree.XML(output) xmlutil.validate_schema(root, 'metadata') metadata_dict = fixture['metadata'] metadata_elems = root.findall('{0}meta'.format(NS)) self.assertEqual(len(metadata_elems), 3) for i, metadata_elem in enumerate(metadata_elems): (meta_key, meta_value) = metadata_dict.items()[i] self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) actual = minidom.parseString(output.replace(" ", "")) expected = minidom.parseString(""" <metadata xmlns="http://docs.x7.org/compute/api/v1.1"> <meta key="key2">value2</meta> <meta key="key9">value9</meta> <meta key="key1">value1</meta> </metadata> """.replace(" ", "").replace("\n", "")) self.assertEqual(expected.toxml(), actual.toxml())
def test_xml_declaration(self): serializer = common.MetadataXMLSerializer() fixture = { 'metadata': { 'one': 'two', 'three': 'four', }, } output = serializer.serialize(fixture, 'index') print output has_dec = output.startswith("<?xml version='1.0' encoding='UTF-8'?>") self.assertTrue(has_dec)
def test_update_item(self): serializer = common.MetadataXMLSerializer() fixture = { 'meta': { 'one': 'two', }, } output = serializer.serialize(fixture, 'update') print output root = etree.XML(output) meta_dict = fixture['meta'] (meta_key, meta_value) = meta_dict.items()[0] self.assertEqual(str(root.get('key')), str(meta_key)) self.assertEqual(root.text.strip(), meta_value)
def test_index_unicode(self): serializer = common.MetadataXMLSerializer() fixture = { 'metadata': { u'three': u'Jos\xe9', }, } output = serializer.serialize(fixture, 'index') print output root = etree.XML(output) xmlutil.validate_schema(root, 'metadata') metadata_dict = fixture['metadata'] metadata_elems = root.findall('{0}meta'.format(NS)) self.assertEqual(len(metadata_elems), 1) for i, metadata_elem in enumerate(metadata_elems): (meta_key, meta_value) = metadata_dict.items()[i] self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) self.assertEqual(metadata_elem.text.strip(), meta_value)
def test_update_all(self): serializer = common.MetadataXMLSerializer() fixture = { 'metadata': { 'key6': 'value6', 'key4': 'value4', }, } output = serializer.serialize(fixture, 'update_all') print output root = etree.XML(output) xmlutil.validate_schema(root, 'metadata') metadata_dict = fixture['metadata'] metadata_elems = root.findall('{0}meta'.format(NS)) self.assertEqual(len(metadata_elems), 2) for i, metadata_elem in enumerate(metadata_elems): (meta_key, meta_value) = metadata_dict.items()[i] self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) self.assertEqual(str(metadata_elem.text).strip(), str(meta_value))
def test_delete(self): serializer = common.MetadataXMLSerializer() output = serializer.serialize(None, 'delete') self.assertEqual(output, '')