Exemple #1
0
def app():
    config = Configurator()
    config.include('pyramid_wiring')

    graph = Graph()
    graph.register_scope(RequestScope, RequestScope())
    class Counter(object):
        def __init__(self):
            self.count = 1
    graph.register_provider('counter', FactoryProvider(Counter, scope=RequestScope))
    config.set_object_graph(graph)

    def count(request, counter=injected('counter')):
        # Increment the counter
        count = counter.count
        counter.count += 1

        # Get the counter from the graph again and make sure it's the same
        assert graph.get('counter') is counter

        return count
    config.add_route('count', '/count')
    config.add_view(count, route_name='count', renderer='string')

    return TestApp(config.make_wsgi_app())
Exemple #2
0
def test_view_mapping(view):
    config = Configurator()
    config.include('pyramid_wiring')

    # Create a graph with the message.
    graph = Graph()
    graph.register_instance('hello', "Hello, world!")
    config.set_object_graph(graph)

    # Add the view.
    config.add_route('test', '/')
    args = dict(route_name='test', renderer='string')
    if hasattr(view, 'attr'):
        args['attr'] = 'attr'
    config.add_view(view, **args)

    app = TestApp(config.make_wsgi_app())
    assert app.get('/').body == b"Hello, world!"