Skip to content

Commit 9ea2e2d

Browse files
committed
Add unit test with unittest
1 parent 5a0bcca commit 9ea2e2d

File tree

5 files changed

+283
-4
lines changed

5 files changed

+283
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ __pycache__
44
.DS_Store
55
.env
66
*.db
7+
.coverage

Pipfile

+2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ verify_ssl = true
55

66
[dev-packages]
77
pylint = "*"
8+
coverage = "*"
89

910
[packages]
1011
flask = "*"
1112
python-dotenv = "*"
1213
flask-sqlalchemy = "*"
1314
flask-login = "*"
15+
click = "*"
1416

1517
[requires]
1618
python_version = "3.7"

Pipfile.lock

+39-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
@Author: alphapenng
33
@Date: 2020-06-04 16:11:13
44
@LastEditors: alphapenng
5-
@LastEditTime: 2020-06-11 09:31:47
5+
@LastEditTime: 2020-06-11 16:51:34
66
@Description:
77
'''
88
import os
@@ -24,6 +24,7 @@
2424
app = Flask(__name__)
2525
app.config['SQLALCHEMY_DATABASE_URI'] = prefix + os.path.join(app.root_path, 'data.db')
2626
app.config['SECRET_KEY'] = 'dev'
27+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
2728
db = SQLAlchemy(app)
2829
login_manager = LoginManager(app)
2930
login_manager.login_view = 'login'
@@ -175,7 +176,7 @@ def edit(movie_id):
175176
movie.title = title
176177
movie.year = year
177178
db.session.commit()
178-
flash('Item Updated.')
179+
flash('Item updated.')
179180
return redirect(url_for('index'))
180181
return render_template('edit.html', movie=movie)
181182

test_watchlist.py

+238
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
'''
2+
@Author: alphapenng
3+
@Date: 2020-06-11 10:02:27
4+
@LastEditors: alphapenng
5+
@LastEditTime: 2020-06-11 16:50:26
6+
@Description:
7+
'''
8+
import unittest
9+
10+
from app import app, db, Movie, User, forge, initdb
11+
12+
class WatchlistTestCase(unittest.TestCase):
13+
def setUp(self):
14+
app.config.update(
15+
TESTING=True,
16+
SQLALCHEMY_DATABASE_URI='sqlite:///:memory:'
17+
)
18+
db.create_all()
19+
user = User(name='Test', username='test')
20+
user.set_password('123')
21+
movie = Movie(title='Test Movie Title', year='2019')
22+
db.session.add_all([user, movie])
23+
db.session.commit()
24+
25+
self.client = app.test_client()
26+
self.runner = app.test_cli_runner()
27+
28+
def tearDown(self):
29+
db.session.remove()
30+
db.drop_all()
31+
32+
def test_app_exist(self):
33+
self.assertIsNotNone(app)
34+
35+
def test_app_is_testing(self):
36+
self.assertTrue(app.config['TESTING'])
37+
38+
def test_404_page(self):
39+
response = self.client.get('/nothing')
40+
data = response.get_data(as_text=True)
41+
self.assertIn('Page Not Found - 404', data)
42+
self.assertIn('Go Back', data)
43+
self.assertEqual(response.status_code, 404)
44+
45+
def test_index_page(self):
46+
response = self.client.get('/')
47+
data = response.get_data(as_text=True)
48+
self.assertIn('Test\'s Watchlist', data)
49+
self.assertIn('Test Movie Title', data)
50+
self.assertEqual(response.status_code, 200)
51+
52+
def login(self):
53+
self.client.post('/login', data=dict(
54+
username='test',
55+
password='123'
56+
), follow_redirects=True)
57+
58+
def test_create_item(self):
59+
self.login()
60+
response = self.client.post('/', data=dict(
61+
title='New Movie',
62+
year='2019'
63+
), follow_redirects=True)
64+
data = response.get_data(as_text=True)
65+
self.assertIn('Item created.', data)
66+
self.assertIn('New Movie', data)
67+
68+
response = self.client.post('/', data=dict(
69+
title='',
70+
year='2019'
71+
), follow_redirects=True)
72+
data = response.get_data(as_text=True)
73+
self.assertNotIn('Item created.', data)
74+
self.assertIn('Invalid input.', data)
75+
76+
response = self.client.post('/', data=dict(
77+
title='New Movie',
78+
year=''
79+
), follow_redirects=True)
80+
data = response.get_data(as_text=True)
81+
self.assertNotIn('Item created.', data)
82+
self.assertIn('Invalid input.', data)
83+
84+
def test_update_item(self):
85+
self.login()
86+
response = self.client.get('/movie/edit/1')
87+
data = response.get_data(as_text=True)
88+
self.assertIn('Edit item', data)
89+
self.assertIn('Test Movie Title', data)
90+
self.assertIn('2019', data)
91+
92+
response = self.client.post('/movie/edit/1', data=dict(
93+
title='New Movie Edited',
94+
year = '2019'
95+
), follow_redirects=True)
96+
data = response.get_data(as_text=True)
97+
self.assertIn('Item updated.', data)
98+
self.assertIn('New Movie Edited', data)
99+
100+
response = self.client.post('/movie/edit/1', data=dict(
101+
title='',
102+
year = '2019'
103+
), follow_redirects=True)
104+
data = response.get_data(as_text=True)
105+
self.assertNotIn('Item updated.', data)
106+
self.assertIn('Invalid input.', data)
107+
108+
response = self.client.post('/movie/edit/1', data=dict(
109+
title='New Movie Edited Again',
110+
year = ''
111+
), follow_redirects=True)
112+
data = response.get_data(as_text=True)
113+
self.assertNotIn('Item updated.', data)
114+
self.assertNotIn('New Movie Edited Again', data)
115+
self.assertIn('Invalid input.', data)
116+
117+
def test_delete_item(self):
118+
self.login()
119+
response = self.client.post('/movie/delete/1', follow_redirects=True)
120+
data = response.get_data(as_text=True)
121+
self.assertIn('Item deleted.', data)
122+
self.assertNotIn('Test Movie Title', data)
123+
124+
def test_login_protect(self):
125+
response = self.client.get('/')
126+
data = response.get_data(as_text=True)
127+
self.assertNotIn('Logout', data)
128+
self.assertNotIn('Settings', data)
129+
self.assertNotIn('<form method="POST">', data)
130+
self.assertNotIn('Delete', data)
131+
# self.assertNotIn('Edit', data)
132+
133+
def test_login(self):
134+
response = self.client.post('/login', data=dict(
135+
username='test',
136+
password='123'
137+
), follow_redirects=True)
138+
data = response.get_data(as_text=True)
139+
self.assertIn('Login success.', data)
140+
self.assertIn('Logout', data)
141+
self.assertIn('Settings', data)
142+
self.assertIn('Delete', data)
143+
self.assertIn('Edit', data)
144+
self.assertIn('<form method="POST">', data)
145+
146+
response = self.client.post('/login', data=dict(
147+
username='test',
148+
password='456'
149+
), follow_redirects=True)
150+
data = response.get_data(as_text=True)
151+
self.assertNotIn('Login success.', data)
152+
self.assertIn('Invalid username or password.', data)
153+
154+
response = self.client.post('/login', data=dict(
155+
username='wrong',
156+
password='123'
157+
), follow_redirects=True)
158+
data = response.get_data(as_text=True)
159+
self.assertNotIn('Login success.', data)
160+
self.assertIn('Invalid username or password.', data)
161+
162+
response = self.client.post('/login', data=dict(
163+
username='',
164+
password='123'
165+
), follow_redirects=True)
166+
data = response.get_data(as_text=True)
167+
self.assertNotIn('Login success.', data)
168+
self.assertIn('Invalid input.', data)
169+
170+
response = self.client.post('/login', data=dict(
171+
username='test',
172+
password=''
173+
), follow_redirects=True)
174+
data = response.get_data(as_text=True)
175+
self.assertNotIn('Login success.', data)
176+
self.assertIn('Invalid input.', data)
177+
178+
def test_logout(self):
179+
self.login()
180+
response = self.client.get('/logout', follow_redirects=True)
181+
data = response.get_data(as_text=True)
182+
self.assertIn('Goodbye.', data)
183+
self.assertNotIn('Logout', data)
184+
self.assertNotIn('Settings', data)
185+
self.assertNotIn('Delete', data)
186+
# self.assertNotIn('Edit', data)
187+
self.assertNotIn('<form method="POST">', data)
188+
189+
def test_settings(self):
190+
self.login()
191+
response = self.client.get('/settings')
192+
data = response.get_data(as_text=True)
193+
self.assertIn('Settings', data)
194+
self.assertIn('Your Name', data)
195+
196+
response = self.client.post('/settings', data=dict(
197+
name='Grey Li',
198+
), follow_redirects=True)
199+
data = response.get_data(as_text=True)
200+
self.assertIn('Settings updated.', data)
201+
self.assertIn('Grey Li', data)
202+
203+
response = self.client.post('/settings', data=dict(
204+
name='',
205+
), follow_redirects=True)
206+
data = response.get_data(as_text=True)
207+
self.assertNotIn('Settings updated.', data)
208+
self.assertIn('Invalid input.', data)
209+
210+
def test_forge_command(self):
211+
result = self.runner.invoke(forge)
212+
self.assertIn('Done.', result.output)
213+
self.assertNotEqual(Movie.query.count(), 0)
214+
215+
def test_initdb_command(self):
216+
result = self.runner.invoke(initdb)
217+
self.assertIn('Initialized database.', result.output)
218+
219+
def test_admin_command(self):
220+
db.drop_all()
221+
db.create_all()
222+
result = self.runner.invoke(args=['admin', '--username', 'grey', '--password', '123'])
223+
self.assertIn('Creating user...', result.output)
224+
self.assertIn('Done.', result.output)
225+
self.assertEqual(User.query.count(), 1)
226+
self.assertEqual(User.query.first().username, 'grey')
227+
self.assertTrue(User.query.first().validate_password('123'))
228+
229+
def test_admin_command_update(self):
230+
result = self.runner.invoke(args=['admin', '--username', 'peter', '--password', '456'])
231+
self.assertIn('Updating user...', result.output)
232+
self.assertIn('Done.', result.output)
233+
self.assertEqual(User.query.count(), 1)
234+
self.assertEqual(User.query.first().username, 'peter')
235+
self.assertTrue(User.query.first().validate_password('456'))
236+
237+
if __name__ == '__main__':
238+
unittest.main()

0 commit comments

Comments
 (0)