-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_watchlist.py
284 lines (237 loc) · 10 KB
/
test_watchlist.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import unittest
# from app import app, db, Movie, User
# 导入命令函数
from app import app, db, Movie, User, forge, initdb
class WatchlistTestCase(unittest.TestCase):
def setUp(self):
# 更新配置
app.config.update(
TESTING=True,
SQLALCHEMY_DATABASE_URI='sqlite:///:memory:'
)
# 创建数据库和表
db.create_all()
# 创建测试数据,一个用户,一个电影条目
user = User(name='Test', username='test')
user.set_password('123')
movie = Movie(title='Test Movie Title', year='2019')
# 使用 add_all() 方法一次添加多个模型类实例,传入列表
db.session.add_all([user, movie])
db.session.commit()
self.client = app.test_client() # 创建测试客户端
self.runner = app.test_cli_runner() # 创建测试命令运行器
def tearDown(self):
db.session.remove() # 清除数据库会话
db.drop_all() # 删除数据库表
def login(self):
self.client.post('/login', data=dict(
username='test',
password='123'
), follow_redirects=True)
# 测试程序实例是否存在
def test_app_exist(self):
self.assertIsNotNone(app)
# 测试程序是否处于测试模式
def test_app_is_testing(self):
self.assertTrue(app.config['TESTING'])
# 测试 404 页面
def test_404_page(self):
response = self.client.get('/nothing') # 传入目标 URL
data = response.get_data(as_text=True)
self.assertIn('Page Not Found - 404', data)
self.assertIn('Go Back', data)
self.assertEqual(response.status_code, 404) # 判断响应状态码
# 测试主页
def test_index_page(self):
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertIn('Test\'s Watchlist', data)
self.assertIn('Test Movie Title', data)
self.assertEqual(response.status_code, 200)
# 辅助方法,用于登入用户
# 测试创建条目
def test_create_item(self):
self.login()
# 测试创建条目操作
response = self.client.post('/', data=dict(
title='New Movie',
year='2019'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Item created.', data)
self.assertIn('New Movie', data)
# 测试创建条目操作,但电影标题为空
response = self.client.post('/', data=dict(
title='',
year='2019'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item created.', data)
self.assertIn('Invalid input.', data)
# 测试创建条目操作,但电影年份为空
response = self.client.post('/', data=dict(
title='New Movie',
year=''
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item created.', data)
self.assertIn('Invalid input.', data)
# 测试更新条目
def test_update_item(self):
self.login()
# 测试更新页面
response = self.client.get('/movie/edit/1')
data = response.get_data(as_text=True)
self.assertIn('Edit item', data)
self.assertIn('Test Movie Title', data)
self.assertIn('2019', data)
# 测试更新条目操作
response = self.client.post('/movie/edit/1', data=dict(
title='New Movie Edited',
year='2019'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Item updated.', data)
self.assertIn('New Movie Edited', data)
# 测试更新条目操作,但电影标题为空
response = self.client.post('/movie/edit/1', data=dict(
title='',
year='2019'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item updated.', data)
self.assertIn('Invalid input.', data)
# 测试更新条目操作,但电影年份为空
response = self.client.post('/movie/edit/1', data=dict(
title='New Movie Edited Again',
year=''
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Item updated.', data)
self.assertNotIn('New Movie Edited Again', data)
self.assertIn('Invalid input.', data)
# 测试删除条目
def test_delete_item(self):
self.login()
response = self.client.post('/movie/delete/1', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Item deleted.', data)
self.assertNotIn('Test Movie Title', data)
# 测试登录保护
def test_login_protect(self):
response = self.client.get('/')
data = response.get_data(as_text=True)
self.assertNotIn('Logout', data)
self.assertNotIn('Settings', data)
self.assertNotIn('<form method="post">', data)
self.assertNotIn('Delete', data)
self.assertNotIn('Edit', data)
# 测试登录
def test_login(self):
response = self.client.post('/login', data=dict(
username='test',
password='123'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Login success.', data)
self.assertIn('Logout', data)
self.assertIn('Settings', data)
self.assertIn('Delete', data)
self.assertIn('Edit', data)
# self.assertIn('<form method="post">', data)
self.assertIn('<form class="inline-form" method="post" action="/movie/delete/1">', data)
# 测试使用错误的密码登录
response = self.client.post('/login', data=dict(
username='test',
password='456'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Login success.', data)
self.assertIn('Invalid username or password.', data)
# 测试使用错误的用户名登录
response = self.client.post('/login', data=dict(
username='wrong',
password='123'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Login success.', data)
self.assertIn('Invalid username or password.', data)
# 测试使用空用户名登录
response = self.client.post('/login', data=dict(
username='',
password='123'
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Login success.', data)
self.assertIn('Invalid input.', data)
# 测试使用空密码登录
response = self.client.post('/login', data=dict(
username='test',
password=''
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Login success.', data)
self.assertIn('Invalid input.', data)
# 测试登出
def test_logout(self):
self.login()
response = self.client.get('/logout', follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Goodbye.', data)
self.assertNotIn('Logout', data)
self.assertNotIn('Settings', data)
self.assertNotIn('Delete', data)
self.assertNotIn('Edit', data)
self.assertNotIn('<form method="post">', data)
# 测试设置
def test_settings(self):
self.login()
# 测试设置页面
response = self.client.get('/settings')
data = response.get_data(as_text=True)
self.assertIn('Settings', data)
self.assertIn('Your Name', data)
# 测试更新设置
response = self.client.post('/settings', data=dict(
name='Grey Li',
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertIn('Settings updated.', data)
self.assertIn('Grey Li', data)
# 测试更新设置,名称为空
response = self.client.post('/settings', data=dict(
name='',
), follow_redirects=True)
data = response.get_data(as_text=True)
self.assertNotIn('Settings updated.', data)
self.assertIn('Invalid input.', data)
# 测试虚拟数据
def test_forge_command(self):
result = self.runner.invoke(forge)
self.assertIn('Done.', result.output)
self.assertNotEqual(Movie.query.count(), 0)
# 测试初始化数据库
def test_initdb_command(self):
result = self.runner.invoke(initdb)
self.assertIn('Initialized database.', result.output)
# 测试生成管理员账户
def test_admin_command(self):
db.drop_all()
db.create_all()
result = self.runner.invoke(args=['admin', '--username', 'lh', '--password', '930527'])
self.assertIn('Creating user...', result.output)
self.assertIn('Done.', result.output)
self.assertEqual(User.query.count(), 1)
self.assertEqual(User.query.first().username, 'lh')
self.assertTrue(User.query.first().validate_password('930527'))
# 测试更新管理员账户
def test_admin_command_update(self):
# 使用 args 参数给出完整的命令参数列表
result = self.runner.invoke(args=['admin', '--username', 'kyf', '--password', '930527'])
self.assertIn('Updating user...', result.output)
self.assertIn('Done.', result.output)
self.assertEqual(User.query.count(), 1)
self.assertEqual(User.query.first().username, 'kyf')
self.assertTrue(User.query.first().validate_password('930527'))
if __name__ == '__main__':
unittest.main()