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.
 
 
 

94 lines
3.1 KiB

  1. import nilmdb
  2. from nose.tools import *
  3. from nose.tools import assert_raises
  4. import distutils.version
  5. import itertools
  6. import os
  7. import shutil
  8. import sys
  9. import cherrypy
  10. import threading
  11. import urllib2
  12. from urllib2 import urlopen, HTTPError
  13. import Queue
  14. import cStringIO
  15. from test_helpers import *
  16. from nilmdb.layout import *
  17. class TestLayouts(object):
  18. # Some nilmdb.layout tests. Not complete, just fills in missing
  19. # coverage.
  20. def test_layouts(self):
  21. x = nilmdb.layout.named["PrepData"].description()
  22. def test_parsing(self):
  23. # invalid layout
  24. with assert_raises(TypeError) as e:
  25. parser = Parser("NoSuchLayout")
  26. # too little data
  27. parser = Parser("PrepData")
  28. data = ( "1234567890.000000 1.1 2.2 3.3 4.4 5.5\n" +
  29. "1234567890.100000 1.1 2.2 3.3 4.4 5.5\n")
  30. with assert_raises(ParserError) as e:
  31. parser.parse(data)
  32. in_("error", str(e.exception))
  33. # too much data
  34. parser = Parser("PrepData")
  35. data = ( "1234567890.000000 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9\n" +
  36. "1234567890.100000 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9\n")
  37. with assert_raises(ParserError) as e:
  38. parser.parse(data)
  39. in_("error", str(e.exception))
  40. # just right
  41. parser = Parser("PrepData")
  42. data = ( "1234567890.000000 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8\n" +
  43. "1234567890.100000 1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8\n")
  44. parser.parse(data)
  45. eq_(parser.min_timestamp, 1234567890.0)
  46. eq_(parser.max_timestamp, 1234567890.1)
  47. eq_(parser.data, [[1234567890.0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8],
  48. [1234567890.1,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8]])
  49. # try RawData too, with clamping
  50. parser = Parser("RawData")
  51. data = ( "1234567890.000000 1 2 3 4 5 6\n" +
  52. "1234567890.100000 1 2 3 4 5 6\n" )
  53. parser.parse(data)
  54. eq_(parser.data, [[1234567890.0,1,2,3,4,5,6],
  55. [1234567890.1,1,2,3,4,5,6]])
  56. # pass an instantiated class
  57. parser = Parser(RawNotchedData())
  58. data = ( "1234567890.000000 1 2 3 4 5 6 7 8 9\n" +
  59. "1234567890.100000 1 2 3 4 5 6 7 8 9\n" )
  60. parser.parse(data)
  61. # non-monotonic
  62. parser = Parser("RawData")
  63. data = ( "1234567890.100000 1 2 3 4 5 6\n" +
  64. "1234567890.000000 1 2 3 4 5 6\n" )
  65. with assert_raises(ParserError) as e:
  66. parser.parse(data)
  67. in_("not monotonically increasing", str(e.exception))
  68. # RawData with values out of bounds
  69. parser = Parser("RawData")
  70. data = ( "1234567890.000000 1 2 3 4 500000 6\n" +
  71. "1234567890.100000 1 2 3 4 5 6\n" )
  72. with assert_raises(ParserError) as e:
  73. parser.parse(data)
  74. in_("value out of range", str(e.exception))
  75. # Empty data should work but is useless
  76. parser = Parser("RawData")
  77. data = ""
  78. parser.parse(data)
  79. assert(parser.min_timestamp is None)
  80. assert(parser.max_timestamp is None)