38
38
def test_missing(self):
39
39
cache = lru_cache.LRUCache(max_cache=10)
41
self.assertFalse('foo' in cache)
41
self.failIf('foo' in cache)
42
42
self.assertRaises(KeyError, cache.__getitem__, 'foo')
44
44
cache['foo'] = 'bar'
45
45
self.assertEqual('bar', cache['foo'])
46
self.assertTrue('foo' in cache)
47
self.assertFalse('bar' in cache)
46
self.failUnless('foo' in cache)
47
self.failIf('bar' in cache)
49
49
def test_map_None(self):
50
50
# Make sure that we can properly map None as a key.
51
51
cache = lru_cache.LRUCache(max_cache=10)
52
self.assertFalse(None in cache)
52
self.failIf(None in cache)
54
54
self.assertEqual(1, cache[None])
75
75
# With a max cache of 1, adding 'baz' should pop out 'foo'
76
76
cache['baz'] = 'biz'
78
self.assertFalse('foo' in cache)
79
self.assertTrue('baz' in cache)
78
self.failIf('foo' in cache)
79
self.failUnless('baz' in cache)
81
81
self.assertEqual('biz', cache['baz'])
133
133
self.assertEqual([(2, 20), (2, 25)], cleanup_called)
135
def test_cleanup_error_maintains_linked_list(self):
137
def cleanup_func(key, val):
138
cleanup_called.append((key, val))
139
raise ValueError('failure during cleanup')
141
cache = lru_cache.LRUCache(max_cache=10)
143
cache.add(i, i, cleanup=cleanup_func)
144
for i in xrange(10, 20):
145
self.assertRaises(ValueError,
146
cache.add, i, i, cleanup=cleanup_func)
148
self.assertEqual([(i, i) for i in xrange(10)], cleanup_called)
150
self.assertEqual(range(19, 9, -1), [n.key for n in cache._walk_lru()])
152
def test_cleanup_during_replace_still_replaces(self):
154
def cleanup_func(key, val):
155
cleanup_called.append((key, val))
156
raise ValueError('failure during cleanup')
158
cache = lru_cache.LRUCache(max_cache=10)
160
cache.add(i, i, cleanup=cleanup_func)
161
self.assertRaises(ValueError,
162
cache.add, 1, 20, cleanup=cleanup_func)
163
# We also still update the recent access to this node
164
self.assertEqual([1, 9, 8, 7, 6, 5, 4, 3, 2, 0],
165
[n.key for n in cache._walk_lru()])
166
self.assertEqual(20, cache[1])
168
self.assertEqual([(1, 1)], cleanup_called)
169
self.assertEqual([1, 9, 8, 7, 6, 5, 4, 3, 2, 0],
170
[n.key for n in cache._walk_lru()])
172
135
def test_len(self):
173
136
cache = lru_cache.LRUCache(max_cache=10, after_cleanup_count=10)
283
246
self.assertEqual([2, 3, 4, 5, 6], sorted(cache.keys()))
248
def test_after_cleanup_size_deprecated(self):
249
obj = self.callDeprecated([
250
'LRUCache.__init__(after_cleanup_size) was deprecated in 1.11.'
251
' Use after_cleanup_count instead.'],
252
lru_cache.LRUCache, 50, after_cleanup_size=25)
253
self.assertEqual(obj._after_cleanup_count, 25)
285
255
def test_resize_smaller(self):
286
256
cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=4)