def test_find_parameter_errors_when_no_match_found(): parameters = parameters_validator([ ID_IN_PATH, USERNAME_IN_PATH, PAGE_IN_QUERY, PAGE_SIZE_IN_QUERY, ]) #sanity check assert not filter_parameters(parameters, name='not-in-parameters') with pytest.raises(ValueError): find_parameter(parameters, name='not-in-parameters')
def test_find_parameter_errors_when_multiple_found(): parameters = parameters_validator([ ID_IN_PATH, USERNAME_IN_PATH, PAGE_IN_QUERY, PAGE_SIZE_IN_QUERY, ]) #sanity check sanity = filter_parameters(parameters, in_=PATH) assert len(sanity) == 2 with pytest.raises(ValueError): find_parameter(parameters, in_=PATH)
def test_find_parameter_errors_when_no_match_found(): serializer = ParameterSerializer(many=True, data=[ ID_IN_PATH, USERNAME_IN_PATH, PAGE_IN_QUERY, PAGE_SIZE_IN_QUERY, ]) assert serializer.is_valid(), serializer.errors parameters = serializer.object #sanity check assert not filter_parameters(parameters, name='not-in-parameters') with pytest.raises(ValueError): find_parameter(parameters, name='not-in-parameters')
def test_find_parameter_errors_when_multiple_found(): serializer = ParameterSerializer(many=True, data=[ ID_IN_PATH, USERNAME_IN_PATH, PAGE_IN_QUERY, PAGE_SIZE_IN_QUERY, ]) assert serializer.is_valid(), serializer.errors parameters = serializer.object #sanity check sanity = filter_parameters(parameters, in_=PATH) assert len(sanity) == 2 with pytest.raises(ValueError): find_parameter(parameters, in_=PATH)
def test_find_parameter(lookup_kwargs, expected): parameters = parameters_validator([ ID_IN_PATH, USERNAME_IN_PATH, PAGE_IN_QUERY, PAGE_SIZE_IN_QUERY, ]) actual = find_parameter(parameters, **lookup_kwargs) for key in lookup_kwargs: assert actual[key] == expected[key]
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_merge_parameters_uses_last_write_wins(): duplicate_a = { 'name': 'duplicate', 'in': PATH, 'description': 'duplicate_a', 'type': INTEGER, 'required': True } duplicate_b = { 'name': 'duplicate', 'in': PATH, 'description': 'duplicate_b', 'type': STRING, 'required': True } main_serializer = ParameterSerializer(many=True, data=[ ID_IN_PATH, USERNAME_IN_PATH, duplicate_a, PAGE_IN_QUERY, ]) assert main_serializer.is_valid(), main_serializer.errors main_parameters = main_serializer.object sub_serializer = ParameterSerializer(many=True, data=[ duplicate_b, PAGE_SIZE_IN_QUERY, ]) assert sub_serializer.is_valid(), sub_serializer.errors sub_parameters = sub_serializer.object merged_parameters = merge_parameter_lists(main_parameters, sub_parameters) assert len(merged_parameters) == 5 assert find_parameter(merged_parameters, description='duplicate_b') assert not filter_parameters(merged_parameters, description='duplicate_a') assert find_parameter(merged_parameters, in_=PATH, name='id') assert find_parameter(merged_parameters, in_=PATH, name='username') assert find_parameter(merged_parameters, in_=QUERY, name='page') assert find_parameter(merged_parameters, in_=QUERY, name='page_size')
def test_find_parameter(lookup_kwargs, expected): serializer = ParameterSerializer(many=True, data=[ ID_IN_PATH, USERNAME_IN_PATH, PAGE_IN_QUERY, PAGE_SIZE_IN_QUERY, ]) assert serializer.is_valid(), serializer.errors parameters = serializer.object actual = find_parameter(parameters, **lookup_kwargs) for key in lookup_kwargs: assert actual[key] == expected[key]
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_merge_parameters_uses_last_write_wins(): duplicate_a = { 'name': 'duplicate', 'in': PATH, 'description': 'duplicate_a', 'type': INTEGER, 'required': True } duplicate_b = { 'name': 'duplicate', 'in': PATH, 'description': 'duplicate_b', 'type': STRING, 'required': True } main_parameters = parameters_validator([ ID_IN_PATH, USERNAME_IN_PATH, duplicate_a, PAGE_IN_QUERY, ]) sub_parameters = parameters_validator([ duplicate_b, PAGE_SIZE_IN_QUERY, ]) merged_parameters = merge_parameter_lists(main_parameters, sub_parameters) assert len(merged_parameters) == 5 assert find_parameter(merged_parameters, description='duplicate_b') assert not filter_parameters(merged_parameters, description='duplicate_a') assert find_parameter(merged_parameters, in_=PATH, name='id') assert find_parameter(merged_parameters, in_=PATH, name='username') assert find_parameter(merged_parameters, in_=QUERY, name='page') assert find_parameter(merged_parameters, in_=QUERY, name='page_size')
def process_path_part(part, parameters): """ Given a part of a path either: - If it is a parameter: parse it to a regex group - Otherwise: escape any special regex characters """ if PARAMETER_REGEX.match(part): parameter_name = part.strip('{}') try: parameter = find_parameter(parameters, name=parameter_name, in_=PATH) except ValueError: pass else: return construct_parameter_pattern(parameter) return escape_regex_special_chars(part)