|
| 1 | +# encoding: utf-8 |
| 2 | + |
| 3 | +""" |
| 4 | +Test suite for docx.image.png module |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import absolute_import, print_function |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from docx.compat import BytesIO |
| 12 | +from docx.image.helpers import StreamReader |
| 13 | +from docx.image.png import Png |
| 14 | + |
| 15 | +from ..unitutil import ( |
| 16 | + initializer_mock, class_mock, instance_mock, method_mock |
| 17 | +) |
| 18 | + |
| 19 | + |
| 20 | +class DescribePng(object): |
| 21 | + |
| 22 | + def it_can_construct_from_a_png_stream(self, from_stream_fixture): |
| 23 | + # fixture ---------------------- |
| 24 | + (stream_, blob_, filename_, StreamReader_, _parse_png_headers_, |
| 25 | + stream_rdr_, Png__init__, cx, cy, attrs, png_) = from_stream_fixture |
| 26 | + # exercise --------------------- |
| 27 | + png = Png.from_stream(stream_, blob_, filename_) |
| 28 | + # verify ----------------------- |
| 29 | + StreamReader_.assert_called_once_with(stream_, '>') |
| 30 | + _parse_png_headers_.assert_called_once_with(stream_rdr_) |
| 31 | + Png__init__.assert_called_once_with(blob_, filename_, cx, cy, attrs) |
| 32 | + assert isinstance(png, Png) |
| 33 | + |
| 34 | + # fixtures ------------------------------------------------------- |
| 35 | + |
| 36 | + @pytest.fixture |
| 37 | + def attrs(self): |
| 38 | + return dict() |
| 39 | + |
| 40 | + @pytest.fixture |
| 41 | + def blob_(self, request): |
| 42 | + return instance_mock(request, bytes) |
| 43 | + |
| 44 | + @pytest.fixture |
| 45 | + def filename_(self, request): |
| 46 | + return instance_mock(request, str) |
| 47 | + |
| 48 | + @pytest.fixture |
| 49 | + def from_stream_fixture( |
| 50 | + self, stream_, blob_, filename_, StreamReader_, |
| 51 | + _parse_png_headers_, stream_rdr_, Png__init__, attrs, png_): |
| 52 | + cx, cy = 42, 24 |
| 53 | + attrs.update({'px_width': cx, 'px_height': cy}) |
| 54 | + return ( |
| 55 | + stream_, blob_, filename_, StreamReader_, _parse_png_headers_, |
| 56 | + stream_rdr_, Png__init__, cx, cy, attrs, png_ |
| 57 | + ) |
| 58 | + |
| 59 | + @pytest.fixture |
| 60 | + def Png__init__(self, request): |
| 61 | + return initializer_mock(request, Png) |
| 62 | + |
| 63 | + @pytest.fixture |
| 64 | + def _parse_png_headers_(self, request, attrs): |
| 65 | + return method_mock( |
| 66 | + request, Png, '_parse_png_headers', return_value=attrs |
| 67 | + ) |
| 68 | + |
| 69 | + @pytest.fixture |
| 70 | + def png_(self, request): |
| 71 | + return instance_mock(request, Png) |
| 72 | + |
| 73 | + @pytest.fixture |
| 74 | + def StreamReader_(self, request, stream_rdr_): |
| 75 | + return class_mock( |
| 76 | + request, 'docx.image.png.StreamReader', return_value=stream_rdr_ |
| 77 | + ) |
| 78 | + |
| 79 | + @pytest.fixture |
| 80 | + def stream_(self, request): |
| 81 | + return instance_mock(request, BytesIO) |
| 82 | + |
| 83 | + @pytest.fixture |
| 84 | + def stream_rdr_(self, request): |
| 85 | + return instance_mock(request, StreamReader) |
0 commit comments