1313from docx .image .image import Image
1414from docx .opc .constants import CONTENT_TYPE as CT
1515
16- from ..unitutil import class_mock , instance_mock , method_mock , test_file
16+ from ..unitutil import (
17+ function_mock , class_mock , instance_mock , loose_mock , method_mock ,
18+ test_file
19+ )
1720
1821
1922class DescribeImage (object ):
@@ -26,20 +29,44 @@ def it_can_construct_from_an_image_path(self, from_path_fixture):
2629 _from_stream_ .assert_called_once_with (stream_ , blob , filename )
2730 assert image is image_
2831
29- def it_can_construct_from_an_image_stream (self , from_stream_fixture ):
30- image_stream , _from_stream_ , blob , image_ = from_stream_fixture
32+ def it_can_construct_from_an_image_file_like (self , from_filelike_fixture ):
33+ image_stream , _from_stream_ , blob , image_ = from_filelike_fixture
3134 image = Image .from_file (image_stream )
3235 _from_stream_ .assert_called_once_with (image_stream , blob , None )
3336 assert image is image_
3437
38+ def it_can_construct_from_an_image_stream (self , from_stream_fixture ):
39+ (stream_ , blob_ , filename_ , image_ , image_cls_that_can_parse_ ,
40+ image_cls_ ) = from_stream_fixture
41+ image = Image ._from_stream (stream_ , blob_ , filename_ )
42+ image_cls_that_can_parse_ .assert_called_once_with (stream_ )
43+ image_cls_ .assert_called_once_with (stream_ , blob_ , filename_ )
44+ assert image is image_
45+
3546 # fixtures -------------------------------------------------------
3647
48+ @pytest .fixture
49+ def blob_ (self , request ):
50+ return instance_mock (request , str )
51+
3752 @pytest .fixture
3853 def BytesIO_ (self , request , stream_ ):
3954 return class_mock (
4055 request , 'docx.image.image.BytesIO' , return_value = stream_
4156 )
4257
58+ @pytest .fixture
59+ def filename_ (self , request ):
60+ return instance_mock (request , str )
61+
62+ @pytest .fixture
63+ def from_filelike_fixture (self , _from_stream_ , image_ ):
64+ image_path = test_file ('python-icon.png' )
65+ with open (image_path , 'rb' ) as f :
66+ blob = f .read ()
67+ image_stream = BytesIO (blob )
68+ return image_stream , _from_stream_ , blob , image_
69+
4370 @pytest .fixture
4471 def from_path_fixture (self , _from_stream_ , BytesIO_ , stream_ , image_ ):
4572 filename = 'python-icon.png'
@@ -49,12 +76,13 @@ def from_path_fixture(self, _from_stream_, BytesIO_, stream_, image_):
4976 return image_path , _from_stream_ , stream_ , blob , filename , image_
5077
5178 @pytest .fixture
52- def from_stream_fixture (self , _from_stream_ , image_ ):
53- image_path = test_file ('python-icon.png' )
54- with open (image_path , 'rb' ) as f :
55- blob = f .read ()
56- image_stream = BytesIO (blob )
57- return image_stream , _from_stream_ , blob , image_
79+ def from_stream_fixture (
80+ self , stream_ , blob_ , filename_ , image_ ,
81+ image_cls_that_can_parse_ , image_cls_ ):
82+ return (
83+ stream_ , blob_ , filename_ , image_ , image_cls_that_can_parse_ ,
84+ image_cls_
85+ )
5886
5987 @pytest .fixture
6088 def _from_stream_ (self , request , image_ ):
@@ -66,6 +94,17 @@ def _from_stream_(self, request, image_):
6694 def image_ (self , request ):
6795 return instance_mock (request , Image )
6896
97+ @pytest .fixture
98+ def image_cls_ (self , request , image_ ):
99+ return loose_mock (request , return_value = image_ )
100+
101+ @pytest .fixture
102+ def image_cls_that_can_parse_ (self , request , image_cls_ ):
103+ return function_mock (
104+ request , 'docx.image.image.image_cls_that_can_parse' ,
105+ return_value = image_cls_
106+ )
107+
69108 @pytest .fixture
70109 def stream_ (self , request ):
71110 return instance_mock (request , BytesIO )
0 commit comments