File: test_schema_utils.py

package info (click to toggle)
sphinxcontrib-openapi 0.8.4-4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 880 kB
  • sloc: python: 7,575; makefile: 23
file content (253 lines) | stat: -rw-r--r-- 9,340 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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import pytest

from sphinxcontrib.openapi.schema_utils import example_from_schema


@pytest.mark.parametrize(
    ["schema", "expected"],
    [
        pytest.param(
            {
                "type": "object",
                "properties": {
                    "string": {"type": "string"},
                    "integer": {"type": "integer"},
                    "number": {"type": "number"},
                    "array_of_numbers": {"type": "array", "items": {"type": "number"}},
                    "array_of_strings": {"type": "array", "items": {"type": "string"}},
                },
            },
            {
                "string": "string",
                "integer": 1,
                "number": 1.0,
                "array_of_numbers": [1.0, 1.0],
                "array_of_strings": ["string", "string"],
            },
            id="no_examples",
        ),
        pytest.param(
            {
                "type": "object",
                "properties": {
                    "string": {"type": "string", "example": "example_string"},
                    "integer": {"type": "integer", "example": 2},
                    "number": {"type": "number", "example": 2.0},
                    "array_of_numbers": {
                        "type": "array",
                        "items": {"type": "number", "example": 3.0},
                    },
                    "array_of_strings": {
                        "type": "array",
                        "items": {"type": "string", "example": "example_string"},
                    },
                },
            },
            {
                "string": "example_string",
                "integer": 2,
                "number": 2.0,
                "array_of_numbers": [3.0, 3.0],
                "array_of_strings": ["example_string", "example_string"],
            },
            id="with_examples",
        ),
        pytest.param(
            {
                "type": "object",
                "properties": {
                    "string": {"type": "string"},
                    "date": {"type": "string", "format": "date"},
                    "date-time": {"type": "string", "format": "date-time"},
                    "password": {"type": "string", "format": "password"},
                    "byte": {"type": "string", "format": "byte"},
                    "ipv4": {"type": "string", "format": "ipv4"},
                    "ipv6": {"type": "string", "format": "ipv6"},
                    "unknown": {"type": "string", "format": "unknown"},
                    "max": {"type": "string", "maxLength": 3},
                    "min": {"type": "string", "minLength": 10},
                    "max_min_big": {"type": "string", "maxLength": 15, "minLength": 10},
                    "max_min_small": {"type": "string", "maxLength": 3, "minLength": 1},
                },
            },
            {
                "string": "string",
                "date": "2020-01-01",
                "date-time": "2020-01-01T01:01:01Z",
                "password": "********",
                "byte": "QG1pY2hhZWxncmFoYW1ldmFucw==",
                "ipv4": "127.0.0.1",
                "ipv6": "::1",
                "unknown": "string",
                "max": "str",
                "min": "stringstri",
                "max_min_big": "stringstri",
                "max_min_small": "str",
            },
            id="strings",
        ),
        pytest.param(
            {
                "type": "object",
                "properties": {
                    "inner_object": {"type": "object"},
                    "inner_array_of_objects": {
                        "type": "array",
                        "items": {"type": "object"},
                    },
                    "inner_array_of_arrays": {
                        "type": "array",
                        "items": {"type": "array", "items": {"type": "integer"}},
                    },
                },
            },
            {
                "inner_object": {},
                "inner_array_of_objects": [{}, {}],
                "inner_array_of_arrays": [[1, 1], [1, 1]],
            },
            id="nested",
        ),
        pytest.param(
            {
                "type": "object",
                "properties": {
                    "string_enum": {"type": "string", "enum": ["three", "two", "one"]},
                    "integer_enum": {"type": "integer", "enum": [3, 2, 1]},
                },
            },
            {"string_enum": "three", "integer_enum": 3},
            id="enum",
        ),
        pytest.param(
            {
                "type": "object",
                "properties": {
                    "mixed_type_array": {
                        "type": "array",
                        "items": {"oneOf": {"string", "integer"}},
                    },
                    "any_type_array": {"type": "array", "items": {}},
                    "0_to_1_length_array": {
                        "type": "array",
                        "minItems": 0,
                        "maxItems": 1,
                        "items": {"type": "integer"},
                    },
                    "5_to_10_length_array": {
                        "type": "array",
                        "minItems": 5,
                        "maxItems": 10,
                        "items": {"type": "integer"},
                    },
                },
            },
            {
                "mixed_type_array": [1, 1],
                "any_type_array": ["string", 1],
                "0_to_1_length_array": [1],
                "5_to_10_length_array": [1, 1, 1, 1, 1],
            },
            id="arrays",
        ),
        pytest.param(
            {
                "type": "object",
                "properties": {
                    "oneOf": {
                        "oneOf": [
                            {
                                "type": "object",
                                "properties": {"one": {"type": "string"}},
                            },
                            {
                                "type": "object",
                                "properties": {"two": {"type": "string"}},
                            },
                        ]
                    },
                    "anyOf": {
                        "anyOf": [
                            {
                                "type": "object",
                                "properties": {"three": {"type": "string"}},
                            },
                            {
                                "type": "object",
                                "properties": {"four": {"type": "string"}},
                            },
                        ]
                    },
                    "allOf": {
                        "allOf": [
                            {
                                "type": "object",
                                "properties": {"five": {"type": "string"}},
                            },
                            {
                                "type": "object",
                                "properties": {"six": {"type": "string"}},
                            },
                        ]
                    },
                },
            },
            {
                "oneOf": {"one": "string"},
                "anyOf": {"three": "string"},
                "allOf": {"five": "string", "six": "string"},
            },
            id="oneOf_anyOf_allOf",
        ),
        pytest.param(
            {
                "type": "object",
                "properties": {"anything": {"description": "this can be anything"}},
            },
            {"anything": 1},
            id="any_type",
        ),
        pytest.param(
            {
                "type": "object",
                "properties": {
                    "min_int": {"type": "integer", "minimum": 10},
                    "max_int": {"type": "integer", "maximum": -10},
                    "min_and_max_int": {
                        "type": "integer",
                        "minimum": 10,
                        "maximum": 20,
                    },
                    "exclusive_int": {
                        "type": "integer",
                        "minimum": -10,
                        "maximum": 10,
                        "exclusiveMinimum": True,
                        "exclusiveMaximum": True,
                    },
                    "min_num": {"type": "number", "minimum": 10.0},
                    "max_num": {"type": "number", "maximum": -10.0},
                    "exclusive_num": {
                        "type": "number",
                        "minimum": -10,
                        "maximum": 10,
                        "exclusiveMinimum": True,
                        "exclusiveMaximum": True,
                    },
                },
            },
            {
                "min_int": 11,
                "max_int": -11,
                "min_and_max_int": 15,
                "exclusive_int": 0,
                "min_num": 11.0,
                "max_num": -11.0,
                "exclusive_num": 0.0,
            },
            id="min_max",
        ),
    ],
)
def test_generate_example_from_schema(schema, expected):
    assert example_from_schema(schema) == expected