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.0 KiB

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