1
+ import plotly
1
2
import plotly .io as pio
2
3
import plotly .graph_objs as go
3
4
import os
13
14
14
15
# Constants
15
16
# ---------
16
- images_root = 'plotly/tests/test_orca/images/'
17
-
17
+ project_root = os .path .dirname (os .path .realpath (plotly .__file__ ))
18
+ images_root = os .path .join (project_root , 'tests' , 'test_orca' , 'images' )
19
+ print (images_root )
18
20
if sys .platform .startswith ('linux' ):
19
- images_dir = images_root + 'linux/'
21
+ images_dir = os . path . join ( images_root , 'linux' )
20
22
elif sys .platform == 'darwin' :
21
- images_dir = images_root + 'darwin/'
23
+ images_dir = os . path . join ( images_root , 'darwin' )
22
24
else :
23
25
raise ValueError (
24
26
'No reference images available for platform: {platform}'
25
27
.format (platform = sys .platform ))
26
28
27
29
28
- failed_dir = images_dir + 'failed/'
29
- tmp_dir = images_dir + 'tmp/'
30
+ failed_dir = os . path . join ( images_dir , 'failed/' )
31
+ tmp_dir = os . path . join ( images_dir , 'tmp/' )
30
32
# These formats are deterministic. PDF and svg don't seem to be
31
33
image_formats = ['eps' ]
32
34
@@ -157,7 +159,7 @@ def latexfig():
157
159
# Utilities
158
160
# ---------
159
161
def assert_image_bytes (img_bytes , file_name , _raise = True ):
160
- expected_img_path = images_dir + file_name
162
+ expected_img_path = os . path . join ( images_dir , file_name )
161
163
162
164
try :
163
165
with open (expected_img_path , 'rb' ) as f :
@@ -166,7 +168,7 @@ def assert_image_bytes(img_bytes, file_name, _raise=True):
166
168
assert expected == img_bytes
167
169
168
170
except (OSError , AssertionError ) as e :
169
- failed_path = failed_dir + file_name
171
+ failed_path = os . path . join ( failed_dir , file_name )
170
172
print ('Saving failed image to "{failed_path}"'
171
173
.format (failed_path = failed_path ))
172
174
@@ -199,13 +201,13 @@ def test_write_image_string(fig1, format):
199
201
file_name = 'fig1.' + format
200
202
file_path = tmp_dir + file_name
201
203
202
- pio .write_image (fig1 , tmp_dir + file_name ,
204
+ pio .write_image (fig1 , os . path . join ( tmp_dir , file_name ) ,
203
205
format = format , width = 700 , height = 500 )
204
206
205
207
with open (file_path , 'rb' ) as f :
206
208
written_bytes = f .read ()
207
209
208
- with open (images_dir + file_name , 'rb' ) as f :
210
+ with open (os . path . join ( images_dir , file_name ) , 'rb' ) as f :
209
211
expected_bytes = f .read ()
210
212
211
213
assert written_bytes == expected_bytes
@@ -214,7 +216,7 @@ def test_write_image_string(fig1, format):
214
216
def test_write_image_writeable (fig1 , format ):
215
217
216
218
file_name = 'fig1.' + format
217
- with open (images_dir + file_name , 'rb' ) as f :
219
+ with open (os . path . join ( images_dir , file_name ) , 'rb' ) as f :
218
220
expected_bytes = f .read ()
219
221
220
222
mock_file = MagicMock ()
@@ -227,54 +229,54 @@ def test_write_image_writeable(fig1, format):
227
229
def test_write_image_string_format_inference (fig1 , format ):
228
230
# Build file paths
229
231
file_name = 'fig1.' + format
230
- file_path = tmp_dir + file_name
232
+ file_path = os . path . join ( tmp_dir , file_name )
231
233
232
234
# Use file extension to infer image type.
233
- pio .write_image (fig1 , tmp_dir + file_name ,
235
+ pio .write_image (fig1 , os . path . join ( tmp_dir , file_name ) ,
234
236
width = 700 , height = 500 )
235
237
236
238
with open (file_path , 'rb' ) as f :
237
239
written_bytes = f .read ()
238
240
239
- with open (images_dir + file_name , 'rb' ) as f :
241
+ with open (os . path . join ( images_dir , file_name ) , 'rb' ) as f :
240
242
expected_bytes = f .read ()
241
243
242
244
assert written_bytes == expected_bytes
243
245
244
246
245
247
def test_write_image_string_no_extension_failure (fig1 ):
246
248
# No extension
247
- file_path = tmp_dir + 'fig1'
249
+ file_path = os . path . join ( tmp_dir , 'fig1' )
248
250
249
251
# Use file extension to infer image type.
250
252
with pytest .raises (ValueError ) as err :
251
- pio .write_image (fig1 , file_path + 'fig1' )
253
+ pio .write_image (fig1 , os . path . join ( file_path , 'fig1' ) )
252
254
253
255
assert 'add a file extension or specify the type' in str (err .value )
254
256
255
257
256
258
def test_write_image_string_bad_extension_failure (fig1 ):
257
259
# Bad extension
258
- file_path = tmp_dir + 'fig1.bogus'
260
+ file_path = os . path . join ( tmp_dir , 'fig1.bogus' )
259
261
260
262
# Use file extension to infer image type.
261
263
with pytest .raises (ValueError ) as err :
262
- pio .write_image (fig1 , file_path + 'fig1' )
264
+ pio .write_image (fig1 , os . path . join ( file_path , 'fig1' ) )
263
265
264
266
assert 'must be specified as one of the following' in str (err .value )
265
267
266
268
267
269
def test_write_image_string_bad_extension_override (fig1 ):
268
270
# Bad extension
269
271
file_name = 'fig1.bogus'
270
- tmp_path = tmp_dir + file_name
272
+ tmp_path = os . path . join ( tmp_dir , file_name )
271
273
272
274
pio .write_image (fig1 , tmp_path , format = 'eps' , width = 700 , height = 500 )
273
275
274
276
with open (tmp_path , 'rb' ) as f :
275
277
written_bytes = f .read ()
276
278
277
- with open (images_dir + 'fig1.eps' , 'rb' ) as f :
279
+ with open (os . path . join ( images_dir , 'fig1.eps' ) , 'rb' ) as f :
278
280
expected_bytes = f .read ()
279
281
280
282
assert written_bytes == expected_bytes
0 commit comments