示例#1
0
def test_traject_nested_with_variable():
    app = App()

    traject = Traject()

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

    def get_special(id):
        result = Special()
        result.id = id
        return result

    traject.register('{id}', get_model)
    traject.register('{id}/sub', get_special)
    app.register(generic.traject, [App], lambda base: traject)

    found, obj, stack = traject_consumer(app, parse_path('a'), Lookup(app))
    assert found
    assert isinstance(obj, Model)
    assert stack == []
    found, obj, stack = traject_consumer(app, parse_path('b'), Lookup(app))
    assert found
    assert isinstance(obj, Model)
    assert stack == []
    found, obj, stack = traject_consumer(app, parse_path('a/sub'), Lookup(app))
    assert found
    assert isinstance(obj, Special)
    assert stack == []
示例#2
0
def test_traject_with_multiple_variables():
    app = App()

    traject = Traject()

    def get_model(first_id):
        result = Model()
        result.first_id = first_id
        return result

    def get_special(first_id, second_id):
        result = Special()
        result.first_id = first_id
        result.second_id = second_id
        return result
    traject.register('{first_id}', get_model)
    traject.register('{first_id}/{second_id}', get_special)
    app.register(generic.traject, [App], lambda base: traject)

    found, obj, stack = traject_consumer(app, parse_path('a'), Lookup(app))
    assert found
    assert isinstance(obj, Model)
    assert stack == []
    assert obj.first_id == 'a'
    assert not hasattr(obj, 'second_id')
    found, obj, stack = traject_consumer(app, parse_path('a/b'), Lookup(app))
    assert found
    assert isinstance(obj, Special)
    assert stack == []
    assert obj.first_id == 'a'
    assert obj.second_id == 'b'
示例#3
0
def test_traject_consumer_combination():
    app = App()

    traject = Traject()

    def get_model(foo):
        result = Model()
        result.foo = foo
        return result

    traject.register('special', Special)
    traject.register('{foo}', get_model)
    app.register(generic.traject, [App], lambda base: traject)

    found, obj, stack = traject_consumer(app, parse_path('something'),
                                         Lookup(app))
    assert found
    assert isinstance(obj, Model)
    assert stack == []
    assert obj.foo == 'something'
    found, obj, stack = traject_consumer(app, parse_path('special'),
                                         Lookup(app))
    assert found
    assert isinstance(obj, Special)
    assert stack == []
示例#4
0
def test_traject_nested_not_resolved_entirely_by_consumer():
    app = App()
    traject = Traject()
    traject.register('a', Model)
    app.register(generic.traject, [App], lambda base: traject)

    found, obj, stack = traject_consumer(app, parse_path('a'), Lookup(app))
    assert found
    assert isinstance(obj, Model)
    assert stack == []
    found, obj, stack = traject_consumer(app, parse_path('a/b'), Lookup(app))
    assert found
    assert isinstance(obj, Model)
    assert stack == [('default', 'b')]
示例#5
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
示例#6
0
def test_traject_nested():
    app = App()

    traject = Traject()
    traject.register('a', Model)
    traject.register('a/b', Special)
    app.register(generic.traject, [App], lambda base: traject)

    found, obj, stack = traject_consumer(app, parse_path('a'), Lookup(app))
    assert found
    assert isinstance(obj, Model)
    assert stack == []
    found, obj, stack = traject_consumer(app, parse_path('a/b'), Lookup(app))
    assert found
    assert isinstance(obj, Special)
    assert stack == []
示例#7
0
def test_register_root():
    app = App()
    root = Root()
    app.root_model = Root
    app.root_obj = root
    lookup = Lookup(ChainClassLookup(app, global_app))

    register_root(app, Root, lambda: root)
    request = get_request()
    request.lookup = lookup
    assert generic.path(request, root, lookup=lookup) == ''
    assert generic.base(root, lookup=lookup) is app
示例#8
0
def test_traject_consumer_factory_returns_none():
    app = App()

    traject = Traject()

    def get_model():
        return None

    traject.register('sub', get_model)
    app.register(generic.traject, [App], lambda base: traject)

    found, obj, stack = traject_consumer(app, parse_path('sub'), Lookup(app))
    assert not found
    assert obj is app
    assert stack == [(u'default', 'sub')]
示例#9
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'
示例#10
0
文件: app.py 项目: kingel/morepath
 def lookup(self):
     return Lookup(self.class_lookup())
示例#11
0
def initialize():
    registry = ClassRegistry()
    implicit.initialize(Lookup(CachingClassLookup(registry)))
示例#12
0
def get_request(*args, **kw):
    app = kw.pop('app')
    lookup = Lookup(app.class_lookup())
    result = Request(EnvironBuilder(*args, **kw).get_environ())
    result.lookup = lookup
    return result
示例#13
0
文件: setup.py 项目: oohlaf/morepath
def app_lookup(model):
    return Lookup(model.class_lookup())
示例#14
0
def test_traject_consumer_not_found():
    app = App()
    found, obj, stack = traject_consumer(app, parse_path('sub'), Lookup(app))
    assert not found
    assert obj is app
    assert stack == [(u'default', 'sub')]
示例#15
0
def get_lookup(registry):
    return Lookup(registry)