~ubuntu-branches/ubuntu/natty/moin/natty-updates

« back to all changes in this revision

Viewing changes to MoinMoin/action/_tests/test_cache.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2008-06-22 21:17:13 UTC
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20080622211713-inlv5k4eifxckelr
ImportĀ upstreamĀ versionĀ 1.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: iso-8859-1 -*-
2
 
"""
3
 
    MoinMoin - tests of cache action functions
4
 
 
5
 
    @copyright: 2008 MoinMoin:ThomasWaldmann
6
 
    @license: GNU GPL, see COPYING for details.
7
 
"""
8
 
 
9
 
import os, StringIO
10
 
 
11
 
from MoinMoin import caching
12
 
from MoinMoin.action import AttachFile, cache
13
 
 
14
 
from MoinMoin._tests import become_trusted, create_page, nuke_page
15
 
 
16
 
class TestSendCached:
17
 
    """ testing action cache """
18
 
    pagename = u"AutoCreatedSillyPageToTestAttachments"
19
 
 
20
 
    def test_cache_key_content(self):
21
 
        request = self.request
22
 
        result1 = cache.key(request, content='foo', secret='bar')
23
 
        result2 = cache.key(request, content='foo', secret='baz')
24
 
        assert result1  # not empty
25
 
        assert result1 != result2  # different for different secret
26
 
        result3 = cache.key(request, content='foofoo', secret='baz')
27
 
        assert result3 != result2  # different for different content
28
 
        result4 = cache.key(request, content='foo'*1000, secret='baz')
29
 
        assert len(result4) == len(result3)  # same length of key for different input lengths
30
 
 
31
 
    def test_cache_key_attachment(self):
32
 
        request = self.request
33
 
        pagename = self.pagename
34
 
        attachname = 'foo.txt'
35
 
 
36
 
        become_trusted(request)
37
 
        create_page(request, pagename, u"Foo!")
38
 
 
39
 
        AttachFile.add_attachment(request, pagename, attachname, "Test content1", True)
40
 
 
41
 
        result1 = cache.key(request, itemname=pagename, attachname=attachname, secret='bar')
42
 
        result2 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
43
 
        assert result1  # not empty
44
 
        assert result1 != result2  # different for different secret
45
 
 
46
 
        # test below does not work, because mtime is often same, inode can be same due to how add_attachment
47
 
        # works, file size is same, attachment name is same, wikiname/pagename is same.
48
 
        # In practice, this should rather rarely cause problems:
49
 
        #AttachFile.add_attachment(request, pagename, attachname, "Test content2", True)
50
 
        #result3 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
51
 
        #assert result3 != result2  # different for different content
52
 
 
53
 
        AttachFile.add_attachment(request, pagename, attachname, "Test content33333", True)
54
 
        result4 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
55
 
        assert len(result4) == len(result2)  # same length of key for different input lengths
56
 
        nuke_page(request, pagename)
57
 
 
58
 
    def test_put_cache_minimal(self):
59
 
        """Test if put_cache() works"""
60
 
        request = self.request
61
 
        key = 'nooneknowsit'
62
 
        data = "dontcare"
63
 
        cache.put(request, key, data)
64
 
        url = cache.url(request, key)
65
 
 
66
 
        assert key in url
67
 
        meta_cache = caching.CacheEntry(request,
68
 
                                        arena=cache.cache_arena,
69
 
                                        scope=cache.cache_scope,
70
 
                                        key=key+'.meta', use_pickle=True)
71
 
        meta = meta_cache.content()
72
 
        assert meta['httpdate_last_modified'].endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified
73
 
        assert ("Content-Type", "application/octet-stream") in meta['headers']
74
 
        assert ("Content-Length", len(data)) in meta['headers']
75
 
 
76
 
    def test_put_cache_guess_ct_give_lm(self):
77
 
        """Test if put_cache() works, when we give filename (so it guesses content_type) and last_modified"""
78
 
        request = self.request
79
 
        key = 'nooneknowsit'
80
 
        filename = "test.png"
81
 
        data = "dontcare"
82
 
        cache.put(request, key, data, filename=filename, last_modified=1)
83
 
        url = cache.url(request, key)
84
 
        assert key in url
