99import pytest
1010
1111from docx .compat import BytesIO
12- from docx .image import image_cls_that_can_parse , Image_OLD
12+ from docx .image import Image_OLD
1313from docx .image .bmp import Bmp
1414from docx .image .exceptions import UnrecognizedImageError
1515from docx .image .gif import Gif
16- from docx .image .image import BaseImageHeader , Image
16+ from docx .image .image import BaseImageHeader , Image , _ImageHeaderFactory
1717from docx .image .jpeg import Exif , Jfif
1818from docx .image .png import Png
1919from docx .image .tiff import Tiff
2525)
2626
2727
28- class Describe_image_cls_that_can_parse (object ):
29-
30- def it_can_recognize_an_image_stream (self , image_cls_lookup_fixture ):
31- stream , expected_class = image_cls_lookup_fixture
32- ImageSubclass = image_cls_that_can_parse (stream )
33- assert ImageSubclass is expected_class
34-
35- def it_raises_on_unrecognized_image_stream (self ):
36- stream = BytesIO (b'foobar 666 not an image stream' )
37- with pytest .raises (UnrecognizedImageError ):
38- image_cls_that_can_parse (stream )
39-
40- # fixtures -------------------------------------------------------
41-
42- @pytest .fixture (params = [
43- ('python-icon.png' , Png ),
44- ('python-icon.jpeg' , Jfif ),
45- ('exif-420-dpi.jpg' , Exif ),
46- ('sonic.gif' , Gif ),
47- ('72-dpi.tiff' , Tiff ),
48- ('little-endian.tif' , Tiff ),
49- ('python.bmp' , Bmp ),
50- ])
51- def image_cls_lookup_fixture (self , request ):
52- image_filename , expected_class = request .param
53- image_path = test_file (image_filename )
54- with open (image_path , 'rb' ) as f :
55- blob = f .read ()
56- image_stream = BytesIO (blob )
57- image_stream .seek (666 )
58- return image_stream , expected_class
59-
60-
6128class DescribeImage (object ):
6229
6330 def it_can_construct_from_an_image_path (self , from_path_fixture ):
@@ -86,6 +53,23 @@ def it_can_construct_from_an_image_stream(self, from_stream_fixture):
8653 Image__init_ .assert_called_once_with (blob_ , filename_ , image_header_ )
8754 assert isinstance (image , Image )
8855
56+ def it_knows_the_image_content_type (self , content_type_fixture ):
57+ image_header_ , content_type = content_type_fixture
58+ image = Image (None , None , image_header_ )
59+ assert image .content_type == content_type
60+
61+ def it_knows_the_image_dimensions (self , dimensions_fixture ):
62+ image_header_ , px_width , px_height = dimensions_fixture
63+ image = Image (None , None , image_header_ )
64+ assert image .px_width == px_width
65+ assert image .px_height == px_height
66+
67+ def it_knows_the_horz_and_vert_dpi_of_the_image (self , dpi_fixture ):
68+ image_header_ , horz_dpi , vert_dpi = dpi_fixture
69+ image = Image (None , None , image_header_ )
70+ assert image .horz_dpi == horz_dpi
71+ assert image .vert_dpi == vert_dpi
72+
8973 # fixtures -------------------------------------------------------
9074
9175 @pytest .fixture
@@ -98,6 +82,26 @@ def BytesIO_(self, request, stream_):
9882 request , 'docx.image.image.BytesIO' , return_value = stream_
9983 )
10084
85+ @pytest .fixture
86+ def content_type_fixture (self , image_header_ ):
87+ content_type = 'image/foobar'
88+ image_header_ .content_type = content_type
89+ return image_header_ , content_type
90+
91+ @pytest .fixture
92+ def dimensions_fixture (self , image_header_ ):
93+ px_width , px_height = 111 , 222
94+ image_header_ .px_width = px_width
95+ image_header_ .px_height = px_height
96+ return image_header_ , px_width , px_height
97+
98+ @pytest .fixture
99+ def dpi_fixture (self , image_header_ ):
100+ horz_dpi , vert_dpi = 333 , 444
101+ image_header_ .horz_dpi = horz_dpi
102+ image_header_ .vert_dpi = vert_dpi
103+ return image_header_ , horz_dpi , vert_dpi
104+
101105 @pytest .fixture
102106 def filename_ (self , request ):
103107 return instance_mock (request , str )
@@ -157,6 +161,40 @@ def stream_(self, request):
157161 return instance_mock (request , BytesIO )
158162
159163
164+ class Describe_ImageHeaderFactory (object ):
165+
166+ def it_constructs_the_right_class_for_a_given_image_stream (
167+ self , call_fixture ):
168+ stream , expected_class = call_fixture
169+ image_header = _ImageHeaderFactory (stream )
170+ assert isinstance (image_header , expected_class )
171+
172+ def it_raises_on_unrecognized_image_stream (self ):
173+ stream = BytesIO (b'foobar 666 not an image stream' )
174+ with pytest .raises (UnrecognizedImageError ):
175+ _ImageHeaderFactory (stream )
176+
177+ # fixtures -------------------------------------------------------
178+
179+ @pytest .fixture (params = [
180+ ('python-icon.png' , Png ),
181+ ('python-icon.jpeg' , Jfif ),
182+ ('exif-420-dpi.jpg' , Exif ),
183+ ('sonic.gif' , Gif ),
184+ ('72-dpi.tiff' , Tiff ),
185+ ('little-endian.tif' , Tiff ),
186+ ('python.bmp' , Bmp ),
187+ ])
188+ def call_fixture (self , request ):
189+ image_filename , expected_class = request .param
190+ image_path = test_file (image_filename )
191+ with open (image_path , 'rb' ) as f :
192+ blob = f .read ()
193+ image_stream = BytesIO (blob )
194+ image_stream .seek (666 )
195+ return image_stream , expected_class
196+
197+
160198class DescribeBaseImageHeader (object ):
161199
162200 def it_knows_the_image_dimensions (self ):
0 commit comments