From 315bc57ac39b5257c512e58b08a41a3a7c52e8cd Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Wed, 23 May 2012 20:00:01 +0000 Subject: [PATCH] More complete test coverage of nilmdb.httpclient, yay git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@10882 ddd99763-3ecb-0310-9145-efcb8ce7c51f --- nilmdb/client.py | 12 ++---------- nilmdb/nilmdb.py | 5 ++++- nilmdb/server.py | 9 ++++++--- tests/test_client.py | 37 +++++++++++++++++++++++++++++++++++++ tests/test_cmdline.py | 4 ++-- 5 files changed, 51 insertions(+), 16 deletions(-) diff --git a/nilmdb/client.py b/nilmdb/client.py index 7cf8acd..09d676e 100644 --- a/nilmdb/client.py +++ b/nilmdb/client.py @@ -127,7 +127,7 @@ class Client(object): def stream_extract(self, path, start = None, end = None, bare = False): """ Extract data from a stream. Returns a generator that yields - chunks of ASCII-formatted data that matches the database + lines of ASCII-formatted data that matches the database layout for the given path. Multiple requests are made to the server if shorter requests get truncated. """ @@ -139,12 +139,4 @@ class Client(object): if end is not None: params["end"] = repr(end) params["bare"] = bare - - more = True - while more: - (intervals, more) = self.http.get("stream/intervals", params) - for interval in intervals: - yield interval - if more: - # Restart where we left off - params["start"] = repr(intervals[-1][1]) + return self.http.get_gen("stream/intervals", params) diff --git a/nilmdb/nilmdb.py b/nilmdb/nilmdb.py index 97f6391..bb1c799 100644 --- a/nilmdb/nilmdb.py +++ b/nilmdb/nilmdb.py @@ -242,7 +242,10 @@ class NilmDB(object): stream_id = self._stream_id(path) intervals = self._get_intervals(stream_id) - requested = Interval(start or 0, end or 1e12) + try: + requested = Interval(start or 0, end or 1e12) + except IntervalError: + raise NilmDBError("invalid interval") result = [] for n, i in enumerate(intervals.intersection(requested)): if n >= max_results: diff --git a/nilmdb/server.py b/nilmdb/server.py index eab4f2a..8784952 100644 --- a/nilmdb/server.py +++ b/nilmdb/server.py @@ -191,12 +191,15 @@ class Stream(NilmApp): end = float(end) while True: - (intervals, restart) = self.db.stream_intervals(path, start, end) + try: + (intervals, more) = self.db.stream_intervals(path, start, end) + except nilmdb.nilmdb.NilmDBError as e: + raise cherrypy.HTTPError("400 Bad Request", e.message) for interval in intervals: yield json.dumps(interval) + "\n" - if restart == 0: + if more == 0: break - start = restart + start = more # /stream/extract?path=/newton/prep&start=1234567890.0&end=1234567899.0 @cherrypy.expose diff --git a/tests/test_client.py b/tests/test_client.py index 68e7aa4..48ca324 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -12,6 +12,7 @@ import os import sys import threading import cStringIO +import simplejson as json from test_helpers import * @@ -167,3 +168,39 @@ class TestClient(object): result = client.stream_insert("/newton/prep", data) in_("400 Bad Request", str(e.exception)) in_("OverlapError", str(e.exception)) + + def test_client_4_generators(self): + # A lot of the client functionality is already tested by test_cmdline, + # but this gets a bit more coverage that cmdline misses. + client = nilmdb.Client(url = "http://localhost:12380/") + + # Trigger a client error in generator + start = datetime_tz.datetime_tz.smartparse("20120323T2000") + end = datetime_tz.datetime_tz.smartparse("20120323T1000") + with assert_raises(ClientError) as e: + client.stream_intervals("/newton/prep", + start.totimestamp(), + end.totimestamp()).next() + in_("400 Bad Request", str(e.exception)) + in_("invalid interval", str(e.exception)) + + # Trigger a curl error in generator + with assert_raises(ServerError) as e: + client.http.get_gen("http://nosuchurl/").next() + + # Check non-json version of string output + eq_(json.loads(client.http.get("/stream/list",retjson=False)), + client.http.get("/stream/list",retjson=True)) + + # Check non-json version of generator output + for (a, b) in itertools.izip( + client.http.get_gen("/stream/list",retjson=False), + client.http.get_gen("/stream/list",retjson=True)): + eq_(json.loads(a), b) + + # Check PUT with generator out + with assert_raises(ClientError) as e: + client.http.put_gen("stream/insert", "", + { "path": "/newton/prep" }).next() + in_("400 Bad Request", str(e.exception)) + in_("no data provided", str(e.exception)) diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 6ca5c52..2a7da32 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -356,8 +356,8 @@ class TestCmdline(object): self.contain("Error getting stream info") # full dump - # self.ok("extract -a /newton/prep --start 2000-01-01 --end 2020-01-01") - # self.dump() + #self.ok("extract -a /newton/prep --start 2000-01-01 --end 2020-01-01") + #self.dump() def test_cmdline_9_truncated(self): # Test truncated responses by overriding the nilmdb response_size