Package logilab :: Package common :: Package test :: Module unittest_cache
[frames] | no frames]

Source Code for Module logilab.common.test.unittest_cache

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