def setup_app(app, extra_configs=None, ioloop=None, listen=None): if ioloop is None: ioloop = tornado.ioloop.IOLoop.current() if extra_configs is None: extra_configs = [] if isinstance(app, tornado.util.basestring_type): module_path = app module_name = 'root' if ':' in module_path: module_path, module_name = module_path.split(':', 1) module_path = module_path.replace('/', '.').strip('.') module = __import__(module_path, fromlist=[module_name]) module = getattr(module, module_name) app = rw.httpbase.Application(root=module, extra_configs=extra_configs) http_server_settings = app.scope['settings'].get('httpserver', {}) server = tornado.httpserver.HTTPServer(app, **http_server_settings) if listen: server.listen(*listen) scope = rw.scope.Scope() with scope(): ioloop.run_sync(rw.server.start) return app.scope
def test_rule_match(self): # match must be used inside scope with rw.routing:plugin activated scope = rw.scope.Scope() with scope(): scope.activate(rw.routing.plugin, callback=self.inside_scope) self.wait()
def test_submodules(): rt0 = rw.routing.RoutingTable('root') rt0.add_route('get', '/', 0, generate_route_func('index')) rt1 = rw.routing.RoutingTable('sub') rt1.add_route('get', '/', 1, generate_route_func('index')) rt2 = rw.routing.RoutingTable('subsub') rt2.add_route('get', '/', 2, generate_route_func('index')) rt2.add_route('get', '/fun', 2, generate_route_func('fun')) rt0.add_child('/sub', rt1) rt1.add_child('/subsub', rt2) rt0.setup() scope = rw.scope.Scope() with scope(): prefix, moudle, func, args = rt0.find_route('get', '/') assert prefix == '' assert func.__name__ == 'index' prefix, moudle, func, args = rt0.find_route('get', '/sub') assert prefix == 'sub' assert func.__name__ == 'index' prefix, moudle, func, args = rt0.find_route('get', '/sub/subsub') assert prefix == 'sub.subsub' assert func.__name__ == 'index' prefix, moudle, func, args = rt0.find_route('get', '/sub/subsub/fun') assert prefix == 'sub.subsub' assert func.__name__ == 'fun'
def test_submodules(): rt0 = rw.routing.RoutingTable("root") rt0.add_route("get", "/", 0, generate_route_func("index")) rt1 = rw.routing.RoutingTable("sub") rt1.add_route("get", "/", 1, generate_route_func("index")) rt2 = rw.routing.RoutingTable("subsub") rt2.add_route("get", "/", 2, generate_route_func("index")) rt2.add_route("get", "/fun", 2, generate_route_func("fun")) rt0.add_child("/sub", rt1) rt1.add_child("/subsub", rt2) rt0.setup() scope = rw.scope.Scope() with scope(): prefix, moudle, func, args = rt0.find_route("get", "/") assert prefix == "" assert func.__name__ == "index" prefix, moudle, func, args = rt0.find_route("get", "/sub") assert prefix == "sub" assert func.__name__ == "index" prefix, moudle, func, args = rt0.find_route("get", "/sub/subsub") assert prefix == "sub.subsub" assert func.__name__ == "index" prefix, moudle, func, args = rt0.find_route("get", "/sub/subsub/fun") assert prefix == "sub.subsub" assert func.__name__ == "fun"
def test_recursion(): """Entering the same scope twice should not produce unexpected behaviour""" scope = rw.scope.Scope() scope2 = rw.scope.Scope() with scope(): assert rw.scope.get_current_scope() is scope with scope2(): assert rw.scope.get_current_scope() is scope2 with scope2(): assert rw.scope.get_current_scope() is scope2 with scope(): assert rw.scope.get_current_scope() is scope assert rw.scope.get_current_scope() is scope2 assert rw.scope.get_current_scope() is scope2 assert rw.scope.get_current_scope() is scope assert rw.scope.get_current_scope() is None
def test_python3_typehinted_injection(): scope = rw.scope.Scope() scope['some_static_value'] = 42 @rw.scope.inject def bar(some_static_value: int): return some_static_value with scope(): assert bar() == 42
def test_scope_leaking(self): # if an exception ocurus inside a scope the scope might not # get clean up correctly. scope = rw.scope.Scope() with pytest.raises(NotImplementedError): with scope(): raise NotImplementedError('Just some random error') # no we are outside of the scope assert rw.scope.get_current_scope() is None
def test_basic(self): scope = rw.scope.Scope() plugin = rw.plugin.Plugin('rw.test') @plugin.init def init(scope): scope['foo'] = 1 with scope(): scope.activate(plugin, callback=self.inside_scope) self.wait()
def test_basic(): scope = rw.scope.Scope() scope['some_static_value'] = 42 current_user = object() def get_current_user(): return current_user scope.provider('user', get_current_user) @rw.scope.inject def foo(user): return user @rw.scope.inject def bar(some_static_value): return some_static_value @rw.scope.inject def some_function_with_a_default_value(my_paramenter='my_default_value'): return my_paramenter with scope(): assert foo() is current_user assert foo(1) == 1 assert foo() is current_user assert bar() == 42 assert bar(10) == 10 assert bar(some_static_value=11) == 11 # normal calling behaviour must be preserved assert some_function_with_a_default_value('value') == 'value' assert some_function_with_a_default_value() == 'my_default_value' # check nested scope nested_scope = rw.scope.Scope() nested_scope['user'] = 2 with nested_scope(): assert foo() == 2 assert bar() == 42 assert foo() is current_user assert bar() == 42
def test_mount_variables(self): scope = rw.scope.Scope() with scope(): scope.activate(rw.routing.plugin, callback=self.mount_variables) self.wait()