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_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!'
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'
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!'
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'
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'}
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')
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_notfound(): setup() app = App() model = Model() response = publish(get_request(path='', app=app), model) assert response.status == '404 NOT FOUND'