85
 
 
86
 
        meta_cache = caching.CacheEntry(request,
87
 
                                        arena=cache.cache_arena,
88
 
                                        scope=cache.cache_scope,
89
 
                                        key=key+'.meta', use_pickle=True)
90
 
        meta = meta_cache.content()
91
 
        assert meta['httpdate_last_modified'] == 'Thu, 01 Jan 1970 00:00:01 GMT'
92
 
        assert ("Content-Type", "image/png") in meta['headers']
93
 
        assert ("Content-Length", len(data)) in meta['headers']
94
 
 
95
 
    def test_put_cache_file_like_data(self):
96
 
        """Test if put_cache() works when we give it a file like object for the content"""
97
 
        request = self.request
98
 
        key = 'nooneknowsit'
99
 
        filename = "test.png"
100
 
        data = "dontcareatall"
101
 
        data_file = StringIO.StringIO(data)
102
 
        cache.put(request, key, data_file)
103
 
        url = cache.url(request, key)
104
 
 
105
 
        assert key in url
106
 
        meta_cache = caching.CacheEntry(request,
107
 
                                        arena=cache.cache_arena,
108
 
                                        scope=cache.cache_scope,
109
 
                                        key=key+'.meta', use_pickle=True)
110
 
        meta = meta_cache.content()
111
 
        assert meta['httpdate_last_modified'].endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified
112
 
        assert ("Content-Type", "application/octet-stream") in meta['headers']
113
 
        assert ("Content-Length", len(data)) in meta['headers']
114
 
 
115
 
        data_cache = caching.CacheEntry(request,
116
 
                                        arena=cache.cache_arena,
117
 
                                        scope=cache.cache_scope,
118
 
                                        key=key+'.data')
119
 
        cached = data_cache.content()
120
 
        assert data == cached
121
 
 
122
 
    def test_put_cache_complex(self):
123
 
        """Test if put_cache() works for a more complex, practical scenario:
124
 
 
125
 
           As 'source' we just use some random integer as count value.
126
 
 
127
 
           The 'rendered representation' of it is just the word "spam" repeated
128
 
           count times, which we cache.
129
 
 
130
 
           The cache key calculation (for the 'non-guessable' keys) is also
131
 
           rather simple.
132
 
 
133
 
           In real world, source would be likely some big image, rendered
134
 
           representation of it a thumbnail / preview of it. Or some LaTeX
135
 
           source and its rendered representation as png image.
136
 
           Key calculation could be some MAC or some other hard to guess and
137
 
           unique string.
138
 
        """
139
 
        import random
140
 
        request = self.request
141
 
        render = lambda data: "spam" * data
142
 
        secret = 4223
143
 
        keycalc = lambda data: str(data * secret)
144
 
 
145
 
        source = random.randint(1, 100)
146
 
        rendered1 = render(source)
147
 
        key1 = keycalc(source)
148
 
 
149
 
        cache.put(request, key1, rendered1)
150
 
        url1 = cache.url(request, key1)
151
 
        assert 'key=%s' % key1 in url1
152
 
 
153
 
        data_cache = caching.CacheEntry(request,
154
 
                                        arena=cache.cache_arena,
155
 
                                        scope=cache.cache_scope,
156
 
                                        key=key1+'.data')
157
 
        cached1 = data_cache.content()
158
 
 
159
 
        assert render(source) == cached1
160
 
        # if that succeeds, we have stored the rendered representation of source in the cache under key1
161
 
 
162
 
        # now we use some different source, render it and store it in the cache
163
 
        source = source * 2
164
 
        rendered2 = render(source)
165
 
        key2 = keycalc(source)
166
 
 
167
 
        cache.put(request, key2, rendered2)
168
 
        url2 = cache.url(request, key2)
169
 
        assert 'key=%s' % key2 in url2
170
 
 
171
 
        data_cache = caching.CacheEntry(request,
172
 
                                        arena=cache.cache_arena,
173
 
                                        scope=cache.cache_scope,
174
 
                                        key=key2+'.data')
175
 
        cached2 = data_cache.content()
176
 
 
177
 
        assert render(source) == cached2
178
 
        # if that succeeds, we have stored the rendered representation of updated source in the cache under key2
179
 
 
180
 
        assert url2 != url1  # URLs must be different for different source (implies different keys)
181
 
 
182
 
 
183
 
coverage_modules = ['MoinMoin.action.cache']
184