-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathtest_main.py
More file actions
323 lines (274 loc) · 8.93 KB
/
test_main.py
File metadata and controls
323 lines (274 loc) · 8.93 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
from io import StringIO
from unittest import mock
import pytest
from openapi_spec_validator import __version__
from openapi_spec_validator.__main__ import main
def test_schema_v2_detect(capsys):
"""Test schema v2 is detected"""
testargs = ["./tests/integration/data/v2.0/petstore.yaml"]
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "./tests/integration/data/v2.0/petstore.yaml: OK\n" in out
def test_schema_v31_detect(capsys):
"""Test schema v3.1 is detected"""
testargs = ["./tests/integration/data/v3.1/petstore.yaml"]
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "./tests/integration/data/v3.1/petstore.yaml: OK\n" in out
def test_schema_v31(capsys):
"""No errors when calling proper v3.1 file."""
testargs = [
"--schema",
"3.1.0",
"./tests/integration/data/v3.1/petstore.yaml",
]
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "./tests/integration/data/v3.1/petstore.yaml: OK\n" in out
def test_schema_v30(capsys):
"""No errors when calling proper v3.0 file."""
testargs = [
"--schema",
"3.0.0",
"./tests/integration/data/v3.0/petstore.yaml",
]
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "./tests/integration/data/v3.0/petstore.yaml: OK\n" in out
def test_schema_v2(capsys):
"""No errors when calling with proper v2 file."""
testargs = [
"--schema",
"2.0",
"./tests/integration/data/v2.0/petstore.yaml",
]
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "./tests/integration/data/v2.0/petstore.yaml: OK\n" in out
def test_many(capsys):
"""No errors when calling with proper v2 and v3 files."""
testargs = [
"./tests/integration/data/v2.0/petstore.yaml",
"./tests/integration/data/v3.0/petstore.yaml",
]
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "./tests/integration/data/v2.0/petstore.yaml: OK\n" in out
assert "./tests/integration/data/v3.0/petstore.yaml: OK\n" in out
def test_errors_on_missing_description_best(capsys):
"""An error is obviously printed given an empty schema."""
testargs = [
"./tests/integration/data/v3.0/missing-description.yaml",
"--schema=3.0.0",
]
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert (
"./tests/integration/data/v3.0/missing-description.yaml: Validation Error:"
in out
)
assert "Failed validating" in out
assert "'description' is a required property" in out
assert "'$ref' is a required property" not in out
assert "1 more subschemas errors" in out
def test_errors_on_missing_description_full(capsys):
"""An error is obviously printed given an empty schema."""
testargs = [
"./tests/integration/data/v3.0/missing-description.yaml",
"--subschema-errors=all",
"--schema=3.0.0",
]
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert (
"./tests/integration/data/v3.0/missing-description.yaml: Validation Error:"
in out
)
assert "Failed validating" in out
assert "'description' is a required property" in out
assert "'$ref' is a required property" in out
assert "1 more subschema error" not in out
def test_schema_unknown(capsys):
"""Errors on running with unknown schema."""
testargs = [
"--schema",
"x.x",
"./tests/integration/data/v2.0/petstore.yaml",
]
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert "error: argument --schema" in err
assert not out
def test_validation_error(capsys):
"""SystemExit on running with ValidationError."""
testargs = [
"--schema",
"3.0.0",
"./tests/integration/data/v2.0/petstore.yaml",
]
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert (
"./tests/integration/data/v2.0/petstore.yaml: Validation Error:" in out
)
assert "Failed validating" in out
assert "'openapi' is a required property" in out
@mock.patch(
"openapi_spec_validator.__main__.OpenAPIV30SpecValidator.validate",
side_effect=Exception,
)
def test_unknown_error(m_validate, capsys):
"""SystemExit on running with unknown error."""
testargs = [
"--schema",
"3.0.0",
"./tests/integration/data/v2.0/petstore.yaml",
]
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "./tests/integration/data/v2.0/petstore.yaml: Error:" in out
def test_nonexisting_file(capsys):
"""Calling with non-existing file should sys.exit."""
testargs = ["i_dont_exist.yaml"]
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "No such file: i_dont_exist.yaml\n" in out
def test_schema_stdin(capsys):
"""Test schema from STDIN"""
spes_path = "./tests/integration/data/v3.0/petstore.yaml"
with open(spes_path) as spec_file:
spec_lines = spec_file.readlines()
spec_io = StringIO("".join(spec_lines))
testargs = ["--schema", "3.0.0", "-"]
with mock.patch("openapi_spec_validator.__main__.sys.stdin", spec_io):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "stdin: OK\n" in out
def test_malformed_schema_stdin(capsys):
"""Malformed schema from STDIN reports validation error."""
spec_io = StringIO(
"""
openapi: 3.1.0
info:
version: "1"
title: "Title"
components:
schemas:
Component:
type: object
properties:
name: string
"""
)
testargs = ["--schema", "3.1.0", "-"]
with mock.patch("openapi_spec_validator.__main__.sys.stdin", spec_io):
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "stdin: Validation Error:" in out
assert "stdin: OK" not in out
def test_errors_all_lists_all_validation_errors(capsys):
spec_io = StringIO(
"""
openapi: 3.0.0
"""
)
testargs = ["--validation-errors", "all", "--schema", "3.0.0", "-"]
with mock.patch("openapi_spec_validator.__main__.sys.stdin", spec_io):
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert "stdin: Validation Error: [1]" in out
assert "stdin: Validation Error: [2]" in out
assert "'info' is a required property" in out
assert "'paths' is a required property" in out
assert "stdin: 2 validation errors found" in out
def test_error_alias_controls_subschema_errors_and_warns(capsys):
testargs = [
"./tests/integration/data/v3.0/missing-description.yaml",
"--error",
"all",
"--schema=3.0.0",
]
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert "'$ref' is a required property" in out
assert "validation errors found" not in out
assert (
"DeprecationWarning: --errors/--error is deprecated. "
"Use --subschema-errors instead."
) in err
def test_error_alias_warning_can_be_disabled(capsys):
testargs = [
"./tests/integration/data/v3.0/missing-description.yaml",
"--error",
"all",
"--schema=3.0.0",
]
with mock.patch.dict(
"openapi_spec_validator.__main__.os.environ",
{"OPENAPI_SPEC_VALIDATOR_WARN_DEPRECATED": "0"},
clear=False,
):
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert "'$ref' is a required property" in out
assert not err
def test_deprecated_error_ignored_when_new_flag_used(capsys):
spec_io = StringIO(
"""
openapi: 3.0.0
"""
)
testargs = [
"--error",
"all",
"--subschema-errors",
"best-match",
"--validation-errors",
"all",
"--schema",
"3.0.0",
"-",
]
with mock.patch("openapi_spec_validator.__main__.sys.stdin", spec_io):
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert "stdin: Validation Error: [1]" in out
assert "# Probably due to this subschema error" not in out
assert (
"DeprecationWarning: --errors/--error is deprecated and ignored when "
"--subschema-errors is provided."
) in err
assert "stdin: 2 validation errors found" in out
def test_version(capsys):
"""Test --version flag outputs correct version."""
testargs = ["--version"]
with pytest.raises(SystemExit):
main(testargs)
out, err = capsys.readouterr()
assert not err
assert out.strip() == f"openapi-spec-validator {__version__}"