[r4324]: trunk / tg / tests / base.py Maximize Restore History

Download this file

base.py    104 lines (83 with data), 3.2 kB

  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
# -*- coding: utf-8 -*-
import os, shutil
from unittest import TestCase
from xmlrpclib import loads, dumps
import webob
import beaker
import pylons
from paste.registry import Registry
from paste.registry import RegistryManager
from paste.fixture import TestApp
from paste.wsgiwrappers import WSGIRequest, WSGIResponse
from paste import httpexceptions
from tg import tmpl_context
from pylons.util import ContextObj, PylonsContext
from pylons.controllers.util import Request, Response
from tg.controllers import TGController
from pylons.testutil import ControllerWrap, SetupCacheGlobal
#import pylons.tests
from beaker.middleware import CacheMiddleware
data_dir = os.path.dirname(os.path.abspath(__file__))
session_dir = os.path.join(data_dir, 'session')
def setup_session_dir():
if not os.path.exists(session_dir):
os.makedirs(session_dir)
def teardown_session_dir():
shutil.rmtree(session_dir, ignore_errors=True)
default_environ = {
'pylons.use_webob' : True,
'pylons.routes_dict': dict(action='index'),
'paste.config': dict(global_conf=dict(debug=True))
}
def make_app(controller_klass=None, environ=None):
"""Creates a `TestApp` instance."""
if environ is None:
environ = {}
environ['pylons.routes_dict'] = {}
environ['pylons.routes_dict']['action'] = "route"
if controller_klass is None:
controller_klass = TGController
app = ControllerWrap(controller_klass)
app = SetupCacheGlobal(app, environ, setup_cache=True, setup_session=True)
app = RegistryManager(app)
app = beaker.middleware.SessionMiddleware(app, {}, data_dir=session_dir)
app = CacheMiddleware(app, {}, data_dir=os.path.join(data_dir, 'cache'))
app = httpexceptions.make_middleware(app)
return TestApp(app)
def create_request(path, environ=None):
"""Helper used in test cases to quickly setup a request obj.
``path``
The path will become PATH_INFO
``environ``
Additional environment
Returns an instance of the `webob.Request` object.
"""
# setup the environ
if environ is None:
environ = {}
environ.update(default_environ)
# create a "blank" WebOb Request object
# using Pylon's Request which is a webob Request plus
# some compatibility methods
req = Request.blank(path, environ)
# setup a Registry
reg = environ.setdefault('paste.registry', Registry())
reg.prepare()
# setup pylons.request to point to our Registry
reg.register(pylons.request, req)
# setup tmpl context
tmpl_context._push_object(ContextObj())
return req
class TestWSGIController(TestCase):
def setUp(self):
tmpl_context._push_object(ContextObj())
def tearDown(self):
tmpl_context._pop_object()
def get_response(self, **kargs):
url = kargs.pop('_url', '/')
self.environ['pylons.routes_dict'].update(kargs)
return self.app.get(url, extra_environ=self.environ)
def post_response(self, **kargs):
url = kargs.pop('_url', '/')
self.environ['pylons.routes_dict'].update(kargs)
return self.app.post(url, extra_environ=self.environ, params=kargs)