コード例 #1
0
    def setUp(self):
        self.key = 'tss_test'
        self.now = time.time()

        self.tss = TimeSortedSet(redis.Redis(), self.key)
コード例 #2
0
ファイル: test_sets.py プロジェクト: indatalabs/redset
    def setUp(self):
        self.key = 'tss_test'
        self.now = time.time()

        self.tss = TimeSortedSet(redis.Redis(), self.key)
コード例 #3
0
class TimeSortedSetTest(unittest.TestCase):
    def setUp(self):
        self.key = 'tss_test'
        self.now = time.time()

        self.tss = TimeSortedSet(redis.Redis(), self.key)

    def tearDown(self):
        self.tss.clear()

    def test_length(self):
        for i in range(5):
            self.tss.add(i)

        self.assertEquals(
            len(self.tss),
            5,
        )

    def test_contains(self):
        for i in range(5):
            self.tss.add(i)

        self.assertTrue(0 in self.tss)

        self.assertFalse(-1 in self.tss)

    def test_add_at(self):
        for i in range(5):
            self.tss.add(i, score=(self.now - i))

        self.assertEquals(
            [str(i) for i in reversed(range(5))],
            [self.tss.pop() for __ in range(5)],
        )

    def test_add_dup(self):
        for i in range(5):
            self.tss.add(i)

        dup_added_at = self.now + 10
        self.tss.add(0, score=dup_added_at)

        self.assertEquals(
            len(self.tss),
            5,
        )

        self.assertEquals(
            int(self.tss.score(0)),
            int(dup_added_at),
        )

    def test_clear(self):
        self.assertFalse(self.tss.clear())

        for i in range(5):
            self.tss.add(i)

        self.assertTrue(self.tss.clear())
        self.assertEquals(
            len(self.tss),
            0,
        )

    def test_discard(self):
        self.tss.add(0)

        self.assertTrue(self.tss.discard(0))
        self.assertFalse(self.tss.discard(0))

    def test_peek(self):
        with self.assertRaises(KeyError):
            self.tss.peek()

        self.tss.add(0)

        for __ in range(2):
            self.assertEquals(
                self.tss.peek(),
                '0',
            )

        with self.assertRaises(KeyError):
            self.tss.peek(position=1)

        self.tss.add(1)

        for __ in range(2):
            self.assertEquals(
                self.tss.peek(position=1),
                '1',
            )

    def test_score(self):
        self.assertEquals(
            None,
            self.tss.score(0),
        )

        self.tss.add(0, self.now)

        self.assertEquals(
            int(self.now),
            int(self.tss.score(0)),
        )

    def test_oldest_time(self):
        self.assertEquals(
            None,
            self.tss.peek_score(),
        )

        for i in range(3):
            self.tss.add(i, self.now - i)

        self.assertEquals(
            int(self.now - 2),
            int(self.tss.peek_score()),
        )

        self.tss.pop()

        self.assertEquals(
            int(self.now - 1),
            int(self.tss.peek_score()),
        )
コード例 #4
0
ファイル: test_sets.py プロジェクト: indatalabs/redset
class TimeSortedSetTest(unittest.TestCase):

    def setUp(self):
        self.key = 'tss_test'
        self.now = time.time()

        self.tss = TimeSortedSet(redis.Redis(), self.key)

    def tearDown(self):
        self.tss.clear()

    def test_length(self):
        for i in range(5):
            self.tss.add(i)

        self.assertEquals(
            len(self.tss),
            5,
        )

    def test_contains(self):
        for i in range(5):
            self.tss.add(i)

        self.assertTrue(
            0 in self.tss
        )

        self.assertFalse(
            -1 in self.tss
        )

    def test_add_at(self):
        for i in range(5):
            self.tss.add(i, score=(self.now - i))

        self.assertEquals(
            [str(i) for i in reversed(range(5))],
            [self.tss.pop() for __ in range(5)],
        )

    def test_add_dup(self):
        for i in range(5):
            self.tss.add(i)

        dup_added_at = self.now + 10
        self.tss.add(0, score=dup_added_at)

        self.assertEquals(
            len(self.tss),
            5,
        )

        self.assertEquals(
            int(self.tss.score(0)),
            int(dup_added_at),
        )

    def test_clear(self):
        self.assertFalse(self.tss.clear())

        for i in range(5):
            self.tss.add(i)

        self.assertTrue(self.tss.clear())
        self.assertEquals(
            len(self.tss),
            0,
        )

    def test_discard(self):
        self.tss.add(0)

        self.assertTrue(self.tss.discard(0))
        self.assertFalse(self.tss.discard(0))

    def test_peek(self):
        with self.assertRaises(KeyError):
            self.tss.peek()

        self.tss.add(0)

        for __ in range(2):
            self.assertEquals(
                self.tss.peek(),
                '0',
            )

        with self.assertRaises(KeyError):
            self.tss.peek(position=1)

        self.tss.add(1)

        for __ in range(2):
            self.assertEquals(
                self.tss.peek(position=1),
                '1',
            )

    def test_score(self):
        self.assertEquals(
            None,
            self.tss.score(0),
        )

        self.tss.add(0, self.now)

        self.assertEquals(
            int(self.now),
            int(self.tss.score(0)),
        )

    def test_oldest_time(self):
        self.assertEquals(
            None,
            self.tss.peek_score(),
        )

        for i in range(3):
            self.tss.add(i, self.now - i)

        self.assertEquals(
            int(self.now - 2),
            int(self.tss.peek_score()),
        )

        self.tss.pop()

        self.assertEquals(
            int(self.now - 1),
            int(self.tss.peek_score()),
        )