-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_docstring_patcher.py
More file actions
351 lines (313 loc) · 12.2 KB
/
test_docstring_patcher.py
File metadata and controls
351 lines (313 loc) · 12.2 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import logging
import types
from unittest.mock import Mock
from domaintools.docstring_patcher import DocstringPatcher
class TestDocstringPatcher:
def _setup_mock_api(
self,
spec_dict: dict,
spec_name: str,
method_name: str,
path: str,
http_methods: list,
docstring: str,
) -> Mock:
"""
Helper to create a mock API instance with a mock decorated method.
"""
# Create the mock API instance
mock_api = Mock()
mock_api.specs = {spec_name: spec_dict}
# Create the underlying function that was decorated
def original_func():
"""Original docstring."""
pass
original_func.__doc__ = docstring
original_func._api_spec_name = spec_name
original_func._api_path = path
original_func._api_methods = http_methods
# Create the mock instance method
mock_method = types.MethodType(original_func, mock_api)
setattr(mock_api, method_name, mock_method)
return mock_api, method_name
def setup_method(self, method):
"""Pytest setup hook, runs before each test."""
self.patcher = DocstringPatcher()
# SPEC 1: The very first spec with non-standard 'parameters' in requestBody
self.SPEC_1_NON_STANDARD_PARAMS = {
"openapi": "3.0.0",
"info": {"title": "Spec 1"},
"components": {
"parameters": {
"LimitParam": {
"name": "limit",
"in": "query",
"description": "Max number of items.",
"schema": {"type": "integer"},
}
},
"schemas": {"User": {"type": "object"}},
"requestBodies": {
"UserBody": {
"description": "User object.",
"required": True,
"content": {
"application/json": {"schema": {"$ref": "#/components/schemas/User"}}
},
"parameters": [{"$ref": "#/components/parameters/LimitParam"}],
}
},
},
"paths": {
"/users": {
"post": {
"summary": "Create user",
"requestBody": {"$ref": "#/components/requestBodies/UserBody"},
},
}
},
}
# SPEC 2: The spec with UserRequestParameters (name, age)
self.SPEC_2_SCHEMA_PROPS = {
"openapi": "3.0.0",
"info": {"title": "Spec 2"},
"components": {
"schemas": {
"UserRequestParameters": {
"type": "object",
"properties": {
"name": {"type": "string", "description": "User's name"},
"age": {"type": "int", "description": "User's age"},
},
},
},
"requestBodies": {
"UserBody": {
"description": "User object to create.",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/UserRequestParameters"}
}
},
}
},
},
"paths": {
"/users": {
"post": {
"summary": "Create a new user",
"requestBody": {"$ref": "#/components/requestBodies/UserBody"},
},
}
},
}
# SPEC 3: The final spec with the "lookup-by-name" logic
self.SPEC_3_LOOKUP_BY_NAME = {
"openapi": "3.0.0",
"info": {"title": "Spec 3"},
"components": {
"parameters": {
"LimitParam": {
"name": "limit",
"in": "query",
"description": "Max number of items to return.",
"schema": {"type": "integer"},
}
},
"schemas": {
"UserRequestParameters": {
"type": "object",
"properties": {"limit": {"$ref:": "#/components/schemas/ApexDomain"}},
},
},
"requestBodies": {
"UserBody": {
"description": "User object to create.",
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/UserRequestParameters"}
}
},
}
},
},
"paths": {
"/users": {
"post": {
"summary": "Create a new user",
"requestBody": {"$ref": "#/components/requestBodies/UserBody"},
},
}
},
}
# SPEC 4: A full spec for GET, including Responses
self.SPEC_4_WITH_RESPONSE = {
"openapi": "3.0.0",
"info": {"title": "Spec 4"},
"components": {
"parameters": {
"LimitParam": {
"name": "limit",
"in": "query",
"description": "Max items.",
"schema": {"type": "integer"},
}
},
"schemas": {"User": {"type": "object"}},
},
"paths": {
"/users": {
"get": {
"summary": "Get all users",
"parameters": [
{
"name": "status",
"in": "query",
"required": True,
"description": "User status.",
"schema": {"type": "string"},
},
{"$ref": "#/components/parameters/LimitParam"},
],
"responses": {
"200": {
"description": "A list of users.",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {"$ref": "#/components/schemas/User"},
}
}
},
}
},
},
}
},
}
def test_spec_1_non_standard_params(self):
"""
Tests the first spec: parameters inside requestBody should
be displayed under 'Request Body'.
"""
mock_api, method_name = self._setup_mock_api(
spec_dict=self.SPEC_1_NON_STANDARD_PARAMS,
spec_name="spec1",
method_name="create_user",
path="/users",
http_methods=["post"],
docstring="This creates a user.",
)
self.patcher.patch(mock_api)
doc = getattr(mock_api, method_name).__doc__
assert "This creates a user." in doc
assert "--- Operation: POST /users ---" in doc
assert "Request Body:" in doc
assert "**User**" in doc
assert "Parameters (associated with this body):" in doc
assert "**limit** (integer) [in: query]" in doc
assert "Description: Max number of items." in doc
assert "Query Parameters:" in doc
assert "(No query parameters)" in doc
def test_spec_2_schema_props(self):
"""
Tests the second spec: requestBody schema properties (name, age)
should be unpacked and displayed.
"""
mock_api, method_name = self._setup_mock_api(
spec_dict=self.SPEC_2_SCHEMA_PROPS,
spec_name="spec2",
method_name="create_user",
path="/users",
http_methods=["post"],
docstring="Creates user.",
)
self.patcher.patch(mock_api)
doc = getattr(mock_api, method_name).__doc__
assert "Creates user." in doc
assert "--- Operation: POST /users ---" in doc
assert "Request Body:" in doc
assert "**UserRequestParameters**" in doc
assert "Description: User object to create." in doc
assert "Properties:" in doc
assert "**name** (string)" in doc
assert "Description: User's name" in doc
assert "**age** (int)" in doc
assert "Description: User's age" in doc
assert "Parameters (associated with this body):" not in doc
def test_spec_3_lookup_by_name(self):
"""
Tests the final spec: requestBody property 'limit' should
be matched with components.parameters.LimitParam by name.
"""
mock_api, method_name = self._setup_mock_api(
spec_dict=self.SPEC_3_LOOKUP_BY_NAME,
spec_name="spec3",
method_name="create_user",
path="/users",
http_methods=["post"],
docstring="Creates user.",
)
self.patcher.patch(mock_api)
doc = getattr(mock_api, method_name).__doc__
assert "--- Operation: POST /users ---" in doc
assert "Request Body:" in doc
assert "**UserRequestParameters**" in doc
assert "Properties:" in doc
# This is the key assertion:
assert "**limit** (integer)" in doc
assert "Description: Max number of items to return." in doc
# Ensure it didn't use the $ref: value
assert "ApexDomain" not in doc
def test_spec_4_get_with_response(self):
"""
Tests a GET operation with query params and a response.
"""
mock_api, method_name = self._setup_mock_api(
spec_dict=self.SPEC_4_WITH_RESPONSE,
spec_name="spec4",
method_name="get_users",
path="/users",
http_methods=["get"],
docstring="Gets users.",
)
self.patcher.patch(mock_api)
doc = getattr(mock_api, method_name).__doc__
assert "Gets users." in doc
assert "--- Operation: GET /users ---" in doc
# Check Query Params
assert "Query Parameters:" in doc
assert "**status** (string)" in doc
assert "Required: True" in doc
assert "Description: User status." in doc
assert "**limit** (integer)" in doc
assert "Description: Max items." in doc
# Check Request Body
assert "Request Body:" in doc
assert "(No request body)" in doc
# Check Responses
assert "Result Body (Responses):" in doc
assert "**200**: (array[User])" in doc
assert "Description: A list of users." in doc
def test_patching_error_path(self, caplog):
"""
Tests that a failure to find the operation generates the
correct error docstring and logs a warning.
"""
mock_api, method_name = self._setup_mock_api(
spec_dict=self.SPEC_1_NON_STANDARD_PARAMS, # Spec doesn't matter
spec_name="spec1",
method_name="get_pets",
path="/pets", # This path doesn't exist in the spec
http_methods=["get"],
docstring="Original doc.",
)
with caplog.at_level(logging.WARNING):
self.patcher.patch(mock_api)
doc = getattr(mock_api, method_name).__doc__
assert "Original doc." in doc
assert "--- API Details Error ---" in doc
assert "(Could not find operations ['get'] for path '/pets' in spec 'spec1')" in doc
# Test that no *parsing* error was logged
assert "Error parsing spec" not in caplog.text