1212from docx .parts .document import DocumentPart , InlineShapes
1313from docx .text import Paragraph , Run
1414
15- from .unitutil import class_mock , instance_mock , method_mock , var_mock
15+ from .unitutil import (
16+ instance_mock , class_mock , method_mock , property_mock , var_mock
17+ )
1618
1719
1820class DescribeDocument (object ):
@@ -69,6 +71,15 @@ def it_can_add_a_styled_paragraph(self, add_styled_paragraph_fixture):
6971 p = document .add_paragraph (style = style )
7072 assert p .style == style
7173
74+ def it_can_add_a_picture (self , add_picture_fixture ):
75+ (document , image_path , width , height , inline_shapes_ , expected_width ,
76+ expected_height , picture_ ) = add_picture_fixture
77+ picture = document .add_picture (image_path , width , height )
78+ inline_shapes_ .add_picture .assert_called_once_with (image_path )
79+ assert picture .width == expected_width
80+ assert picture .height == expected_height
81+ assert picture is picture_
82+
7283 def it_provides_access_to_the_document_body (self , document ):
7384 body = document .body
7485 assert body is document ._document_part .body
@@ -83,16 +94,6 @@ def it_provides_access_to_the_document_paragraphs(
8394 paragraphs = document .paragraphs
8495 assert paragraphs is paragraphs_
8596
86- def it_can_add_an_inline_picture (self , add_picture_fixture ):
87- document , inline_shapes , image_path_or_stream_ , inline_picture_ = (
88- add_picture_fixture
89- )
90- inline_picture = document .add_inline_picture (image_path_or_stream_ )
91- inline_shapes .add_picture .assert_called_once_with (
92- image_path_or_stream_
93- )
94- assert inline_picture is inline_picture_
95-
9697 def it_can_save_the_package (self , save_fixture ):
9798 document , package_ , file_ = save_fixture
9899 document .save (file_ )
@@ -117,14 +118,23 @@ def add_paragraph_(self, request, p_):
117118 request , Document , 'add_paragraph' , return_value = p_
118119 )
119120
120- @pytest .fixture
121- def add_picture_fixture (self , request , open_ , document_part_ ):
121+ @pytest .fixture (params = [
122+ (None , None , 200 , 100 ),
123+ (1000 , 500 , 1000 , 500 ),
124+ (2000 , None , 2000 , 1000 ),
125+ (None , 2000 , 4000 , 2000 ),
126+ ])
127+ def add_picture_fixture (
128+ self , request , Document_inline_shapes_ , inline_shapes_ ):
129+ width , height , expected_width , expected_height = request .param
122130 document = Document ()
123- inline_shapes = instance_mock (request , InlineShapes )
124- document_part_ .inline_shapes = inline_shapes
125- image_path_ = instance_mock (request , str )
126- picture_shape_ = inline_shapes .add_picture .return_value
127- return document , inline_shapes , image_path_ , picture_shape_
131+ image_path_ = instance_mock (request , str , name = 'image_path_' )
132+ picture_ = inline_shapes_ .add_picture .return_value
133+ picture_ .width , picture_ .height = 200 , 100
134+ return (
135+ document , image_path_ , width , height , inline_shapes_ ,
136+ expected_width , expected_height , picture_
137+ )
128138
129139 @pytest .fixture
130140 def add_styled_paragraph_fixture (self , document , p_ ):
@@ -140,6 +150,12 @@ def add_text_paragraph_fixture(self, document, p_, r_):
140150 def default_docx_ (self , request ):
141151 return var_mock (request , 'docx.api._default_docx_path' )
142152
153+ @pytest .fixture
154+ def Document_inline_shapes_ (self , request , inline_shapes_ ):
155+ return property_mock (
156+ request , Document , 'inline_shapes' , return_value = inline_shapes_
157+ )
158+
143159 @pytest .fixture
144160 def document (self , open_ ):
145161 return Document ()
@@ -161,6 +177,10 @@ def docx_(self, request):
161177 def init_fixture (self , docx_ , open_ ):
162178 return docx_ , open_
163179
180+ @pytest .fixture
181+ def inline_shapes_ (self , request ):
182+ return instance_mock (request , InlineShapes )
183+
164184 @pytest .fixture
165185 def open_ (self , request , document_part_ , package_ ):
166186 return method_mock (
0 commit comments