1
# Copyright (C) 2008 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
"""Tests for the fifo_cache module."""
25
class TestFIFOCache(tests.TestCase):
26
"""Test that FIFO cache properly keeps track of entries."""
28
def test_add_is_present(self):
29
c = fifo_cache.FIFOCache()
31
self.assertTrue(1 in c)
32
self.assertEqual(1, len(c))
33
self.assertEqual(2, c[1])
34
self.assertEqual(2, c.get(1))
35
self.assertEqual(2, c.get(1, None))
36
self.assertEqual([1], c.keys())
37
self.assertEqual([1], list(c.iterkeys()))
38
self.assertEqual([(1, 2)], c.items())
39
self.assertEqual([(1, 2)], list(c.iteritems()))
40
self.assertEqual([2], c.values())
41
self.assertEqual([2], list(c.itervalues()))
42
self.assertEqual({1: 2}, c)
44
def test_missing(self):
45
c = fifo_cache.FIFOCache()
46
self.assertRaises(KeyError, c.__getitem__, 1)
47
self.assertFalse(1 in c)
48
self.assertEqual(0, len(c))
49
self.assertEqual(None, c.get(1))
50
self.assertEqual(None, c.get(1, None))
51
self.assertEqual([], c.keys())
52
self.assertEqual([], list(c.iterkeys()))
53
self.assertEqual([], c.items())
54
self.assertEqual([], list(c.iteritems()))
55
self.assertEqual([], c.values())
56
self.assertEqual([], list(c.itervalues()))
57
self.assertEqual({}, c)
59
def test_add_maintains_fifo(self):
60
c = fifo_cache.FIFOCache(4, 4)
65
self.assertEqual([1, 2, 3, 4], sorted(c.keys()))
67
# This should pop out the oldest entry
68
self.assertEqual([2, 3, 4, 5], sorted(c.keys()))
69
# Replacing an item doesn't change the stored keys
71
self.assertEqual([2, 3, 4, 5], sorted(c.keys()))
72
# But it does change the position in the FIFO
74
self.assertEqual([2, 4, 5, 6], sorted(c.keys()))
75
self.assertEqual([4, 5, 2, 6], list(c._queue))
77
def test_default_after_cleanup_count(self):
78
c = fifo_cache.FIFOCache(5)
79
self.assertEqual(4, c._after_cleanup_count)
85
# So far, everything fits
86
self.assertEqual([1, 2, 3, 4, 5], sorted(c.keys()))
88
# But adding one more should shrink down to after_cleanup_count
89
self.assertEqual([3, 4, 5, 6], sorted(c.keys()))
92
c = fifo_cache.FIFOCache(5)
99
self.assertEqual([2, 3, 4, 5], sorted(c.keys()))
101
self.assertEqual([], c.keys())
102
self.assertEqual([], list(c._queue))
103
self.assertEqual({}, c)
105
def test_copy_not_implemented(self):
106
c = fifo_cache.FIFOCache()
107
self.assertRaises(NotImplementedError, c.copy)
109
def test_pop_not_implemeted(self):
110
c = fifo_cache.FIFOCache()
111
self.assertRaises(NotImplementedError, c.pop, 'key')
113
def test_popitem_not_implemeted(self):
114
c = fifo_cache.FIFOCache()
115
self.assertRaises(NotImplementedError, c.popitem)
117
def test_setdefault(self):
118
c = fifo_cache.FIFOCache(5, 4)
123
self.assertIs(myobj, c.setdefault('four', myobj))
124
self.assertEqual({'one': 1, 'two': 2, 'three': 3, 'four': myobj}, c)
125
self.assertEqual(3, c.setdefault('three', myobj))
126
c.setdefault('five', myobj)
127
c.setdefault('six', myobj)
128
self.assertEqual({'three': 3, 'four': myobj, 'five': myobj,
131
def test_update(self):
132
c = fifo_cache.FIFOCache(5, 4)
133
# We allow an iterable
134
c.update([(1, 2), (3, 4)])
135
self.assertEqual({1: 2, 3: 4}, c)
137
c.update(foo=3, bar=4)
138
self.assertEqual({1: 2, 3: 4, 'foo': 3, 'bar': 4}, c)
139
# Even a dict (This triggers a cleanup)
140
c.update({'baz': 'biz', 'bing': 'bang'})
141
self.assertEqual({'foo': 3, 'bar': 4, 'baz': 'biz', 'bing': 'bang'}, c)
142
# We only allow 1 iterable, just like dict
143
self.assertRaises(TypeError, c.update, [(1, 2)], [(3, 4)])
144
# But you can mix and match. kwargs take precedence over iterable
145
c.update([('a', 'b'), ('d', 'e')], a='c', q='r')
146
self.assertEqual({'baz': 'biz', 'bing': 'bang',
147
'a': 'c', 'd': 'e', 'q': 'r'}, c)
149
def test_cleanup_funcs(self):
151
def logging_cleanup(key, value):
152
log.append((key, value))
153
c = fifo_cache.FIFOCache(5, 4)
154
c.add(1, 2, cleanup=logging_cleanup)
155
c.add(2, 3, cleanup=logging_cleanup)
156
c.add(3, 4, cleanup=logging_cleanup)
157
c.add(4, 5, cleanup=None) # no cleanup for 4
158
c[5] = 6 # no cleanup for 5
159
self.assertEqual([], log)
160
# Adding another key should cleanup 1 & 2
161
c.add(6, 7, cleanup=logging_cleanup)
162
self.assertEqual([(1, 2), (2, 3)], log)
164
# replacing 3 should trigger a cleanup
165
c.add(3, 8, cleanup=logging_cleanup)
166
self.assertEqual([(3, 4)], log)
169
self.assertEqual([(3, 8)], log)
171
# Clearing everything should call all remaining cleanups
173
self.assertEqual([(6, 7)], log)
175
c.add(8, 9, cleanup=logging_cleanup)
176
# __delitem__ should also trigger a cleanup
178
self.assertEqual([(8, 9)], log)
180
def test_cleanup_at_deconstruct(self):
182
def logging_cleanup(key, value):
183
log.append((key, value))
184
c = fifo_cache.FIFOCache()
185
c.add(1, 2, cleanup=logging_cleanup)
187
# TODO: We currently don't support calling the cleanup() funcs during
188
# __del__. We might want to consider implementing this.
189
self.expectFailure("we don't call cleanups during __del__",
190
self.assertEqual, [(1, 2)], log)
191
self.assertEqual([(1, 2)], log)
194
class TestFIFOSizeCache(tests.TestCase):
196
def test_add_is_present(self):
197
c = fifo_cache.FIFOSizeCache()
199
self.assertTrue(1 in c)
200
self.assertEqual(1, len(c))
201
self.assertEqual('2', c[1])
202
self.assertEqual('2', c.get(1))
203
self.assertEqual('2', c.get(1, None))
204
self.assertEqual([1], c.keys())
205
self.assertEqual([1], list(c.iterkeys()))
206
self.assertEqual([(1, '2')], c.items())
207
self.assertEqual([(1, '2')], list(c.iteritems()))
208
self.assertEqual(['2'], c.values())
209
self.assertEqual(['2'], list(c.itervalues()))
210
self.assertEqual({1: '2'}, c)
212
def test_missing(self):
213
c = fifo_cache.FIFOSizeCache()
214
self.assertRaises(KeyError, c.__getitem__, 1)
215
self.assertFalse(1 in c)
216
self.assertEqual(0, len(c))
217
self.assertEqual(None, c.get(1))
218
self.assertEqual(None, c.get(1, None))
219
self.assertEqual([], c.keys())
220
self.assertEqual([], list(c.iterkeys()))
221
self.assertEqual([], c.items())
222
self.assertEqual([], list(c.iteritems()))
223
self.assertEqual([], c.values())
224
self.assertEqual([], list(c.itervalues()))
225
self.assertEqual({}, c)
227
def test_add_maintains_fifo(self):
228
c = fifo_cache.FIFOSizeCache(10, 8)
232
self.assertEqual({1: 'ab', 2: 'cde', 3: 'fghi'}, c)
233
c[4] = 'jkl' # Collapse
234
self.assertEqual({3: 'fghi', 4: 'jkl'}, c)
235
# Replacing an item will bump it to the end of the queue
237
self.assertEqual({3: 'mnop', 4: 'jkl'}, c)
239
self.assertEqual({3: 'mnop', 5: 'qrst'}, c)
241
def test_adding_large_key(self):
242
c = fifo_cache.FIFOSizeCache(10, 8)
243
c[1] = 'abcdefgh' # Adding a large key won't get cached at all
244
self.assertEqual({}, c)
246
self.assertEqual({1: 'abcdefg'}, c)
247
# Replacing with a too-large key will remove it
249
self.assertEqual({}, c)
250
self.assertEqual(0, c._value_size)