diff options
| author | Michael Williamson <mike@zwobble.org> | 2018-01-19 20:12:18 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2018-01-19 20:12:18 +0000 |
| commit | df5644cde6ba9b70870ddb01caaee423e00e49ad (patch) | |
| tree | 393d0be67c91b2ab62f02fd660952a1258ef65c9 /tests/docx/docx_tests.py | |
| parent | dcca2c6ffe71e399caeeac6c2d44070c097a9cc5 (diff) | |
Read document XML path from _rels/.rels
Diffstat (limited to 'tests/docx/docx_tests.py')
| -rw-r--r-- | tests/docx/docx_tests.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/docx/docx_tests.py b/tests/docx/docx_tests.py index ea69dc2..fd53d4f 100644 --- a/tests/docx/docx_tests.py +++ b/tests/docx/docx_tests.py @@ -1,3 +1,7 @@ +import io +import textwrap +import zipfile + from nose.tools import istest, assert_equal from mammoth import docx, documents @@ -18,3 +22,52 @@ class ReadTests(object): ]) ]) assert_equal(expected_document, result.value) + + +_relationship_namespaces = { + "r": "http://schemas.openxmlformats.org/package/2006/relationships", +} + + +@istest +def when_document_xml_is_not_present_then_document_is_found_using_file_relationships(): + fileobj = _create_zip({ + "word/document2.xml": textwrap.dedent("""\ + <?xml version="1.0" encoding="utf-8" ?> + <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"> + <w:body> + <w:p> + <w:r> + <w:t>Hello.</w:t> + </w:r> + </w:p> + </w:body> + </w:document> + """), + "_rels/.rels": textwrap.dedent("""\ + <?xml version="1.0" encoding="utf-8"?> + <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"> + <Relationship Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="/word/document2.xml" Id="rId1"/> + </Relationships> + """), + }) + result = docx.read(fileobj=fileobj) + expected_document = documents.document([ + documents.paragraph([ + documents.run([ + documents.text("Hello.") + ]) + ]) + ]) + assert_equal(expected_document, result.value) + + +def _create_zip(files): + fileobj = io.BytesIO() + + with zipfile.ZipFile(fileobj, "w") as zip_file: + for name, contents in files.items(): + zip_file.writestr(name, contents) + + fileobj.seek(0) + return fileobj |
