-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
365 lines (241 loc) · 12.4 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
##################
from flask import Flask
app = Flask(__name__)
@app.route("/")
def homepage():
return 'Yes, I am here.'
#########################
############
test.py
from server import homepage, app
import unittest
# Write a bunch of unit test that test all the cases and just an integration test
# that test only the integration Selenium test
class MyFlaskUnitTests(unittest.TestCase): ###TEST UNIT
def test_homepage(self):
result = homepage()
self.assertIn("here", result)
if __name__ == '__main__':
unittest.main()
class MyFlaskIntegrationTests(unittest.TestCase): ###TEST INTEGRATION
def setUp(self):
self.client = app.test_client()
def test_homepage(self):
self.client = app.test_client()
result = self.client.get("/")
print dir(result) #### status_code, content_type=HTML, data
self.asserIn("here", result.data)
self.assertEqual(result.status_code,200)
# Testing DataBase
# We beed to connect to db
import unittest
from model import db, connect_to_db
from server import app
from flask import session, request
class My RatingDBTest(unittest.TestCase):
def setUp(self):# it runs before each test
print "connection to db"
connect_to_db(app, 'sqlite://test.db')
db.create_all()
self.client = app.test_client()
_add_movie()
def tearDown(self):# It runs after each test
print 'doing my teardown'
Movie.query.filter_by(title="Mad Max").delete()
db.session.commit()
def test_have_movie(self):
movies = Movies.query.all()
self.assertTrue(len(movies) > 100)
def test_some_route(self):
self.client.get('/movie/mad-max')
def test_login():
self.client.post("/login",{"user":"test", "password":""})
# with create_test_session() as session:
result = self.client.get("/")
self.asserIn("Welcome", result.data)
def _add_movie(self):
fury_road = Movie(title="Mad Max")
db.session.add(fury_road)
mm = Movie.query.filter_by(title = "Mad Max").
self.assertEqual()
import
self.assertEqual(classmethod(), return)
# Contest manager
with open("/tmp/hello.txt") as f:
print f.read()
print ""
def test_load_login(self):
"""Tests to see if the login function works."""
result = self.client.post('/login_confirm',
{"user":"[email protected]","password":"semagna"})
self.assertEqual(result.status_code, 200)
result = self.client.get("/")
self.asserIn("Sign Out", result.data)
def test_load_about(self):
"""Tests to see if the about page comes up."""
result = self.client.get('/about')
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Parktake was inspired by a love of adventure.', result.data)
def test_load_logout(self):
"""Tests to see if logout occurs properly."""
result = self.client.get('/logout')
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('<a id="nav-login" href="/login">Log In</a>', result.data)
#############################################################################
# Test any functions that will query data.
def test_process_signup_new_user(self):
"""Test to see if the signup form will process new user properly."""
result = self.client.post('/process-signup',
data={'first_name': "Jane",
'last_name': "Smith",
'zipcode': "94306",
'email': "[email protected]",
'password': 'password'},
follow_redirects=True)
self.assertIn('<a href="/view-park" class="view-parks">Your Parks</a>', result.data)
self.assertNotIn('<a id="nav-login" href="/login">Log In</a>', result.data)
def test_process_signup_known_user(self):
"""Test to see if the signup form will process a user currently in the database properly."""
result = self.client.post('/process-signup',
data={'first_name': "Jane",
'last_name': "Smith",
'zipcode': "94306",
'email': "[email protected]",
'password': 'password'},
follow_redirects=True)
self.assertIn('/login', result.data)
self.assertNotIn('Welcome, ', result.data)
self.assertIn('You already have an account. Please login', result.data)
def test_process_login_known(self):
"""Test to see if the login form will process properly with a known user."""
result = self.client.post("/process-login",
data={"email": '[email protected]', 'password': 'brindlepuppy'},
follow_redirects=True)
self.assertIn('Welcome back, Lucy!', result.data)
self.assertNotIn('Log In', result.data)
self.assertNotIn('Please enter a valid email or password.', result.data)
def test_process_login_unknown(self):
"""Test to see if the login form will process properly with an unknown user."""
result = self.client.post("/process-login",
data={"email": '[email protected]', 'password': 'acky'},
follow_redirects=True)
self.assertNotIn('Welcome back,', result.data)
self.assertIn('Log In', result.data)
self.assertIn('Please enter a valid email or password.', result.data)
def test_process_login_bad_pwd(self):
"""Test to see if the login form will process properly with a known user and wrong password."""
result = self.client.post("/process-login",
data={"email": '[email protected]', 'password': 'WRONG'},
follow_redirects=True)
self.assertNotIn('Welcome back,', result.data)
self.assertIn('Log In', result.data)
self.assertIn('That email and password combination does not exist.', result.data)
def test_show_account_not_logged_in(self):
"""Test to see if account page will show up if a user isn't logged in."""
result = self.client.get("/account")
self.assertNotIn('Your Account', result.data)
self.assertIn('/login', result.data)
def test_save_account_not_logged_in(self):
"""Test to see if save account page will show up if a user is not logged in."""
result = self.client.post('/save-changes', data={'first_name': 'Lucy',
'last_name': 'Vo',
'email': '[email protected]',
'password': 'squirrel',
'zipcode': '94306'},
follow_redirects=True)
self.assertNotIn('Your account has been updated.', result.data)
self.assertNotIn('Lucy', result.data)
self.assertIn('/login', result.data)
#############################################################################
# Test any functions to see if they're an instance of a built-in class
def test_get_parks(self):
"""Test to see if this function returns a dictionary."""
visited_parks = db.session.query(Rec_Area).join(Visited_Park).filter(Visited_Park.user_id == 2).all()
self.assertIsInstance(get_parks(visited_parks), dict)
class ParkTestsSession(unittest.TestCase):
"""Testa for Parktake app for functions that don't require sessions."""
def setUp(self):
# set up fake test browser
self.client = app.test_client()
# connect to temporary database
connect_to_db(app, "sqlite:///")
# This line makes a 500 error in a route raise an error in a test
app.config['TESTING'] = True
# create tables and add sample data
db.create_all()
example_data_rec_areas()
example_data_users()
example_data_visits()
# initiate a session
with self.client as c:
with c.session_transaction() as sess:
sess['user'] = '2'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
#############################################################################
# Test any functions that only render a template.
def test_load_landing(self):
"""Tests to see if the landing page comes up."""
result = self.client.get('/landing', data={'mapkey': mapkey}, follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Your Parks', result.data)
#############################################################################
# Test any functions that will query data.
def test_process_login_already_logged(self):
"""Test to see if the login form will show if user is already logged in."""
result = self.client.get('/login')
self.assertIn('You are already logged in', result.data)
def test_show_account_logged_in(self):
"""Test to see if account page will show up if a user is logged in."""
result = self.client.get('/account')
self.assertIn('Your Account', result.data)
self.assertIn('Lucy', result.data)
self.assertNotIn('/login', result.data)
def test_update_account_logged_in(self):
"""Test to see if update account page will show up if a user is logged in."""
result = self.client.get('/update-account')
self.assertIn('Update Your Account', result.data)
self.assertIn('Lucy', result.data)
self.assertNotIn('/login', result.data)
def test_save_account_logged_in(self):
"""Test to see if save account page will show up if a user is logged in."""
result = self.client.post('/save-changes', data={'first_name': 'Lucy',
'last_name': 'Vo',
'email': '[email protected]',
'password': 'squirrel',
'zipcode': '94306'},
follow_redirects=True)
self.assertIn('Your account has been updated.', result.data)
self.assertIn('Lucy', result.data)
self.assertNotIn('/login', result.data)
def test_view_park(self):
"""Test to see if user can view their parks if a user is logged in."""
result = self.client.get('/view-park')
self.assertIn('Based on where you\'ve been,', result.data)
def test_add_park(self):
"""Test to see if a park will add properly."""
result = self.client.post('/add-park', data={'park-id': '2941'},
follow_redirects=True)
self.assertIn('Park Added', result.data)
#############################################################################
# Test any functions to see if they're an instance of a built-in class
def test_parks_json(self):
"""Test to see if /parks.json route will return json object."""
response = self.client.get("/parks.json")
self.assertIsInstance(response, object)
def test_visited_parks_json(self):
"""Test to see if /parks-visited.json route will return json object."""
response = self.client.get("/parks-visited.json")
self.assertIsInstance(response, object)
def test_get_chart_data(self):
"""Test to see if /parks-visited.json route will return json object."""
response = self.client.get("/parks-in-states.json")
self.assertIsInstance(response, object)
#############################################################################
# Test suggestion feature
def test_suggestion_feature(self):
"""Test to see if the proper park suggestion is offered."""
response = self.client.get("/suggest-park")
self.assertIn('Zion National Park', response.data)