Skip to content

Commit 3e2b8b0

Browse files
authored
Allow jpeg format to xfail in test_dot (#46)
* allow jpeg format to xfail in test_dot * fix NameError
1 parent 0303d1e commit 3e2b8b0

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

xsimlab/tests/test_dot.py

+50-46
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
import pytest
66
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
714

815
from xsimlab.dot import to_graphviz, dot_graph, _hash_variable
916
from xsimlab.utils import variables_dict
@@ -83,32 +90,31 @@ def test_to_graphviz_attributes(model):
8390
assert to_graphviz(model, rankdir='BT').graph_attr['rankdir'] == 'BT'
8491

8592

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):
89104
# Use a name that the shell would interpret specially to ensure that we're
90105
# not vulnerable to shell injection when interacting with `dot`.
91106
filename = str(tmpdir.join('$(touch should_not_get_created.txt)'))
92107

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)
106112

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)
112118

113119
# format supported by graphviz but not by IPython
114120
with pytest.raises(ValueError) as excinfo:
@@ -124,28 +130,28 @@ def test_dot_graph_no_ipython(model):
124130
assert result is None
125131

126132

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")
147154
def test_filenames_and_formats(model):
148-
ipydisp = pytest.importorskip('IPython.display')
149155

150156
# Test with a variety of user provided args
151157
filenames = ['modelpdf', 'model.pdf', 'model.pdf', 'modelpdf',
@@ -155,11 +161,9 @@ def test_filenames_and_formats(model):
155161
'model.pdf.svg']
156162

157163
result_types = {
158-
'png': ipydisp.Image,
159-
'jpeg': ipydisp.Image,
160-
'dot': type(None),
164+
'png': Image,
161165
'pdf': type(None),
162-
'svg': ipydisp.SVG,
166+
'svg': SVG,
163167
}
164168

165169
for filename, format, target in zip(filenames, formats, targets):

0 commit comments

Comments
 (0)