Back to index

moin  1.9.0~rc2
test_caching.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     MoinMoin - MoinMoin.caching Tests
00004 
00005     @copyright: 2007 by MoinMoin:ThomasWaldmann
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 import py
00010 
00011 import time
00012 
00013 from MoinMoin import caching
00014 from MoinMoin.PageEditor import PageEditor
00015 
00016 
00017 class TestCaching(object):
00018     """ Tests the caching module """
00019 
00020     def test_persistence_simple(self):
00021         """ test if cache persists (on disk) """
00022         test_data = '12345abcde'
00023         cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki')
00024         cache.update(test_data)
00025         del cache
00026         cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki')
00027         assert test_data == cache.content()
00028 
00029     def test_persistence_pickle(self):
00030         """ test if cache persists (on disk), use pickle """
00031         test_data = {1: 2, 2: 3, 3: [4, 5, ], }
00032         cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki', use_pickle=True)
00033         cache.update(test_data)
00034         del cache
00035         cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki', use_pickle=True)
00036         assert test_data == cache.content()
00037 
00038     def test_persistence_encode(self):
00039         """ test if cache persists (on disk), use encoded string """
00040         test_data = u"üöäÜÖÄß"
00041         cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki', use_encode=True)
00042         cache.update(test_data)
00043         del cache
00044         cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki', use_encode=True)
00045         cache_data = cache.content()
00046         assert type(cache_data) == type(test_data)
00047         assert cache_data == test_data
00048 
00049     def test_mtime(self):
00050         """ test if cache mtime yields correct values """
00051         test_data = '12345abcde'
00052         now = time.time()
00053         cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki')
00054         cache.update(test_data)
00055         assert now - 2 <= cache.mtime() <= now + 2
00056 
00057     def test_remove(self):
00058         """ test if cache file removal works """
00059         cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki')
00060         assert cache.exists()
00061         cache.remove()
00062         assert not cache.exists()
00063 
00064     def test_update_needed(self):
00065         """ test update check) """
00066         test_data1 = u'does not matter'
00067         test_data2 = u'something else'
00068         page_name = u'Caching_TestPage'
00069         page = PageEditor(self.request, page_name)
00070         page._write_file(test_data1)
00071         cache = caching.CacheEntry(self.request, page, 'test_key', 'item')
00072         cache.update(test_data1)
00073         assert not cache.needsUpdate(page._text_filename())
00074         time.sleep(3) # XXX fails without, due to mtime granularity
00075         page = PageEditor(self.request, page_name)
00076         page._write_file(test_data2)
00077         assert cache.needsUpdate(page._text_filename())
00078 
00079     def test_filelike_readwrite(self):
00080         request = self.request
00081         key = 'nooneknowsit'
00082         arena = 'somethingfunny'
00083         data = "dontcare"
00084         cacheentry = caching.CacheEntry(request, arena, key, scope='wiki', do_locking=True,
00085                  use_pickle=False, use_encode=True)
00086         cacheentry.open(mode='w')
00087         cacheentry.write(data)
00088         cacheentry.close()
00089 
00090         assert cacheentry.exists()
00091 
00092         cacheentry.open(mode='r')
00093         rdata = cacheentry.read()
00094         cacheentry.close()
00095 
00096         assert data == rdata
00097 
00098 coverage_modules = ['MoinMoin.caching']
00099