1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
import pytest
from swagger_spec_validator.common import SwaggerValidationError
from swagger_spec_validator.validator20 import validate_definitions
@pytest.mark.parametrize(
"property_spec",
[
{"type": "integer", "default": 1},
{"type": "boolean", "default": True},
{"type": "null", "default": None},
{"type": "number", "default": 2},
{"type": "number", "default": 3.4},
{"type": "object", "default": {"a_random_property": "valid"}},
{"type": "array", "items": {"type": "integer"}, "default": [5, 6, 7]},
{"default": -1}, # if type is not defined any value is a valid value
{"type": "string", "default": ""},
],
)
def test_api_check_default_succeed(property_spec):
definitions = {
"injected_definition": {
"properties": {
"property": property_spec,
},
},
}
# Success if no exception are raised
validate_definitions(definitions, lambda x: x)
@pytest.mark.parametrize(
"property_spec, validator, instance",
[
[
{"type": "integer", "default": "wrong_type"},
"type",
"wrong_type",
],
[
{"type": "boolean", "default": "wrong_type"},
"type",
"wrong_type",
],
[
{"type": "null", "default": "wrong_type"},
"type",
"wrong_type",
],
[
{"type": "number", "default": "wrong_type"},
"type",
"wrong_type",
],
[
{"type": "object", "default": "wrong_type"},
"type",
"wrong_type",
],
[
{"type": "array", "default": "wrong_type"},
"type",
"wrong_type",
],
[
{"type": "string", "default": -1},
"type",
-1,
],
[
{"type": "string", "minLength": 100, "default": "short_string"},
"minLength",
"short_string",
],
],
)
def test_api_check_default_fails(property_spec, validator, instance):
definitions = {
"injected_definition": {
"properties": {
"property": property_spec,
},
},
}
with pytest.raises(SwaggerValidationError) as excinfo:
validate_definitions(definitions, lambda x: x)
validation_error = excinfo.value.args[1]
assert validation_error.instance == instance
assert validation_error.validator == validator
def test_multiple_types_fail():
definitions = {
"definition_1": {
"type": ["number", "boolean"],
},
}
with pytest.raises(
SwaggerValidationError,
match=r"In definition of .*, type must be a string; lists are not allowed \(.*\)",
) as excinfo:
validate_definitions(definitions, lambda x: x)
assert str(definitions["definition_1"]["type"]) in str(excinfo.value)
def test_type_array_with_items_succeed_validation():
definitions = {
"definition_1": {
"type": "array",
"items": {
"type": "string",
},
},
}
# Success if no exception are raised
validate_definitions(definitions, lambda x: x)
def test_type_array_without_items_succeed_fails():
definitions = {
"definition_1": {
"type": "array",
},
}
with pytest.raises(SwaggerValidationError) as excinfo:
validate_definitions(definitions, lambda x: x)
assert (
str(excinfo.value)
== "Definition of type array must define `items` property (definition #/definitions/definition_1)."
)
def test_inline_model_is_not_valid_validation_fails():
definitions = {
"definition_1": {
"properties": {
"property": {
"type": "array",
},
},
},
}
with pytest.raises(SwaggerValidationError) as excinfo:
validate_definitions(definitions, lambda x: x)
assert (
str(excinfo.value) == "Definition of type array must define `items` property "
"(definition #/definitions/definition_1/properties/property)."
)
|