/
test_resolver.py
238 lines (204 loc) · 11.7 KB
/
test_resolver.py
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
import pytest
import connexion.apps
from connexion.exceptions import ResolverError
from connexion.operations import Swagger2Operation
from connexion.resolver import Resolver, RestyResolver
PARAMETER_DEFINITIONS = {'myparam': {'in': 'path', 'type': 'integer'}}
def test_standard_get_function():
function = Resolver().resolve_function_from_operation_id('connexion.FlaskApp.common_error_handler')
assert function == connexion.FlaskApp.common_error_handler
def test_resty_get_function():
function = RestyResolver('connexion').resolve_function_from_operation_id('connexion.FlaskApp.common_error_handler')
assert function == connexion.FlaskApp.common_error_handler
def test_missing_operation_id():
# Missing operationIDs should result in a well-defined error that can
# be handled upstream.
with pytest.raises(ResolverError):
Resolver().resolve_function_from_operation_id(None)
with pytest.raises(ResolverError):
RestyResolver('connexion').resolve_function_from_operation_id(None)
def test_bad_operation_id():
# Unresolvable operationIDs should result in a well-defined error that can
# be handled upstream.
with pytest.raises(ResolverError):
Resolver().resolve_function_from_operation_id('ohai.I.do.not.exist')
with pytest.raises(ResolverError):
RestyResolver('connexion').resolve_function_from_operation_id('ohai.I.do.not.exist')
def test_standard_resolve_x_router_controller():
operation = Swagger2Operation(api=None,
method='GET',
path='endpoint',
path_parameters=[],
operation={
'x-swagger-router-controller': 'fakeapi.hello',
'operationId': 'post_greeting',
},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=Resolver())
assert operation.operation_id == 'fakeapi.hello.post_greeting'
def test_resty_resolve_operation_id():
operation = Swagger2Operation(api=None,
method='GET',
path='endpoint',
path_parameters=[],
operation={
'operationId': 'fakeapi.hello.post_greeting',
},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.hello.post_greeting'
def test_resty_resolve_x_router_controller_with_operation_id():
operation = Swagger2Operation(api=None,
method='GET',
path='endpoint',
path_parameters=[],
operation={
'x-swagger-router-controller': 'fakeapi.hello',
'operationId': 'post_greeting',
},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.hello.post_greeting'
def test_resty_resolve_x_router_controller_without_operation_id():
operation = Swagger2Operation(api=None,
method='GET',
path='/hello/{id}',
path_parameters=[],
operation={'x-swagger-router-controller': 'fakeapi.hello'},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.hello.get'
def test_resty_resolve_with_default_module_name():
operation = Swagger2Operation(api=None,
method='GET',
path='/hello/{id}',
path_parameters=[],
operation={},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.hello.get'
def test_resty_resolve_with_default_module_name_lowercase_verb():
operation = Swagger2Operation(api=None,
method='get',
path='/hello/{id}',
path_parameters=[],
operation={},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.hello.get'
def test_resty_resolve_with_default_module_name_will_translate_dashes_in_resource_name():
operation = Swagger2Operation(api=None,
method='GET',
path='/foo-bar',
path_parameters=[],
operation={},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.foo_bar.search'
def test_resty_resolve_with_default_module_name_can_resolve_api_root():
operation = Swagger2Operation(api=None,
method='GET',
path='/',
path_parameters=[],
operation={},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.get'
def test_resty_resolve_with_default_module_name_will_resolve_resource_root_get_as_search():
operation = Swagger2Operation(api=None,
method='GET',
path='/hello',
path_parameters=[],
operation={},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.hello.search'
def test_resty_resolve_with_default_module_name_and_x_router_controller_will_resolve_resource_root_get_as_search():
operation = Swagger2Operation(api=None,
method='GET',
path='/hello',
path_parameters=[],
operation={
'x-swagger-router-controller': 'fakeapi.hello',
},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.hello.search'
def test_resty_resolve_with_default_module_name_will_resolve_resource_root_as_configured():
operation = Swagger2Operation(api=None,
method='GET',
path='/hello',
path_parameters=[],
operation={},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi', 'api_list'))
assert operation.operation_id == 'fakeapi.hello.api_list'
def test_resty_resolve_with_default_module_name_will_resolve_resource_root_post_as_post():
operation = Swagger2Operation(api=None,
method='POST',
path='/hello',
path_parameters=[],
operation={},
app_produces=['application/json'],
app_consumes=['application/json'],
app_security=[],
security_definitions={},
definitions={},
parameter_definitions=PARAMETER_DEFINITIONS,
resolver=RestyResolver('fakeapi'))
assert operation.operation_id == 'fakeapi.hello.post'