comparison MoinMoin/action/_tests/test_sendcached.py @ 3799:738bd5aec961

sendcached: minor bugfix, added unit tests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Fri, 27 Jun 2008 18:27:14 +0200
parents
children 30ed528054ed
comparison
equal deleted inserted replaced
3798:17e94abc5320 3799:738bd5aec961
1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - tests of sendcached 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 sendcached
13
14 from MoinMoin._tests import become_trusted, create_page, nuke_page
15
16 class TestSendCached:
17 """ testing action sendcached """
18 pagename = u"AutoCreatedSillyPageToTestAttachments"
19
20 def test_put_cache_minimal(self):
21 """Test if put_cache() works"""
22 request = self.request
23 key = 'nooneknowsit'
24 data = "dontcare"
25 url = sendcached.put_cache(request, key, data)
26
27 assert key in url
28 meta_cache = caching.CacheEntry(request,
29 arena=sendcached.sendcached_arena,
30 scope=sendcached.sendcached_scope,
31 key=key+'.meta', use_pickle=True)
32 last_modified, headers = meta_cache.content()
33 assert last_modified.endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified
34 assert "Content-Type: application/octet-stream" in headers
35 assert "Content-Length: %d" % len(data) in headers
36
37 def test_put_cache_guess_ct_give_lm(self):
38 """Test if put_cache() works, when we give filename (so it guesses content_type) and last_modified"""
39 request = self.request
40 key = 'nooneknowsit'
41 filename = "test.png"
42 data = "dontcare"
43 url = sendcached.put_cache(request, key, data,
44 filename=filename, last_modified=1)
45 assert key in url
46
47 meta_cache = caching.CacheEntry(request,
48 arena=sendcached.sendcached_arena,
49 scope=sendcached.sendcached_scope,
50 key=key+'.meta', use_pickle=True)
51 last_modified, headers = meta_cache.content()
52 assert last_modified == 'Thu, 01 Jan 1970 00:00:01 GMT'
53 assert "Content-Type: image/png" in headers
54 assert "Content-Length: %d" % len(data) in headers
55
56 def test_put_cache_file_like_data(self):
57 """Test if put_cache() works when we give it a file like object for the content"""
58 request = self.request
59 key = 'nooneknowsit'
60 filename = "test.png"
61 data = "dontcareatall"
62 data_file = StringIO.StringIO(data)
63 url = sendcached.put_cache(request, key, data_file)
64
65 assert key in url
66 meta_cache = caching.CacheEntry(request,
67 arena=sendcached.sendcached_arena,
68 scope=sendcached.sendcached_scope,
69 key=key+'.meta', use_pickle=True)
70 last_modified, headers = meta_cache.content()
71 assert last_modified.endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified
72 assert "Content-Type: application/octet-stream" in headers
73 assert "Content-Length: %d" % len(data) in headers
74
75 data_cache = caching.CacheEntry(request,
76 arena=sendcached.sendcached_arena,
77 scope=sendcached.sendcached_scope,
78 key=key+'.data')
79 cached = data_cache.content()
80 assert data == cached
81
82 def test_put_cache_complex(self):
83 """Test if put_cache() works for a more complex, practical scenario:
84
85 As 'source' we just use some random integer as count value.
86
87 The 'rendered representation' of it is just the word "spam" repeated
88 count times, which we cache.
89
90 The cache key calculation (for the 'non-guessable' keys) is also
91 rather simple.
92
93 In real world, source would be likely some big image, rendered
94 representation of it a thumbnail / preview of it. Or some LaTeX
95 source and its rendered representation as png image.
96 Key calculation could be some MAC or some other hard to guess and
97 unique string.
98 """
99 import random
100 request = self.request
101 render = lambda data: "spam" * data
102 secret = 4223
103 keycalc = lambda data: str(data * secret)
104
105 source = random.randint(1, 100)
106 rendered1 = render(source)
107 key1 = keycalc(source)
108
109 url1 = sendcached.put_cache(request, key1, rendered1)
110 assert 'key=%s' % key1 in url1
111
112 data_cache = caching.CacheEntry(request,
113 arena=sendcached.sendcached_arena,
114 scope=sendcached.sendcached_scope,
115 key=key1+'.data')
116 cached1 = data_cache.content()
117
118 assert render(source) == cached1
119 # if that succeeds, we have stored the rendered representation of source in the cache under key1
120
121 # now we use some different source, render it and store it in the cache
122 source = source * 2
123 rendered2 = render(source)
124 key2 = keycalc(source)
125
126 url2 = sendcached.put_cache(request, key2, rendered2)
127 assert 'key=%s' % key2 in url2
128
129 data_cache = caching.CacheEntry(request,
130 arena=sendcached.sendcached_arena,
131 scope=sendcached.sendcached_scope,
132 key=key2+'.data')
133 cached2 = data_cache.content()
134
135 assert render(source) == cached2
136 # if that succeeds, we have stored the rendered representation of updated source in the cache under key2
137
138 assert url2 != url1 # URLs must be different for different source (implies different keys)
139
140
141 coverage_modules = ['MoinMoin.action.sendcached']
142