99import pytest
1010
1111from docx .compat import BytesIO
12+ from docx .image .exceptions import InvalidImageStreamError
1213from docx .image .helpers import StreamReader
1314from docx .image .png import Png
1415
@@ -31,16 +32,37 @@ def it_can_construct_from_a_png_stream(self, from_stream_fixture):
3132 Png__init__ .assert_called_once_with (blob_ , filename_ , cx , cy , attrs )
3233 assert isinstance (png , Png )
3334
35+ def it_parses_PNG_headers_to_access_attrs (self , parse_png_fixture ):
36+ (stream_ , _parse_chunk_offsets_ , _parse_chunks_ , chunk_offsets_ ,
37+ attrs_ ) = parse_png_fixture
38+ attrs = Png ._parse_png_headers (stream_ )
39+ _parse_chunk_offsets_ .assert_called_once_with (stream_ )
40+ _parse_chunks_ .assert_called_once_with (stream_ , chunk_offsets_ )
41+ assert attrs == attrs_
42+
43+ def it_raises_on_png_having_no_IHDR_chunk (self , no_IHDR_fixture ):
44+ stream_ = no_IHDR_fixture
45+ with pytest .raises (InvalidImageStreamError ):
46+ Png ._parse_png_headers (stream_ )
47+
3448 # fixtures -------------------------------------------------------
3549
3650 @pytest .fixture
3751 def attrs (self ):
3852 return dict ()
3953
54+ @pytest .fixture
55+ def attrs_ (self , request ):
56+ return instance_mock (request , dict )
57+
4058 @pytest .fixture
4159 def blob_ (self , request ):
4260 return instance_mock (request , bytes )
4361
62+ @pytest .fixture
63+ def chunk_offsets_ (self , request ):
64+ return dict ()
65+
4466 @pytest .fixture
4567 def filename_ (self , request ):
4668 return instance_mock (request , str )
@@ -57,15 +79,42 @@ def from_stream_fixture(
5779 )
5880
5981 @pytest .fixture
60- def Png__init__ (self , request ):
61- return initializer_mock (request , Png )
82+ def no_IHDR_fixture (
83+ self , stream_ , _parse_chunk_offsets_ , _parse_chunks_ ):
84+ return stream_
85+
86+ @pytest .fixture
87+ def parse_png_fixture (
88+ self , stream_rdr_ , _parse_chunk_offsets_ , _parse_chunks_ ,
89+ chunk_offsets_ , attrs_ ):
90+ chunk_offsets_ ['IHDR' ] = 666
91+ return (
92+ stream_rdr_ , _parse_chunk_offsets_ , _parse_chunks_ ,
93+ chunk_offsets_ , attrs_
94+ )
95+
96+ @pytest .fixture
97+ def _parse_chunk_offsets_ (self , request , chunk_offsets_ ):
98+ return method_mock (
99+ request , Png , '_parse_chunk_offsets' , return_value = chunk_offsets_
100+ )
101+
102+ @pytest .fixture
103+ def _parse_chunks_ (self , request , attrs_ ):
104+ return method_mock (
105+ request , Png , '_parse_chunks' , return_value = attrs_
106+ )
62107
63108 @pytest .fixture
64109 def _parse_png_headers_ (self , request , attrs ):
65110 return method_mock (
66111 request , Png , '_parse_png_headers' , return_value = attrs
67112 )
68113
114+ @pytest .fixture
115+ def Png__init__ (self , request ):
116+ return initializer_mock (request , Png )
117+
69118 @pytest .fixture
70119 def png_ (self , request ):
71120 return instance_mock (request , Png )
0 commit comments