1313from docx .image .bmp import Bmp
1414from docx .image .exceptions import UnrecognizedImageError
1515from docx .image .gif import Gif
16- from docx .image .image import Image
16+ from docx .image .image import BaseImageHeader , Image
1717from docx .image .jpeg import Exif , Jfif
1818from docx .image .png import Png
1919from docx .image .tiff import Tiff
2020from docx .opc .constants import CONTENT_TYPE as CT
2121
2222from ..unitutil import (
23- function_mock , class_mock , instance_mock , loose_mock , method_mock ,
23+ function_mock , class_mock , initializer_mock , instance_mock , method_mock ,
2424 test_file
2525)
2626
@@ -75,14 +75,16 @@ def it_can_construct_from_an_image_file_like(self, from_filelike_fixture):
7575 assert image is image_
7676
7777 def it_can_construct_from_an_image_stream (self , from_stream_fixture ):
78- (stream_ , blob_ , filename_ , image_ , image_cls_that_can_parse_ ,
79- image_cls_ ) = from_stream_fixture
78+ # fixture ----------------------
79+ stream_ , blob_ , filename_ = from_stream_fixture [:3 ]
80+ _ImageHeaderFactory_ , image_header_ = from_stream_fixture [3 :5 ]
81+ Image__init_ = from_stream_fixture [5 ]
82+ # exercise ---------------------
8083 image = Image ._from_stream (stream_ , blob_ , filename_ )
81- image_cls_that_can_parse_ .assert_called_once_with (stream_ )
82- image_cls_ .from_stream .assert_called_once_with (
83- stream_ , blob_ , filename_
84- )
85- assert image is image_
84+ # verify -----------------------
85+ _ImageHeaderFactory_ .assert_called_once_with (stream_ )
86+ Image__init_ .assert_called_once_with (blob_ , filename_ , image_header_ )
87+ assert isinstance (image , Image )
8688
8789 # fixtures -------------------------------------------------------
8890
@@ -118,11 +120,11 @@ def from_path_fixture(self, _from_stream_, BytesIO_, stream_, image_):
118120
119121 @pytest .fixture
120122 def from_stream_fixture (
121- self , stream_ , blob_ , filename_ , image_ ,
122- image_cls_that_can_parse_ , image_cls_ ):
123+ self , stream_ , blob_ , filename_ , _ImageHeaderFactory_ ,
124+ image_header_ , Image__init_ ):
123125 return (
124- stream_ , blob_ , filename_ , image_ , image_cls_that_can_parse_ ,
125- image_cls_
126+ stream_ , blob_ , filename_ , _ImageHeaderFactory_ , image_header_ ,
127+ Image__init_
126128 )
127129
128130 @pytest .fixture
@@ -136,23 +138,40 @@ def image_(self, request):
136138 return instance_mock (request , Image )
137139
138140 @pytest .fixture
139- def image_cls_ (self , request , image_ ):
140- image_cls_ = loose_mock (request )
141- image_cls_ .from_stream .return_value = image_
142- return image_cls_
143-
144- @pytest .fixture
145- def image_cls_that_can_parse_ (self , request , image_cls_ ):
141+ def _ImageHeaderFactory_ (self , request , image_header_ ):
146142 return function_mock (
147- request , 'docx.image.image_cls_that_can_parse ' ,
148- return_value = image_cls_
143+ request , 'docx.image.image._ImageHeaderFactory ' ,
144+ return_value = image_header_
149145 )
150146
147+ @pytest .fixture
148+ def image_header_ (self , request ):
149+ return instance_mock (request , BaseImageHeader )
150+
151+ @pytest .fixture
152+ def Image__init_ (self , request ):
153+ return initializer_mock (request , Image )
154+
151155 @pytest .fixture
152156 def stream_ (self , request ):
153157 return instance_mock (request , BytesIO )
154158
155159
160+ class DescribeBaseImageHeader (object ):
161+
162+ def it_knows_the_image_dimensions (self ):
163+ px_width , px_height = 42 , 24
164+ image_header = BaseImageHeader (px_width , px_height , None , None )
165+ assert image_header .px_width == px_width
166+ assert image_header .px_height == px_height
167+
168+ def it_knows_the_horz_and_vert_dpi_of_the_image (self ):
169+ horz_dpi , vert_dpi = 42 , 24
170+ image_header = BaseImageHeader (None , None , horz_dpi , vert_dpi )
171+ assert image_header .horz_dpi == horz_dpi
172+ assert image_header .vert_dpi == vert_dpi
173+
174+
156175class DescribeImage_OLD (object ):
157176
158177 def it_can_construct_from_an_image_path (self ):
0 commit comments