|
8 | 8 |
|
9 | 9 | import pytest |
10 | 10 |
|
| 11 | +from docx.compat import BytesIO |
11 | 12 | from docx.image import Image_OLD |
| 13 | +from docx.image.image import Image |
12 | 14 | from docx.opc.constants import CONTENT_TYPE as CT |
13 | 15 |
|
14 | | -from ..unitutil import test_file |
| 16 | +from ..unitutil import class_mock, instance_mock, method_mock, test_file |
| 17 | + |
| 18 | + |
| 19 | +class DescribeImage(object): |
| 20 | + |
| 21 | + def it_can_construct_from_an_image_path(self, from_path_fixture): |
| 22 | + image_path, _from_stream_, stream_, blob, filename, image_ = ( |
| 23 | + from_path_fixture |
| 24 | + ) |
| 25 | + image = Image.from_file(image_path) |
| 26 | + _from_stream_.assert_called_once_with(stream_, blob, filename) |
| 27 | + assert image is image_ |
| 28 | + |
| 29 | + def it_can_construct_from_an_image_stream(self, from_stream_fixture): |
| 30 | + image_stream, _from_stream_, blob, image_ = from_stream_fixture |
| 31 | + image = Image.from_file(image_stream) |
| 32 | + _from_stream_.assert_called_once_with(image_stream, blob, None) |
| 33 | + assert image is image_ |
| 34 | + |
| 35 | + # fixtures ------------------------------------------------------- |
| 36 | + |
| 37 | + @pytest.fixture |
| 38 | + def BytesIO_(self, request, stream_): |
| 39 | + return class_mock( |
| 40 | + request, 'docx.image.image.BytesIO', return_value=stream_ |
| 41 | + ) |
| 42 | + |
| 43 | + @pytest.fixture |
| 44 | + def from_path_fixture(self, _from_stream_, BytesIO_, stream_, image_): |
| 45 | + filename = 'python-icon.png' |
| 46 | + image_path = test_file(filename) |
| 47 | + with open(image_path, 'rb') as f: |
| 48 | + blob = f.read() |
| 49 | + return image_path, _from_stream_, stream_, blob, filename, image_ |
| 50 | + |
| 51 | + @pytest.fixture |
| 52 | + def from_stream_fixture(self, _from_stream_, image_): |
| 53 | + image_path = test_file('python-icon.png') |
| 54 | + with open(image_path, 'rb') as f: |
| 55 | + blob = f.read() |
| 56 | + image_stream = BytesIO(blob) |
| 57 | + return image_stream, _from_stream_, blob, image_ |
| 58 | + |
| 59 | + @pytest.fixture |
| 60 | + def _from_stream_(self, request, image_): |
| 61 | + return method_mock( |
| 62 | + request, Image, '_from_stream', return_value=image_ |
| 63 | + ) |
| 64 | + |
| 65 | + @pytest.fixture |
| 66 | + def image_(self, request): |
| 67 | + return instance_mock(request, Image) |
| 68 | + |
| 69 | + @pytest.fixture |
| 70 | + def stream_(self, request): |
| 71 | + return instance_mock(request, BytesIO) |
15 | 72 |
|
16 | 73 |
|
17 | 74 | class DescribeImage_OLD(object): |
|
0 commit comments