def parse_and_validate_parameters(raw_params):
    initialize_specification()

    clean_params = {}
    for parameter in _parsed_spec.spec_dict['parameters']:
        parameter_spec = _parsed_spec.spec_dict['parameters'][parameter]
        parameter_type = parameter_spec['type']
        parameter_name = parameter_spec['name']
        try:
            value = param.cast_request_param(parameter_type, parameter, raw_params.get(parameter_name))
            validate.validate_schema_object(_parsed_spec, parameter_spec, value)
            clean_params[parameter] = marshal.marshal_schema_object(_parsed_spec, parameter_spec, value)
        except Exception as e:
            raise ValueError(e)

    return clean_params
예제 #2
0
def test_logs_cast_failure(mock_logger):
    cast_request_param('integer', 'gimme_int', 'not_int')
    assert mock_logger.warn.call_count == 1
예제 #3
0
def test_empty_string_stays_empty_string_for_type_string():
    assert '' == cast_request_param('string', 'address3', '')
예제 #4
0
def test_empty_string_becomes_none_for_type_boolean():
    assert cast_request_param('boolean', 'is_open', '') is None
예제 #5
0
def test_empty_string_becomes_none_for_type_number():
    assert cast_request_param('number', 'score', '') is None
예제 #6
0
def test_empty_string_becomes_none_for_type_integer():
    assert cast_request_param('integer', 'biz_id', '') is None
예제 #7
0
def test_number_cast():
    assert 2.34 == cast_request_param('number', 'score', '2.34')
예제 #8
0
def test_integer_cast():
    assert 34 == cast_request_param('integer', 'biz_id', '34')
예제 #9
0
def test_none_returns_none():
    assert cast_request_param('integer', 'biz_id', None) is None
예제 #10
0
def test_unknown_type_returns_untouched_value():
    assert 'abc123' == cast_request_param('unknown_type', 'blah', 'abc123')
예제 #11
0
def test_cast_failures_return_untouched_value(mock_logger):
    initial_val = 'not_int'
    result_val = cast_request_param('integer', 'gimme_int', initial_val)
    assert result_val == initial_val
예제 #12
0
def test_boolean():
    assert cast_request_param('boolean', 'is_open', 1)
    assert not cast_request_param('boolean', 'is_open', 0)
예제 #13
0
def test_cast_failure_returns_untouched_value(_):
    assert 'i_cant_be_casted_to_an_integer' == cast_request_param(
        'integer', 'biz_id', 'i_cant_be_casted_to_an_integer')
예제 #14
0
def test_type_error_returns_untouched_value_and_logs(mock_logger):
    initial_val = ['123', '456']
    result_val = cast_request_param('integer', 'gimme_int', initial_val)
    assert result_val == initial_val
    assert result_val is initial_val
    assert mock_logger.warning.call_count == 1