Browse Source

Add test for client.stream_insert_context

tags/nilmdb-1.1^2
Jim Paris 9 years ago
parent
commit
bba9ad131e
2 changed files with 75 additions and 4 deletions
  1. +12
    -2
      nilmdb/client/client.py
  2. +63
    -2
      tests/test_client.py

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

@@ -164,8 +164,8 @@ class Client(object):
lines of ASCII-formatted data that matches the database
layout for the given path.

Specify count=True to just get a count of values rather than
the actual data.
Specify count = True to return a count of matching data points
rather than the actual data. The output format is unchanged.
"""
params = {
"path": path,
@@ -179,6 +179,13 @@ class Client(object):

return self.http.get_gen("stream/extract", params, retjson = False)

def stream_count(self, path, start = None, end = None):
"""
Return the number of rows of data in the stream that satisfy
the given timestamps.
"""
return int(self.stream_extract(path, start, end, count = True)[0])

class StreamInserter(object):
"""Object returned by stream_insert_context() that manages
the insertion of rows of data into a particular path.
@@ -257,6 +264,9 @@ class StreamInserter(object):
def insert_line(self, line):
"""Insert a single line of ASCII formatted data. Line
must be terminated with '\\n'."""
if line and (len(line) < 1 or line[-1] != '\n'):
raise ValueError("lines must end in with a newline character")

# Store this new line, but process the previous (old) one.
# This lets us "look ahead" to the next line.
self._line_old = self._line_new


+ 63
- 2
tests/test_client.py View File

@@ -391,12 +391,73 @@ class TestClient(object):
"probably not closing properly.")
if test == 1:
# explicit close
client = nilmdb.Client(url = "http://localhost:12380/")
client = nilmdb.Client(url = testurl)
with assert_raises(ClientError) as e:
client.stream_remove("/newton/prep", 123, 120)
client.close() # remove this to see the failure
elif test == 2:
# use the context manager
with nilmdb.Client(url = "http://localhost:12380/") as c:
with nilmdb.Client(url = testurl) as c:
with assert_raises(ClientError) as e:
c.stream_remove("/newton/prep", 123, 120)

def test_client_10_context(self):
# Test using the client's stream insertion context manager to
# insert data.
client = nilmdb.Client(testurl)

client.stream_create("/context/test", "uint16_1")
with client.stream_insert_context("/context/test") as ctx:
# override _max_data to trigger frequent server updates
ctx._max_data = 15

with assert_raises(ValueError):
ctx.insert_line("100 1")

ctx.insert_line("100 1\n")
ctx.insert_iter([ "101 1\n",
"102 1\n",
"103 1\n" ])
ctx.insert_line("104 1\n")
ctx.insert_line("105 1\n")
ctx.finalize()

ctx.insert_line("106 1\n")
ctx.update_end(106.5)
ctx.finalize()
ctx.update_start(106.8)
ctx.insert_line("107 1\n")
ctx.insert_line("108 1\n")
ctx.insert_line("109 1\n")
ctx.insert_line("110 1\n")
ctx.insert_line("111 1\n")
ctx.update_end(113)
ctx.insert_line("112 1\n")
ctx.update_end(114)
ctx.insert_line("113 1\n")
ctx.update_end(115)
ctx.insert_line("114 1\n")
ctx.finalize()

with assert_raises(ClientError):
with client.stream_insert_context("/context/test", 100, 200) as ctx:
ctx.insert_line("115 1\n")

with assert_raises(ClientError):
with client.stream_insert_context("/context/test", 200, 300) as ctx:
ctx.insert_line("115 1\n")

with client.stream_insert_context("/context/test", 200, 300) as ctx:
# make sure our override wasn't permanent
ne_(ctx._max_data, 15)
ctx.insert_line("225 1\n")
ctx.finalize()

eq_(list(client.stream_intervals("/context/test")),
[ [ 100, 105.000001 ],
[ 106, 106.5 ],
[ 106.8, 115 ],
[ 200, 300 ] ])

client.stream_destroy("/context/test")
client.close()

Loading…
Cancel
Save