-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
362 lines (254 loc) · 12.3 KB
/
tests.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
import json
import unittest
from model import connect_to_db, db, User, Interaction, Contact, Note, example_data
import server
import helper
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime
class FlaskTestsDatabase(unittest.TestCase):
"""Flask tests that use the database."""
def setUp(self):
"""Stuff to do before every test."""
# Get the Flask test client
self.client = server.app.test_client()
server.app.config['TESTING'] = True
server.app.secret_key = "ABC"
# Connect to test database
connect_to_db(server.app, "postgresql:///testdb")
# Create tables and add sample data
db.create_all()
# inputs sample data for testing from model.py
example_data()
# establish a client session for use in tests
with self.client as c:
with c.session_transaction() as sess:
sess['logged_in_user_id'] = 1
sess['logged_in_email'] = 'email@domain.com'
sess['logged_in_user_name'] = 'First'
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
################################################################################
# Tests for routes to handle RENDERING PAGES
def test_index(self):
"""Test homepage page."""
result = self.client.get("/")
self.assertIn("Streamline your social interactions", result.data)
def test_show_dashboard(self):
"""Test dashboard page."""
result = self.client.get("/dashboard/1")
self.assertIn("First's Profile", result.data)
################################################################################
# Tests for routes to handle LOGIN and LOGOUT
def test_login(self):
"""Test login modal."""
# a test for the case of correct email/password
result = self.client.post("/login",
data={"email": "email@domain.com",
"password": "password"},
follow_redirects=True)
self.assertIn("First's Profile", result.data)
# a test for the case of correct email, wrong password
result = self.client.post("/login",
data={"email": "email@domain.com",
"password": "wrong"},
follow_redirects=True)
self.assertIn("Incorrect credentials", result.data)
# a test for the case of no user registered
result = self.client.post("/login",
data={"email": "wrong_email@domain.com",
"password": "password"},
follow_redirects=True)
self.assertIn("No user registered with that email", result.data)
def test_register(self):
"""Tests the registration route"""
result = self.client.post("/register", data={
'first-name': 'Joe',
'last-name': 'Schmoe',
'email': 'email@place.com',
'zipcode': '12345',
'password': 'notsafe'
}, follow_redirects=True)
self.assertIn("Joe's Profile", result.data)
def test_logout(self):
"""Tests the logout route"""
result = self.client.get('/logout', follow_redirects=True)
self.assertIn("Streamline your social interactions", result.data)
def test_get_user(self):
"""Test the get_user() route"""
result = self.client.get('/getUser.json')
self.assertIn("email@domain.com", result.data)
def test_edit_profile(self):
"""Tests the edit_profile route"""
result = self.client.post('/editProfile', data={
'first-name': 'First',
'last-name': 'Last',
'email': 'email@place.com',
'zipcode': '12345',
'password': 'notsafe'
}, follow_redirects=True)
self.assertIn("First's Profile", result.data)
################################################################################
# Tests for routes to handle adding/deleting/editing CONTACTS
def test_add_contact(self):
"""Tests the add contact route"""
result = self.client.post('/addContact', data={'first-name': 'First_3'},
follow_redirects=True)
self.assertIn("First's Profile", result.data)
def test_get_contact(self):
"""Tests the get_contact route"""
result = self.client.post('/getContact.json', data={'id': 1},
follow_redirects=True)
self.assertIn("This is a test note", result.data)
def test_edit_contact(self):
"""Tests the add contact route"""
result = self.client.post('/editContact', data={
'contact-id': 1,
'first-name': 'First_1'},
follow_redirects=True)
self.assertIn("First's Profile", result.data)
def test_delete_contact(self):
"""Tests the delete contact route"""
result = self.client.post('/deleteContact', data={
'contact-id': 1}, follow_redirects=True)
self.assertIn("First's Profile", result.data)
################################################################################
# Tests for routes to handle adding/deleting/editing and INTERACTIONS
def test_add_interaction(self):
"""Tests the add_interaction route"""
result = self.client.post('/addInteraction',
data={
'contact-id': 1,
'frienergy': 3,
'date': datetime.date(2016, 4, 5),
'notes': "This is a note",
}, follow_redirects=True)
self.assertIn("First's Profile", result.data)
def test_get_interaction(self):
"""Tests the get_interaction route"""
# tests the case of a note attached to the interaction
result = self.client.post('/getInteraction.json', data={'id': 1})
self.assertIn("This is a test note", result.data)
# tests the case of a note not attached to the itneraction
result = self.client.post('/getInteraction.json', data={'id': 2})
self.assertNotIn("This is a test note", result.data)
def test_edit_interaction(self):
"""Tests the edit_interaction route"""
# tests the case of a note pre-existing and being updated
result = self.client.post('/editInteraction',
data= {
'interaction-id': 3,
'date': datetime.date(2016, 5, 11),
'frienergy': 9,
'notes': "Updated note",
'note-id': 1,
}, follow_redirects=True)
self.assertIn("First's Profile", result.data)
# tests the case of a new note being added to the interaction
result = self.client.post('/editInteraction',
data= {
'interaction-id': 2,
'date': datetime.date(2016, 5, 14),
'frienergy': 9,
'notes': "New note",
}, follow_redirects=True)
self.assertIn("First's Profile", result.data)
def test_delete_interaction(self):
"""Tests the delete_interaction route"""
result = self.client.post('/deleteInteraction',
data={'interaction-id': 1}, follow_redirects=True)
self.assertIn("First's Profile", result.data)
################################################################################
# Routes to handle NOTES
def test_get_note(self):
"""Tests the get_note route"""
result = self.client.get('/getNote.json?id=1')
self.assertEqual(result.status_code, 200)
################################################################################
# Tests for routes to get and post REMINDERS
def test_calculate_reminders(self):
"""Tests the calculate_reminders route"""
result = self.client.post('/getReminders.json')
self.assertEqual(result.status_code, 200)
self.assertIn("First_1", result.data)
################################################################################
# Tests for HELPER FUNCTIONS
def test_sort_interactions_by_date(self):
"""Tests the sort interactions by date helper function"""
result = helper.sort_user_interactions_by_date(1)
self.assertEqual(len(result), 3)
def test_get_and_sort_contacts_by_power(self):
"""Tests the get_and_sort_contacts_by_power helper function"""
result = helper.get_and_sort_contacts_by_power(1)
self.assertEqual(len(result), 2)
def test_calculate_t_delta_since_last_int(self):
"""Tests the calculate_t_delta_since_last_int helper function"""
helper.calculate_t_delta_since_last_int(1)
int_list = db.session.query(Interaction).filter(Interaction.contact_id == 1).all()
self.assertEqual(int_list[2].t_delta_since_last_int, 3)
def test_update_t_since_last_int(self):
"""Tests the update_t_since_last_int helper function"""
result = helper.update_t_since_last_int(1)
self.assertNotEqual(Contact.query.get(1).t_since_last_int, -1)
def test_update_total_frienergy(self):
"""Tests the update_total_frienergy helper function"""
result = helper.update_total_frienergy(1)
self.assertEqual(Contact.query.get(1).total_frienergy, 24)
def test_update_avg_t_btwn_ints(self):
"""Tests the update_avg_t_btwn_ints helper function"""
# tests the case where the contact has multiple interactions
helper.update_avg_t_btwn_ints(1)
self.assertEqual(Contact.query.get(1).avg_t_btwn_ints, 3)
# tests the case where the contact has no interactions
helper.update_avg_t_btwn_ints(2)
self.assertEqual(Contact.query.get(2).avg_t_btwn_ints, 0)
def test_calculate_power(self):
"""Tests the calculate_power helper function"""
self.assertEqual(helper.calculate_power(1), 8.0)
# class FrontEndTests(unittest.TestCase):
# """Tests the JavaScript front end AJAX/JSON functionality"""
# def setUp(self):
# """Stuff to do before every test."""
# # Get the Flask test client
# self.client = server.app.test_client()
# server.app.config['TESTING'] = True
# server.app.secret_key = "ABC"
# # Connect to test database
# connect_to_db(server.app, "postgresql:///testdb")
# # Create tables and add sample data
# db.create_all()
# # inputs sample data for testing from model.py
# example_data()
# # establish a client session for use in tests
# with self.client as c:
# with c.session_transaction() as sess:
# sess['logged_in_user_id'] = 1
# sess['logged_in_email'] = 'email@domain.com'
# sess['logged_in_user_name'] = 'First'
# # creates a client in which to test front end functionality
# self.browser = webdriver.Firefox()
# # loads the dashboard for the fake user
# self.browser.get('http://localhost:5000/dashboard/1')
# def tearDown(self):
# """Do at end of every test."""
# db.session.close()
# db.drop_all()
# self.browser.quit()
# def test_add_interaction_modal_ajax(self):
# """Tests that the add interaction modal appears with correct info"""
# WebDriverWait(self.browser, 10).until(
# EC.presence_of_element_located((By.CSS_SELECTOR, "#relationshipModal"))
# )
# self.browser.find_element_by_css_selector('[data-contact-id="1"]').click()
# WebDriverWait(self.browser, 10).until(
# EC.text_to_be_present_in_element((By.CSS_SELECTOR, "#contact-name-for-form"), "First_1")
# )
# element = self.browser.find_element(By.CSS_SELECTOR, "#contact-name-for-form")
# self.assertEqual(element.text, "First_1")
if __name__ == "__main__":
import unittest
unittest.main()