You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

220 lines
7.2 KiB

  1. import nilmdb
  2. from nose.tools import *
  3. from nose.tools import assert_raises
  4. import distutils.version
  5. import simplejson as json
  6. import itertools
  7. import os
  8. import shutil
  9. import sys
  10. import cherrypy
  11. import threading
  12. import urllib2
  13. from urllib2 import urlopen, HTTPError
  14. import Queue
  15. import cStringIO
  16. import time
  17. from nilmdb.utils import serializer_proxy
  18. testdb = "tests/testdb"
  19. #@atexit.register
  20. #def cleanup():
  21. # os.unlink(testdb)
  22. from testutil.helpers import *
  23. class Test00Nilmdb(object): # named 00 so it runs first
  24. def test_NilmDB(self):
  25. recursive_unlink(testdb)
  26. with assert_raises(IOError):
  27. nilmdb.NilmDB("/nonexistant-db/foo")
  28. db = nilmdb.NilmDB(testdb)
  29. db.close()
  30. db = nilmdb.NilmDB(testdb, sync=False)
  31. db.close()
  32. # test timer, just to get coverage
  33. capture = cStringIO.StringIO()
  34. old = sys.stdout
  35. sys.stdout = capture
  36. with nilmdb.utils.Timer("test"):
  37. time.sleep(0.01)
  38. sys.stdout = old
  39. in_("test: ", capture.getvalue())
  40. def test_stream(self):
  41. db = nilmdb.NilmDB(testdb, sync=False)
  42. eq_(db.stream_list(), [])
  43. # Bad path
  44. with assert_raises(ValueError):
  45. db.stream_create("foo/bar/baz", "PrepData")
  46. with assert_raises(ValueError):
  47. db.stream_create("/foo", "PrepData")
  48. # Bad layout type
  49. with assert_raises(ValueError):
  50. db.stream_create("/newton/prep", "NoSuchLayout")
  51. db.stream_create("/newton/prep", "PrepData")
  52. db.stream_create("/newton/raw", "RawData")
  53. db.stream_create("/newton/zzz/rawnotch", "RawNotchedData")
  54. # Verify we got 3 streams
  55. eq_(db.stream_list(), [ ["/newton/prep", "PrepData"],
  56. ["/newton/raw", "RawData"],
  57. ["/newton/zzz/rawnotch", "RawNotchedData"]
  58. ])
  59. # Match just one type or one path
  60. eq_(db.stream_list(layout="RawData"), [ ["/newton/raw", "RawData"] ])
  61. eq_(db.stream_list(path="/newton/raw"), [ ["/newton/raw", "RawData"] ])
  62. # Verify that columns were made right (pytables specific)
  63. if "h5file" in db.data.__dict__:
  64. h5file = db.data.h5file
  65. eq_(len(h5file.getNode("/newton/prep").cols), 9)
  66. eq_(len(h5file.getNode("/newton/raw").cols), 7)
  67. eq_(len(h5file.getNode("/newton/zzz/rawnotch").cols), 10)
  68. assert(not h5file.getNode("/newton/prep").colindexed["timestamp"])
  69. assert(not h5file.getNode("/newton/prep").colindexed["c1"])
  70. # Set / get metadata
  71. eq_(db.stream_get_metadata("/newton/prep"), {})
  72. eq_(db.stream_get_metadata("/newton/raw"), {})
  73. meta1 = { "description": "The Data",
  74. "v_scale": "1.234" }
  75. meta2 = { "description": "The Data" }
  76. meta3 = { "v_scale": "1.234" }
  77. db.stream_set_metadata("/newton/prep", meta1)
  78. db.stream_update_metadata("/newton/prep", {})
  79. db.stream_update_metadata("/newton/raw", meta2)
  80. db.stream_update_metadata("/newton/raw", meta3)
  81. eq_(db.stream_get_metadata("/newton/prep"), meta1)
  82. eq_(db.stream_get_metadata("/newton/raw"), meta1)
  83. # fill in some test coverage for start >= end
  84. with assert_raises(nilmdb.server.NilmDBError):
  85. db.stream_remove("/newton/prep", 0, 0)
  86. with assert_raises(nilmdb.server.NilmDBError):
  87. db.stream_remove("/newton/prep", 1, 0)
  88. db.stream_remove("/newton/prep", 0, 1)
  89. db.close()
  90. class TestBlockingServer(object):
  91. def setUp(self):
  92. self.db = serializer_proxy(nilmdb.NilmDB)(testdb, sync=False)
  93. def tearDown(self):
  94. self.db.close()
  95. def test_blocking_server(self):
  96. # Server should fail if the database doesn't have a "_thread_safe"
  97. # property.
  98. with assert_raises(KeyError):
  99. nilmdb.Server(object())
  100. # Start web app on a custom port
  101. self.server = nilmdb.Server(self.db, host = "127.0.0.1",
  102. port = 12380, stoppable = True)
  103. # Run it
  104. event = threading.Event()
  105. def run_server():
  106. self.server.start(blocking = True, event = event)
  107. thread = threading.Thread(target = run_server)
  108. thread.start()
  109. if not event.wait(timeout = 10):
  110. raise AssertionError("server didn't start in 10 seconds")
  111. # Send request to exit.
  112. req = urlopen("http://127.0.0.1:12380/exit/", timeout = 1)
  113. # Wait for it
  114. thread.join()
  115. def geturl(path):
  116. req = urlopen("http://127.0.0.1:12380" + path, timeout = 10)
  117. return req.read()
  118. def getjson(path):
  119. return json.loads(geturl(path))
  120. class TestServer(object):
  121. def setUp(self):
  122. # Start web app on a custom port
  123. self.db = serializer_proxy(nilmdb.NilmDB)(testdb, sync=False)
  124. self.server = nilmdb.Server(self.db, host = "127.0.0.1",
  125. port = 12380, stoppable = False)
  126. self.server.start(blocking = False)
  127. def tearDown(self):
  128. # Close web app
  129. self.server.stop()
  130. self.db.close()
  131. def test_server(self):
  132. # Make sure we can't force an exit, and test other 404 errors
  133. for url in [ "/exit", "/", "/favicon.ico" ]:
  134. with assert_raises(HTTPError) as e:
  135. geturl(url)
  136. eq_(e.exception.code, 404)
  137. # Check version
  138. eq_(distutils.version.LooseVersion(getjson("/version")),
  139. distutils.version.LooseVersion(nilmdb.__version__))
  140. def test_stream_list(self):
  141. # Known streams that got populated by an earlier test (test_nilmdb)
  142. streams = getjson("/stream/list")
  143. eq_(streams, [
  144. ['/newton/prep', 'PrepData'],
  145. ['/newton/raw', 'RawData'],
  146. ['/newton/zzz/rawnotch', 'RawNotchedData'],
  147. ])
  148. streams = getjson("/stream/list?layout=RawData")
  149. eq_(streams, [['/newton/raw', 'RawData']])
  150. streams = getjson("/stream/list?layout=NoSuchLayout")
  151. eq_(streams, [])
  152. def test_stream_metadata(self):
  153. with assert_raises(HTTPError) as e:
  154. getjson("/stream/get_metadata?path=foo")
  155. eq_(e.exception.code, 404)
  156. data = getjson("/stream/get_metadata?path=/newton/prep")
  157. eq_(data, {'description': 'The Data', 'v_scale': '1.234'})
  158. data = getjson("/stream/get_metadata?path=/newton/prep"
  159. "&key=v_scale")
  160. eq_(data, {'v_scale': '1.234'})
  161. data = getjson("/stream/get_metadata?path=/newton/prep"
  162. "&key=v_scale&key=description")
  163. eq_(data, {'description': 'The Data', 'v_scale': '1.234'})
  164. data = getjson("/stream/get_metadata?path=/newton/prep"
  165. "&key=v_scale&key=foo")
  166. eq_(data, {'foo': None, 'v_scale': '1.234'})
  167. data = getjson("/stream/get_metadata?path=/newton/prep"
  168. "&key=foo")
  169. eq_(data, {'foo': None})
  170. def test_insert(self):
  171. # GET instead of POST (no body)
  172. # (actual POST test is done by client code)
  173. with assert_raises(HTTPError) as e:
  174. getjson("/stream/insert?path=/newton/prep&start=0&end=0")
  175. eq_(e.exception.code, 400)