1111from mock import Mock
1212
1313from docx .enum .shape import WD_INLINE_SHAPE
14- from docx .oxml .parts import CT_Document
14+ from docx .oxml .parts import CT_Body , CT_Document
1515from docx .oxml .shared import nsmap
16- from docx .parts import _Body , _Document , InlineShape , InlineShapes
16+ from docx .oxml .text import CT_R
17+ from docx .parts import _Body , _Document , Image , InlineShape , InlineShapes
1718from docx .table import Table
1819from docx .text import Paragraph
1920
2627)
2728from .oxml .unitdata .text import a_p , a_sectPr , an_r
2829from .unitutil import (
29- function_mock , class_mock , initializer_mock , instance_mock
30+ function_mock , class_mock , initializer_mock , instance_mock , property_mock
3031)
3132
3233
@@ -360,8 +361,57 @@ def it_raises_on_indexed_access_out_of_range(
360361 too_high = inline_shape_count
361362 inline_shapes [too_high ]
362363
364+ def it_can_add_an_inline_picture_to_the_document (
365+ self , add_picture_fixture ):
366+ (inline_shapes , image_descriptor_ , document_ , InlineShape_ , r_ ,
367+ image_ , rId_ , shape_id_ , new_picture_shape_ ) = add_picture_fixture
368+ picture_shape = inline_shapes .add_picture (image_descriptor_ )
369+ document_ .add_image .assert_called_once_with (image_descriptor_ )
370+ InlineShape_ .new_picture .assert_called_once_with (
371+ r_ , image_ , rId_ , shape_id_
372+ )
373+ assert picture_shape is new_picture_shape_
374+
363375 # fixtures -------------------------------------------------------
364376
377+ @pytest .fixture
378+ def add_picture_fixture (
379+ self , request , body_ , document_ , image_descriptor_ , InlineShape_ ,
380+ r_ , image_ , rId_ , shape_id_ , new_picture_shape_ ):
381+ inline_shapes = InlineShapes (body_ )
382+ property_mock (request , InlineShapes , 'part' , return_value = document_ )
383+ return (
384+ inline_shapes , image_descriptor_ , document_ , InlineShape_ , r_ ,
385+ image_ , rId_ , shape_id_ , new_picture_shape_
386+ )
387+
388+ @pytest .fixture
389+ def body_ (self , request , r_ ):
390+ body_ = instance_mock (request , CT_Body )
391+ body_ .add_p .return_value .add_r .return_value = r_
392+ return body_
393+
394+ @pytest .fixture
395+ def document_ (self , request , rId_ , image_ , shape_id_ ):
396+ document_ = instance_mock (request , _Document , name = 'document_' )
397+ document_ .add_image .return_value = rId_ , image_
398+ document_ .next_id = shape_id_
399+ return document_
400+
401+ @pytest .fixture
402+ def image_ (self , request ):
403+ return instance_mock (request , Image )
404+
405+ @pytest .fixture
406+ def image_descriptor_ (self , request ):
407+ return instance_mock (request , str )
408+
409+ @pytest .fixture
410+ def InlineShape_ (self , request , new_picture_shape_ ):
411+ InlineShape_ = class_mock (request , 'docx.parts.InlineShape' )
412+ InlineShape_ .new_picture .return_value = new_picture_shape_
413+ return InlineShape_
414+
365415 @pytest .fixture
366416 def inline_shapes_fixture (self ):
367417 inline_shape_count = 2
@@ -380,3 +430,19 @@ def inline_shapes_fixture(self):
380430 ).element
381431 inline_shapes = InlineShapes (body )
382432 return inline_shapes , inline_shape_count
433+
434+ @pytest .fixture
435+ def new_picture_shape_ (self , request ):
436+ return instance_mock (request , InlineShape )
437+
438+ @pytest .fixture
439+ def r_ (self , request ):
440+ return instance_mock (request , CT_R )
441+
442+ @pytest .fixture
443+ def rId_ (self , request ):
444+ return instance_mock (request , str )
445+
446+ @pytest .fixture
447+ def shape_id_ (self , request ):
448+ return instance_mock (request , int )
0 commit comments