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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
from os.path import join
from re import match
import pytest
from . import none_of
from prance import _TranslatingParser
@pytest.fixture
def tester(request):
pattern = r"test_(.+)"
test_function_name = request.node.name
name_match = match(pattern, test_function_name)
test_name = name_match[1]
file_name = f"{test_name}.spec.yaml"
path = join("tests", "specs", "translating_parser", file_name)
return SpecificationTester(path)
class SpecificationTester:
"""
Helper class that tests correct reference translation. Loads a specification file from tests/specs, parses it with
the TranslatingParser and provides assertion methods:
* assert_schemas to check what’s in /components/schemas.
* assert_path_ref to check the $ref of the only operation response in the specification.
* assert_schema_ref to check the $ref of a schema in /components/schemas.
Typical usage:
tester = SpecificationTester("my_spec_file")
tester.assert_path_ref("some_other_refd_file.spec.yaml_SomeObject")
tester.assert_schemas(
{"some_other_refd_file.spec.yaml_SomeObject", "some_other_refd_file.spec.yaml_SomeOtherObject"}
)
tester.assert_schema_ref(
"some_other_refd_file.spec.yaml_SomeObject", "some_other_refd_file.spec.yaml_SomeOtherObject",
)
"""
@staticmethod
def _assert_ref(schema, ref):
assert "$ref" in schema
assert schema["$ref"] == f"#/components/schemas/{ref}"
def __init__(self, url):
parser = _TranslatingParser(url, backend="openapi-spec-validator")
parser.parse()
self.specification = parser.specification
def assert_schemas(self, keys):
"""
Asserts schema names in /components/schemas.
"""
assert self.specification["components"]["schemas"].keys() == keys
def assert_path_ref(self, ref):
"""
Asserts $ref value in /paths/hosts/get/responses/default/content/application/json/schema.
"""
responses = self.specification["paths"]["/hosts"]["get"]["responses"]
schema = responses["default"]["content"]["application/json"]["schema"]
self._assert_ref(schema, ref)
def assert_schema_ref(self, key, ref, getter=lambda schema: schema):
"""
Asserts $ref value in /components/schemas/{key}. May use a custom getter function to find the $ref deeper in
the schema.
Example:
tester.assert_schema_ref(
"SomeComplexObject",
"schemas.spec.yaml_SomeOtherObject",
lambda schema: schema["properties"]["some_property"]
)
"""
schemas = self.specification["components"]["schemas"]
assert key in schemas
schema = getter(schemas[key])
self._assert_ref(schema, ref)
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_local_reference_from_root(tester):
tester.assert_path_ref("PlainObject")
tester.assert_schemas({"PlainObject"})
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_file_reference_from_root(tester):
tester.assert_path_ref("file_reference_from_root_schemas.spec.yaml_PlainObject")
tester.assert_schemas({"file_reference_from_root_schemas.spec.yaml_PlainObject"})
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_local_reference_from_file(tester):
tester.assert_path_ref("local_reference_from_file_schemas.spec.yaml_RefObject")
tester.assert_schemas(
{
"local_reference_from_file_schemas.spec.yaml_RefObject",
"local_reference_from_file_schemas.spec.yaml_PlainObject",
}
)
tester.assert_schema_ref(
"local_reference_from_file_schemas.spec.yaml_RefObject",
"local_reference_from_file_schemas.spec.yaml_PlainObject",
)
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_same_file_reference_from_file(tester):
tester.assert_path_ref("same_file_reference_from_file_schemas.spec.yaml_RefObject")
tester.assert_schemas(
{
"same_file_reference_from_file_schemas.spec.yaml_RefObject",
"same_file_reference_from_file_schemas.spec.yaml_PlainObject",
}
)
tester.assert_schema_ref(
"same_file_reference_from_file_schemas.spec.yaml_RefObject",
"same_file_reference_from_file_schemas.spec.yaml_PlainObject",
)
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_different_file_reference_from_file(tester):
tester.assert_path_ref(
"different_file_reference_from_file_schemas1.spec.yaml_RefObject"
)
tester.assert_schemas(
{
"different_file_reference_from_file_schemas1.spec.yaml_RefObject",
"different_file_reference_from_file_schemas2.spec.yaml_PlainObject",
}
)
tester.assert_schema_ref(
"different_file_reference_from_file_schemas1.spec.yaml_RefObject",
"different_file_reference_from_file_schemas2.spec.yaml_PlainObject",
)
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_root_file_reference_from_file(tester):
tester.assert_path_ref("root_file_reference_from_file_schemas.spec.yaml_RefObject")
tester.assert_schemas(
{"PlainObject", "root_file_reference_from_file_schemas.spec.yaml_RefObject"}
)
tester.assert_schema_ref(
"root_file_reference_from_file_schemas.spec.yaml_RefObject", "PlainObject"
)
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_root_file_reference_from_root(tester):
tester.assert_path_ref("PlainObject")
tester.assert_schemas({"PlainObject"})
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_recursive_reference_in_root(tester):
tester.assert_schema_ref(
"RecursiveObject",
"RecursiveObject",
lambda schema: schema["additionalProperties"],
)
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_recursive_reference_in_file(tester):
tester.assert_path_ref(
"recursive_reference_in_file_schemas.spec.yaml_RecursiveObject"
)
tester.assert_schemas(
{"recursive_reference_in_file_schemas.spec.yaml_RecursiveObject"}
)
tester.assert_schema_ref(
"recursive_reference_in_file_schemas.spec.yaml_RecursiveObject",
"recursive_reference_in_file_schemas.spec.yaml_RecursiveObject",
lambda schema: schema["additionalProperties"],
)
@pytest.mark.skipif(
none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
def test_nested_recursive_reference_in_file(tester):
tester.assert_path_ref("Response")
tester.assert_schemas(
{
"Response",
"ResultsItem",
"ReferenceObject",
"nested_recursive_reference_in_file_schemas.spec.yaml_ComplexObject",
"nested_recursive_reference_in_file_schemas.spec.yaml_ComplexObjectProperty",
"nested_recursive_reference_in_file_schemas.spec.yaml_RecursiveObject",
}
)
tester.assert_schema_ref(
"nested_recursive_reference_in_file_schemas.spec.yaml_ComplexObject",
"nested_recursive_reference_in_file_schemas.spec.yaml_ComplexObjectProperty",
lambda schema: schema["properties"]["property"],
)
tester.assert_schema_ref(
"nested_recursive_reference_in_file_schemas.spec.yaml_ComplexObjectProperty",
"nested_recursive_reference_in_file_schemas.spec.yaml_RecursiveObject",
lambda schema: schema["properties"]["recursive"],
)
tester.assert_schema_ref(
"nested_recursive_reference_in_file_schemas.spec.yaml_RecursiveObject",
"nested_recursive_reference_in_file_schemas.spec.yaml_RecursiveObject",
lambda schema: schema["additionalProperties"]["oneOf"][0],
)
|