1111from mock import call
1212
1313from docx .compat import BytesIO
14- from docx .image .constants import MIME_TYPE , PNG_CHUNK_TYPE , TAG
14+ from docx .image .constants import MIME_TYPE , PNG_CHUNK_TYPE
1515from docx .image .exceptions import InvalidImageStreamError
1616from docx .image .helpers import BIG_ENDIAN , StreamReader
1717from docx .image .png import (
2020)
2121
2222from ..unitutil import (
23- function_mock , class_mock , initializer_mock , instance_mock , method_mock ,
24- test_file
23+ function_mock , class_mock , initializer_mock , instance_mock , method_mock
2524)
2625
2726
@@ -36,97 +35,12 @@ def it_can_construct_from_a_png_stream(self, from_stream_fixture):
3635 Png__init__ .assert_called_once_with (cx , cy , horz_dpi , vert_dpi )
3736 assert isinstance (png , Png )
3837
39- def it_parses_PNG_headers_to_access_attrs (self , parse_png_fixture ):
40- (stream_ , _parse_chunk_offsets_ , _parse_chunks_ , chunk_offsets ,
41- attrs_ ) = parse_png_fixture
42- attrs = Png ._parse_png_headers (stream_ )
43- _parse_chunk_offsets_ .assert_called_once_with (stream_ )
44- _parse_chunks_ .assert_called_once_with (stream_ , chunk_offsets )
45- assert attrs == attrs_
46-
47- def it_parses_chunk_offsets_to_help_chunk_parser (
48- self , chunk_offset_fixture ):
49- stream , expected_chunk_offsets = chunk_offset_fixture
50- chunk_offsets = Png ._parse_chunk_offsets (stream )
51- assert chunk_offsets == expected_chunk_offsets
52-
53- def it_parses_chunks_to_extract_fields (self , parse_chunks_fixture ):
54- (stream_ , chunk_offsets , _parse_IHDR_ , ihdr_offset , _parse_pHYs_ ,
55- phys_offset , expected_attrs ) = parse_chunks_fixture
56- attrs = Png ._parse_chunks (stream_ , chunk_offsets )
57- _parse_IHDR_ .assert_called_once_with (stream_ , ihdr_offset )
58- if phys_offset is not None :
59- _parse_pHYs_ .assert_called_once_with (stream_ , phys_offset )
60- assert attrs == expected_attrs
61-
62- def it_raises_on_png_having_no_IHDR_chunk (self , no_IHDR_fixture ):
63- stream_ , chunk_offsets = no_IHDR_fixture
64- with pytest .raises (InvalidImageStreamError ):
65- Png ._parse_chunks (stream_ , chunk_offsets )
66-
67- def it_can_parse_an_IHDR_chunk (self , parse_IHDR_fixture ):
68- stream , offset , expected_attrs = parse_IHDR_fixture
69- attrs = Png ._parse_IHDR (stream , offset )
70- assert attrs == expected_attrs
71-
72- def it_can_parse_an_pHYs_chunk (self , parse_pHYs_fixture ):
73- stream , offset , expected_attrs = parse_pHYs_fixture
74- attrs = Png ._parse_pHYs (stream , offset )
75- assert attrs == expected_attrs
76-
7738 def it_knows_its_content_type (self ):
7839 png = Png (None , None , None , None )
7940 assert png .content_type == MIME_TYPE .PNG
8041
81- # def it_knows_its_dpi(self, dpi_fixture):
82- # png, expected_dpi = dpi_fixture
83- # assert png.horz_dpi == expected_dpi
84- # assert png.vert_dpi == expected_dpi
85-
8642 # fixtures -------------------------------------------------------
8743
88- @pytest .fixture
89- def attrs (self ):
90- return dict ()
91-
92- @pytest .fixture
93- def attrs_ (self , request ):
94- return instance_mock (request , dict )
95-
96- @pytest .fixture (params = [
97- ('150-dpi.png' , {
98- 'IHDR' : 16 , 'pHYs' : 41 , 'iCCP' : 62 , 'cHRM' : 2713 , 'IDAT' : 2757 ,
99- 'IEND' : 146888 }),
100- ('300-dpi.png' , {
101- 'IHDR' : 16 , 'pHYs' : 41 , 'tEXt' : 62 , 'IDAT' : 99 , 'IEND' : 39917 }),
102- ])
103- def chunk_offset_fixture (self , request ):
104- filename , expected_chunk_offsets = request .param
105- path = test_file (filename )
106- with open (path , 'rb' ) as f :
107- blob = f .read ()
108- stream = BytesIO (blob )
109- stream_rdr = StreamReader (stream , BIG_ENDIAN )
110- return stream_rdr , expected_chunk_offsets
111-
112- @pytest .fixture
113- def chunk_offsets (self , request ):
114- return dict ()
115-
116- @pytest .fixture (params = [
117- (5906 , 1 , 150 ), (11811 , 1 , 300 ), (5906 , 0 , 72 ), (None , 0 , 72 ),
118- (666 , 0 , 72 ), (2835 , 1 , 72 )
119- ])
120- def dpi_fixture (self , request ):
121- px_per_unit , units_specifier , expected_dpi = request .param
122- attrs = {
123- TAG .HORZ_PX_PER_UNIT : px_per_unit ,
124- TAG .VERT_PX_PER_UNIT : px_per_unit ,
125- TAG .UNITS_SPECIFIER : units_specifier
126- }
127- png = Png (None , None , None , attrs )
128- return png , expected_dpi
129-
13044 @pytest .fixture
13145 def from_stream_fixture (
13246 self , stream_ , _PngParser_ , png_parser_ , Png__init__ ):
@@ -140,98 +54,10 @@ def from_stream_fixture(
14054 horz_dpi , vert_dpi
14155 )
14256
143- @pytest .fixture
144- def no_IHDR_fixture (self , stream_ , chunk_offsets ):
145- return stream_ , chunk_offsets
146-
147- @pytest .fixture (params = [(42 , 24 ), (42 , None )])
148- def parse_chunks_fixture (
149- self , request , stream_rdr_ , _parse_IHDR_ , _parse_pHYs_ ):
150- ihdr_offset , phys_offset = request .param
151- chunk_offsets = {'IHDR' : ihdr_offset }
152- expected_attrs = dict (_parse_IHDR_ .return_value )
153- if phys_offset is not None :
154- chunk_offsets ['pHYs' ] = phys_offset
155- expected_attrs .update (_parse_pHYs_ .return_value )
156- return (
157- stream_rdr_ , chunk_offsets , _parse_IHDR_ , ihdr_offset ,
158- _parse_pHYs_ , phys_offset , expected_attrs
159- )
160-
161- @pytest .fixture
162- def parse_IHDR_fixture (self ):
163- bytes_ = b'\x00 \x00 \x00 \x2A \x00 \x00 \x00 \x18 '
164- stream = BytesIO (bytes_ )
165- stream_rdr = StreamReader (stream , BIG_ENDIAN )
166- offset = 0
167- expected_attrs = {TAG .PX_WIDTH : 42 , TAG .PX_HEIGHT : 24 }
168- return stream_rdr , offset , expected_attrs
169-
170- @pytest .fixture
171- def parse_pHYs_fixture (self ):
172- bytes_ = b'\x00 \x00 \x17 \x12 \x00 \x00 \x1E \xC2 \x01 '
173- stream = BytesIO (bytes_ )
174- stream_rdr = StreamReader (stream , BIG_ENDIAN )
175- offset = 0
176- expected_attrs = {
177- TAG .HORZ_PX_PER_UNIT : 5906 , TAG .VERT_PX_PER_UNIT : 7874 ,
178- TAG .UNITS_SPECIFIER : 1
179- }
180- return stream_rdr , offset , expected_attrs
181-
182- @pytest .fixture
183- def parse_png_fixture (
184- self , stream_rdr_ , _parse_chunk_offsets_ , _parse_chunks_ ,
185- chunk_offsets , attrs_ ):
186- chunk_offsets ['IHDR' ] = 666
187- return (
188- stream_rdr_ , _parse_chunk_offsets_ , _parse_chunks_ ,
189- chunk_offsets , attrs_
190- )
191-
192- @pytest .fixture
193- def _parse_chunk_offsets_ (self , request , chunk_offsets ):
194- return method_mock (
195- request , Png , '_parse_chunk_offsets' , return_value = chunk_offsets
196- )
197-
198- @pytest .fixture
199- def _parse_chunks_ (self , request , attrs_ ):
200- return method_mock (
201- request , Png , '_parse_chunks' , return_value = attrs_
202- )
203-
204- @pytest .fixture
205- def _parse_IHDR_ (self , request ):
206- return method_mock (
207- request , Png , '_parse_IHDR' , return_value = {
208- TAG .PX_WIDTH : 12 , TAG .PX_HEIGHT : 34
209- }
210- )
211-
212- @pytest .fixture
213- def _parse_pHYs_ (self , request ):
214- return method_mock (
215- request , Png , '_parse_pHYs' , return_value = {
216- TAG .HORZ_PX_PER_UNIT : 56 , TAG .VERT_PX_PER_UNIT : 78 ,
217- TAG .UNITS_SPECIFIER : 1
218- }
219- )
220-
221- @pytest .fixture
222- def _parse_png_headers_ (self , request , attrs ):
223- return method_mock (
224- request , Png , '_parse_png_headers' , return_value = attrs
225- )
226-
22757 @pytest .fixture
22858 def Png__init__ (self , request ):
22959 return initializer_mock (request , Png )
23060
231- @pytest .fixture
232- def png_ (self , request ):
233- return instance_mock (request , Png )
234-
23561 @pytest .fixture
23662 def _PngParser_ (self , request , png_parser_ ):
23763 _PngParser_ = class_mock (request , 'docx.image.png._PngParser' )
@@ -242,20 +68,10 @@ def _PngParser_(self, request, png_parser_):
24268 def png_parser_ (self , request ):
24369 return instance_mock (request , _PngParser )
24470
245- @pytest .fixture
246- def StreamReader_ (self , request , stream_rdr_ ):
247- return class_mock (
248- request , 'docx.image.png.StreamReader' , return_value = stream_rdr_
249- )
250-
25171 @pytest .fixture
25272 def stream_ (self , request ):
25373 return instance_mock (request , BytesIO )
25474
255- @pytest .fixture
256- def stream_rdr_ (self , request ):
257- return instance_mock (request , StreamReader )
258-
25975
26076class Describe_PngParser (object ):
26177
0 commit comments