Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ Alternatively, you can use the ``nose`` command (which has a ton of available op
.. code-block:: shell

nosetests
nosetests tests.olsr
nosetests tests.olsr:TestOlsrParser
nosetests tests.olsr:TestOlsrParser.test_parse

See test coverage with:

Expand Down
12 changes: 10 additions & 2 deletions netdiff/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
class NetParserException(Exception):
class NetdiffException(Exception):
pass


class NetJsonException(Exception):
class NetParserException(NetdiffException):
pass


class NetParserJsonException(NetdiffException):
pass


class NetJsonException(NetdiffException):
pass
9 changes: 5 additions & 4 deletions netdiff/parsers/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import six
import json
import os
import requests
import telnetlib
from collections import OrderedDict
Expand All @@ -9,7 +10,7 @@
except ImportError:
import urllib.parse as urlparse

from ..exceptions import NetParserException, NetJsonException
from ..exceptions import NetParserException, NetParserJsonException, NetJsonException


class BaseParser(object):
Expand Down Expand Up @@ -57,7 +58,7 @@ def _to_python(self, data):
if isinstance(data, six.string_types):
up = urlparse.urlparse(data)
# if it looks like a file path
if True in [data.startswith('./'), data.startswith('../'), data.startswith('/')]:
if os.path.isfile(data):
data = open(data).read()
# if it looks like a HTTP URL
elif up.scheme in ['http', 'https']:
Expand All @@ -75,7 +76,7 @@ def _to_python(self, data):
try:
return json.loads(data)
except ValueError:
raise NetParserException('Could not decode JSON data')
raise NetParserJsonException('Could not decode JSON data')
elif isinstance(data, dict):
return data
else:
Expand All @@ -101,7 +102,7 @@ def json(self, dict=False, **args):
raise NetJsonException('protocol cannot be None')
if self.version is None:
raise NetJsonException('version cannot be None')
if self.metric is None:
if self.metric is None and self.protocol != 'static':
raise NetJsonException('metric cannot be None')
# prepare lists
nodes = [{'id': node} for node in graph.nodes()]
Expand Down
2 changes: 1 addition & 1 deletion netdiff/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TestCase(unittest.TestCase):
def _test_expected_links(self, links, expected_links):
"""
Ensures the contents of links is the same of expected_links,
indipendently from the ordering
independently from the ordering
links and expected_links should be list of tuples.
"""
found = 0
Expand Down
6 changes: 3 additions & 3 deletions tests/base/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from netdiff import get_version
from netdiff.parsers.base import BaseParser
from netdiff.exceptions import NetParserException
from netdiff.exceptions import NetParserException, NetParserJsonException


__all__ = ['TestBaseParser']
Expand All @@ -20,7 +20,7 @@ def test_parse_file(self):
path = '{0}/../static/olsr-2-links.json'.format(dir)
p = BaseParser(path)
self.assertIsInstance(p.original_data, dict)
with self.assertRaises(IOError):
with self.assertRaises(NetParserJsonException):
BaseParser('../wrong.json')

def test_parse_http(self):
Expand All @@ -39,7 +39,7 @@ def test_parse_dict(self):
self.assertIsInstance(p.original_data, dict)

def test_parse_json_exception(self):
with self.assertRaises(NetParserException):
with self.assertRaises(NetParserJsonException):
BaseParser('wrong [] ; .')

def test_parse_exception(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/olsr/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def test_added_1_link(self):
old = OlsrParser(links2)
new = OlsrParser(links3)
result = diff(old, new)
# ensure there are no differences
# ensure there are differences
self.assertEqual(len(result['added']), 1)
self.assertEqual(len(result['removed']), 0)
# ensure 1 link added
Expand All @@ -98,7 +98,7 @@ def test_removed_1_link(self):
self.assertTrue(type(result) is dict)
self.assertTrue(type(result['added']) is list)
self.assertTrue(type(result['removed']) is list)
# ensure there are no differences
# ensure there are differences
self.assertEqual(len(result['added']), 0)
self.assertEqual(len(result['removed']), 1)
# ensure 1 link removed
Expand All @@ -109,7 +109,7 @@ def test_simple_diff(self):
old = OlsrParser(links3)
new = OlsrParser(links5)
result = diff(old, new)
# ensure there are no differences
# ensure there are differences
self.assertEqual(len(result['added']), 3)
self.assertEqual(len(result['removed']), 1)
# ensure 3 links added
Expand Down