File: test_convert.py

package info (click to toggle)
python-prance 25.4.8.0%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,144 kB
  • sloc: python: 3,381; makefile: 205
file content (146 lines) | stat: -rw-r--r-- 4,219 bytes parent folder | download
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
"""Test suite for prance.convert ."""

__author__ = "Jens Finkhaeuser"
__copyright__ = "Copyright (c) 2018-2021 Jens Finkhaeuser"
__license__ = "MIT"
__all__ = ()

import pytest

from . import none_of

from prance import convert


@pytest.fixture
def petstore_yaml():
    from prance.util import fs

    return fs.read_file("tests/examples/v2.0/yaml/petstore.yaml")


@pytest.fixture
def petstore_json():
    from prance.util import fs

    return fs.read_file("tests/examples/v2.0/json/petstore.json")


@pytest.mark.requires_network()
def test_convert_petstore_yaml(petstore_yaml):
    converted, content_type = convert.convert_str(
        petstore_yaml, content_type="application/yaml"
    )

    # Check correct content type
    assert "yaml" in content_type

    # Parsing can't fail.
    from prance.util import formats

    parsed = formats.parse_spec(converted, content_type=content_type)

    # Assert the correct target version
    assert "openapi" in parsed
    assert parsed["openapi"].startswith("3.")


@pytest.mark.requires_network()
def test_convert_petstore_json(petstore_json):
    converted, content_type = convert.convert_str(
        petstore_json, content_type="application/json"
    )

    # Check correct content type
    assert "json" in content_type

    # Parsing can't fail.
    from prance.util import formats

    parsed = formats.parse_spec(converted, content_type=content_type)

    # Assert the correct target version
    assert "openapi" in parsed
    assert parsed["openapi"].startswith("3.")


@pytest.mark.requires_network()
def test_convert_petstore_yaml_explicit_name(petstore_yaml):
    converted, content_type = convert.convert_str(petstore_yaml, filename="foo.yml")

    # Check correct content type
    assert "yaml" in content_type


@pytest.mark.requires_network()
def test_convert_url():
    from prance.util import url

    converted, content_type = convert.convert_url(
        url.absurl("python://tests/specs/petstore.yaml")
    )

    # Check correct content type
    assert "yaml" in content_type

    # Parsing can't fail.
    from prance.util import formats

    parsed = formats.parse_spec(converted, content_type=content_type)

    # Assert the correct target version
    assert "openapi" in parsed
    assert parsed["openapi"].startswith("3.")


@pytest.mark.requires_network()
def test_convert_spec():
    from prance import BaseParser, ResolvingParser, ValidationError

    parser = BaseParser("tests/specs/petstore.yaml")

    # Conversion should fail with the default backend.
    with pytest.raises(ValidationError):
        converted = convert.convert_spec(parser.specification, backend="flex")

    # However, with the lazy flag it should work.
    converted = convert.convert_spec(parser.specification, lazy=True)
    assert isinstance(converted, BaseParser)

    # Passing a ResolvingParser class should also work.
    converted = convert.convert_spec(parser.specification, ResolvingParser, lazy=True)
    assert isinstance(converted, ResolvingParser)


@pytest.mark.requires_network()
def test_convert_parser_lazy_swagger_backend():
    from prance import BaseParser, ResolvingParser, ValidationError

    parser = BaseParser("tests/specs/petstore.yaml")

    # Conversion should fail with the default backend.
    with pytest.raises(ValidationError):
        converted = convert.convert_spec(parser, backend="flex")

    # However, with the lazy flag it should work.
    converted = convert.convert_spec(parser, lazy=True)
    assert isinstance(converted, BaseParser)

    # Passing a ResolvingParser class should also work.
    converted = convert.convert_spec(parser, ResolvingParser, lazy=True)
    assert isinstance(converted, ResolvingParser)


@pytest.mark.skipif(
    none_of("openapi-spec-validator"), reason="Missing openapi-spec-validator"
)
@pytest.mark.requires_network()
def test_convert_parser_validated():
    from prance import BaseParser

    parser = BaseParser("tests/specs/petstore.yaml", backend="openapi-spec-validator")

    # Conversion should work: it's the right backend, and it validates.
    converted = convert.convert_spec(parser)
    assert isinstance(converted, BaseParser)
    assert converted.version_parsed[0] == 3