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_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_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_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_parse(): """Parse a path to a stack, default namespaces. """ assert ([(DEFAULT, u'c'), (DEFAULT, u'b'), (DEFAULT, u'a')] == parse_path(u'/a/b/c'))
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_parse_ns(): """Parse a path to a stack with namespaces. """ assert ([(VIEW, u'c'), (DEFAULT, u'b'), (DEFAULT, u'a')] == parse_path(u'/a/b/++view++c'))
def test_resolve_no_consumers(): lookup = get_lookup(get_registry()) base = object() stack = parse_path(u'/a') obj, unconsumed, lookup = resolve_model(base, stack, lookup) assert obj is base assert unconsumed == [(DEFAULT, u'a')] assert lookup is lookup
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 test_resolve_traverse(): reg = get_registry() lookup = get_lookup(reg) reg.register(generic.consumer, [Container], Traverser(traverse_container)) base = get_structure() assert resolve_model(base, parse_path(u'/a'), lookup) == ( base['a'], [], lookup) assert resolve_model(base, parse_path(u'/sub'), lookup) == ( base['sub'], [], lookup) assert resolve_model(base, parse_path(u'/sub/b'), lookup) == ( base['sub']['b'], [], lookup) # there is no /c assert resolve_model(base, parse_path(u'/c'), lookup) == ( base, [(DEFAULT, u'c')], lookup) # there is a sub, but no c in sub assert resolve_model(base, parse_path(u'/sub/c'), lookup) == ( base['sub'], [(DEFAULT, u'c')], lookup)
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_create_ns_weird_no_open(): # a namespace that closes but doesn't open assert ([(DEFAULT, u'view++c'), (DEFAULT, u'b'), (DEFAULT, u'a')] == parse_path(u'/a/b/view++c'))
def test_create_ns_weird_no_close(): assert ([(DEFAULT, u'++c'), (DEFAULT, u'b'), (DEFAULT, u'a')] == parse_path(u'/a/b/++c'))
def test_parse_ns_shortcut_not_at_beginning(): # shortcuts should be at the beginning of a step to be recognized assert ([(DEFAULT, u'a@@c'), (DEFAULT, u'b'), (DEFAULT, u'a')] == parse_path(u'/a/b/a@@c', shortcuts={u'@@': VIEW}))
def test_parse_ns_shortcut(): assert ([(VIEW, u'c'), (DEFAULT, u'b'), (DEFAULT, u'a')] == parse_path(u'/a/b/@@c', shortcuts={u'@@': VIEW}))
def test_multi_slash(): assert parse_path(u'/a/b/c') == parse_path(u'/a///b//c') assert parse_path(u'/a/b/c') == parse_path(u'/a/b/c/')
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')]