Example #1
0
def test_register_path_with_parameters():
    config = setup()
    app = App(testing_config=config)
    root = Root()
    lookup = app.lookup

    def get_model(id, param='default'):
        model = Model()
        model.id = id
        model.param = param
        return model

    config.commit()

    register_path(app, Root,  '', lambda m: {}, None, None, None, lambda: root)
    register_path(app, Model, '{id}', lambda model: {'id': model.id,
                                                     'param': model.param},
                  None, None, None, get_model)
    app.register(generic.context, [object], lambda obj: {})

    obj, request = consume(app, 'a')
    assert obj.id == 'a'
    assert obj.param == 'default'

    obj, request = consume(app, 'a', {'param': 'value'})
    assert obj.id == 'a'
    assert obj.param == 'value'

    model = Model()
    model.id = 'b'
    model.param = 'other'
    assert generic.path(model, lookup=lookup) == ('b', {'param': ['other']})
Example #2
0
def test_traject_path_with_leading_slash():
    config = setup()

    class App(morepath.App):
        testing_config = config

    app = App()
    root = Root()

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

    config.commit()

    registry = app.registry

    register_path(registry, Root, '', lambda m: {}, None, None, None, False,
                  lambda: root)
    register_path(registry, Model, '/foo/{id}', lambda model: {'id': model.id},
                  None, None, None, False, get_model)
    registry.register(generic.context, [object], lambda obj: {})

    obj, request = consume(app, 'foo/a')
    assert obj.id == 'a'
    obj, request = consume(app, '/foo/a')
    assert obj.id == 'a'
Example #3
0
def test_register_path():
    config = setup()

    class App(morepath.App):
        testing_config = config

    root = Root()

    app = App()
    lookup = app.lookup

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

    config.commit()

    registry = app.registry

    register_path(registry, Root, '', lambda m: {},
                  None, None, None, False,
                  lambda: root)
    register_path(registry, Model, '{id}', lambda model: {'id': model.id},
                  None, None, None, False, get_model)
    registry.register(generic.context, [object], lambda obj: {})

    obj, request = consume(app, 'a')
    assert obj.id == 'a'
    model = Model()
    model.id = 'b'
    assert generic.path(model, lookup=lookup) == ('b', {})
Example #4
0
def test_view_raises_http_error():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    from werkzeug.exceptions import BadRequest
    def view(self, request):
        raise BadRequest()

    register_path(app, Model, 'foo', None, None, None, Model)
    register_view(app, Model, view)

    response = publish(app.request(get_environ(path='foo')), app.mounted())

    assert response.status == '400 BAD REQUEST'
Example #5
0
def test_view_raises_http_error():
    config = setup()
    app = App(testing_config=config)
    config.commit()

    def view(self, request):
        raise HTTPBadRequest()

    register_path(app, Model, 'foo', None, None, None, None, Model)
    register_view(app, Model, view)

    request = app.request(get_environ(path='foo'))
    request.mounts.append(app.mounted())

    with pytest.raises(HTTPBadRequest):
        publish(request)
Example #6
0
def test_view_raises_http_error():
    config = setup()

    class app(morepath.App):
        testing_config = config

    config.commit()

    def view(self, request):
        raise HTTPBadRequest()

    registry = app.registry
    register_path(registry, Model, 'foo', None, None, None, None, False, Model)
    register_view(registry, dict(model=Model), view)

    request = app().request(get_environ(path='foo'))

    with pytest.raises(HTTPBadRequest):
        publish(request)
Example #7
0
def test_traject_path_with_leading_slash():
    config = setup()
    app = App(testing_config=config)
    root = Root()

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

    config.commit()

    register_path(app, Root, '', lambda m: {}, None, None, lambda: root)
    register_path(app, Model, '/foo/{id}', lambda model: {'id': model.id},
                  None, None, get_model)
    obj, request = consume(app, 'foo/a')
    assert obj.id == 'a'
    obj, request = consume(app, '/foo/a')
    assert obj.id == 'a'
Example #8
0
def test_register_path_with_parameters():
    config = setup()

    class App(morepath.App):
        testing_config = config

    root = Root()

    def get_model(id, param='default'):
        model = Model()
        model.id = id
        model.param = param
        return model

    config.commit()

    registry = App.registry

    register_path(registry, Root, '', lambda m: {}, None, None, None, False,
                  lambda: root)
    register_path(registry, Model, '{id}',
                  lambda model: {'id': model.id, 'param': model.param},
                  None, None, None, False, get_model)

    mount = App()

    obj, request = consume(mount, 'a')
    assert obj.id == 'a'
    assert obj.param == 'default'

    obj, request = consume(mount, 'a', {'param': 'value'})
    assert obj.id == 'a'
    assert obj.param == 'value'

    model = Model()
    model.id = 'b'
    model.param = 'other'
    assert generic.path(model, lookup=mount.lookup) == (
        'b', {'param': ['other']})
Example #9
0
def test_register_path():
    config = setup()
    app = App(testing_config=config)
    root = Root()
    lookup = app.lookup()

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

    config.commit()

    register_path(app, Root, '', lambda m: {}, None, None, lambda: root)
    register_path(app, Model, '{id}', lambda model: {'id': model.id},
                  None, None, get_model)

    obj, request = consume(app, 'a')
    assert obj.id == 'a'
    model = Model()
    model.id = 'b'
    assert generic.path(model, lookup=lookup) == ('b', {})
    assert generic.app(model, lookup=lookup) is app