diff --git a/README.rst b/README.rst index c214c07..3d87551 100644 --- a/README.rst +++ b/README.rst @@ -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: diff --git a/netdiff/exceptions.py b/netdiff/exceptions.py index 5176ac5..4289c38 100644 --- a/netdiff/exceptions.py +++ b/netdiff/exceptions.py @@ -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 diff --git a/netdiff/parsers/base.py b/netdiff/parsers/base.py index 37612df..c83f544 100644 --- a/netdiff/parsers/base.py +++ b/netdiff/parsers/base.py @@ -1,5 +1,6 @@ import six import json +import os import requests import telnetlib from collections import OrderedDict @@ -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): @@ -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']: @@ -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: @@ -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()] diff --git a/netdiff/tests.py b/netdiff/tests.py index 40ccc71..b374002 100644 --- a/netdiff/tests.py +++ b/netdiff/tests.py @@ -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 diff --git a/tests/base/tests.py b/tests/base/tests.py index 77e8462..0533c70 100644 --- a/tests/base/tests.py +++ b/tests/base/tests.py @@ -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'] @@ -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): @@ -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): diff --git a/tests/olsr/tests.py b/tests/olsr/tests.py index 221352a..2cfe905 100644 --- a/tests/olsr/tests.py +++ b/tests/olsr/tests.py @@ -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 @@ -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 @@ -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