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 == []
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'
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 == []
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')]
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
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 == []
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
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')]
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'
def lookup(self): return Lookup(self.class_lookup())
def initialize(): registry = ClassRegistry() implicit.initialize(Lookup(CachingClassLookup(registry)))
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
def app_lookup(model): return Lookup(model.class_lookup())
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')]
def get_lookup(registry): return Lookup(registry)