Browse Source

More complete test coverage of nilmdb.httpclient, yay

git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@10882 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/bxinterval-last
Jim Paris 12 years ago
parent
commit
315bc57ac3
5 changed files with 51 additions and 16 deletions
  1. +2
    -10
      nilmdb/client.py
  2. +4
    -1
      nilmdb/nilmdb.py
  3. +6
    -3
      nilmdb/server.py
  4. +37
    -0
      tests/test_client.py
  5. +2
    -2
      tests/test_cmdline.py

+ 2
- 10
nilmdb/client.py View File

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

+ 4
- 1
nilmdb/nilmdb.py View File

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


+ 6
- 3
nilmdb/server.py View File

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


+ 37
- 0
tests/test_client.py View File

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

+ 2
- 2
tests/test_cmdline.py View File

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


Loading…
Cancel
Save