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
def test_logs_cast_failure(mock_logger): cast_request_param('integer', 'gimme_int', 'not_int') assert mock_logger.warn.call_count == 1
def test_empty_string_stays_empty_string_for_type_string(): assert '' == cast_request_param('string', 'address3', '')
def test_empty_string_becomes_none_for_type_boolean(): assert cast_request_param('boolean', 'is_open', '') is None
def test_empty_string_becomes_none_for_type_number(): assert cast_request_param('number', 'score', '') is None
def test_empty_string_becomes_none_for_type_integer(): assert cast_request_param('integer', 'biz_id', '') is None
def test_number_cast(): assert 2.34 == cast_request_param('number', 'score', '2.34')
def test_integer_cast(): assert 34 == cast_request_param('integer', 'biz_id', '34')
def test_none_returns_none(): assert cast_request_param('integer', 'biz_id', None) is None
def test_unknown_type_returns_untouched_value(): assert 'abc123' == cast_request_param('unknown_type', 'blah', 'abc123')
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
def test_boolean(): assert cast_request_param('boolean', 'is_open', 1) assert not cast_request_param('boolean', 'is_open', 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')
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