Browse Source

Improve code coverage

tags/nilmdb-2.0.0
Jim Paris 3 years ago
parent
commit
caa5604d81
4 changed files with 28 additions and 14 deletions
  1. +3
    -3
      nilmdb/client/errors.py
  2. +2
    -2
      nilmdb/client/httpclient.py
  3. +0
    -7
      nilmdb/server/__init__.py
  4. +23
    -2
      tests/test_client.py

+ 3
- 3
nilmdb/client/errors.py View File

@@ -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


+ 2
- 2
nilmdb/client/httpclient.py View File

@@ -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 " +


+ 0
- 7
nilmdb/server/__init__.py View File

@@ -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


+ 23
- 2
tests/test_client.py View File

@@ -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)


Loading…
Cancel
Save