Back to index

moin  1.9.0~rc2
Public Member Functions | Static Public Attributes
test_cache.TestSendCached Class Reference
Collaboration diagram for test_cache.TestSendCached:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def test_cache_key_content
def test_cache_key_attachment
def test_put_cache_minimal
def test_put_cache_guess_ct_give_lm
def test_put_cache_file_like_data
def test_put_cache_complex

Static Public Attributes

string pagename = u"AutoCreatedSillyPageToTestAttachments"

Detailed Description

testing action cache 

Definition at line 16 of file test_cache.py.


Member Function Documentation

Definition at line 31 of file test_cache.py.

00031 
00032     def test_cache_key_attachment(self):
00033         request = self.request
00034         pagename = self.pagename
00035         attachname = 'foo.txt'
00036 
00037         become_trusted(request)
00038         create_page(request, pagename, u"Foo!")
00039 
00040         AttachFile.add_attachment(request, pagename, attachname, "Test content1", True)
00041 
00042         result1 = cache.key(request, itemname=pagename, attachname=attachname, secret='bar')
00043         result2 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
00044         assert result1  # not empty
00045         assert result1 != result2  # different for different secret
00046 
00047         # test below does not work, because mtime is often same, inode can be same due to how add_attachment
00048         # works, file size is same, attachment name is same, wikiname/pagename is same.
00049         # In practice, this should rather rarely cause problems:
00050         #AttachFile.add_attachment(request, pagename, attachname, "Test content2", True)
00051         #result3 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
00052         #assert result3 != result2  # different for different content
00053 
00054         AttachFile.add_attachment(request, pagename, attachname, "Test content33333", True)
00055         result4 = cache.key(request, itemname=pagename, attachname=attachname, secret='baz')
00056         assert len(result4) == len(result2)  # same length of key for different input lengths
00057         nuke_page(request, pagename)

Here is the call graph for this function:

Definition at line 20 of file test_cache.py.

00020 
00021     def test_cache_key_content(self):
00022         request = self.request
00023         result1 = cache.key(request, content='foo', secret='bar')
00024         result2 = cache.key(request, content='foo', secret='baz')
00025         assert result1  # not empty
00026         assert result1 != result2  # different for different secret
00027         result3 = cache.key(request, content='foofoo', secret='baz')
00028         assert result3 != result2  # different for different content
00029         result4 = cache.key(request, content='foo'*1000, secret='baz')
00030         assert len(result4) == len(result3)  # same length of key for different input lengths

Here is the call graph for this function:

Test if put_cache() works for a more complex, practical scenario:

   As 'source' we just use some random integer as count value.

   The 'rendered representation' of it is just the word "spam" repeated
   count times, which we cache.

   The cache key calculation (for the 'non-guessable' keys) is also
   rather simple.

   In real world, source would be likely some big image, rendered
   representation of it a thumbnail / preview of it. Or some LaTeX
   source and its rendered representation as png image.
   Key calculation could be some MAC or some other hard to guess and
   unique string.

Definition at line 122 of file test_cache.py.

00122 
00123     def test_put_cache_complex(self):
00124         """Test if put_cache() works for a more complex, practical scenario:
00125 
00126            As 'source' we just use some random integer as count value.
00127 
00128            The 'rendered representation' of it is just the word "spam" repeated
00129            count times, which we cache.
00130 
00131            The cache key calculation (for the 'non-guessable' keys) is also
00132            rather simple.
00133 
00134            In real world, source would be likely some big image, rendered
00135            representation of it a thumbnail / preview of it. Or some LaTeX
00136            source and its rendered representation as png image.
00137            Key calculation could be some MAC or some other hard to guess and
00138            unique string.
00139         """
00140         import random
00141         request = self.request
00142         render = lambda data: "spam" * data
00143         secret = 4223
00144         keycalc = lambda data: str(data * secret)
00145 
00146         source = random.randint(1, 100)
00147         rendered1 = render(source)
00148         key1 = keycalc(source)
00149 
00150         cache.put(request, key1, rendered1)
00151         url1 = cache.url(request, key1)
00152         assert 'key=%s' % key1 in url1
00153 
00154         data_cache = caching.CacheEntry(request,
00155                                         arena=cache.cache_arena,
00156                                         scope=cache.cache_scope,
00157                                         key=key1+'.data')
00158         cached1 = data_cache.content()
00159 
00160         assert render(source) == cached1
00161         # if that succeeds, we have stored the rendered representation of source in the cache under key1
00162 
00163         # now we use some different source, render it and store it in the cache
00164         source = source * 2
00165         rendered2 = render(source)
00166         key2 = keycalc(source)
00167 
00168         cache.put(request, key2, rendered2)
00169         url2 = cache.url(request, key2)
00170         assert 'key=%s' % key2 in url2
00171 
00172         data_cache = caching.CacheEntry(request,
00173                                         arena=cache.cache_arena,
00174                                         scope=cache.cache_scope,
00175                                         key=key2+'.data')
00176         cached2 = data_cache.content()
00177 
00178         assert render(source) == cached2
00179         # if that succeeds, we have stored the rendered representation of updated source in the cache under key2
00180 
00181         assert url2 != url1  # URLs must be different for different source (implies different keys)
00182 

