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.
 
 
 

89 lines
2.8 KiB

  1. import nilmdb
  2. from nilmdb.printf import *
  3. import datetime_tz
  4. from nose.tools import *
  5. from nose.tools import assert_raises
  6. import os
  7. import sys
  8. import cStringIO
  9. def eq_(a, b):
  10. if not a == b:
  11. raise AssertionError("%r != %r" % (a, b))
  12. def ne_(a, b):
  13. if not a != b:
  14. raise AssertionError("unexpected %r == %r" % (a, b))
  15. class TestTimestamper(object):
  16. # Not a very comprehensive test, but it's good enough.
  17. def test_timestamper(self):
  18. def join(list):
  19. return "\n".join(list) + "\n"
  20. start = datetime_tz.datetime_tz.smartparse("03/24/2012").totimestamp()
  21. lines_in = [ "hello", "world", "hello world", "# commented out" ]
  22. lines_out = [ "1332561600.000000 hello",
  23. "1332561600.000125 world",
  24. "1332561600.000250 hello world" ]
  25. # full
  26. input = cStringIO.StringIO(join(lines_in))
  27. ts = nilmdb.timestamper.TimestamperRate(input, start, 8000)
  28. foo = ts.readlines()
  29. eq_(foo, join(lines_out))
  30. # first 30 or so bytes means the first 2 lines
  31. input = cStringIO.StringIO(join(lines_in))
  32. ts = nilmdb.timestamper.TimestamperRate(input, start, 8000)
  33. foo = ts.readlines(30)
  34. eq_(foo, join(lines_out[0:2]))
  35. # stop iteration early
  36. input = cStringIO.StringIO(join(lines_in))
  37. ts = nilmdb.timestamper.TimestamperRate(input, start, 8000,
  38. 1332561600.000200)
  39. foo = ""
  40. for line in ts:
  41. foo += line
  42. eq_(foo, join(lines_out[0:2]))
  43. # stop iteration early (readlines)
  44. input = cStringIO.StringIO(join(lines_in))
  45. ts = nilmdb.timestamper.TimestamperRate(input, start, 8000,
  46. 1332561600.000200)
  47. foo = ts.readlines()
  48. eq_(foo, join(lines_out[0:2]))
  49. # stop iteration really early
  50. input = cStringIO.StringIO(join(lines_in))
  51. ts = nilmdb.timestamper.TimestamperRate(input, start, 8000,
  52. 1332561600.000000)
  53. foo = ts.readlines()
  54. eq_(foo, "")
  55. # use iterator
  56. input = cStringIO.StringIO(join(lines_in))
  57. ts = nilmdb.timestamper.TimestamperRate(input, start, 8000)
  58. foo = ""
  59. for line in ts:
  60. foo += line
  61. eq_(foo, join(lines_out))
  62. # check that TimestamperNow gives similar result
  63. input = cStringIO.StringIO(join(lines_in))
  64. ts = nilmdb.timestamper.TimestamperNow(input)
  65. foo = ts.readlines()
  66. ne_(foo, join(lines_out))
  67. eq_(len(foo), len(join(lines_out)))
  68. # Test passing a file (should be empty)
  69. ts = nilmdb.timestamper.TimestamperNow("/dev/null")
  70. for line in ts:
  71. raise AssertionError
  72. ts.close()