4
4
5
5
import pytest
6
6
pytest .importorskip ("graphviz" )
7
+ try :
8
+ from IPython .display import Image , SVG
9
+ ipython_installed = True
10
+ except ImportError :
11
+ Image = None ,
12
+ SVG = None
13
+ ipython_installed = False
7
14
8
15
from xsimlab .dot import to_graphviz , dot_graph , _hash_variable
9
16
from xsimlab .utils import variables_dict
@@ -83,32 +90,31 @@ def test_to_graphviz_attributes(model):
83
90
assert to_graphviz (model , rankdir = 'BT' ).graph_attr ['rankdir' ] == 'BT'
84
91
85
92
86
- def test_dot_graph (model , tmpdir ):
87
- ipydisp = pytest .importorskip ('IPython.display' )
88
-
93
+ @pytest .mark .skipif (ipython_installed == False ,
94
+ reason = "IPython is not installed" )
95
+ @pytest .mark .parametrize ('format,typ' , [
96
+ ('png' , Image ),
97
+ pytest .mark .xfail (('jpeg' , Image ),
98
+ reason = 'jpeg not always supported in dot' ),
99
+ ('dot' , type (None )),
100
+ ('pdf' , type (None )),
101
+ ('svg' , SVG ),
102
+ ])
103
+ def test_dot_graph (model , tmpdir , format , typ ):
89
104
# Use a name that the shell would interpret specially to ensure that we're
90
105
# not vulnerable to shell injection when interacting with `dot`.
91
106
filename = str (tmpdir .join ('$(touch should_not_get_created.txt)' ))
92
107
93
- # Map from format extension to expected return type.
94
- result_types = {
95
- 'png' : ipydisp .Image ,
96
- 'jpeg' : ipydisp .Image ,
97
- 'dot' : type (None ),
98
- 'pdf' : type (None ),
99
- 'svg' : ipydisp .SVG ,
100
- }
101
- for format in result_types :
102
- target = '.' .join ([filename , format ])
103
- _ensure_not_exists (target )
104
- try :
105
- result = dot_graph (model , filename = filename , format = format )
108
+ target = '.' .join ([filename , format ])
109
+ _ensure_not_exists (target )
110
+ try :
111
+ result = dot_graph (model , filename = filename , format = format )
106
112
107
- assert not os .path .exists ('should_not_get_created.txt' )
108
- assert os .path .isfile (target )
109
- assert isinstance (result , result_types [ format ] )
110
- finally :
111
- _ensure_not_exists (target )
113
+ assert not os .path .exists ('should_not_get_created.txt' )
114
+ assert os .path .isfile (target )
115
+ assert isinstance (result , typ )
116
+ finally :
117
+ _ensure_not_exists (target )
112
118
113
119
# format supported by graphviz but not by IPython
114
120
with pytest .raises (ValueError ) as excinfo :
@@ -124,28 +130,28 @@ def test_dot_graph_no_ipython(model):
124
130
assert result is None
125
131
126
132
127
- def test_dot_graph_no_filename (tmpdir , model ):
128
- ipydisp = pytest .importorskip ('IPython.display' )
129
-
130
- # Map from format extension to expected return type.
131
- result_types = {
132
- 'png' : ipydisp .Image ,
133
- 'jpeg' : ipydisp .Image ,
134
- 'dot' : type (None ),
135
- 'pdf' : type (None ),
136
- 'svg' : ipydisp .SVG ,
137
- }
138
- for format in result_types :
139
- before = tmpdir .listdir ()
140
- result = dot_graph (model , filename = None , format = format )
141
- # We shouldn't write any files if filename is None.
142
- after = tmpdir .listdir ()
143
- assert before == after
144
- assert isinstance (result , result_types [format ])
145
-
146
-
133
+ @pytest .mark .skipif (ipython_installed == False ,
134
+ reason = "IPython is not installed" )
135
+ @pytest .mark .parametrize ('format,typ' , [
136
+ ('png' , Image ),
137
+ pytest .mark .xfail (('jpeg' , Image ),
138
+ reason = 'jpeg not always supported in dot' ),
139
+ ('dot' , type (None )),
140
+ ('pdf' , type (None )),
141
+ ('svg' , SVG ),
142
+ ])
143
+ def test_dot_graph_no_filename (tmpdir , model , format , typ ):
144
+ before = tmpdir .listdir ()
145
+ result = dot_graph (model , filename = None , format = format )
146
+ # We shouldn't write any files if filename is None.
147
+ after = tmpdir .listdir ()
148
+ assert before == after
149
+ assert isinstance (result , typ )
150
+
151
+
152
+ @pytest .mark .skipif (ipython_installed == False ,
153
+ reason = "IPython is not installed" )
147
154
def test_filenames_and_formats (model ):
148
- ipydisp = pytest .importorskip ('IPython.display' )
149
155
150
156
# Test with a variety of user provided args
151
157
filenames = ['modelpdf' , 'model.pdf' , 'model.pdf' , 'modelpdf' ,
@@ -155,11 +161,9 @@ def test_filenames_and_formats(model):
155
161
'model.pdf.svg' ]
156
162
157
163
result_types = {
158
- 'png' : ipydisp .Image ,
159
- 'jpeg' : ipydisp .Image ,
160
- 'dot' : type (None ),
164
+ 'png' : Image ,
161
165
'pdf' : type (None ),
162
- 'svg' : ipydisp . SVG ,
166
+ 'svg' : SVG ,
163
167
}
164
168
165
169
for filename , format , target in zip (filenames , formats , targets ):
0 commit comments