示例#1
0
 def test_to_dict(self):
     name, value, unit = "Size", "33", "M"
     p = ParameterFactory(name, value, unit)
     d = p.to_dict()
     self.assertEqual(d["name"], name)
     self.assertEqual(d["value"], value)
     self.assertEqual(d["unit"], unit)
示例#2
0
 def test_to_xml(self):
     name, value, unit = "Size", "33", "M"
     p = ParameterFactory(name, value, unit)
     el = p.to_xml()
     expected_el = ET.Element("param", {"name": name, "unit": unit})
     expected_el.text = value
     self.assertElementsEquals(el, expected_el)
示例#3
0
def test_collection_format_defaults_to_csv():
    raw_parameter = ParameterFactory()
    raw_parameter.pop('collectionFormat', None)
    value = single_parameter_validator(raw_parameter)

    with pytest.raises(AssertionError):
        # TODO: how do we set a default in the return object.
        assert value.get('collectionFormat') == CSV
示例#4
0
def test_collection_format_defaults_to_csv():
    raw_parameter = ParameterFactory()
    raw_parameter.pop("collectionFormat", None)
    value = single_parameter_validator(raw_parameter)

    with pytest.raises(AssertionError):
        # TODO: how do we set a default in the return object.
        assert value.get("collectionFormat") == CSV
示例#5
0
    def test_value_property(self):
        p = ParameterFactory()
        with self.assertRaises(ValidationError) as e:

            class Err:
                def __str__(self):
                    return 1

            p.value = Err()

            self.assertEqual(str(e), "value must be a string")
示例#6
0
def test_when_in_value_is_body_a_schema_is_required():
    parameter = ParameterFactory(**{
        'in': BODY,
    })
    parameter.pop('schema', None)
    with pytest.raises(ValidationError) as err:
        single_parameter_validator(parameter)

    assert_message_in_errors(
        MESSAGES['schema']['body_parameters_must_include_a_schema'],
        err.value.detail,
        '^schema',
    )
示例#7
0
def test_when_in_value_is_not_body_type_is_required(in_):
    parameter = ParameterFactory(**{
        'in': in_,
    })
    parameter.pop('type', None)
    with pytest.raises(ValidationError) as err:
        single_parameter_validator(parameter)

    assert_message_in_errors(
        MESSAGES['type']['non_body_parameters_must_declare_a_type'],
        err.value.detail,
        '^type',
    )
示例#8
0
def test_when_in_value_is_body_a_schema_is_required():
    parameter = ParameterFactory(**{
        'in': BODY,
    })
    parameter.pop('schema', None)
    with pytest.raises(ValidationError) as err:
        single_parameter_validator(parameter)

    assert_message_in_errors(
        MESSAGES['schema']['body_parameters_must_include_a_schema'],
        err.value.detail,
        '^schema',
    )
示例#9
0
def test_when_in_value_is_not_body_type_is_required(in_):
    parameter = ParameterFactory(**{
        'in': in_,
    })
    parameter.pop('type', None)
    with pytest.raises(ValidationError) as err:
        single_parameter_validator(parameter)

    assert_message_in_errors(
        MESSAGES['type']['non_body_parameters_must_declare_a_type'],
        err.value.detail,
        '^type',
    )
示例#10
0
def test_that_when_type_is_array_items_is_required():
    parameter = ParameterFactory(type=ARRAY)
    with pytest.raises(ValidationError) as err:
        single_parameter_validator(parameter)

    assert_message_in_errors(
        MESSAGES['items']['items_required_for_type_array'],
        err.value.detail,
        'items',
    )
示例#11
0
def test_parameter_validation_with_default_present():
    context = {'deferred_references': set()}
    parameter = ParameterFactory(default='0')
    assert 'default' in parameter
    try:
        single_parameter_validator(parameter, context=context)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_path_not_in_errors('default', errors)
示例#12
0
def test_when_in_value_is_path_required_must_be_true():
    with pytest.raises(ValidationError) as err:
        single_parameter_validator(
            ParameterFactory(**{
                'in': PATH,
                'required': False,
            }))

    assert_message_in_errors(
        MESSAGES['required']['path_parameters_must_be_required'],
        err.value.detail,
        '^required',
    )
示例#13
0
def test_multi_format_invalid_in_values(in_):
    parameter = ParameterFactory(**{
        'collectionFormat': MULTI,
        'in': in_,
    })
    with pytest.raises(ValidationError) as err:
        single_parameter_validator(parameter)

    assert_message_in_errors(
        MESSAGES['collection_format']['invalid_based_on_in_value'],
        err.value.detail,
        'collectionFormat',
    )
示例#14
0
def test_multi_format_valid_in_values(in_):
    parameter = ParameterFactory(**{
        'collectionFormat': MULTI,
        'in': in_,
    })
    try:
        single_parameter_validator(parameter)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_message_not_in_errors(
        MESSAGES['collection_format']['invalid_based_on_in_value'],
        errors,
    )
示例#15
0
def test_basic_valid_parameter():
    context = {'deferred_references': set()}
    raw_parameter = ParameterFactory()
    try:
        definitions_validator(
            {
                'parameters': {
                    'SomeParameter': raw_parameter,
                },
            },
            context=context)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_path_not_in_errors(
        'parameters',
        errors,
    )
示例#16
0
def test_default_as_non_array_is_invalid():
    """
    https://github.com/swagger-api/swagger-spec/issues/343#issuecomment-128792251
    """
    context = {'deferred_references': set()}
    parameter = ParameterFactory(
        name="status",
        type=ARRAY,
        items={"type": STRING},
        collectionFormat=MULTI,
        default="active",
    )
    with pytest.raises(ValidationError) as err:
        single_parameter_validator(parameter, context=context)

    assert_message_in_errors(
        MESSAGES['default']['invalid_type'],
        err.value.detail,
        'default',
    )
示例#17
0
def test_default_as_array_is_valid():
    """
    https://github.com/swagger-api/swagger-spec/issues/343#issuecomment-128792251
    """
    context = {'deferred_references': set()}
    parameter = ParameterFactory(
        name="status",
        type=ARRAY,
        items={"type": STRING},
        collectionFormat=MULTI,
        default=["active"],
    )
    try:
        single_parameter_validator(parameter, context=context)
    except ValidationError as err:
        errors = err.detail
    else:
        errors = {}

    assert_path_not_in_errors('default', errors)
示例#18
0
 def test_from_xml(self):
     p = ParameterFactory()
     el = p.to_xml()
     parsed_p = Parameter.from_xml(el)
     self.assertEqual(p.to_dict(), parsed_p.to_dict())