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
|
"""Test suite for prance.BaseParser ."""
__author__ = "Jens Finkhaeuser"
__copyright__ = "Copyright (c) 2016-2021 Jens Finkhaeuser"
__license__ = "MIT"
__all__ = ()
import pytest
from prance import BaseParser
from prance import ValidationError
from prance.util.fs import FileNotFoundError
from . import none_of
@pytest.fixture
def petstore_parser():
return BaseParser("tests/specs/petstore.yaml")
@pytest.fixture
def petstore_parser_from_string():
yaml = None
with open("tests/specs/petstore.yaml", "rb") as f:
x = f.read()
yaml = x.decode("utf8")
return BaseParser(spec_string=yaml)
def test_load_fail():
from prance.util.url import ResolutionError
with pytest.raises(ResolutionError):
BaseParser("tests/specs/missing.yaml")
def test_parse_fail():
with pytest.raises(ValidationError):
BaseParser(
spec_string="""---
invalid 'a'sda YAML"""
)
def test_version_fail():
with pytest.raises(ValidationError):
BaseParser(
spec_string="""---
openapi: 4.0.0"""
)
def test_filename_or_spec():
with pytest.raises(AssertionError):
BaseParser("", "")
@pytest.mark.skipif(
none_of("openapi-spec-validator", "swagger-spec-validator", "flex"),
reason="Backends missing.",
)
def test_load_and_parse_valid(petstore_parser):
assert petstore_parser.specification, "No specs loaded!"
@pytest.mark.skipif(
none_of("openapi-spec-validator", "swagger-spec-validator", "flex"),
reason="Backends missing.",
)
def test_load_and_parse_lazy():
parser = BaseParser("tests/specs/petstore.yaml", lazy=True)
assert parser.specification is None, "Should not have specs yet!"
parser.parse()
assert parser.specification, "No specs loaded!"
@pytest.mark.skipif(
none_of("openapi-spec-validator", "swagger-spec-validator", "flex"),
reason="Backends missing.",
)
def test_yaml_valid(petstore_parser):
assert petstore_parser.yaml(), "Did not get YAML representation of specs!"
@pytest.mark.skipif(
none_of("openapi-spec-validator", "swagger-spec-validator", "flex"),
reason="Backends missing.",
)
def test_json_valid(petstore_parser):
assert petstore_parser.json(), "Did not get JSON representation of specs!"
@pytest.mark.skipif(
none_of("openapi-spec-validator", "swagger-spec-validator", "flex"),
reason="Backends missing.",
)
def test_cache_specs_mixin(petstore_parser):
# In order to test the caching, we need to first use either the YAML or the
# JSON mixin. Let's use YAML, because it's more swagger-ish
yaml = petstore_parser.yaml()
assert yaml, "Did not get YAML representation of specs!"
# Caching should mean that if the specifications do not change, then neither
# does the YAML representation.
assert yaml == petstore_parser.yaml(), "YAML representation changed!"
# In fact, the objects shouldn't even change.
assert id(yaml) == id(petstore_parser.yaml()), (
"YAML did not change but " "got regenerated!"
)
# However, when the specs change, then so must the YAML representation.
petstore_parser.specification["foo"] = "bar"
assert yaml != petstore_parser.yaml(), "YAML representation did not change!"
@pytest.mark.skipif(
none_of("openapi-spec-validator", "swagger-spec-validator", "flex"),
reason="Backends missing.",
)
def test_relative_urls_from_string(petstore_parser_from_string):
# This must succeed
assert (
petstore_parser_from_string.yaml()
), "Did not get YAML representation of specs!"
|