diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/docx/files_tests.py | 23 | ||||
| -rw-r--r-- | tests/mammoth_tests.py | 2 | ||||
| -rw-r--r-- | tests/testing.py | 9 |
3 files changed, 31 insertions, 3 deletions
diff --git a/tests/docx/files_tests.py b/tests/docx/files_tests.py index c1e15dc..56dc69a 100644 --- a/tests/docx/files_tests.py +++ b/tests/docx/files_tests.py @@ -1,7 +1,7 @@ from nose.tools import istest, assert_equal -from mammoth.docx.files import Files -from ..testing import test_path +from mammoth.docx.files import Files, InvalidFileReferenceError +from ..testing import test_path, assert_raises @istest @@ -23,3 +23,22 @@ def can_open_files_with_relative_uri(): assert_equal(bytes, type(contents)) with open(test_path("tiny-picture.png"), "rb") as source_file: assert_equal(source_file.read(), contents) + + +@istest +def error_is_raised_if_relative_uri_cannot_be_opened(): + files = Files("/tmp") + error = assert_raises(InvalidFileReferenceError, lambda: files.open("not-a-real-file.png")) + expected_message = ( + "could not open external image: 'not-a-real-file.png' (document directory: '/tmp')\n" + + "[Errno 2] No such file or directory: '/tmp/not-a-real-file.png'" + ) + assert_equal(expected_message, str(error)) + + +@istest +def error_is_raised_if_file_uri_cannot_be_opened(): + files = Files("/tmp") + error = assert_raises(InvalidFileReferenceError, lambda: files.open("file:///not-a-real-file.png")) + expected_message = "could not open external image: 'file:///not-a-real-file.png' (document directory: '/tmp')\n" + assert str(error).startswith(expected_message) diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index f29ae0f..906f49b 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -96,7 +96,7 @@ def warn_if_images_stored_outside_of_document_are_not_found(): with open(document_path, "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) assert_equal("", result.value) - expected_warning = "could not open external image 'tiny-picture.png' from document directory '{0}'\n[Errno 2] No such file or directory".format(temp_dir.path) + expected_warning = "could not open external image: 'tiny-picture.png'" assert_equal("warning", result.messages[0].type) assert result.messages[0].message.startswith(expected_warning), "message was: " + result.messages[0].message assert_equal(1, len(result.messages)) diff --git a/tests/testing.py b/tests/testing.py index d70ded6..c5013dc 100644 --- a/tests/testing.py +++ b/tests/testing.py @@ -7,3 +7,12 @@ from nose.tools import nottest def test_path(path): this_dir = os.path.dirname(__file__) return os.path.join(this_dir, "test-data", path) + + +def assert_raises(exception, func): + try: + func() + assert False, "Expected " + exception.__name__ + except exception as error: + return error + |
