1
1
import plotly .io as pio
2
2
import plotly .io .kaleido
3
3
import sys
4
+ from pathlib import Path
4
5
from contextlib import contextmanager
5
6
6
7
if sys .version_info >= (3 , 3 ):
11
12
fig = {"layout" : {"title" : {"text" : "figure title" }}}
12
13
13
14
15
+ def make_writeable_mocks ():
16
+
17
+ # A mock for a file descriptor
18
+ # ----------------------------
19
+ mock_file_descriptor = Mock ()
20
+
21
+ # A file descriptor has no write_bytes method, unlike a pathlib Path.
22
+ del mock_file_descriptor .write_bytes
23
+
24
+ # The expected write method for a file descriptor is .write
25
+ mock_file_descriptor .active_write_function = mock_file_descriptor .write
26
+
27
+ # Since there is no filename, there should be no format detected.
28
+ mock_file_descriptor .expected_format = None
29
+
30
+ # A mock for a pathlib path
31
+ # -------------------------
32
+ mock_pathlib_path = Mock (spec = Path )
33
+
34
+ # A pathlib Path object has no write method, unlike a file descriptor.
35
+ del mock_pathlib_path .write
36
+
37
+ # The expected write method for a pathlib Path is .write_bytes
38
+ mock_pathlib_path .active_write_function = mock_pathlib_path .write_bytes
39
+
40
+ # Mock a path with PNG suffix
41
+ mock_pathlib_path .suffix = ".png"
42
+ mock_pathlib_path .expected_format = "png"
43
+
44
+ return mock_file_descriptor , mock_pathlib_path
45
+
46
+
14
47
@contextmanager
15
48
def mocked_scope ():
16
49
# Code to acquire resource, e.g.:
@@ -44,16 +77,19 @@ def test_kaleido_engine_to_image():
44
77
45
78
46
79
def test_kaleido_engine_write_image ():
47
- writeable_mock = Mock ()
48
- with mocked_scope () as scope :
49
- pio .write_image (fig , writeable_mock , engine = "kaleido" , validate = False )
80
+ for writeable_mock in make_writeable_mocks ():
81
+ with mocked_scope () as scope :
82
+ pio .write_image (fig , writeable_mock , engine = "kaleido" , validate = False )
50
83
51
- scope .transform .assert_called_with (
52
- fig , format = None , width = None , height = None , scale = None
53
- )
84
+ scope .transform .assert_called_with (
85
+ fig ,
86
+ format = writeable_mock .expected_format ,
87
+ width = None ,
88
+ height = None ,
89
+ scale = None ,
90
+ )
54
91
55
- count = writeable_mock .write .call_count + writeable_mock .write_bytes .call_count
56
- assert count == 1
92
+ assert writeable_mock .active_write_function .call_count == 1
57
93
58
94
59
95
def test_kaleido_engine_to_image_kwargs ():
@@ -74,25 +110,24 @@ def test_kaleido_engine_to_image_kwargs():
74
110
75
111
76
112
def test_kaleido_engine_write_image_kwargs ():
77
- writeable_mock = Mock ()
78
- with mocked_scope () as scope :
79
- pio .write_image (
80
- fig ,
81
- writeable_mock ,
82
- format = "jpg" ,
83
- width = 700 ,
84
- height = 600 ,
85
- scale = 2 ,
86
- engine = "kaleido" ,
87
- validate = False ,
113
+ for writeable_mock in make_writeable_mocks ():
114
+ with mocked_scope () as scope :
115
+ pio .write_image (
116
+ fig ,
117
+ writeable_mock ,
118
+ format = "jpg" ,
119
+ width = 700 ,
120
+ height = 600 ,
121
+ scale = 2 ,
122
+ engine = "kaleido" ,
123
+ validate = False ,
124
+ )
125
+
126
+ scope .transform .assert_called_with (
127
+ fig , format = "jpg" , width = 700 , height = 600 , scale = 2
88
128
)
89
129
90
- scope .transform .assert_called_with (
91
- fig , format = "jpg" , width = 700 , height = 600 , scale = 2
92
- )
93
-
94
- count = writeable_mock .write .call_count + writeable_mock .write_bytes .call_count
95
- assert count == 1
130
+ assert writeable_mock .active_write_function .call_count == 1
96
131
97
132
98
133
def test_image_renderer ():
0 commit comments