| 1 | # unit tests for the cache module |
|---|
| 2 | |
|---|
| 3 | from logilab.common.testlib import TestCase, unittest_main |
|---|
| 4 | from logilab.common.cache import Cache |
|---|
| 5 | |
|---|
| 6 | class CacheTestCase(TestCase): |
|---|
| 7 | |
|---|
| 8 | def setUp(self): |
|---|
| 9 | self.cache = Cache(5) |
|---|
| 10 | |
|---|
| 11 | def test_setitem1(self): |
|---|
| 12 | """Checks that the setitem method works""" |
|---|
| 13 | self.cache[1] = 'foo' |
|---|
| 14 | self.assert_(self.cache.data[1] == 'foo',"1 : 'foo' is not in cache.data") |
|---|
| 15 | self.assert_(len(self.cache._usage) == 1, "lenght of usage list is not 1") |
|---|
| 16 | self.assert_(self.cache._usage[-1] == 1, '1 is not the most recently used key') |
|---|
| 17 | self.assert_(self.cache._usage.sort() == self.cache.data.keys().sort(), "usage list and data keys are different") |
|---|
| 18 | |
|---|
| 19 | def test_setitem2(self): |
|---|
| 20 | """Checks that the setitem method works for multiple items""" |
|---|
| 21 | self.cache[1] = 'foo' |
|---|
| 22 | self.cache[2] = 'bar' |
|---|
| 23 | self.assert_(self.cache.data[2] == 'bar',"2 : 'bar' is not in cache.data") |
|---|
| 24 | self.assert_(len(self.cache._usage) == 2, "lenght of usage list is not 2") |
|---|
| 25 | self.assert_(self.cache._usage[-1] == 2, '1 is not the most recently used key') |
|---|
| 26 | self.assert_(self.cache._usage.sort() == self.cache.data.keys().sort(), "usage list and data keys are different") |
|---|
| 27 | |
|---|
| 28 | def test_setitem3(self): |
|---|
| 29 | """Checks that the setitem method works when replacing an element in the cache""" |
|---|
| 30 | self.cache[1] = 'foo' |
|---|
| 31 | self.cache[1] = 'bar' |
|---|
| 32 | self.assert_(self.cache.data[1] == 'bar',"1 : 'bar' is not in cache.data") |
|---|
| 33 | self.assert_(len(self.cache._usage) == 1, "lenght of usage list is not 1") |
|---|
| 34 | self.assert_(self.cache._usage[-1] == 1, '1 is not the most recently used key') |
|---|
| 35 | self.assert_(self.cache._usage.sort() == self.cache.data.keys().sort(), "usage list and data keys are different") |
|---|
| 36 | |
|---|
| 37 | def test_recycling1(self): |
|---|
| 38 | """Checks the removal of old elements""" |
|---|
| 39 | self.cache[1] = 'foo' |
|---|
| 40 | self.cache[2] = 'bar' |
|---|
| 41 | self.cache[3] = 'baz' |
|---|
| 42 | self.cache[4] = 'foz' |
|---|
| 43 | self.cache[5] = 'fuz' |
|---|
| 44 | self.cache[6] = 'spam' |
|---|
| 45 | self.assert_(not self.cache.data.has_key(1), 'key 1 has not been suppressed from the cache dictionnary') |
|---|
| 46 | self.assert_(1 not in self.cache._usage, 'key 1 has not been suppressed from the cache LRU list') |
|---|
| 47 | self.assert_(len(self.cache._usage) == 5, "lenght of usage list is not 5") |
|---|
| 48 | self.assert_(self.cache._usage[-1] == 6, '6 is not the most recently used key') |
|---|
| 49 | self.assert_(self.cache._usage.sort() == self.cache.data.keys().sort(), "usage list and data keys are different") |
|---|
| 50 | |
|---|
| 51 | def test_recycling2(self): |
|---|
| 52 | """Checks that accessed elements get in the front of the list""" |
|---|
| 53 | self.cache[1] = 'foo' |
|---|
| 54 | self.cache[2] = 'bar' |
|---|
| 55 | self.cache[3] = 'baz' |
|---|
| 56 | self.cache[4] = 'foz' |
|---|
| 57 | a = self.cache[1] |
|---|
| 58 | self.assert_(a == 'foo') |
|---|
| 59 | self.assert_(self.cache._usage[-1] == 1, '1 is not the most recently used key') |
|---|
| 60 | self.assert_(self.cache._usage.sort() == self.cache.data.keys().sort(), "usage list and data keys are different") |
|---|
| 61 | |
|---|
| 62 | def test_delitem(self): |
|---|
| 63 | """Checks that elements are removed from both element dict and element |
|---|
| 64 | list. |
|---|
| 65 | """ |
|---|
| 66 | self.cache['foo'] = 'bar' |
|---|
| 67 | del self.cache['foo'] |
|---|
| 68 | self.assert_('foo' not in self.cache.data.keys(),"Element 'foo' was not removed cache dictionnary") |
|---|
| 69 | self.assert_('foo' not in self.cache._usage,"Element 'foo' was not removed usage list") |
|---|
| 70 | self.assert_(self.cache._usage.sort() == self.cache.data.keys().sort(), "usage list and data keys are different") |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | def test_nullsize(self): |
|---|
| 74 | """Checks that a 'NULL' size cache doesn't store anything |
|---|
| 75 | """ |
|---|
| 76 | null_cache = Cache(0) |
|---|
| 77 | null_cache['foo'] = 'bar' |
|---|
| 78 | self.assert_(null_cache.size == 0, 'Cache size should be O, not %d' % \ |
|---|
| 79 | null_cache.size) |
|---|
| 80 | self.assert_(len(null_cache) == 0, 'Cache should be empty !') |
|---|
| 81 | # Assert null_cache['foo'] raises a KeyError |
|---|
| 82 | self.assertRaises(KeyError, null_cache.__getitem__, 'foo') |
|---|
| 83 | # Deleting element should not raise error |
|---|
| 84 | del null_cache['foo'] |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | if __name__ == "__main__": |
|---|
| 88 | unittest_main() |
|---|
| 89 | |
|---|
| 90 | |
|---|