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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
import os
import shutil
import time
from webtest import TestApp
from paste.registry import RegistryManager
from beaker.middleware import CacheMiddleware
import pylons
from pylons.decorators.cache import beaker_cache, create_cache_key
from pylons.controllers import WSGIController, XMLRPCController
from pylons.testutil import SetupCacheGlobal, ControllerWrap
from __init__ import data_dir, TestWSGIController
class CacheController(WSGIController):
@beaker_cache(key=None, invalidate_on_startup=True)
def test_default_cache_decorator_invalidate(self):
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
@beaker_cache(key=None)
def test_default_cache_decorator(self):
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
def test_default_cache_decorator_func(self):
def func():
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
func = beaker_cache(key=None)(func)
return func()
def test_response_cache_func(self, use_cache_status=True):
pylons.response.status_int = 404
def func():
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
if use_cache_status:
func = beaker_cache(key=None)(func)
else:
func = beaker_cache(key=None, cache_response=False)(func)
return func()
@beaker_cache(key=None, type='dbm')
def test_dbm_cache_decorator(self):
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
@beaker_cache(key="param", query_args=True)
def test_get_cache_decorator(self):
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
@beaker_cache(query_args=True)
def test_get_cache_default(self):
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
@beaker_cache(expire=1)
def test_expire_cache_decorator(self):
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
@beaker_cache(expire=1)
def test_expire_dbm_cache_decorator(self):
pylons.app_globals.counter += 1
return 'Counter=%s' % pylons.app_globals.counter
@beaker_cache(key="id")
def test_key_cache_decorator(self, id):
pylons.app_globals.counter += 1
return 'Counter=%s, id=%s' % (pylons.app_globals.counter, id)
@beaker_cache(key=["id", "id2"])
def test_keyslist_cache_decorator(self, id, id2="123"):
pylons.app_globals.counter += 1
return 'Counter=%s, id=%s' % (pylons.app_globals.counter, id)
def test_invalidate_cache(self):
ns, key = create_cache_key(CacheController.test_default_cache_decorator)
c = pylons.cache.get_cache(ns)
c.remove_value(key)
def test_invalidate_dbm_cache(self):
ns, key = create_cache_key(CacheController.test_dbm_cache_decorator)
c = pylons.cache.get_cache(ns, type='dbm')
c.remove_value(key)
@beaker_cache(cache_headers=('content-type','content-length', 'x-powered-by'))
def test_header_cache(self):
pylons.response.headers['Content-Type'] = 'application/special'
pylons.response.headers['x-powered-by'] = 'pylons'
pylons.response.headers['x-dont-include'] = 'should not be included'
return "Hello folks, time is %s" % time.time()
@beaker_cache(query_args=True)
def test_cache_key_dupe(self):
return "Hello folks, time is %s" % time.time()
cache_dir = os.path.join(data_dir, 'cache')
try:
shutil.rmtree(cache_dir)
except:
pass
environ = {}
app = ControllerWrap(CacheController)
app = sap = SetupCacheGlobal(app, environ, setup_cache=True)
app = CacheMiddleware(app, {}, data_dir=cache_dir)
app = RegistryManager(app)
app = TestApp(app)
# This one is missing cache middleware and the cache object to miss on purpsoe
bad_app = ControllerWrap(CacheController)
bad_app = SetupCacheGlobal(bad_app, environ, setup_cache=False)
bad_app = RegistryManager(bad_app)
bad_app = TestApp(bad_app)
class TestBadCacheDecorator(TestWSGIController):
def setUp(self):
self.app = bad_app
TestWSGIController.setUp(self)
environ.update(self.environ)
def test_no_cache(self):
self.assertRaises(Exception, lambda: self.get_response(action='test_default_cache_decorator'))
class TestCacheDecorator(TestWSGIController):
def setUp(self):
self.app = app
TestWSGIController.setUp(self)
environ.update(self.environ)
def test_default_cache_decorator(self):
sap.g.counter = 0
self.get_response(action='test_default_cache_decorator_invalidate')
response = self.get_response(action='test_default_cache_decorator_invalidate')
assert 'text/html' in response.headers['content-type']
assert 'Counter=1' in response
response = self.get_response(action='test_default_cache_decorator_invalidate')
assert 'Counter=1' in response
def test_default_cache_decorator(self):
sap.g.counter = 0
self.get_response(action='test_invalidate_cache')
response = self.get_response(action='test_default_cache_decorator')
assert 'text/html' in response.headers['content-type']
assert 'Counter=1' in response
response = self.get_response(action='test_default_cache_decorator')
assert 'Counter=1' in response
response = self.get_response(action='test_get_cache_decorator', _url='/?param=123')
assert 'Counter=2' in response
response = self.get_response(action='test_get_cache_decorator', _url="/?param=123")
assert 'Counter=2' in response
response = self.get_response(action='test_expire_cache_decorator')
assert 'Counter=3' in response
response = self.get_response(action='test_expire_cache_decorator')
assert 'Counter=3' in response
time.sleep(2)
response = self.get_response(action='test_expire_cache_decorator')
assert 'Counter=4' in response
response = self.get_response(action='test_key_cache_decorator', id=1)
assert 'Counter=5' in response
response = self.get_response(action='test_key_cache_decorator', id=2)
assert 'Counter=6' in response
response = self.get_response(action='test_key_cache_decorator', id=1)
assert 'Counter=5' in response
response = self.get_response(action='test_keyslist_cache_decorator', id=1, id2=2)
assert 'Counter=7' in response
response = self.get_response(action='test_keyslist_cache_decorator', id=1, id2=2)
assert 'Counter=7' in response
response = self.get_response(action='test_get_cache_default', _url='/?param=1243')
assert 'Counter=8' in response
response = self.get_response(action='test_get_cache_default', _url="/?param=1243")
assert 'Counter=8' in response
response = self.get_response(action='test_get_cache_default', _url="/?param=123")
assert 'Counter=9' in response
response = self.get_response(action='test_default_cache_decorator_func')
assert 'text/html' in response.headers['content-type']
assert 'Counter=10' in response
response = self.get_response(action='test_default_cache_decorator_func')
assert 'Counter=10' in response
response = self.get_response(action='test_response_cache_func', use_cache_status=True)
assert 'Counter=10' in response
response = self.get_response(action='test_response_cache_func', use_cache_status=False,
test_args=dict(status=404))
assert 'Counter=10' in response
def test_dbm_cache_decorator(self):
sap.g.counter = 0
self.get_response(action="test_invalidate_dbm_cache")
response = self.get_response(action="test_dbm_cache_decorator")
assert "Counter=1" in response
response = self.get_response(action="test_dbm_cache_decorator")
assert "Counter=1" in response
self.get_response(action="test_invalidate_dbm_cache")
response = self.get_response(action="test_dbm_cache_decorator")
assert "Counter=2" in response
sap.g.counter = 0
response = self.get_response(action="test_expire_dbm_cache_decorator")
assert "Counter=1" in response
response = self.get_response(action="test_expire_dbm_cache_decorator")
assert "Counter=1" in response
time.sleep(2)
response = self.get_response(action="test_expire_dbm_cache_decorator")
assert "Counter=2" in response
def test_cache_key(self):
key = create_cache_key(TestCacheDecorator.test_default_cache_decorator)
assert key == ('%s.TestCacheDecorator' % self.__module__, 'test_default_cache_decorator')
response = self.get_response(action='test_invalidate_cache')
response = self.get_response(action='test_default_cache_decorator')
assert 'Counter=1' in response
response = self.get_response(action='test_default_cache_decorator')
assert 'Counter=1' in response
response = self.get_response(action='test_invalidate_cache')
response = self.get_response(action='test_default_cache_decorator')
assert 'Counter=2' in response
def test_cache_key_dupe(self):
response = self.get_response(action='test_cache_key_dupe',
_url='/test_cache_key_dupe?id=1')
time.sleep(0.1)
response2 = self.get_response(action='test_cache_key_dupe',
_url='/test_cache_key_dupe?id=2&id=1')
assert str(response) != str(response2)
def test_header_cache(self):
response = self.get_response(action='test_header_cache')
assert response.headers['content-type'] == 'application/special'
assert response.headers['x-powered-by'] == 'pylons'
assert 'x-dont-include' not in response.headers
output = response.body
time.sleep(1)
response = self.get_response(action='test_header_cache')
assert response.body == output
assert response.headers['content-type'] == 'application/special'
assert response.headers['x-powered-by'] == 'pylons'
assert 'x-dont-include' not in response.headers
def test_nocache(self):
sap.g.counter = 0
pylons.config['cache_enabled'] = 'False'
response = self.get_response(action='test_default_cache_decorator')
assert 'Counter=1' in response
response = self.get_response(action='test_default_cache_decorator')
assert 'Counter=2' in response
pylons.config['cache_enabled'] = 'True'
|