def test_get_all_graphic_images_from_xml(self): graphic_01 = '<graphic xlink:href="1234-5678-rctb-45-05-0110-e01.jpg"/>' graphic_02 = '<inline-graphic xlink:href="1234-5678-rctb-45-05-0110-e02.gif"/>' xml_file = BASE_XML.format(graphic_01, graphic_02).encode("utf-8") xml_file_path = os.path.join(self.work_dir, self.xml_filename) with open(xml_file_path, "wb") as fp: fp.write(xml_file) xml_web_optimiser = utils.XMLWebOptimiser( self.xml_filename, self.image_filenames, self.mocked_read_file, self.work_dir, ) result = xml_web_optimiser._get_all_graphic_images_from_xml( self.image_filenames) expected = { "1234-5678-rctb-45-05-0110-e01.jpg", "1234-5678-rctb-45-05-0110-e02.gif", "1234-5678-rctb-45-05-0110-gf03.tiff", "1234-5678-rctb-45-05-0110-gf03.png", "1234-5678-rctb-45-05-0110-gf03.thumbnail.jpg", "1234-5678-rctb-45-05-0110-e04.tif", } for graphic_filename, expected_filename in zip(result, expected): self.assertEqual(graphic_filename, expected_filename)
def setUp(self): graphic_01 = '<graphic xlink:href="1234-5678-rctb-45-05-0110-e01"/>' graphic_02 = '<inline-graphic xlink:href="1234-5678-rctb-45-05-0110-e02"/>' self.xml_file = BASE_XML.format(graphic_01, graphic_02).encode("utf-8") self.xml_filename = "1234-5678-rctb-45-05-0110.xml" self.work_dir = tempfile.mkdtemp() self.image_filenames = [ "1234-5678-rctb-45-05-0110-e01.tif", "1234-5678-rctb-45-05-0110-e02.tiff", "1234-5678-rctb-45-05-0110-gf03.tiff", "1234-5678-rctb-45-05-0110-gf03.png", "1234-5678-rctb-45-05-0110-gf03.thumbnail.jpg", "1234-5678-rctb-45-05-0110-e04.tif", ] image_format_seq = ["TIFF", "TIFF", "TIFF", "PNG", "JPEG", "TIFF"] for filename, format in zip(self.image_filenames, image_format_seq): image_file_path = os.path.join(self.work_dir, filename) create_image_file(image_file_path, format) xml_file_path = os.path.join(self.work_dir, self.xml_filename) with open(xml_file_path, "wb") as fp: fp.write(self.xml_file) self.xml_web_optimiser = utils.XMLWebOptimiser( self.xml_filename, self.image_filenames, self.mocked_read_file, self.work_dir, )
def test_get_all_images_to_thumbnail_does_not_return_images_with_thumbnail( self): graphic_01 = "" graphic_02 = '<inline-graphic xlink:href="1234-5678-rctb-45-05-0110-e02.gif"/>' xml_file = BASE_XML.format(graphic_01, graphic_02).encode("utf-8") xml_file_path = os.path.join(self.work_dir, self.xml_filename) with open(xml_file_path, "wb") as fp: fp.write(xml_file) xml_web_optimiser = utils.XMLWebOptimiser( self.xml_filename, self.image_filenames, self.mocked_read_file, self.work_dir, ) images = xml_web_optimiser._get_all_images_to_thumbnail() result = [image for image in images] self.assertEqual(len(result), 0)
def test_add_alternative_to_alternatives_tag_add_image_tags_to_existing_alternatives( self, ): xml_file = BASE_XML.format("", "").encode("utf-8") xml_file_path = os.path.join(self.work_dir, self.xml_filename) with open(xml_file_path, "wb") as fp: fp.write(xml_file) xml_web_optimiser = utils.XMLWebOptimiser( self.xml_filename, self.image_filenames, self.mocked_read_file, self.work_dir, ) XML_TAG = """<article xmlns:xlink="http://www.w3.org/1999/xlink"> <p>Bla, bla, bla <alternatives> <inline-graphic xlink:href="1234-5678-rctb-45-05-0110-gf03.tiff"/> <inline-graphic xlink:href="1234-5678-rctb-45-05-0110-gf03.png" specific-use="scielo-web"/> </alternatives> </p> </article>""" xml_tag = etree.fromstring( XML_TAG, etree.XMLParser(remove_blank_text=True, no_network=True)) xml_web_optimiser._xml_file = xml_tag image_element = xml_tag.find(".//inline-graphic") alternative_attr_values = ( ( "{http://www.w3.org/1999/xlink}href", "1234-5678-rctb-45-05-0110-gf03.gif", ), ("specific-use", "scielo-web"), ) xml_web_optimiser._add_alternative_to_alternatives_tag( image_element, alternative_attr_values) self.assertIsNone(xml_tag.find("p/inline-graphic")) alternatives_tags = xml_tag.findall("p/alternatives") self.assertIsNotNone(alternatives_tags) self.assertEqual(len(alternatives_tags), 1) expected_hrefs = [ "1234-5678-rctb-45-05-0110-gf03.tiff", "1234-5678-rctb-45-05-0110-gf03.png", "1234-5678-rctb-45-05-0110-gf03.gif", ] for image_element in alternatives_tags[0].getchildren(): self.assertEqual(image_element.tag, "inline-graphic")
def test_get_optimised_xml_no_reader_file(self): graphic_01 = '<graphic xlink:href="1234-5678-rctb-45-05-0110-e01.tif"/>' graphic_02 = '<inline-graphic xlink:href="1234-5678-rctb-45-05-0110-e02.tiff"/>' xml_file = BASE_XML.format(graphic_01, graphic_02).encode("utf-8") xml_file_path = os.path.join(self.work_dir, self.xml_filename) with open(xml_file_path, "wb") as fp: fp.write(xml_file) with self.assertRaises(exceptions.XMLWebOptimiserError) as exc_info: xml_web_optimiser = utils.XMLWebOptimiser( self.xml_filename, self.image_filenames, None, self.work_dir, ) self.assertEqual( str(exc_info.exception), "Error instantiating XMLWebOptimiser: read_file cannot be None", )
def test_get_all_images_to_optimise_does_not_return_optimised_images(self): graphic_01 = '<graphic xlink:href="1234-5678-rctb-45-05-0110-e01.jpg"/>' graphic_02 = '<inline-graphic xlink:href="1234-5678-rctb-45-05-0110-e02.gif"/>' xml_file = BASE_XML.format(graphic_01, graphic_02).encode("utf-8") xml_file_path = os.path.join(self.work_dir, self.xml_filename) with open(xml_file_path, "wb") as fp: fp.write(xml_file) xml_web_optimiser = utils.XMLWebOptimiser( self.xml_filename, self.image_filenames, self.mocked_read_file, self.work_dir, ) images = xml_web_optimiser._get_all_images_to_optimise() expected = ["1234-5678-rctb-45-05-0110-e04.tif"] result = [image for image in images] self.assertEqual(len(result), len(expected)) image_filename, __ = result[0] self.assertEqual(expected[0], image_filename)