-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
260 lines (210 loc) · 6.61 KB
/
tests.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import pytest
from apistar import App, ASyncApp
from apistar.test import TestClient
from apistar.http import JSONResponse
from apistar.server.handlers import serve_schema
from pydantic import BaseModel
from apistar_pydantic import (
PathParam, QueryParam, BodyData, DictQueryData,
PydanticBodyData, PydanticQueryData,
Route, components,
)
def _compute(self):
return {
'integer': 10 * self.integer,
'text': self.text.upper()
}
class Model(dict):
def __init__(self, value):
try:
new_value = {}
new_value['integer'] = int(value['integer'])
new_value['text'] = str(value['text'])
super().__init__(new_value)
except Exception:
raise Exception("Invalid model")
def __getattr__(self, key):
return self[key]
compute = _compute
class PydanticModel(BaseModel):
integer: int
text: str
compute = _compute
def client_factory(app_class, routes):
app = app_class(
components=components,
routes=routes
)
return TestClient(app)
@pytest.mark.parametrize('app_class', [ASyncApp, App])
def test_url(app_class):
def handler(arg1: PathParam[str]):
return JSONResponse(arg1.upper())
client = client_factory(app_class, [
Route('/resource/{arg1}', 'GET', handler),
])
res = client.get('/resource/aaa')
assert res.json() == 'AAA'
@pytest.mark.parametrize('app_class', [ASyncApp, App])
def test_query_simple(app_class):
def handler(arg1: QueryParam[int]):
return arg1 * 10
client = client_factory(app_class, [
Route('/resource', 'GET', handler),
])
res = client.get('/resource', params={'arg1': 10})
assert res.json() == 100
args = {
'integer': 10,
'text': 'abc'
}
expected = {
'integer': 100,
'text': 'ABC'
}
@pytest.mark.parametrize('app_class', [ASyncApp, App])
def test_query_model(app_class):
def handler(model: DictQueryData[Model]):
return model.compute()
client = client_factory(app_class, [
Route('/resource', 'GET', handler, documented=False)
])
res = client.get('/resource', params=args)
assert res.json() == expected
@pytest.mark.parametrize('app_class', [ASyncApp, App])
def test_body_model(app_class):
def handler(model: BodyData[Model]):
return model.compute()
client = client_factory(app_class, [
Route('/resource', 'PUT', handler)
])
res = client.put('/resource', json=args)
assert res.json() == expected
@pytest.mark.parametrize('app_class', [ASyncApp, App])
def test_mixed_arguments(app_class):
def handler(query: DictQueryData[Model],
body: BodyData[Model]):
return JSONResponse(query.integer * body.integer * query.text)
client = client_factory(app_class, [
Route('/resource', 'POST', handler, documented=False)
])
res = client.post(
'/resource',
params={
'integer': 2,
'text': 'a'
},
json={
'integer': 2,
'text': ''
})
assert res.json() == 'aaaa'
@pytest.mark.parametrize('app_class', [ASyncApp, App])
def test_pydantic_model(app_class):
def handler(query: PydanticQueryData[PydanticModel],
body: PydanticBodyData[PydanticModel]):
return JSONResponse(query.integer * body.integer * query.text)
client = client_factory(app_class, [
Route('/resource', 'POST', handler, documented=False)
])
res = client.post(
'/resource',
params={
'integer': 2,
'text': 'a'
},
json={
'integer': 2,
'text': ''
})
assert res.json() == 'aaaa'
@pytest.mark.parametrize('app_class', [ASyncApp, App])
def test_schema(app_class):
def add_model(model: BodyData[Model]):
"""add_model description"""
return model.integer
def list_models():
"""list_models description"""
return
def show_model(id: int):
"""show_model description"""
return
def show_model2(id: PathParam[int], name: QueryParam[str]):
"""show_model2 description"""
return
app = app_class(
routes=[
Route('/add_model', 'POST', add_model),
Route('/list_models', 'GET', list_models),
Route('/show_model', 'GET', show_model),
Route('/show/model/{id}', 'GET', show_model2),
Route('/schema/', 'GET', handler=serve_schema, documented=False),
],
# settings={
# 'SCHEMA': {'TITLE': 'My API'}
# }
)
client = TestClient(app)
res = client.get('/schema')
assert res.status_code == 200
assert res.json() == {
'openapi': '3.0.0',
'info': {
'title': '',
'description': '',
'version': ''
},
'paths': {
'/add_model': {
'post': {
'description': 'add_model description',
'operationId': 'add_model',
'requestBody': {
'content': {
'application/json': {
'schema': {'type': 'object'}
}
}
}
}
},
'/list_models': {
'get': {
'description': 'list_models description',
'operationId': 'list_models'
}
},
'/show_model': {
'get': {
'description': 'show_model description',
'operationId': 'show_model',
'parameters': [
{
'name': 'id',
'in': 'query',
'schema': {'type': 'integer'}
}
]
}
},
'/show/model/{id}': {
'get': {
'description': 'show_model2 description',
'operationId': 'show_model2',
'parameters': [
{
'name': 'id',
'in': 'path',
'required': True,
'schema': {'type': 'integer'}
},
{
'name': 'name',
'in': 'query',
'schema': {'type': 'string'}
}
]
}
}
}
}