From caa5604d81fe54394f83a884639df889c2895817 Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Mon, 26 Aug 2019 14:58:28 -0400 Subject: [PATCH] Improve code coverage --- nilmdb/client/errors.py | 6 +++--- nilmdb/client/httpclient.py | 4 ++-- nilmdb/server/__init__.py | 7 ------- tests/test_client.py | 25 +++++++++++++++++++++++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/nilmdb/client/errors.py b/nilmdb/client/errors.py index 7aff04a..f6f31e1 100644 --- a/nilmdb/client/errors.py +++ b/nilmdb/client/errors.py @@ -18,14 +18,14 @@ class Error(Exception): s = sprintf("[%s]", self.status) if self.message: s += sprintf(" %s", self.message) - if show_url and self.url: # pragma: no cover + if show_url and self.url: s += sprintf(" (%s)", self.url) - if self.traceback: # pragma: no cover + if self.traceback: s += sprintf("\nServer traceback:\n%s", self.traceback) return s def __str__(self): return self._format_error(show_url = False) - def __repr__(self): # pragma: no cover + def __repr__(self): return self._format_error(show_url = True) class ClientError(Error): pass diff --git a/nilmdb/client/httpclient.py b/nilmdb/client/httpclient.py index 0cf1420..8aa789d 100644 --- a/nilmdb/client/httpclient.py +++ b/nilmdb/client/httpclient.py @@ -42,11 +42,11 @@ class HTTPClient(object): args["status"] = jsonerror["status"] args["message"] = jsonerror["message"] args["traceback"] = jsonerror["traceback"] - except Exception: # pragma: no cover + except Exception: pass if code >= 400 and code <= 499: raise ClientError(**args) - else: # pragma: no cover + else: if code >= 500 and code <= 599: if args["message"] is None: args["message"] = ("(no message; try disabling " + diff --git a/nilmdb/server/__init__.py b/nilmdb/server/__init__.py index e737c85..89c60a8 100644 --- a/nilmdb/server/__init__.py +++ b/nilmdb/server/__init__.py @@ -1,16 +1,9 @@ """nilmdb.server""" - - # Try to set up pyximport to automatically rebuild Cython modules. If # this doesn't work, it's OK, as long as the modules were built externally. # (e.g. python setup.py build_ext --inplace) try: # pragma: no cover - import Cython - import distutils.version - if (distutils.version.LooseVersion(Cython.__version__) < - distutils.version.LooseVersion("0.17")): # pragma: no cover - raise ImportError("Cython version too old") import pyximport pyximport.install(inplace = True, build_in_temp = False) except (ImportError, TypeError): # pragma: no cover diff --git a/tests/test_client.py b/tests/test_client.py index 3f2a5e1..9745c6c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -5,7 +5,7 @@ import nilmdb.client from nilmdb.utils.printf import * from nilmdb.utils import timestamper -from nilmdb.client import ClientError, ServerError +from nilmdb.client import ClientError, ServerError, Error from nilmdb.utils.sort import sort_human import datetime_tz @@ -79,6 +79,27 @@ class TestClient(object): # Bad URLs should give 404, not 500 with assert_raises(ClientError): client.http.get("/stream/create") + + # Test error handling + url = testurl + args = { "url": url, + "status": "400", + "message": "Something went wrong", + "traceback": None } + with assert_raises(ClientError): + client.http._handle_error(url, 400, json.dumps(args)) + with assert_raises(ClientError): + client.http._handle_error(url, 400, "this is not JSON.. {") + args["status"] = "500" + with assert_raises(ServerError): + client.http._handle_error(url, 500, json.dumps(args)) + args["message"] = None + with assert_raises(ServerError): + client.http._handle_error(url, 500, json.dumps(args)) + args["status"] = "600" + with assert_raises(Error): + client.http._handle_error(url, 600, json.dumps(args)) + client.close() def test_client_02_createlist(self): @@ -212,7 +233,7 @@ class TestClient(object): data = timestamper.TimestamperRate(testfile, start, 120) with assert_raises(ClientError) as e: result = client.stream_insert("/newton/no-such-path", data) - in_("404 Not Found", str(e.exception)) + in_("404 Not Found", repr(e.exception)) # Now try reversed timestamps data = timestamper.TimestamperRate(testfile, start, 120)