/usr/share/pyshared/tests/test_caching.py is in python-turbogears2 2.1.5-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | # -*- coding: utf-8 -*-
""" Test cases for Pylons caching. See:
http://wiki.pylonshq.com/display/pylonsdocs/Caching+in+Templates+and+Controllers
For more details.
"""
import tg
from tg.controllers import TGController
from tg.decorators import expose
from pylons.decorators.cache import beaker_cache
from pylons.controllers.util import etag_cache
from pylons import cache
from routes import Mapper
import pylons
from routes.middleware import RoutesMiddleware
from webob.exc import HTTPNotModified
from tests.base import TestWSGIController, make_app, setup_session_dir, teardown_session_dir
def setup():
setup_session_dir()
def teardown():
teardown_session_dir()
# a variable used to represent state held outside the controllers
mockdb = {}
class MockTime:
""" A very simple class to mock the time module. This lets us slide time
around to fake expiry in beaker.container. """
mock_time = 0
def time(self):
return self.mock_time
def set_time(self, v):
self.mock_time = v
mocktime = MockTime()
import beaker.container
beaker.container.time = mocktime
class SimpleCachingController(TGController):
""" Pylons supports a mechanism for arbitrary caches that can be allocated
within controllers. Each cache value has a creation function associated
with it that is called to retrieve it's results. """
@expose()
def simple(self, a):
c = cache.get_cache("BasicTGController.index")
x = c.get_value(key=a,
createfunc=lambda: "cached %s" % a,
type="memory",
expiretime=3600)
return x
def createfunc(self):
return "cached %s" % mockdb['expiry']
@expose()
def expiry(self, a):
mockdb['expiry'] = a # inject a value into the context
c = cache.get_cache("BasicTGController.index")
x = c.get_value(key='test',
createfunc=self.createfunc,
type="memory",
expiretime=100)
return x
class TestSimpleCaching(TestWSGIController):
def __init__(self, *args, **kargs):
TestWSGIController.__init__(self, *args, **kargs)
self.baseenviron = {}
self.app = make_app(SimpleCachingController, self.baseenviron)
def test_simple_cache(self):
""" test that caches get different results for different cache keys. """
resp = self.app.get('/simple/', params={'a':'foo'})
assert resp.body == 'cached foo'
resp = self.app.get('/simple/', params={'a':'bar'})
assert resp.body == 'cached bar'
resp = self.app.get('/simple/', params={'a':'baz'})
assert resp.body == 'cached baz'
def test_expiry(self):
""" test that values expire from a single cache key. """
mocktime.set_time(0)
resp = self.app.get('/expiry/', params={'a':'foo1'})
assert resp.body == 'cached foo1'
mocktime.set_time(1)
resp = self.app.get('/expiry/', params={'a':'foo2'})
assert resp.body == 'cached foo1'
mocktime.set_time(200) # wind clock past expiry
resp = self.app.get('/expiry/', params={'a':'foo2'})
assert resp.body == 'cached foo2'
class DecoratorController(TGController):
@beaker_cache(expire=100, type='memory')
@expose()
def simple(self):
return "cached %s" % mockdb['DecoratorController.simple']
class TestDecoratorCaching(TestWSGIController):
""" Test that the decorators function. """
def __init__(self, *args, **kargs):
TestWSGIController.__init__(self, *args, **kargs)
self.baseenviron = {}
self.app = make_app(DecoratorController, self.baseenviron)
def test_simple(self):
""" Test expiry of cached results for decorated functions. """
mocktime.set_time(0)
mockdb['DecoratorController.simple'] = 'foo1'
resp = self.app.get('/simple/')
assert resp.body == 'cached foo1'
mocktime.set_time(1)
mockdb['DecoratorController.simple'] = 'foo2'
resp = self.app.get('/simple/')
assert resp.body == 'cached foo1'
mocktime.set_time(200)
mockdb['DecoratorController.simple'] = 'foo2'
resp = self.app.get('/simple/')
assert resp.body == 'cached foo2'
class EtagController(TGController):
@expose()
def etagged(self, etag):
etag_cache(etag)
return "bar"
class TestEtagCaching(TestWSGIController):
""" A simple mechanism is provided to set the etag header for returned results. """
def __init__(self, *args, **kargs):
TestWSGIController.__init__(self, *args, **kargs)
self.app = make_app(EtagController)
def test_etags(self):
""" Test that the etag in the response headers is the one we expect. """
resp = self.app.get('/etagged/', params={'etag':'foo'})
assert resp.etag == 'foo', resp.etag
resp = self.app.get('/etagged/', params={'etag':'bar'})
assert resp.etag == 'bar', resp.etag
def test_304(self):
resp = self.app.get('/etagged/', params={'etag':'foo'}, headers={'if-none-match': '"foo"'})
assert "304" in resp.status, resp
class SessionTouchController(TGController):
@expose()
def session_get(self):
if tg.session.accessed():
return 'ACCESSED'
else:
return 'NOTOUCH'
class TestSessionTouch(TestWSGIController):
def __init__(self, *args, **kargs):
TestWSGIController.__init__(self, *args, **kargs)
self.app = make_app(SessionTouchController)
def test_prova(self):
tg.config['beaker.session.tg_avoid_touch'] = False
assert 'ACCESSED' in self.app.get('/session_get')
def test_avoid_touch(self):
tg.config['beaker.session.tg_avoid_touch'] = True
assert 'NOTOUCH' in self.app.get('/session_get')
|