Skip to content

Commit aa26cf7

Browse files
committed
add tests
1 parent 9cf7231 commit aa26cf7

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

tests/test_apidocs.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from starlette.testclient import TestClient
44

55
from star_resty.apidocs import setup_spec
6-
from .utils.method import CreateUser, GetUser, SearchUser
6+
from .utils.method import CreateUser, GetUser, SearchUser, GetItemsEcho
77

88

99
def test_generate_api_docs():
@@ -104,3 +104,25 @@ def test_generate_api_docs_for_path():
104104
'responses': {
105105
'200': {'schema': {'$ref': '#/definitions/tests.utils.method.CreateUserResponse'}},
106106
'400': {'description': 'Bad request'}}}}}
107+
108+
109+
def test_generate_api_docs_for_nested():
110+
app = Starlette()
111+
112+
setup_spec(app, title='test')
113+
app.add_route('/items', GetItemsEcho.as_endpoint(), methods=['GET'])
114+
115+
client = TestClient(app)
116+
resp = client.get('/apidocs.json')
117+
assert resp is not None
118+
body = resp.json()
119+
assert body is not None
120+
assert body.get('paths') == {
121+
'/items': {
122+
'get': {'tags': ['items'], 'description': 'get items', 'produces': ['application/json'], 'parameters': [
123+
{'in': 'query', 'name': 'id', 'required': False, 'collectionFormat': 'multi', 'type': 'array',
124+
'items': {'type': 'integer', 'format': 'int32'}}],
125+
'responses': {'200': {'schema': {'$ref': '#/definitions/tests.utils.method.ItemsModel'}},
126+
'400': {'description': 'Bad request'}}}
127+
}
128+
}

tests/test_endpoints.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from starlette.responses import JSONResponse
44
from starlette.testclient import TestClient
55

6-
from tests.utils.method import SearchUser
6+
from tests.utils.method import SearchUser, GetItemsEcho
77

88

99
def test_search_user():
@@ -31,3 +31,14 @@ async def error(request, exc: ValidationError):
3131
client = TestClient(app)
3232
resp = client.get('/users/invalid?q=Test')
3333
assert resp.status_code == 400
34+
35+
36+
def test_nested_result():
37+
app = Starlette()
38+
39+
app.add_route('/items', GetItemsEcho.as_endpoint(), methods=['GET'])
40+
client = TestClient(app)
41+
resp = client.get('/items?id=1&id=2&id=3')
42+
assert resp.status_code == 200
43+
data = resp.json()
44+
assert data == {'items': [{'id': 1}, {'id': 2}, {'id': 3}]}

tests/utils/method.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ class SearchUserResponse(Schema):
2727
q = fields.String()
2828

2929

30+
class ItemsModel(Schema):
31+
class Item(Schema):
32+
id = fields.Integer()
33+
34+
items = fields.List(fields.Nested(Item))
35+
success = fields.Bool(missing=True)
36+
37+
38+
class GetItemsEcho(Method):
39+
class Input(Schema):
40+
id = fields.List(fields.Integer())
41+
42+
meta = Operation(tag='items', description='get items')
43+
response_schema = ItemsModel
44+
45+
async def execute(self, items: query(Input)):
46+
return {'items': [{'id': item_id} for item_id in items['id']]}
47+
48+
3049
class CreateUser(Method):
3150
meta = Operation(tag='users', description='create user')
3251
response_schema = CreateUserResponse

0 commit comments

Comments
 (0)