def test_index(self): serializer = images.ImageXMLSerializer() fixture = { 'images': [ { 'id': 1, 'name': 'Image1', 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, { 'id': 2, 'name': 'Image2', 'links': [ { 'href': self.IMAGE_HREF % 2, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 2, 'rel': 'bookmark', }, ], }, ] } output = serializer.serialize(fixture, 'index') print output root = etree.XML(output) xmlutil.validate_schema(root, 'images_index') image_elems = root.findall('{0}image'.format(NS)) self.assertEqual(len(image_elems), 2) for i, image_elem in enumerate(image_elems): image_dict = fixture['images'][i] for key in ['name', 'id']: self.assertEqual(image_elem.get(key), str(image_dict[key])) link_nodes = image_elem.findall('{0}link'.format(ATOMNS)) self.assertEqual(len(link_nodes), 2) for i, link in enumerate(image_dict['links']): for key, value in link.items(): self.assertEqual(link_nodes[i].get(key), value)
def test_index_zero_images(self): serializer = images.ImageXMLSerializer() fixtures = { 'images': [], } output = serializer.serialize(fixtures, 'index') print output root = etree.XML(output) xmlutil.validate_schema(root, 'images_index') image_elems = root.findall('{0}image'.format(NS)) self.assertEqual(len(image_elems), 0)
def test_show_no_server(self): serializer = images.ImageXMLSerializer() fixture = { 'image': { 'id': 1, 'name': 'Image1', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'metadata': { 'key1': 'value1', }, 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, } output = serializer.serialize(fixture, 'show') print output root = etree.XML(output) xmlutil.validate_schema(root, 'image') image_dict = fixture['image'] for key in ['name', 'id', 'updated', 'created', 'status']: self.assertEqual(root.get(key), str(image_dict[key])) link_nodes = root.findall('{0}link'.format(ATOMNS)) self.assertEqual(len(link_nodes), 2) for i, link in enumerate(image_dict['links']): for key, value in link.items(): self.assertEqual(link_nodes[i].get(key), value) metadata_root = root.find('{0}metadata'.format(NS)) metadata_elems = metadata_root.findall('{0}meta'.format(NS)) self.assertEqual(len(metadata_elems), 1) for i, metadata_elem in enumerate(metadata_elems): (meta_key, meta_value) = image_dict['metadata'].items()[i] self.assertEqual(str(metadata_elem.get('key')), str(meta_key)) self.assertEqual(str(metadata_elem.text).strip(), str(meta_value)) server_root = root.find('{0}server'.format(NS)) self.assertEqual(server_root, None)
def test_xml_declaration(self): serializer = images.ImageXMLSerializer() fixture = { 'image': { 'id': 1, 'name': 'Image1', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'progress': 80, 'server': { 'id': '1', 'links': [ { 'href': self.SERVER_HREF, 'rel': 'self', }, { 'href': self.SERVER_BOOKMARK, 'rel': 'bookmark', }, ], }, 'metadata': { 'key1': 'value1', }, 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, } output = serializer.serialize(fixture, 'show') print output has_dec = output.startswith("<?xml version='1.0' encoding='UTF-8'?>") self.assertTrue(has_dec)
def test_detail(self): serializer = images.ImageXMLSerializer() fixture = { 'images': [ { 'id': 1, 'name': 'Image1', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { 'id': '1', 'links': [ { 'href': self.SERVER_HREF, 'rel': 'self', }, { 'href': self.SERVER_BOOKMARK, 'rel': 'bookmark', }, ], }, 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, { 'id': '2', 'name': 'Image2', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'SAVING', 'progress': 80, 'metadata': { 'key1': 'value1', }, 'links': [ { 'href': self.IMAGE_HREF % 2, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 2, 'rel': 'bookmark', }, ], }, ] } output = serializer.serialize(fixture, 'detail') print output root = etree.XML(output) xmlutil.validate_schema(root, 'images') image_elems = root.findall('{0}image'.format(NS)) self.assertEqual(len(image_elems), 2) for i, image_elem in enumerate(image_elems): image_dict = fixture['images'][i] for key in ['name', 'id', 'updated', 'created', 'status']: self.assertEqual(image_elem.get(key), str(image_dict[key])) link_nodes = image_elem.findall('{0}link'.format(ATOMNS)) self.assertEqual(len(link_nodes), 2) for i, link in enumerate(image_dict['links']): for key, value in link.items(): self.assertEqual(link_nodes[i].get(key), value) metadata_root = image_elem.find('{0}metadata'.format(NS)) metadata_elems = metadata_root.findall('{0}meta'.format(NS))
def test_show_image_no_metadata_key(self): serializer = images.ImageXMLSerializer() fixture = { 'image': { 'id': 1, 'name': 'Image1', 'created': self.TIMESTAMP, 'updated': self.TIMESTAMP, 'status': 'ACTIVE', 'server': { 'id': '1', 'links': [ { 'href': self.SERVER_HREF, 'rel': 'self', }, { 'href': self.SERVER_BOOKMARK, 'rel': 'bookmark', }, ], }, 'links': [ { 'href': self.IMAGE_HREF % 1, 'rel': 'self', }, { 'href': self.IMAGE_BOOKMARK % 1, 'rel': 'bookmark', }, ], }, } output = serializer.serialize(fixture, 'show') print output root = etree.XML(output) xmlutil.validate_schema(root, 'image') image_dict = fixture['image'] for key in ['name', 'id', 'updated', 'created', 'status']: self.assertEqual(root.get(key), str(image_dict[key])) link_nodes = root.findall('{0}link'.format(ATOMNS)) self.assertEqual(len(link_nodes), 2) for i, link in enumerate(image_dict['links']): for key, value in link.items(): self.assertEqual(link_nodes[i].get(key), value) metadata_root = root.find('{0}metadata'.format(NS)) meta_nodes = root.findall('{0}meta'.format(ATOMNS)) self.assertEqual(len(meta_nodes), 0) server_root = root.find('{0}server'.format(NS)) self.assertEqual(server_root.get('id'), image_dict['server']['id']) link_nodes = server_root.findall('{0}link'.format(ATOMNS)) self.assertEqual(len(link_nodes), 2) for i, link in enumerate(image_dict['server']['links']): for key, value in link.items(): self.assertEqual(link_nodes[i].get(key), value)