Skip to content

Commit

Permalink
test backend.view with mock
Browse files Browse the repository at this point in the history
  • Loading branch information
xflr6 committed May 9, 2017
1 parent 31c2d73 commit f3a6ce0
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ before_install:
- sudo apt-get install -y graphviz
install:
- pip install -e .
- pip install "pytest>=3" pytest-cov coveralls
- pip install mock "pytest>=3" pytest-cov coveralls
script: pytest
after_success: if [[ $TRAVIS_PYTHON_VERSION == 3.6* ]]; then coveralls; fi
2 changes: 1 addition & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Changelog
Version 0.7.1 (in development)
------------------------------


Fix graphivz.view exception on unsupported platform.


Version 0.7
Expand Down
2 changes: 1 addition & 1 deletion graphviz/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def view(filepath):
"""
try:
view_func = getattr(view, PLATFORM)
except KeyError:
except AttributeError:
raise RuntimeError('platform %r not supported' % PLATFORM)
view_func(filepath)

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# exact dependency versions used in development
# dependencies for testing and building
mock
pytest>=3
pytest-cov
coverage
Expand Down
45 changes: 37 additions & 8 deletions tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@

import subprocess

import mock
import pytest

from graphviz.backend import render, pipe


def test_render_filepath_missing():
with pytest.raises(subprocess.CalledProcessError) as e:
render('dot', 'pdf', 'doesnotexist')
assert e.value.returncode == 2
from graphviz.backend import render, pipe, view


def test_render_engine_unknown():
Expand All @@ -25,7 +20,13 @@ def test_render_format_unknown():
e.match(r'format')


def test_pipe_invalid_dot():
def test_render_missingfile():
with pytest.raises(subprocess.CalledProcessError) as e:
render('dot', 'pdf', 'doesnotexist')
assert e.value.returncode == 2


def test_pipe_invalid_data():
with pytest.raises(subprocess.CalledProcessError) as e:
pipe('dot', 'svg', b'spam', quiet=True)
assert e.value.returncode == 1
Expand All @@ -34,3 +35,31 @@ def test_pipe_invalid_dot():
def test_pipe(svg_pattern):
src = pipe('dot', 'svg', b'graph { spam }').decode('ascii')
assert svg_pattern.match(src)


@mock.patch('graphviz.backend.PLATFORM', 'spam')
def test_view_unsupported():
with pytest.raises(RuntimeError) as e:
view('spam')
e.match(r'platform')


@mock.patch('graphviz.backend.PLATFORM', 'darwin')
@mock.patch('subprocess.Popen')
def test_view_darwin(Popen):
view('spam')
Popen.assert_called_once_with(['open', 'spam'])


@mock.patch('graphviz.backend.PLATFORM', 'linux')
@mock.patch('subprocess.Popen')
def test_view_linux(Popen):
view('spam')
Popen.assert_called_once_with(['xdg-open', 'spam'])


@mock.patch('graphviz.backend.PLATFORM', 'windows')
@mock.patch('os.startfile')
def test_view_windows(startfile):
view('spam')
startfile.assert_called_once_with('spam')

0 comments on commit f3a6ce0

Please sign in to comment.