|
- import nilmdb
- from nilmdb.printf import *
- from nilmdb.client import ClientError, ServerError
-
- import datetime_tz
-
- from nose.tools import *
- from nose.tools import assert_raises
- import json
- import itertools
- import distutils.version
- import os
- import shutil
- import sys
- import threading
-
- testdb = "tests/client-testdb"
-
- def eq_(a, b):
- if not a == b:
- raise AssertionError("%r != %r" % (a, b))
-
- def ne_(a, b):
- if not a != b:
- raise AssertionError("unexpected %r == %r" % (a, b))
-
- def setup_module():
- global test_server, test_db
- # Clear out DB
- try:
- shutil.rmtree(testdb)
- except:
- pass
- try:
- os.unlink(testdb)
- except:
- pass
-
- # Start web app on a custom port
- test_db = nilmdb.NilmDB(testdb, sync=False)
- test_server = nilmdb.Server(test_db, host = "127.0.0.1",
- port = 12380, stoppable = False,
- fast_shutdown = True)
- test_server.start(blocking = False)
-
- def teardown_module():
- global test_server, test_db
- # Close web app
- test_server.stop()
- test_db.close()
-
- class TestClient(object):
-
- def test_client_1_basic(self):
- # Test a fake host
- client = nilmdb.Client(url = "http://localhost:1/")
- with assert_raises(nilmdb.ServerError):
- client.version()
-
- # Then a fake URL on a real host
- client = nilmdb.Client(url = "http://localhost:12380/fake/")
- with assert_raises(nilmdb.ClientError):
- client.version()
-
- # Now use the real URL
- client = nilmdb.Client(url = "http://localhost:12380/")
- version = client.version()
- eq_(distutils.version.StrictVersion(version),
- distutils.version.StrictVersion(test_server.version))
-
- def test_client_2_nilmdb(self):
- # Basic stream tests, like those in test_nilmdb:test_stream
- client = nilmdb.Client(url = "http://localhost:12380/")
-
- # Database starts empty
- eq_(client.stream_list(), [])
-
- # Bad path
- with assert_raises(ClientError):
- client.stream_create("foo/bar/baz", "PrepData")
- with assert_raises(ClientError):
- client.stream_create("/foo", "PrepData")
- # Bad layout type
- with assert_raises(ClientError):
- client.stream_create("/newton/prep", "NoSuchLayout")
- # Bad index column
- with assert_raises(ClientError) as e:
- client.stream_create("/newton/prep", "PrepData", ["nonexistant"])
- assert("KeyError: nonexistant" in str(e.exception))
- client.stream_create("/newton/prep", "PrepData")
- client.stream_create("/newton/raw", "RawData")
- client.stream_create("/newton/zzz/rawnotch", "RawNotchedData")
-
- # Verify we got 3 streams
- eq_(client.stream_list(), [ ["/newton/prep", "PrepData"],
- ["/newton/raw", "RawData"],
- ["/newton/zzz/rawnotch", "RawNotchedData"]
- ])
- # Match just one type or one path
- eq_(client.stream_list(layout="RawData"), [ ["/newton/raw", "RawData"] ])
- eq_(client.stream_list(path="/newton/raw"), [ ["/newton/raw", "RawData"] ])
-
- # Set / get metadata
- eq_(client.stream_get_metadata("/newton/prep"), {})
- eq_(client.stream_get_metadata("/newton/raw"), {})
- meta1 = { "description": "The Data",
- "v_scale": "1.234" }
- meta2 = { "description": "The Data" }
- meta3 = { "v_scale": "1.234" }
- client.stream_set_metadata("/newton/prep", meta1)
- client.stream_update_metadata("/newton/prep", {})
- client.stream_update_metadata("/newton/raw", meta2)
- client.stream_update_metadata("/newton/raw", meta3)
- eq_(client.stream_get_metadata("/newton/prep"), meta1)
- eq_(client.stream_get_metadata("/newton/raw"), meta1)
- eq_(client.stream_get_metadata("/newton/raw", [ "description" ] ), meta2)
- eq_(client.stream_get_metadata("/newton/raw", [ "description",
- "v_scale" ] ), meta1)
-
- # test wrong types (list instead of dict)
- with assert_raises(ClientError):
- client.stream_set_metadata("/newton/prep", [1,2,3])
- with assert_raises(ClientError):
- client.stream_update_metadata("/newton/prep", [1,2,3])
-
- def test_client_3_insert(self):
- client = nilmdb.Client(url = "http://localhost:12380/")
-
- datetime_tz.localtz_set("America/New_York")
-
- with open("tests/data/prep-20120323T1000") as prepdata:
- start = datetime_tz.datetime_tz.smartparse("20120323T1000")
- client.stream_insert_nots("/newton/prep", prepdata,
- start, rate)
-
- # insert with start/end
- with open("tests/data/prep-20120323T1002") as prepdata:
- start = datetime_tz.datetime_tz.smartparse("20120323T1002")
- end = start + datetime_tz.timedelta(minutes=2)
- client.stream_insert_untimed("/newton/prep", prepdata,
- start = start, end = end)
-
- # TODO: Just start writing the client side of this as if it works.
- # Then fill things in to make it so!
- pass
|