File: test_rr.py

package info (click to toggle)
python-cachetools 1.1.5-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 248 kB
  • sloc: python: 1,550; makefile: 128; sh: 10
file content (43 lines) | stat: -rw-r--r-- 1,102 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import random
import unittest

from cachetools import RRCache

from . import CacheTestMixin


# random.choice cannot be pickled...
def choice(seq):
    return random.choice(seq)


class RRCacheTest(unittest.TestCase, CacheTestMixin):

    def cache(self, maxsize, choice=choice, missing=None, getsizeof=None):
        return RRCache(maxsize, choice=choice, missing=missing,
                       getsizeof=getsizeof)

    def test_choice(self):
        cache = self.cache(maxsize=2, choice=min)
        self.assertEqual(min, cache.choice)

        cache[1] = 1
        cache[2] = 2
        cache[3] = 3

        self.assertEqual(2, len(cache))
        self.assertEqual(2, cache[2])
        self.assertEqual(3, cache[3])
        self.assertNotIn(1, cache)

        cache[0] = 0
        self.assertEqual(2, len(cache))
        self.assertEqual(0, cache[0])
        self.assertEqual(3, cache[3])
        self.assertNotIn(2, cache)

        cache[4] = 4
        self.assertEqual(2, len(cache))
        self.assertEqual(3, cache[3])
        self.assertEqual(4, cache[4])
        self.assertNotIn(0, cache)