Exemple #1
0
def test_register_model():
    setup()
    app = App()
    root = Root()
    app.root_model = Root
    app.root_obj = root
    lookup = Lookup(ChainClassLookup(app, global_app))

    def get_model(id):
        model = Model()
        model.id = id
        return model

    register_root(app, Root, lambda: root)
    register_model(app, Model, '{id}', lambda model: {'id': model.id},
                   get_model)

    found, obj, stack = traject_consumer(app, parse_path('a'), lookup)
    assert obj.id == 'a'
    model = Model()
    model.id = 'b'
    request = get_request()
    request.lookup = lookup
    assert generic.path(request, model, lookup=lookup) == 'b'
    assert generic.base(model, lookup=lookup) is app
Exemple #2
0
def test_response_returned():
    setup()
    app = App()

    def view(request, model):
        return Response('Hello world!')

    register_view(app, Model, view)
    model = Model()
    response = publish(get_request(path='', app=app), model)
    assert response.data == 'Hello world!'
Exemple #3
0
def test_notfound_with_predicates():
    setup()
    app = App()

    def view(request, model):
        return "view"

    register_view(app, Model, view, predicates=dict(name=''))
    model = Model()
    response = publish(get_request(path='foo', app=app), model)
    assert response.status == '404 NOT FOUND'
Exemple #4
0
def test_response_returned():
    setup()
    app = App()

    def view(request, model):
        return Response('Hello world!')

    register_view(app, Model, view)
    model = Model()
    response = publish(get_request(path='', app=app), model)
    assert response.data == 'Hello world!'
Exemple #5
0
def test_notfound_with_predicates():
    setup()
    app = App()

    def view(request, model):
        return "view"

    register_view(app, Model, view, predicates=dict(name=''))
    model = Model()
    response = publish(get_request(path='foo', app=app), model)
    assert response.status == '404 NOT FOUND'
Exemple #6
0
def test_view():
    setup()
    app = App()

    def view(request, model):
        return "View!"

    register_view(app, Model, view, predicates=dict(name=''))

    model = Model()
    result = publish(get_request(path='', app=app), model)
    assert result.data == 'View!'
Exemple #7
0
def test_view():
    setup()
    app = App()

    def view(request, model):
        return "View!"

    register_view(app, Model, view, predicates=dict(name=''))

    model = Model()
    result = publish(get_request(path='', app=app), model)
    assert result.data == 'View!'
Exemple #8
0
def test_render_html():
    setup()
    app = App()

    def view(request, model):
        return '<p>Hello world!</p>'

    register_view(app, Model, view, render=render_html)

    request = get_request(path='', app=app)
    model = Model()
    response = publish(request, model)
    assert response.data == '<p>Hello world!</p>'
    assert response.content_type == 'text/html'
Exemple #9
0
def test_render_html():
    setup()
    app = App()

    def view(request, model):
        return '<p>Hello world!</p>'

    register_view(app, Model, view, render=render_html)

    request = get_request(path='', app=app)
    model = Model()
    response = publish(request, model)
    assert response.data == '<p>Hello world!</p>'
    assert response.content_type == 'text/html'
Exemple #10
0
def test_request_view():
    setup()
    app = App()

    def view(request, model):
        return {'hey': 'hey'}

    register_view(app, Model, view, render=render_json)

    request = get_request(path='', app=app)
    model = Model()
    response = publish(request, model)
    # when we get the response, the json will be rendered
    assert response.data == '{"hey": "hey"}'
    assert response.content_type == 'application/json'
    # but we get the original json out when we access the view
    assert request.view(model) == {'hey': 'hey'}
Exemple #11
0
def test_request_view():
    setup()
    app = App()

    def view(request, model):
        return {'hey': 'hey'}

    register_view(app, Model, view, render=render_json)

    request = get_request(path='', app=app)
    model = Model()
    response = publish(request, model)
    # when we get the response, the json will be rendered
    assert response.data == '{"hey": "hey"}'
    assert response.content_type == 'application/json'
    # but we get the original json out when we access the view
    assert request.view(model) == {'hey': 'hey'}
Exemple #12
0
def test_predicates():
    setup()
    app = App()

    def view(request, model):
        return "all"

    def post_view(request, model):
        return "post"

    register_view(app, Model, view, predicates=dict(name=''))
    register_view(app, Model, post_view,
                      predicates=dict(name='', request_method='POST'))

    model = Model()
    assert publish(get_request(path='', app=app), model).data == 'all'
    assert (publish(get_request(path='', method='POST', app=app),
                    model).data == 'post')
Exemple #13
0
def test_predicates():
    setup()
    app = App()

    def view(request, model):
        return "all"

    def post_view(request, model):
        return "post"

    register_view(app, Model, view, predicates=dict(name=''))
    register_view(app,
                  Model,
                  post_view,
                  predicates=dict(name='', request_method='POST'))

    model = Model()
    assert publish(get_request(path='', app=app), model).data == 'all'
    assert (publish(get_request(path='', method='POST', app=app),
                    model).data == 'post')
Exemple #14
0
def test_traject_path_with_leading_slash():
    setup()
    app = App()
    root = Root()
    app.root_model = Root
    app.root_obj = root
    lookup = Lookup(ChainClassLookup(app, global_app))

    def get_model(id):
        model = Model()
        model.id = id
        return model

    register_root(app, Root, lambda: root)
    register_model(app, Model, '/foo/{id}', lambda model: {'id': model.id},
                   get_model)
    found, obj, stack = traject_consumer(app, parse_path('foo/a'), lookup)
    assert obj.id == 'a'
    found, obj, stack = traject_consumer(app, parse_path('/foo/a'), lookup)
    assert obj.id == 'a'
Exemple #15
0
def test_notfound():
    setup()
    app = App()
    model = Model()
    response = publish(get_request(path='', app=app), model)
    assert response.status == '404 NOT FOUND'
Exemple #16
0
def test_notfound():
    setup()
    app = App()
    model = Model()
    response = publish(get_request(path='', app=app), model)
    assert response.status == '404 NOT FOUND'