annotate MoinMoin/_tests/test_caching.py @ 6082:2e2f7c6f39eb

add CHANGES entry for additional abuse logging
author 'Karl O. Pinc' <kop@meme.com>
date Wed, 17 Sep 2014 08:17:15 -0500
parents 8cb2f4ebd45f
children
rev   line source
2882
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 """
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 MoinMoin - MoinMoin.caching Tests
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 @copyright: 2007 by MoinMoin:ThomasWaldmann
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 @license: GNU GPL, see COPYING for details.
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7 """
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 import py
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 import time
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13 from MoinMoin import caching
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14 from MoinMoin.PageEditor import PageEditor
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 class TestCaching(object):
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18 """ Tests the caching module """
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20 def test_persistence_simple(self):
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 """ test if cache persists (on disk) """
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 test_data = '12345abcde'
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23 cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki')
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24 cache.update(test_data)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25 del cache
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki')
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 assert test_data == cache.content()
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 def test_persistence_pickle(self):
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 """ test if cache persists (on disk), use pickle """
2929
0ca160b0e5d2 pep8 fix for test_caching
Johannes Berg <johannes AT sipsolutions DOT net>
parents: 2882
diff changeset
31 test_data = {1: 2, 2: 3, 3: [4, 5, ], }
2882
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki', use_pickle=True)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 cache.update(test_data)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34 del cache
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35 cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki', use_pickle=True)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 assert test_data == cache.content()
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
37
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
38 def test_persistence_encode(self):
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
39 """ test if cache persists (on disk), use encoded string """
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40 test_data = u"üöäÜÖÄß"
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41 cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki', use_encode=True)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42 cache.update(test_data)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43 del cache
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44 cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki', use_encode=True)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
45 cache_data = cache.content()
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
46 assert type(cache_data) == type(test_data)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
47 assert cache_data == test_data
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
48
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
49 def test_mtime(self):
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
50 """ test if cache mtime yields correct values """
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
51 test_data = '12345abcde'
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52 now = time.time()
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
53 cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki')
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54 cache.update(test_data)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 assert now - 2 <= cache.mtime() <= now + 2
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
56
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
57 def test_remove(self):
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 """ test if cache file removal works """
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59 cache = caching.CacheEntry(self.request, 'test_arena', 'test_key', 'wiki')
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 assert cache.exists()
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61 cache.remove()
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 assert not cache.exists()
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 def test_update_needed(self):
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 """ test update check) """
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 test_data1 = u'does not matter'
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67 test_data2 = u'something else'
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 page_name = u'Caching_TestPage'
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69 page = PageEditor(self.request, page_name)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 page._write_file(test_data1)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 cache = caching.CacheEntry(self.request, page, 'test_key', 'item')
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 cache.update(test_data1)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 assert not cache.needsUpdate(page._text_filename())
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 time.sleep(3) # XXX fails without, due to mtime granularity
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 page = PageEditor(self.request, page_name)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 page._write_file(test_data2)
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 assert cache.needsUpdate(page._text_filename())
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78
3859
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
79 def test_filelike_readwrite(self):
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
80 request = self.request
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
81 key = 'nooneknowsit'
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
82 arena = 'somethingfunny'
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
83 data = "dontcare"
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
84 cacheentry = caching.CacheEntry(request, arena, key, scope='wiki', do_locking=True,
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
85 use_pickle=False, use_encode=True)
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
86 cacheentry.open(mode='w')
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
87 cacheentry.write(data)
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
88 cacheentry.close()
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
89
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
90 assert cacheentry.exists()
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
91
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
92 cacheentry.open(mode='r')
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
93 rdata = cacheentry.read()
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
94 cacheentry.close()
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
95
8cb2f4ebd45f caching: implemented file-like api (thanks to Thomas Pfaff for the patch)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 3213
diff changeset
96 assert data == rdata
2882
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98 coverage_modules = ['MoinMoin.caching']
14f9075abff4 tests: tests for 'MoinMoin.caching'
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99