import os import random import tempfile import logging import unittest import timeseries class TestDB(unittest.TestCase): def setUp(self): logging.basicConfig(level=logging.INFO) self.tempdir = tempfile.mkdtemp() self.db = timeseries.DBSimpleInt(timeseries.C.INT_MIN, timeseries.C.INT_DAY, self.tempdir, "t-dbsimple") def tearDown(self): self.db.close() for path in os.listdir(self.tempdir): os.remove(os.path.join(self.tempdir, path)) os.rmdir(self.tempdir) class TestEmptyDB(TestDB): def testEmptyDB(self): start=0 end=timeseries.C.INT_DAY self.assertEqual(self.db[start], 0) self.assertEqual(len(os.listdir(self.tempdir)), 1) self.assertEqual(self.db[end-1], 0) self.assertEqual(len(os.listdir(self.tempdir)), 1) self.assertEqual(self.db[end], 0) self.assertEqual(len(os.listdir(self.tempdir)), 2) class TestRandomValues(TestDB): def testRandomValues(self): values={} for i in range(0,100): key = random.randint(0, timeseries.time_ms()) value = random.randint(-2147483648, 2147483647) values[key] = value self.db[key] = value keys = values.keys() random.shuffle(keys) for key in keys: self.assertEqual( self.db[key], values[key] )