Skip to content

Allow jpeg format to xfail in test_dot #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 50 additions & 46 deletions xsimlab/tests/test_dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

import pytest
pytest.importorskip("graphviz")
try:
from IPython.display import Image, SVG
ipython_installed = True
except ImportError:
Image = None,
SVG = None
ipython_installed = False

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


def test_dot_graph(model, tmpdir):
ipydisp = pytest.importorskip('IPython.display')

@pytest.mark.skipif(ipython_installed == False,
reason="IPython is not installed")
@pytest.mark.parametrize('format,typ', [
('png', Image),
pytest.mark.xfail(('jpeg', Image),
reason='jpeg not always supported in dot'),
('dot', type(None)),
('pdf', type(None)),
('svg', SVG),
])
def test_dot_graph(model, tmpdir, format, typ):
# Use a name that the shell would interpret specially to ensure that we're
# not vulnerable to shell injection when interacting with `dot`.
filename = str(tmpdir.join('$(touch should_not_get_created.txt)'))

# Map from format extension to expected return type.
result_types = {
'png': ipydisp.Image,
'jpeg': ipydisp.Image,
'dot': type(None),
'pdf': type(None),
'svg': ipydisp.SVG,
}
for format in result_types:
target = '.'.join([filename, format])
_ensure_not_exists(target)
try:
result = dot_graph(model, filename=filename, format=format)
target = '.'.join([filename, format])
_ensure_not_exists(target)
try:
result = dot_graph(model, filename=filename, format=format)

assert not os.path.exists('should_not_get_created.txt')
assert os.path.isfile(target)
assert isinstance(result, result_types[format])
finally:
_ensure_not_exists(target)
assert not os.path.exists('should_not_get_created.txt')
assert os.path.isfile(target)
assert isinstance(result, typ)
finally:
_ensure_not_exists(target)

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


def test_dot_graph_no_filename(tmpdir, model):
ipydisp = pytest.importorskip('IPython.display')

# Map from format extension to expected return type.
result_types = {
'png': ipydisp.Image,
'jpeg': ipydisp.Image,
'dot': type(None),
'pdf': type(None),
'svg': ipydisp.SVG,
}
for format in result_types:
before = tmpdir.listdir()
result = dot_graph(model, filename=None, format=format)
# We shouldn't write any files if filename is None.
after = tmpdir.listdir()
assert before == after
assert isinstance(result, result_types[format])


@pytest.mark.skipif(ipython_installed == False,
reason="IPython is not installed")
@pytest.mark.parametrize('format,typ', [
('png', Image),
pytest.mark.xfail(('jpeg', Image),
reason='jpeg not always supported in dot'),
('dot', type(None)),
('pdf', type(None)),
('svg', SVG),
])
def test_dot_graph_no_filename(tmpdir, model, format, typ):
before = tmpdir.listdir()
result = dot_graph(model, filename=None, format=format)
# We shouldn't write any files if filename is None.
after = tmpdir.listdir()
assert before == after
assert isinstance(result, typ)


@pytest.mark.skipif(ipython_installed == False,
reason="IPython is not installed")
def test_filenames_and_formats(model):
ipydisp = pytest.importorskip('IPython.display')

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

result_types = {
'png': ipydisp.Image,
'jpeg': ipydisp.Image,
'dot': type(None),
'png': Image,
'pdf': type(None),
'svg': ipydisp.SVG,
'svg': SVG,
}

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