352
352
self.assertEqual(self.cache.get('key3'), 'sausage')
353
353
self.assertEqual(self.cache.get('key4'), 'lobster bisque')
355
def perform_cull_test(self, initial_count, final_count):
356
"""This is implemented as a utility method, because only some of the backends
357
implement culling. The culling algorithm also varies slightly, so the final
358
number of entries will vary between backends"""
359
# Create initial cache key entries. This will overflow the cache, causing a cull
360
for i in range(1, initial_count):
361
self.cache.set('cull%d' % i, 'value', 1000)
363
# Count how many keys are left in the cache.
364
for i in range(1, initial_count):
365
if self.cache.has_key('cull%d' % i):
367
self.assertEqual(count, final_count)
355
369
class DBCacheTests(unittest.TestCase, BaseCacheTests):
357
371
# Spaces are used in the table name to ensure quoting/escaping is working
358
372
self._table_name = 'test cache table'
359
373
management.call_command('createcachetable', self._table_name, verbosity=0, interactive=False)
360
self.cache = get_cache('db://%s' % self._table_name)
374
self.cache = get_cache('db://%s?max_entries=30' % self._table_name)
362
376
def tearDown(self):
363
377
from django.db import connection
364
378
cursor = connection.cursor()
365
379
cursor.execute('DROP TABLE %s' % connection.ops.quote_name(self._table_name))
382
self.perform_cull_test(50, 29)
367
384
class LocMemCacheTests(unittest.TestCase, BaseCacheTests):
369
self.cache = get_cache('locmem://')
386
self.cache = get_cache('locmem://?max_entries=30')
389
self.perform_cull_test(50, 29)
371
391
# memcached backend isn't guaranteed to be available.
372
392
# To check the memcached backend, the test settings file will
406
426
self.assert_(not os.path.exists(os.path.dirname(keypath)))
407
427
self.assert_(not os.path.exists(os.path.dirname(os.path.dirname(keypath))))
430
self.perform_cull_test(50, 28)
409
432
class CacheUtils(unittest.TestCase):
410
433
"""TestCase for django.utils.cache functions."""