-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathtest_decorators.py
More file actions
251 lines (211 loc) · 9.14 KB
/
test_decorators.py
File metadata and controls
251 lines (211 loc) · 9.14 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
import pytest
from unittest.mock import Mock, patch
from domaintools.decorators import api_endpoint
@pytest.fixture
def api_specs():
"""
A fixture that acts as a central registry for all test OpenAPI specs.
"""
return {
# --- Spec V1: Standard ---
"v1": {
"openapi": "3.0.0",
"info": {"title": "Standard Spec", "version": "1.0.0"},
"paths": {
"/users": {
"get": {
"parameters": [
{
"name": "status",
"in": "query",
"required": True,
"schema": {"type": "string"},
}
]
},
"post": {
"requestBody": {
"required": True,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
},
}
}
},
}
},
}
},
},
# --- Spec V3: Complex Lookup (Matches parameter name in components) ---
"v3_complex": {
"openapi": "3.0.0",
"info": {"title": "Complex Lookup Spec", "version": "3.0.0"},
"components": {
"parameters": {
"LimitParam": {
"name": "limit",
"in": "query",
"description": "Max number of items.",
"schema": {"type": "integer"},
}
},
"schemas": {
"UserRequestParameters": {
"type": "object",
"properties": {
"limit": {
# The validator/patcher should match this name to 'LimitParam' above
"$ref:": "#/components/schemas/IgnoredRef",
},
},
},
},
"requestBodies": {
"UserBody": {
"required": True,
"content": {
"application/json": {
"schema": {"$ref": "#/components/schemas/UserRequestParameters"},
},
},
}
},
},
"paths": {
"/users": {
"post": {
"requestBody": {"$ref": "#/components/requestBodies/UserBody"},
},
},
},
},
}
@pytest.fixture
def mock_client(api_specs):
"""
Creates a mock API client that is pre-patched with the specs defined above.
"""
client = Mock()
client.specs = api_specs
return client
class TestApiEndpointDecorator:
def test_metadata_preservation(self, mock_client):
"""
Ensure decorator copies metadata for DocstringPatcher.
"""
@api_endpoint(spec_name="v1", path="/users", methods="GET")
def get_users():
"""Original Docstring"""
pass
bound_method = get_users.__get__(mock_client, Mock)
assert bound_method._api_spec_name == "v1"
assert bound_method._api_path == "/users"
assert bound_method._api_methods == ["GET"]
assert bound_method.__doc__ == "Original Docstring"
def test_valid_post_request(self, mock_client):
"""
Test a valid POST request against 'v1' spec.
"""
@api_endpoint(spec_name="v1", path="/users", methods="POST")
def create_user(request_body=None):
return "Created"
# Mocking validate to ensure arguments are passed correctly,
# but we could also let it run against the real logic if we wanted integration tests.
with patch("domaintools.request_validator.RequestValidator.validate") as mock_validate:
result = create_user(mock_client, request_body={"name": "Alice", "age": 30})
assert result == "Created"
# Check arguments passed to validator
call_kwargs = mock_validate.call_args[1]
assert call_kwargs["spec"] == mock_client.specs["v1"]
assert call_kwargs.get("parameters", {}).get("request_body") == {
"name": "Alice",
"age": 30,
}
def test_validation_failure_blocks_execution(self, mock_client):
"""
Test that if validation fails, the function doesn't run.
"""
inner_logic = Mock()
@api_endpoint(spec_name="v1", path="/users", methods="POST")
def create_user(body=None):
inner_logic()
# Simulate a validation error
with patch(
"domaintools.request_validator.RequestValidator.validate",
side_effect=ValueError("Bad Input"),
):
with pytest.raises(ValueError, match="Bad Input"):
create_user(mock_client, body={"bad": "data"})
inner_logic.assert_not_called()
def test_complex_spec_lookup_integration(self, mock_client):
"""
Test that the decorator works with the complex 'v3_complex' spec
we defined in the fixture.
"""
@api_endpoint(spec_name="v3_complex", path="/users", methods="POST")
def create_user_complex(body=None):
return "Complex Success"
with patch("domaintools.request_validator.RequestValidator.validate") as mock_validate:
create_user_complex(mock_client, body={"limit": 10})
# Verify the correct spec dictionary was retrieved and passed
call_kwargs = mock_validate.call_args[1]
assert call_kwargs["spec"]["info"]["title"] == "Complex Lookup Spec"
assert call_kwargs["path"] == "/users"
def test_missing_spec_skips_validation(self, mock_client):
"""
If we ask for a spec name that isn't in the fixture, it should handle gracefully.
"""
@api_endpoint(spec_name="non_existent_version", path="/users", methods="GET")
def get_users():
return "Ran Safe"
with patch("domaintools.request_validator.RequestValidator.validate") as mock_validate:
result = get_users(mock_client)
assert result == "Ran Safe"
mock_validate.assert_not_called()
def test_positional_arguments_are_mapped(self, mock_client):
"""
Test that passing arguments positionally (args) instead of via keywords (kwargs)
still triggers validation correctly.
"""
# Define function with explicit parameter names
@api_endpoint(spec_name="v1", path="/users", methods="POST")
def create_user(name=None, body=None):
return "Success"
with patch("domaintools.request_validator.RequestValidator.validate") as mock_validate:
# CALL POSITIONALLY: passing client and body as args
# (Note: we pass mock_client manually because create_user is just a function here)
create_user(mock_client, "test-name")
# Verify validator received the data mapped to 'body_data'
mock_validate.assert_called_once()
call_kwargs = mock_validate.call_args[1]
assert call_kwargs.get("parameters") == {"name": "test-name"}
def test_none_arguments_are_filtered_before_validation(self, mock_client):
"""
Test that parameters with None values are excluded from the arguments
passed to the validator.
"""
@api_endpoint(spec_name="v1", path="/users", methods="POST")
def create_user(name=None, status=None):
return "Success"
with patch("domaintools.request_validator.RequestValidator.validate") as mock_validate:
create_user(mock_client, name="Alice", status=None)
call_kwargs = mock_validate.call_args[1]
assert call_kwargs.get("parameters") == {"name": "Alice"}
assert "status" not in call_kwargs.get("parameters", {})
def test_all_none_arguments_passes_empty_parameters(self, mock_client):
"""
Test that when all arguments are None, an empty dict is passed to the validator.
"""
@api_endpoint(spec_name="v1", path="/users", methods="POST")
def create_user(name=None, body=None):
return "Success"
with patch("domaintools.request_validator.RequestValidator.validate") as mock_validate:
create_user(mock_client, name=None, body=None)
call_kwargs = mock_validate.call_args[1]
assert call_kwargs.get("parameters") == {}