Here is the call graph for this function:

Test if put_cache() works when we give it a file like object for the content

Definition at line 95 of file test_cache.py.

00095 
00096     def test_put_cache_file_like_data(self):
00097         """Test if put_cache() works when we give it a file like object for the content"""
00098         request = self.request
00099         key = 'nooneknowsit'
00100         filename = "test.png"
00101         data = "dontcareatall"
00102         data_file = StringIO.StringIO(data)
00103         cache.put(request, key, data_file)
00104         url = cache.url(request, key)
00105 
00106         assert key in url
00107         meta_cache = caching.CacheEntry(request,
00108                                         arena=cache.cache_arena,
00109                                         scope=cache.cache_scope,
00110                                         key=key+'.meta', use_pickle=True)
00111         meta = meta_cache.content()
00112         assert meta['httpdate_last_modified'].endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified
00113         assert ("Content-Type", "application/octet-stream") in meta['headers']
00114         assert ("Content-Length", len(data)) in meta['headers']
00115 
00116         data_cache = caching.CacheEntry(request,
00117                                         arena=cache.cache_arena,
00118                                         scope=cache.cache_scope,
00119                                         key=key+'.data')
00120         cached = data_cache.content()
00121         assert data == cached

Here is the call graph for this function:

Test if put_cache() works, when we give filename (so it guesses content_type) and last_modified

Definition at line 76 of file test_cache.py.

00076 
00077     def test_put_cache_guess_ct_give_lm(self):
00078         """Test if put_cache() works, when we give filename (so it guesses content_type) and last_modified"""
00079         request = self.request
00080         key = 'nooneknowsit'
00081         filename = "test.png"
00082         data = "dontcare"
00083         cache.put(request, key, data, filename=filename, last_modified=1)
00084         url = cache.url(request, key)
00085         assert key in url
00086 
00087         meta_cache = caching.CacheEntry(request,
00088                                         arena=cache.cache_arena,
00089                                         scope=cache.cache_scope,
00090                                         key=key+'.meta', use_pickle=True)
00091         meta = meta_cache.content()
00092         assert meta['httpdate_last_modified'] == 'Thu, 01 Jan 1970 00:00:01 GMT'
00093         assert ("Content-Type", "image/png") in meta['headers']
00094         assert ("Content-Length", len(data)) in meta['headers']

Here is the call graph for this function:

Test if put_cache() works

Definition at line 58 of file test_cache.py.

00058 
00059     def test_put_cache_minimal(self):
00060         """Test if put_cache() works"""
00061         request = self.request
00062         key = 'nooneknowsit'
00063         data = "dontcare"
00064         cache.put(request, key, data)
00065         url = cache.url(request, key)
00066 
00067         assert key in url
00068         meta_cache = caching.CacheEntry(request,
00069                                         arena=cache.cache_arena,
00070                                         scope=cache.cache_scope,
00071                                         key=key+'.meta', use_pickle=True)
00072         meta = meta_cache.content()
00073         assert meta['httpdate_last_modified'].endswith(' GMT') # only a very rough check, it has used cache mtime as last_modified
00074         assert ("Content-Type", "application/octet-stream") in meta['headers']
00075         assert ("Content-Length", len(data)) in meta['headers']

Here is the call graph for this function:


Member Data Documentation

string test_cache.TestSendCached.pagename = u"AutoCreatedSillyPageToTestAttachments" [static]

Definition at line 18 of file test_cache.py.


The documentation for this class was generated from the following file: