1111from mock import Mock
1212
1313from docx .enum .shape import WD_INLINE_SHAPE
14+ from docx .opc .constants import RELATIONSHIP_TYPE as RT
1415from docx .oxml .parts .document import CT_Body , CT_Document
1516from docx .oxml .shared import nsmap
1617from docx .oxml .text import CT_R
18+ from docx .package import ImageParts , Package
1719from docx .parts .document import _Body , _Document , InlineShape , InlineShapes
18- from docx .parts .image import Image
20+ from docx .parts .image import ImagePart
1921from docx .table import Table
2022from docx .text import Paragraph
2123
2931from ..oxml .unitdata .text import a_p , a_sectPr , an_r
3032from ..unitutil import (
3133 function_mock , class_mock , initializer_mock , instance_mock , loose_mock ,
32- property_mock
34+ method_mock , property_mock
3335)
3436
3537
@@ -53,6 +55,18 @@ def it_can_be_constructed_by_opc_part_factory(
5355 )
5456 assert isinstance (doc , _Document )
5557
58+ def it_can_add_an_image_part_to_the_document (
59+ self , get_or_add_image_fixture ):
60+ (document , image_descriptor_ , image_parts_ , relate_to_ , image_part_ ,
61+ rId_ ) = get_or_add_image_fixture
62+ image_part , rId = document .get_or_add_image_part (image_descriptor_ )
63+ image_parts_ .get_or_add_image_part .assert_called_once_with (
64+ image_descriptor_
65+ )
66+ relate_to_ .assert_called_once_with (image_part_ , RT .IMAGE )
67+ assert image_part is image_part_
68+ assert rId == rId_
69+
5670 def it_has_a_body (self , document_body_fixture ):
5771 document , _Body_ , body_elm = document_body_fixture
5872 _body = document .body
@@ -101,6 +115,30 @@ def document_body_fixture(self, request, _Body_):
101115 def _Body_ (self , request ):
102116 return class_mock (request , 'docx.parts.document._Body' )
103117
118+ @pytest .fixture
119+ def get_or_add_image_fixture (
120+ self , request , package_ , image_descriptor_ , image_parts_ ,
121+ relate_to_ , image_part_ , rId_ ):
122+ document = _Document (None , None , None , package_ )
123+ return (
124+ document , image_descriptor_ , image_parts_ , relate_to_ ,
125+ image_part_ , rId_
126+ )
127+
128+ @pytest .fixture
129+ def image_descriptor_ (self , request ):
130+ return instance_mock (request , str )
131+
132+ @pytest .fixture
133+ def image_part_ (self , request ):
134+ return instance_mock (request , ImagePart )
135+
136+ @pytest .fixture
137+ def image_parts_ (self , request , image_part_ ):
138+ image_parts_ = instance_mock (request , ImageParts )
139+ image_parts_ .get_or_add_image_part .return_value = image_part_
140+ return image_parts_
141+
104142 @pytest .fixture
105143 def init (self , request ):
106144 return initializer_mock (request , _Document )
@@ -123,6 +161,22 @@ def inline_shapes_fixture(self, request, InlineShapes_):
123161 def oxml_fromstring_ (self , request ):
124162 return function_mock (request , 'docx.parts.document.oxml_fromstring' )
125163
164+ @pytest .fixture
165+ def package_ (self , request , image_parts_ ):
166+ package_ = instance_mock (request , Package )
167+ package_ .image_parts = image_parts_
168+ return package_
169+
170+ @pytest .fixture
171+ def relate_to_ (self , request , rId_ ):
172+ relate_to_ = method_mock (request , _Document , 'relate_to' )
173+ relate_to_ .return_value = rId_
174+ return relate_to_
175+
176+ @pytest .fixture
177+ def rId_ (self , request ):
178+ return instance_mock (request , str )
179+
126180 @pytest .fixture
127181 def serialize_part_xml_ (self , request ):
128182 return function_mock (
@@ -374,10 +428,15 @@ def it_raises_on_indexed_access_out_of_range(
374428
375429 def it_can_add_an_inline_picture_to_the_document (
376430 self , add_picture_fixture ):
431+ # fixture ----------------------
377432 (inline_shapes , image_descriptor_ , document_ , InlineShape_ , r_ ,
378433 image_ , rId_ , shape_id_ , new_picture_shape_ ) = add_picture_fixture
434+ # exercise ---------------------
379435 picture_shape = inline_shapes .add_picture (image_descriptor_ )
380- document_ .add_image .assert_called_once_with (image_descriptor_ )
436+ # verify -----------------------
437+ document_ .get_or_add_image_part .assert_called_once_with (
438+ image_descriptor_
439+ )
381440 InlineShape_ .new_picture .assert_called_once_with (
382441 r_ , image_ , rId_ , shape_id_
383442 )
@@ -393,12 +452,12 @@ def it_knows_the_part_it_belongs_to(self, inline_shapes_with_parent_):
393452 @pytest .fixture
394453 def add_picture_fixture (
395454 self , request , body_ , document_ , image_descriptor_ , InlineShape_ ,
396- r_ , image_ , rId_ , shape_id_ , new_picture_shape_ ):
455+ r_ , image_part_ , rId_ , shape_id_ , new_picture_shape_ ):
397456 inline_shapes = InlineShapes (body_ , None )
398457 property_mock (request , InlineShapes , 'part' , return_value = document_ )
399458 return (
400459 inline_shapes , image_descriptor_ , document_ , InlineShape_ , r_ ,
401- image_ , rId_ , shape_id_ , new_picture_shape_
460+ image_part_ , rId_ , shape_id_ , new_picture_shape_
402461 )
403462
404463 @pytest .fixture
@@ -408,15 +467,15 @@ def body_(self, request, r_):
408467 return body_
409468
410469 @pytest .fixture
411- def document_ (self , request , rId_ , image_ , shape_id_ ):
470+ def document_ (self , request , rId_ , image_part_ , shape_id_ ):
412471 document_ = instance_mock (request , _Document , name = 'document_' )
413- document_ .add_image .return_value = rId_ , image_
472+ document_ .get_or_add_image_part .return_value = rId_ , image_part_
414473 document_ .next_id = shape_id_
415474 return document_
416475
417476 @pytest .fixture
418- def image_ (self , request ):
419- return instance_mock (request , Image )
477+ def image_part_ (self , request ):
478+ return instance_mock (request , ImagePart )
420479
421480 @pytest .fixture
422481 def image_descriptor_ (self , request ):
0 commit comments