diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/docx/body_xml_tests.py | 41 | ||||
| -rw-r--r-- | tests/docx/content_types_xml_tests.py | 44 | ||||
| -rw-r--r-- | tests/docx/files_tests.py | 25 | ||||
| -rw-r--r-- | tests/mammoth_tests.py | 8 | ||||
| -rw-r--r-- | tests/test-data/external-picture.docx | bin | 0 -> 4120 bytes | |||
| -rw-r--r-- | tests/test-data/tiny-picture.png | bin | 0 -> 110 bytes |
6 files changed, 117 insertions, 1 deletions
diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index 91be80f..d3b552b 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -364,6 +364,39 @@ class ReadXmlElementTests(object): assert_equal("image/x-emf", result.value[0].content_type) expected_warning = results.warning("Image of type image/x-emf is unlikely to display in web browsers") assert_equal([expected_warning], result.messages) + + + @istest + @funk.with_context + def can_read_linked_pictures(self, context): + drawing_element = _create_inline_image( + blip=_linked_blip("rId5"), + description="It's a hat", + ) + + image_bytes = b"Not an image at all!" + + relationships = Relationships({ + "rId5": Relationship(target="file:///media/hat.png") + }) + + files = context.mock() + funk.allows(files).open("file:///media/hat.png").returns(io.BytesIO(image_bytes)) + + content_types = context.mock() + funk.allows(content_types).find_content_type("file:///media/hat.png").returns("image/png") + + image = _read_and_get_document_xml_element( + drawing_element, + content_types=content_types, + relationships=relationships, + files=files, + )[0] + assert_equal(documents.Image, type(image)) + assert_equal("It's a hat", image.alt_text) + assert_equal("image/png", image.content_type) + with image.open() as image_file: + assert_equal(image_bytes, image_file.read()) @istest def footnote_reference_has_id_read(self): @@ -494,4 +527,10 @@ def _create_image_elements(description, blip): ] def _embedded_blip(relationship_id): - return xml_element("a:blip", {"r:embed": relationship_id}) + return _blip({"r:embed": relationship_id}) + +def _linked_blip(relationship_id): + return _blip({"r:link": relationship_id}) + +def _blip(attributes): + return xml_element("a:blip", attributes) diff --git a/tests/docx/content_types_xml_tests.py b/tests/docx/content_types_xml_tests.py index 9df8569..8b104d9 100644 --- a/tests/docx/content_types_xml_tests.py +++ b/tests/docx/content_types_xml_tests.py @@ -36,3 +36,47 @@ def content_type_is_based_on_override_if_present(): "image/hat", content_types.find_content_type("word/media/hat.png"), ) + + +@istest +def fallback_content_types_have_common_image_types(): + element = xml_element("content-types:Types", {}, []) + content_types = read_content_types_xml_element(element) + assert_equal( + "image/png", + content_types.find_content_type("word/media/hat.png"), + ) + assert_equal( + "image/gif", + content_types.find_content_type("word/media/hat.gif"), + ) + assert_equal( + "image/jpeg", + content_types.find_content_type("word/media/hat.jpg"), + ) + assert_equal( + "image/jpeg", + content_types.find_content_type("word/media/hat.jpeg"), + ) + assert_equal( + "image/bmp", + content_types.find_content_type("word/media/hat.bmp"), + ) + assert_equal( + "image/tiff", + content_types.find_content_type("word/media/hat.tif"), + ) + assert_equal( + "image/tiff", + content_types.find_content_type("word/media/hat.tiff"), + ) + + +@istest +def fallback_content_types_are_case_insensitive(): + element = xml_element("content-types:Types", {}, []) + content_types = read_content_types_xml_element(element) + assert_equal( + "image/png", + content_types.find_content_type("word/media/hat.PnG"), + ) diff --git a/tests/docx/files_tests.py b/tests/docx/files_tests.py new file mode 100644 index 0000000..c1e15dc --- /dev/null +++ b/tests/docx/files_tests.py @@ -0,0 +1,25 @@ +from nose.tools import istest, assert_equal + +from mammoth.docx.files import Files +from ..testing import test_path + + +@istest +def can_open_files_with_file_uri(): + path = test_path("tiny-picture.png") + files = Files(None) + with files.open("file:///" + path) as image_file: + contents = image_file.read() + assert_equal(bytes, type(contents)) + with open(path, "rb") as source_file: + assert_equal(source_file.read(), contents) + + +@istest +def can_open_files_with_relative_uri(): + files = Files(test_path("")) + with files.open("tiny-picture.png") as image_file: + contents = image_file.read() + assert_equal(bytes, type(contents)) + with open(test_path("tiny-picture.png"), "rb") as source_file: + assert_equal(source_file.read(), contents) diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index 2c7262c..88aa445 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -47,6 +47,14 @@ def inline_images_are_included_in_output(): result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value) assert_equal([], result.messages) + + +@istest +def images_stored_outside_of_document_are_included_in_output(): + with open(test_path("external-picture.docx"), "rb") as fileobj: + result = mammoth.convert_to_html(fileobj=fileobj) + assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value) + assert_equal([], result.messages) @istest diff --git a/tests/test-data/external-picture.docx b/tests/test-data/external-picture.docx Binary files differnew file mode 100644 index 0000000..3475626 --- /dev/null +++ b/tests/test-data/external-picture.docx diff --git a/tests/test-data/tiny-picture.png b/tests/test-data/tiny-picture.png Binary files differnew file mode 100644 index 0000000..74cb4ba --- /dev/null +++ b/tests/test-data/tiny-picture.png |
