def test_score_scoping(self): timeline = Timeline(connection=self.c1, bucket=self.bucket, max_items=3) timeline.add(self.key, 1, 3) timeline.add(self.key, 2, 2) timeline.add(self.key, 3, 1) results = timeline.get(self.key) self.assertEqual(len(results), 3) self.assertEquals(results[0], 1) self.assertEquals(results[1], 2) self.assertEquals(results[2], 3) timeline.add(self.key, 4, 0) results = timeline.get(self.key) self.assertEqual(len(results), 3) self.assertEquals(results[0], 1) self.assertEquals(results[1], 2) self.assertEquals(results[2], 3) timeline.add(self.key, 5, 5) results = timeline.get(self.key) self.assertEqual(len(results), 3) self.assertEquals(results[0], 5) self.assertEquals(results[1], 1) self.assertEquals(results[2], 2)
def test_delete(self): timeline = Timeline(connection=self.c1, bucket=self.bucket, max_items=3) now = datetime.utcnow() timeline.add(self.key, 1, now) self.assertEqual(len(timeline.get(self.key)), 1) timeline.delete(self.key, 1, now) self.assertEqual(len(timeline.get(self.key)), 0)
def test_max_items(self): """ Verify items > max_items are removed. """ timeline = Timeline(connection=self.c1, bucket=self.bucket, max_items=3) now = datetime.utcnow() timeline.add(self.key, 1, now) timeline.add(self.key, 2, now) timeline.add(self.key, 3, now) self.assertEqual(len(timeline.get(self.key)), 3) timeline.add(self.key, 4, now) self.assertEqual(len(timeline.get(self.key)), 3)
def test_multi_writers(self): now = datetime.utcnow() t1 = Timeline(connection=self.c1, bucket=self.bucket, max_items=10) t2 = Timeline(connection=self.c2, bucket=self.bucket, max_items=10) t1.add(self.key, 1, now) t2.add(self.key, 2, now + timedelta(minutes=1)) self.assertEqual(t1.get(self.key), [2, 1])
def test_timestamp_scores(self): timeline = Timeline(connection=self.c1, bucket=self.bucket, max_items=3) now = datetime.utcnow() timeline.add(self.key, 1, now) timeline.add(self.key, 2, now + timedelta(seconds=1)) timeline.add(self.key, 3, now + timedelta(seconds=2)) results = timeline.get(self.key) self.assertEqual(len(results), 3) self.assertEquals(results[0], 3) self.assertEquals(results[1], 2) self.assertEquals(results[2], 1)