def test_traject_consume_parameter(): class app(morepath.App): pass dectate.commit(app) traject = app.config.path_registry class Model(object): def __init__(self, a): self.a = a get_param = ParameterFactory({'a': 0}, {'a': Converter(int)}, []) traject.add_pattern('sub', (Model, get_param)) mount = app() found, request = consume(mount, 'sub?a=1') assert isinstance(found, Model) assert found.a == 1 assert request.unconsumed == [] found, request = consume(mount, 'sub') assert isinstance(found, Model) assert found.a == 0 assert request.unconsumed == []
def test_extra_parameters(): get_parameters = ParameterFactory({'a': None}, {}, [], True) assert get_parameters(fake_request('?a=foo')) == { 'a': 'foo', 'extra_parameters': {}} assert get_parameters(fake_request('?b=foo')) == { 'a': None, 'extra_parameters': {'b': 'foo'}} assert get_parameters(fake_request('?a=foo&b=bar')) == { 'a': 'foo', 'extra_parameters': {'b': 'bar'}}
def test_parameter_required(): get_parameters = ParameterFactory({'a': None}, {}, ['a']) assert get_parameters(fake_request('?a=foo')) == {'a': 'foo'} with pytest.raises(HTTPBadRequest): get_parameters(fake_request(''))
def test_single_parameter_int_default(): get_parameters = ParameterFactory({'a': 0}, {'a': Converter(int)}, []) assert get_parameters(fake_request('?a=1')) == {'a': 1} assert get_parameters(fake_request('')) == {'a': 0} with pytest.raises(HTTPBadRequest): get_parameters(fake_request('?a=A'))
def test_single_parameter_default(): get_parameters = ParameterFactory({'a': 'default'}, {}, []) assert get_parameters(fake_request('?a=A')) == {'a': 'A'} assert get_parameters(fake_request('')) == {'a': 'default'}
def test_single_parameter(): get_parameters = ParameterFactory({'a': None}, {'a': Converter(str)}, []) assert get_parameters(fake_request('?a=A')) == {'a': 'A'} assert get_parameters(fake_request('')) == {'a': None}
def test_empty_parameter_factory(): get_parameters = ParameterFactory({}, {}, []) assert get_parameters(fake_request('')) == {} # unexpected parameter is ignored assert get_parameters(fake_request('?a=A')) == {}
def test_parse_variables(): assert parse_variables('No variables') == [] assert parse_variables('The {foo} is the {bar}.') == ['foo', 'bar'] with pytest.raises(TrajectError): parse_variables('{}') with pytest.raises(TrajectError): parse_variables('{1illegal}') def consume(app, path): request = app.request(webob.Request.blank(path).environ) return traject_consume(app, request), request paramfac = ParameterFactory({}, {}, []) def test_traject_consume(): class app(morepath.App): pass dectate.commit(app) traject = app.config.path_registry traject.add_pattern('sub', (Model, paramfac)) mount = app() found, request = consume(mount, 'sub') assert isinstance(found, Model)