-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.py
175 lines (151 loc) · 5.21 KB
/
test.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
import unittest
from eave import Doc, Note, Api, PathParam, QueryParam, BodyParam
class TestDemo(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
pass
def tearDown(self):
pass
def test_doc(self):
doc = Doc(title='Test Api Document', version='v2')
doc.host = 'http://www.apitest.com'
doc.description = """
this is a test document
- test1
- test2
"""
html = doc.build()
self.assertIn('<h1>Test Api Document</h1>', html)
self.assertIn('<li>Version: <code>v2</code></li>', html)
self.assertIn('<li>Host: <code>http://www.apitest.com</code></li>', html)
self.assertIn('<li>test1</li>', html)
self.assertIn('<li>test2</li>', html)
def test_note(self):
doc = Doc()
note1 = Note()
note1.title = 'test note1'
note1.content = 'This is the **first** test note'
note2 = Note()
note2.content = 'This note has no title'
doc.add_note(note1)
doc.add_note(note2)
html = doc.build()
self.assertIn('test note1', html)
self.assertIn('This is the <strong>first</strong> test note', html)
self.assertNotIn('note2', html)
self.assertIn('This note has no title', html)
def test_api(self):
doc = Doc()
api = Api()
api.title = "first api"
api.method = "GET"
api.url = "/test1/<id>/"
api.description = "this is the **first** api"
doc.add_api(api)
html = doc.build()
self.assertIn('first api', html)
self.assertIn('<div class="endpoint get">', html)
self.assertIn('/test1/<id>/', html)
self.assertNotIn('<id>', html)
self.assertIn('<strong>first</strong>', html)
def test_path_param(self):
doc = Doc()
api = Api(title='test api', url='/test/<id>/', method='GET')
pp1 = PathParam()
pp1.name = 'id'
pp1.description = 'resource id'
pp1.example = 10
api.params.append(pp1)
doc.add_api(api)
html = doc.build()
self.assertIn('<td><code>id</code></td>', html)
self.assertIn('<td>resource id</td>', html)
self.assertIn('<td>10</td>', html)
def test_query_param(self):
doc = Doc()
api = Api(title='test api', url='/test/', method='GET')
qp1 = QueryParam()
qp1.name = 'page'
qp1.description = 'page number of list'
qp1.default = 1
api.params.append(qp1)
doc.add_api(api)
html = doc.build()
self.assertIn('<td><code>page</code></td>', html)
self.assertIn('<td>page number of list</td>', html)
self.assertIn('<td>1</td>', html)
def test_body_param(self):
doc = Doc()
api = Api(title='test api', url='/test/', method='POST')
bp1 = BodyParam(name='username')
bp2 = BodyParam()
bp2.name = 'password'
api.params = [bp1, bp2]
doc.add_api(api)
html = doc.build()
self.assertIn('<div class="endpoint post">', html)
self.assertIn('<td><code>username</code></td>', html)
self.assertIn('<td><code>password</code></td>', html)
def test_body_example(self):
doc = Doc()
api = Api(title='test api', url='/test/', method='POST')
api.body_example = """
{
"username": "balabala",
"password": "123456"
}
"""
doc.add_api(api)
html = doc.build()
self.assertIn('"username": "balabala"', html)
self.assertIn('"password": "123456"', html)
def test_response_description(self):
doc = Doc()
api = Api(title='test api', url='/test/', method='POST')
api.response_description = 'return *user* info'
doc.add_api(api)
html = doc.build()
self.assertIn('return <em>user</em> info', html)
def test_response_example(self):
doc = Doc()
api = Api(title='test api', url='/test/', method='POST')
api.response_example = """
{
"id": 2,
"username": "balabala",
"password": "123456"
}
"""
doc.add_api(api)
html = doc.build()
self.assertIn('"id": 2', html)
self.assertIn('"username": "balabala"', html)
self.assertIn('"password": "123456"', html)
def test_tips(self):
doc = Doc()
api = Api(title='test api', url='/test/', method='DELETE')
api.tips = '## Be careful to destroy'
doc.add_api(api)
html = doc.build()
self.assertIn('<div class="endpoint delete">', html)
self.assertIn('<h2>Be careful to destroy</h2>', html)
def test_language(self):
doc = Doc()
api = Api(title='test api', url='/test/', method='POST')
api.body_example = '{}'
doc.apis.append(api)
html_en = doc.build()
html_zh = doc.build(language='zh')
self.assertIn('Contents', html_en)
self.assertIn('Request Body Example', html_en)
self.assertIn('Print This Document', html_en)
self.assertIn('目录', html_zh)
self.assertIn('请求数据示例', html_zh)
self.assertIn('打印本文档', html_zh)
if __name__ == '__main__':
unittest.main(verbosity=1)