-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathtest_swagger_utils.py
194 lines (160 loc) · 5.81 KB
/
test_swagger_utils.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from flask_restplus.swagger import extract_path, extract_path_params, parse_docstring
class ExtractPathTest(object):
def test_extract_static_path(self):
path = '/test'
assert extract_path(path) == '/test'
def test_extract_path_with_a_single_simple_parameter(self):
path = '/test/<parameter>'
assert extract_path(path) == '/test/{parameter}'
def test_extract_path_with_a_single_typed_parameter(self):
path = '/test/<string:parameter>'
assert extract_path(path) == '/test/{parameter}'
def test_extract_path_with_a_single_typed_parameter_with_arguments(self):
path = '/test/<string(length=2):parameter>'
assert extract_path(path) == '/test/{parameter}'
def test_extract_path_with_multiple_parameters(self):
path = '/test/<parameter>/<string:other>/'
assert extract_path(path) == '/test/{parameter}/{other}/'
class ExtractPathParamsTestCase(object):
def test_extract_static_path(self):
path = '/test'
assert extract_path_params(path) == {}
def test_extract_single_simple_parameter(self):
path = '/test/<parameter>'
assert extract_path_params(path) == {
'parameter': {
'name': 'parameter',
'type': 'string',
'in': 'path',
'required': True
}
}
def test_single_int_parameter(self):
path = '/test/<int:parameter>'
assert extract_path_params(path) == {
'parameter': {
'name': 'parameter',
'type': 'integer',
'in': 'path',
'required': True
}
}
def test_single_float_parameter(self):
path = '/test/<float:parameter>'
assert extract_path_params(path) == {
'parameter': {
'name': 'parameter',
'type': 'number',
'in': 'path',
'required': True
}
}
def test_extract_path_with_multiple_parameters(self):
path = '/test/<parameter>/<int:other>/'
assert extract_path_params(path) == {
'parameter': {
'name': 'parameter',
'type': 'string',
'in': 'path',
'required': True
},
'other': {
'name': 'other',
'type': 'integer',
'in': 'path',
'required': True
}
}
def test_extract_parameter_with_arguments(self):
path = '/test/<string(length=2):parameter>'
assert extract_path_params(path) == {
'parameter': {
'name': 'parameter',
'type': 'string',
'in': 'path',
'required': True
}
}
# def test_extract_registered_converters(self):
# class ListConverter(BaseConverter):
# def to_python(self, value):
# return value.split(',')
# def to_url(self, values):
# return ','.join(super(ListConverter, self).to_url(value) for value in values)
# self.app.url_map.converters['list'] = ListConverter
# path = '/test/<list:parameters>'
# with self.context():
# self.assertEqual(extract_path_params(path), [{
# 'name': 'parameters',
# 'type': 'number',
# 'in': 'path',
# 'required': True
# }])
class ParseDocstringTest(object):
def test_empty(self):
def without_doc():
pass
parsed = parse_docstring(without_doc)
assert parsed['raw'] is None
assert parsed['summary'] is None
assert parsed['details'] is None
assert parsed['returns'] is None
assert parsed['raises'] == {}
assert parsed['params'] == []
def test_single_line(self):
def func():
'''Some summary'''
pass
parsed = parse_docstring(func)
assert parsed['raw'] == 'Some summary'
assert parsed['summary'] == 'Some summary'
assert parsed['details'] is None
assert parsed['returns'] is None
assert parsed['raises'] == {}
assert parsed['params'] == []
def test_multi_line(self):
def func():
'''
Some summary
Some details
'''
pass
parsed = parse_docstring(func)
assert parsed['raw'] == 'Some summary\nSome details'
assert parsed['summary'] == 'Some summary'
assert parsed['details'] == 'Some details'
assert parsed['returns'] is None
assert parsed['raises'] == {}
assert parsed['params'] == []
def test_multi_line_and_dot(self):
def func():
'''
Some summary. bla bla
Some details
'''
pass
parsed = parse_docstring(func)
assert parsed['raw'] == 'Some summary. bla bla\nSome details'
assert parsed['summary'] == 'Some summary'
assert parsed['details'] == 'bla bla\nSome details'
assert parsed['returns'] is None
assert parsed['raises'] == {}
assert parsed['params'] == []
def test_raises(self):
def func():
'''
Some summary.
:raises SomeException: in case of something
'''
pass
parsed = parse_docstring(func)
assert parsed['raw'] == 'Some summary.\n:raises SomeException: in case of something'
assert parsed['summary'] == 'Some summary'
assert parsed['details'] is None
assert parsed['returns'] is None
assert parsed['params'] == []
assert parsed['raises'] == {
'SomeException': 'in case of something'
}