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.
 
 
 

139 lines
5.2 KiB

  1. import nilmdb
  2. from nilmdb.printf import *
  3. from nilmdb.client import ClientError, ServerError
  4. import datetime_tz
  5. from nose.tools import *
  6. from nose.tools import assert_raises
  7. import json
  8. import itertools
  9. import distutils.version
  10. import os
  11. import sys
  12. import threading
  13. from test_helpers import *
  14. testdb = "tests/client-testdb"
  15. def setup_module():
  16. global test_server, test_db
  17. # Clear out DB
  18. recursive_unlink(testdb)
  19. # Start web app on a custom port
  20. test_db = nilmdb.NilmDB(testdb, sync = False)
  21. test_server = nilmdb.Server(test_db, host = "127.0.0.1",
  22. port = 12380, stoppable = False,
  23. fast_shutdown = True)
  24. test_server.start(blocking = False)
  25. def teardown_module():
  26. global test_server, test_db
  27. # Close web app
  28. test_server.stop()
  29. test_db.close()
  30. class TestClient(object):
  31. def test_client_1_basic(self):
  32. # Test a fake host
  33. client = nilmdb.Client(url = "http://localhost:1/")
  34. with assert_raises(nilmdb.client.ServerError):
  35. client.version()
  36. # Then a fake URL on a real host
  37. client = nilmdb.Client(url = "http://localhost:12380/fake/")
  38. with assert_raises(nilmdb.client.ClientError):
  39. client.version()
  40. # Now use the real URL
  41. client = nilmdb.Client(url = "http://localhost:12380/")
  42. version = client.version()
  43. eq_(distutils.version.StrictVersion(version),
  44. distutils.version.StrictVersion(test_server.version))
  45. def test_client_2_nilmdb(self):
  46. # Basic stream tests, like those in test_nilmdb:test_stream
  47. client = nilmdb.Client(url = "http://localhost:12380/")
  48. # Database starts empty
  49. eq_(client.stream_list(), [])
  50. # Bad path
  51. with assert_raises(ClientError):
  52. client.stream_create("foo/bar/baz", "PrepData")
  53. with assert_raises(ClientError):
  54. client.stream_create("/foo", "PrepData")
  55. # Bad layout type
  56. with assert_raises(ClientError):
  57. client.stream_create("/newton/prep", "NoSuchLayout")
  58. # Bad index column
  59. with assert_raises(ClientError) as e:
  60. client.stream_create("/newton/prep", "PrepData", ["nonexistant"])
  61. in_("KeyError: nonexistant", str(e.exception))
  62. client.stream_create("/newton/prep", "PrepData")
  63. client.stream_create("/newton/raw", "RawData")
  64. client.stream_create("/newton/zzz/rawnotch", "RawNotchedData")
  65. # Verify we got 3 streams
  66. eq_(client.stream_list(), [ ["/newton/prep", "PrepData"],
  67. ["/newton/raw", "RawData"],
  68. ["/newton/zzz/rawnotch", "RawNotchedData"]
  69. ])
  70. # Match just one type or one path
  71. eq_(client.stream_list(layout="RawData"), [ ["/newton/raw", "RawData"] ])
  72. eq_(client.stream_list(path="/newton/raw"), [ ["/newton/raw", "RawData"] ])
  73. # Set / get metadata
  74. eq_(client.stream_get_metadata("/newton/prep"), {})
  75. eq_(client.stream_get_metadata("/newton/raw"), {})
  76. meta1 = { "description": "The Data",
  77. "v_scale": "1.234" }
  78. meta2 = { "description": "The Data" }
  79. meta3 = { "v_scale": "1.234" }
  80. client.stream_set_metadata("/newton/prep", meta1)
  81. client.stream_update_metadata("/newton/prep", {})
  82. client.stream_update_metadata("/newton/raw", meta2)
  83. client.stream_update_metadata("/newton/raw", meta3)
  84. eq_(client.stream_get_metadata("/newton/prep"), meta1)
  85. eq_(client.stream_get_metadata("/newton/raw"), meta1)
  86. eq_(client.stream_get_metadata("/newton/raw", [ "description" ] ), meta2)
  87. eq_(client.stream_get_metadata("/newton/raw", [ "description",
  88. "v_scale" ] ), meta1)
  89. # test wrong types (list instead of dict)
  90. with assert_raises(ClientError):
  91. client.stream_set_metadata("/newton/prep", [1,2,3])
  92. with assert_raises(ClientError):
  93. client.stream_update_metadata("/newton/prep", [1,2,3])
  94. def test_client_3_insert(self):
  95. client = nilmdb.Client(url = "http://localhost:12380/")
  96. datetime_tz.localtz_set("America/New_York")
  97. testfile = "tests/data/prep-20120323T1000"
  98. start = datetime_tz.datetime_tz.smartparse("20120323T1000")
  99. rate = 120
  100. # First try a nonexistent path
  101. data = nilmdb.timestamper.TimestamperRate(testfile, start, 120)
  102. with assert_raises(ClientError) as e:
  103. result = client.stream_insert("/newton/no-such-path", data)
  104. in_("404 Not Found", str(e.exception))
  105. # Now try reversed timestamps
  106. data = nilmdb.timestamper.TimestamperRate(testfile, start, 120)
  107. data = reversed(list(data))
  108. with assert_raises(ClientError) as e:
  109. result = client.stream_insert("/newton/prep", data)
  110. in_("400 Bad Request", str(e.exception))
  111. in_("timestamp is not monotonically increasing", str(e.exception))
  112. # Now do the real load
  113. data = nilmdb.timestamper.TimestamperRate(testfile, start, 120)
  114. result = client.stream_insert("/newton/prep", data)
  115. eq_(result, "ok")