-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathtest_app.py
More file actions
216 lines (168 loc) · 6.92 KB
/
Copy pathtest_app.py
File metadata and controls
216 lines (168 loc) · 6.92 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
from pathlib import Path
from unittest import mock
import pytest
from openapi_core import Config
from openapi_core import OpenAPI
from openapi_core import V3RequestUnmarshaller
from openapi_core import V3RequestValidator
from openapi_core import V3ResponseUnmarshaller
from openapi_core import V3ResponseValidator
from openapi_core.exceptions import SpecError
from openapi_core.protocols import Request
from openapi_core.protocols import Response
from openapi_core.protocols import WebhookRequest
from openapi_core.unmarshalling.request import V32RequestUnmarshaller
from openapi_core.unmarshalling.request import V32WebhookRequestUnmarshaller
from openapi_core.unmarshalling.response import V32ResponseUnmarshaller
from openapi_core.unmarshalling.response import V32WebhookResponseUnmarshaller
from openapi_core.validation.request import V32RequestValidator
from openapi_core.validation.request import V32WebhookRequestValidator
from openapi_core.validation.response import V32ResponseValidator
from openapi_core.validation.response import V32WebhookResponseValidator
class TestOpenAPIFromPath:
def test_valid(self, create_file):
spec_dict = {
"openapi": "3.1.0",
"info": {
"title": "Spec",
"version": "0.0.1",
},
"paths": {},
}
file_path = create_file(spec_dict)
path = Path(file_path)
result = OpenAPI.from_path(path)
assert type(result) == OpenAPI
assert result.spec.read_value() == spec_dict
class TestOpenAPIFromFilePath:
def test_valid(self, create_file):
spec_dict = {
"openapi": "3.1.0",
"info": {
"title": "Spec",
"version": "0.0.1",
},
"paths": {},
}
file_path = create_file(spec_dict)
result = OpenAPI.from_file_path(file_path)
assert type(result) == OpenAPI
assert result.spec.read_value() == spec_dict
class TestOpenAPIFromFile:
def test_valid(self, create_file):
spec_dict = {
"openapi": "3.1.0",
"info": {
"title": "Spec",
"version": "0.0.1",
},
"paths": {},
}
file_path = create_file(spec_dict)
with open(file_path) as f:
result = OpenAPI.from_file(f)
assert type(result) == OpenAPI
assert result.spec.read_value() == spec_dict
class TestOpenAPIFromDict:
def test_spec_error(self):
spec_dict = {}
with pytest.raises(SpecError):
OpenAPI.from_dict(spec_dict)
def test_check_skipped(self):
spec_dict = {}
config = Config(spec_validator_cls=None)
result = OpenAPI.from_dict(spec_dict, config=config)
assert type(result) == OpenAPI
assert result.spec.read_value() == spec_dict
class TestOpenAPIVersion32:
def test_v3_aliases_use_v32(self):
assert V3RequestValidator is V32RequestValidator
assert V3ResponseValidator is V32ResponseValidator
assert V3RequestUnmarshaller is V32RequestUnmarshaller
assert V3ResponseUnmarshaller is V32ResponseUnmarshaller
def test_default_request_validator(self, spec_v32):
result = OpenAPI(spec_v32)
assert result.request_validator_cls is V32RequestValidator
def test_default_response_validator(self, spec_v32):
result = OpenAPI(spec_v32)
assert result.response_validator_cls is V32ResponseValidator
def test_default_request_unmarshaller(self, spec_v32):
result = OpenAPI(spec_v32)
assert result.request_unmarshaller_cls is V32RequestUnmarshaller
def test_default_response_unmarshaller(self, spec_v32):
result = OpenAPI(spec_v32)
assert result.response_unmarshaller_cls is V32ResponseUnmarshaller
def test_default_webhook_request_validator(self, spec_v32):
result = OpenAPI(spec_v32)
assert (
result.webhook_request_validator_cls is V32WebhookRequestValidator
)
def test_default_webhook_response_validator(self, spec_v32):
result = OpenAPI(spec_v32)
assert (
result.webhook_response_validator_cls
is V32WebhookResponseValidator
)
def test_default_webhook_request_unmarshaller(self, spec_v32):
result = OpenAPI(spec_v32)
assert (
result.webhook_request_unmarshaller_cls
is V32WebhookRequestUnmarshaller
)
def test_default_webhook_response_unmarshaller(self, spec_v32):
result = OpenAPI(spec_v32)
assert (
result.webhook_response_unmarshaller_cls
is V32WebhookResponseUnmarshaller
)
class TestOpenAPIIterErrors:
@mock.patch(
"openapi_core.validation.request.validators.V32RequestValidator."
"iter_errors",
)
def test_iter_apicall_request_errors(self, mock_iter_errors, spec_v32):
openapi = OpenAPI(spec_v32)
request = mock.Mock(spec=Request)
errors_iter = iter([ValueError("oops")])
mock_iter_errors.return_value = errors_iter
result = openapi.iter_apicall_request_errors(request)
assert result is errors_iter
mock_iter_errors.assert_called_once_with(request)
@mock.patch(
"openapi_core.validation.request.validators.V32WebhookRequestValidator."
"iter_errors",
)
def test_iter_request_errors_webhook(self, mock_iter_errors, spec_v32):
openapi = OpenAPI(spec_v32)
request = mock.Mock(spec=WebhookRequest)
errors_iter = iter([ValueError("oops")])
mock_iter_errors.return_value = errors_iter
result = openapi.iter_request_errors(request)
assert result is errors_iter
mock_iter_errors.assert_called_once_with(request)
@mock.patch(
"openapi_core.validation.response.validators.V32ResponseValidator."
"iter_errors",
)
def test_iter_apicall_response_errors(self, mock_iter_errors, spec_v32):
openapi = OpenAPI(spec_v32)
request = mock.Mock(spec=Request)
response = mock.Mock(spec=Response)
errors_iter = iter([ValueError("oops")])
mock_iter_errors.return_value = errors_iter
result = openapi.iter_apicall_response_errors(request, response)
assert result is errors_iter
mock_iter_errors.assert_called_once_with(request, response)
@mock.patch(
"openapi_core.validation.response.validators.V32WebhookResponseValidator."
"iter_errors",
)
def test_iter_response_errors_webhook(self, mock_iter_errors, spec_v32):
openapi = OpenAPI(spec_v32)
request = mock.Mock(spec=WebhookRequest)
response = mock.Mock(spec=Response)
errors_iter = iter([ValueError("oops")])
mock_iter_errors.return_value = errors_iter
result = openapi.iter_response_errors(request, response)
assert result is errors_iter
mock_iter_errors.assert_called_once_with(request, response)