def generate_response_header_validator(headers, context, **kwargs): validators = ValidationDict() for key, header_definition in headers.items(): # generate a function that will attempt to cast the header to the # appropriate type. header_processor = generate_value_processor( context=context, **header_definition ) # generate a function that will validate the header. header_validator = functools.partial( validate_object, field_validators=construct_header_validators(header_definition, context=context), ) # Chain the type casting function, the individual header validation # function with a methodcaller that will fetch the header with # `response.headers.get(header_name, EMPTY)` and then feed that into # the type casting function and then into the validation function. validators.add_validator(key, chain_reduce_partial( methodcaller('get', key, EMPTY), header_processor, header_validator, )) return chain_reduce_partial( attrgetter('headers'), functools.partial(validate_object, field_validators=validators), )
def test_number_header_type_with_invalid_value(): header_definition = single_header_validator({ 'type': NUMBER, }) value_processor = generate_value_processor(context={}, **header_definition) actual = value_processor('abc') assert actual == 'abc'
def test_boolean_header_type(input_, expected): header_definition = single_header_validator({ 'type': BOOLEAN, }) value_processor = generate_value_processor(context={}, **header_definition) actual = value_processor(input_) assert actual == expected
def test_integer_header_type_with_invalid_values(value): header_definition = single_header_validator({ 'type': INTEGER, }) value_processor = generate_value_processor(context={}, **header_definition) actual = value_processor(value) assert actual == value
def test_boolean_header_type_for_invalid_value(): header_definition = single_header_validator({ 'type': BOOLEAN, }) value_processor = generate_value_processor(context={}, **header_definition) actual = value_processor('not-a-known-boolean') assert actual == 'not-a-known-boolean'
def test_integer_header_type(): header_definition = single_header_validator({ 'type': INTEGER, }) value_processor = generate_value_processor(context={}, **header_definition) actual = value_processor('123') expected = 123 assert actual == expected
def test_number_header_type(): header_definition = single_header_validator({ 'type': NUMBER, }) value_processor = generate_value_processor(context={}, **header_definition) actual = value_processor('10.5') expected = 10.5 assert actual == expected
def test_integer_header_type_with_invalid_values(value): serializer = HeaderSerializer( data={ 'type': INTEGER, } ) assert serializer.is_valid(), serializer.errors value_processor = generate_value_processor(context={}, **serializer.object) actual = value_processor(value) assert actual == value
def test_array_header_type_casting_with_single_tems(format_, input_): header_definition = single_header_validator({ 'type': ARRAY, 'collectionFormat': format_, 'items': {'type': INTEGER} }) value_processor = generate_value_processor(context={}, **header_definition) actual = value_processor(input_) expected = [1, 2, 3] assert actual == expected
def test_boolean_header_type_for_invalid_value(): serializer = HeaderSerializer( data={ 'type': BOOLEAN, } ) assert serializer.is_valid(), serializer.errors value_processor = generate_value_processor(context={}, **serializer.object) actual = value_processor('not-a-known-boolean') assert actual == 'not-a-known-boolean'
def type_cast_parameters(parameter_values, parameter_definitions, context): typed_parameters = {} for key in parameter_values.keys(): try: parameter_definition = find_parameter(parameter_definitions, name=key) except (KeyError, MultipleParametersFound, NoParameterFound): continue value = parameter_values[key] value_processor = generate_value_processor(context=context, **parameter_definition) typed_parameters[key] = value_processor(value) return typed_parameters
def test_boolean_header_type(input_, expected): serializer = HeaderSerializer( data={ 'type': BOOLEAN, } ) assert serializer.is_valid(), serializer.errors value_processor = generate_value_processor(context={}, **serializer.object) actual = value_processor(input_) assert actual == expected
def test_number_header_type_with_invalid_value(): serializer = HeaderSerializer( data={ 'type': NUMBER, } ) assert serializer.is_valid(), serializer.errors value_processor = generate_value_processor(context={}, **serializer.object) actual = value_processor('abc') assert actual == 'abc'
def test_integer_header_type(): serializer = HeaderSerializer( data={ 'type': INTEGER, } ) assert serializer.is_valid(), serializer.errors value_processor = generate_value_processor(context={}, **serializer.object) actual = value_processor('123') expected = 123 assert actual == expected
def test_number_header_type(): serializer = HeaderSerializer( data={ 'type': NUMBER, } ) assert serializer.is_valid(), serializer.errors value_processor = generate_value_processor(context={}, **serializer.object) actual = value_processor('10.5') expected = 10.5 assert actual == expected
def type_cast_parameters(parameter_values, parameter_definitions, context): typed_parameters = {} for key in parameter_values.keys(): try: parameter_definition = find_parameter(parameter_definitions, name=key) except KeyError: continue value = parameter_values[key] value_processor = generate_value_processor(context=context, **parameter_definition) typed_parameters[key] = value_processor(value) return typed_parameters
def test_array_header_type_casting_with_single_tems(format_, input_): serializer = HeaderSerializer( data={ 'type': ARRAY, 'collectionFormat': format_, 'items': {'type': INTEGER} } ) assert serializer.is_valid(), serializer.errors value_processor = generate_value_processor(context={}, **serializer.object) actual = value_processor(input_) expected = [1, 2, 3] assert actual == expected
def generate_header_validator(headers, context, **kwargs): """ Generates a validation function that will validate a dictionary of headers. """ validators = ValidationDict() for header_definition in headers: header_processor = generate_value_processor(context=context, **header_definition) header_validator = generate_object_validator( field_validators=construct_header_validators(header_definition, context=context) ) validators.add_property_validator( header_definition["name"], chain_reduce_partial(header_processor, header_validator) ) return generate_object_validator(field_validators=validators)
def test_array_header_type_casting_with_multiple_items(): header_definition = single_header_validator({ 'type': ARRAY, 'collectionFormat': CSV, 'items': [ {'type': INTEGER}, {'type': STRING}, {'type': BOOLEAN}, ] }) value_processor = generate_value_processor(context={}, **header_definition) actual = value_processor('1,a,true,2') expected = [1, 'a', True, '2'] assert actual == expected
def test_array_header_type_casting_with_multiple_items(): serializer = HeaderSerializer( data={ 'type': ARRAY, 'collectionFormat': CSV, 'items': [ {'type': INTEGER}, {'type': STRING}, {'type': BOOLEAN}, ] } ) assert serializer.is_valid(), serializer.errors value_processor = generate_value_processor(context={}, **serializer.object) actual = value_processor('1,a,true,2') expected = [1, 'a', True, '2'] assert actual == expected
def generate_header_validator(headers, context, **kwargs): """ Generates a validation function that will validate a dictionary of headers. """ validators = ValidationDict() for header_definition in headers: header_processor = generate_value_processor(context=context, **header_definition) header_validator = generate_object_validator( field_validators=construct_header_validators(header_definition, context=context), ) validators.add_property_validator( header_definition['name'], chain_reduce_partial( header_processor, header_validator, ), ) return generate_object_validator(field_validators=validators)
def generate_header_validator(headers, context, **kwargs): validators = {} for header_definition in headers: header_processor = generate_value_processor( context=context, **header_definition ) header_validator = functools.partial( validate_object, validators=construct_header_validators(header_definition, context=context), inner=True, ) validators[header_definition['name']] = chain_reduce_partial( operator.methodcaller('get', header_definition['name'], EMPTY), header_processor, header_validator, ) return chain_reduce_partial( operator.attrgetter('headers'), functools.partial(validate_object, validators=validators, inner=True), )