Ejemplo n.º 1
0
class TestTimeSeries(TestCase):
    def add_ts(self, ts, count, cat_id=None):
        if not cat_id:
            cat_id = self.cat_id
        self.r.zadd(
            "ts_%s" % self.cat_id, TimeSeries.generate_ts_value(ts, count), ts
        )

    def setUp(self):
        self.r = redis_util.MockRedis(share_state=False)
        self.cat_id = "foobar"
        self.time_series = TimeSeries(self.r, self.cat_id)

    def test_get_all(self):
        start_ts = 100
        expected_counts = [1, 2, 3]
        for i, count in enumerate(expected_counts):
            self.add_ts(start_ts+i, count)

        ts_list = self.time_series.all()
        self.assertEqual(len(ts_list), len(expected_counts))
        for ts, count in ts_list:
            self.assertTrue(count in expected_counts)

    def test_get_range(self):
        for count, ts in enumerate(range(100, 120)):
            self.add_ts(ts, count)

        ts_list = self.time_series.range(110, 115)
        self.assertEqual(len(ts_list), 6)
Ejemplo n.º 2
0
    def test_simple_archive(self):
        count = 10

        self.r.hset(self.counter, self.start_time-1, count)
        TimeSeries.archive_cat_counts(self.r, self.cat_id, self.start_time)
        self.assertRaises(KeyError, self.r.hget, self.counter, self.start_time-1)

        ts = self.r.zrange("ts_%s" % self.cat_id, 0, -1, withscores=True)
        self.assertEqual(len(ts), 1)
        self.assertEqual(ts_to_hits(ts[0]), 10)
Ejemplo n.º 3
0
    def test_archive_skip(self):
        """Test that the archive method skips new entries"""

        self.r.hset(self.counter, self.start_time-1, 1)
        self.r.hset(self.counter, self.start_time+1, 2)
        TimeSeries.archive_cat_counts(self.r, self.cat_id, self.start_time)

        self.assertRaises(KeyError, self.r.hget, self.counter, self.start_time-1)
        self.assertTrue(self.r.hget(self.counter, self.start_time+1))

        ts = self.r.zrange("ts_%s" % self.cat_id, 0, -1, withscores=True)
        self.assertEqual(len(ts), 1)
        self.assertEqual(ts_to_hits(ts[0]), 1)
Ejemplo n.º 4
0
    def test_multiple_archive(self):
        for step in [1,2]:
            self.r.hset(self.counter, self.start_time-step, 1)

        TimeSeries.archive_cat_counts(self.r, self.cat_id, self.start_time)

        for step in [1, 2]:
            self.assertRaises(
                KeyError, self.r.hget, self.counter, self.start_time-step)

        ts = self.r.zrange("ts_%s" % self.cat_id, 0, -1, withscores=True)
        self.assertEqual(len(ts), 2)
        for record in ts:
            self.assertEqual(ts_to_hits(record), 1)
Ejemplo n.º 5
0
 def setUp(self):
     self.r = redis_util.MockRedis(share_state=False)
     self.cat_id = "foobar"
     self.time_series = TimeSeries(self.r, self.cat_id)
Ejemplo n.º 6
0
 def add_ts(self, ts, count, cat_id=None):
     if not cat_id:
         cat_id = self.cat_id
     self.r.zadd(
         "ts_%s" % self.cat_id, TimeSeries.generate_ts_value(ts, count), ts
     )