def test_dot_graph(model, tmpdir): ipydisp = pytest.importorskip('IPython.display') # 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) 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) # format supported by graphviz but not by IPython with pytest.raises(ValueError) as excinfo: dot_graph(model, filename=filename, format='ps') assert "Unknown format" in str(excinfo.value)
def test_filenames_and_formats(model): # Test with a variety of user provided args filenames = [ "modelpdf", "model.pdf", "model.pdf", "modelpdf", "model.pdf.svg", ] formats = ["svg", None, "svg", None, None] targets = [ "modelpdf.svg", "model.pdf", "model.pdf.svg", "modelpdf.png", "model.pdf.svg", ] result_types = { "png": Image, "pdf": type(None), "svg": SVG, } for filename, format, target in zip(filenames, formats, targets): expected_result_type = result_types[target.split(".")[-1]] result = dot_graph(model, filename=filename, format=format) assert os.path.isfile(target) assert isinstance(result, expected_result_type) _ensure_not_exists(target)
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)
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', 'model.pdf.svg' ] formats = ['svg', None, 'svg', None, None] targets = [ 'modelpdf.svg', 'model.pdf', 'model.pdf.svg', 'modelpdf.png', 'model.pdf.svg' ] result_types = { 'png': ipydisp.Image, 'jpeg': ipydisp.Image, 'dot': type(None), 'pdf': type(None), 'svg': ipydisp.SVG, } for filename, format, target in zip(filenames, formats, targets): expected_result_type = result_types[target.split('.')[-1]] result = dot_graph(model, filename=filename, format=format) assert os.path.isfile(target) assert isinstance(result, expected_result_type) _ensure_not_exists(target)
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)")) 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, typ) finally: _ensure_not_exists(target) # format supported by graphviz but not by IPython with pytest.raises(ValueError) as excinfo: dot_graph(model, filename=filename, format="ps") assert "Unknown format" in str(excinfo.value)
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])
def test_dot_graph_no_ipython(model): try: import IPython.display # noqa except ImportError: result = dot_graph(model) assert result is None