From bba9ad131eec206d56168d23eea3fdbc79736aac Mon Sep 17 00:00:00 2001 From: Jim Paris Date: Tue, 19 Feb 2013 17:19:45 -0500 Subject: [PATCH] Add test for client.stream_insert_context --- nilmdb/client/client.py | 14 +++++++-- tests/test_client.py | 65 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 4 deletions(-) diff --git a/nilmdb/client/client.py b/nilmdb/client/client.py index a51dea8..ebc0575 100644 --- a/nilmdb/client/client.py +++ b/nilmdb/client/client.py @@ -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 diff --git a/tests/test_client.py b/tests/test_client.py index 60f5100..347217c 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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()