-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
284 lines (206 loc) · 9.12 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
"""Unittests for Gastronotes app"""
# python3 -m unittest tests.py
from unittest import TestCase
from server import app
from model import connect_to_db, db, example_data, User, Folder, Recipe
from flask import session
import crud
import os
os.system('dropdb testdb')
os.system('createdb testdb')
class TestCrudFunctions(TestCase):
"""Test crud functions"""
def setUp(self):
"""Run before every test"""
self.client = app.test_client()
app.config['TESTING'] = True
connect_to_db(app, "postgresql:///testdb")
db.create_all()
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_create_user(self):
"""Can we create user instance"""
user1 = crud.create_user("ut_email1@test.test", "1234567", "Dave", "Grohl")
self.assertIsNotNone(user1.user_id)
def test_create_folder(self):
"""Can we create folder instance"""
user2 = crud.create_user("ut_email2@test.test", "1234567", "Matthew", "Bellamy")
folder2 = crud.create_folder(user2, "Drinks")
self.assertIsNotNone(folder2.folder_id)
def test_create_recipe(self):
"""Can we create recipe instance"""
user3 = crud.create_user("ut_email3@test.test", "1234567", "Billy", "Corgan")
folder3 = crud.create_folder(user3, "Pies")
recipe3 = crud.create_recipe(folder3, "Apple Pie", "Apples, Crust, Sugar, Cinnamon", "Mix everything together", "http://google.com", "/static/pie.png")
self.assertIsNotNone(recipe3.recipe_id)
self.assertEqual(recipe3.recipe_title, "Apple Pie")
def test_get_user_by_email(self):
"""Can we find a user by email
(email is a unique field in db)"""
example_data()
user = crud.get_user_by_email('testemail@email.test')
self.assertIsNotNone(user.user_id)
self.assertEqual(user.fname, 'Jane')
self.assertEqual(user.lname, 'Doe')
def test_show_user_folders(self):
"""Can we look up folders for a given user"""
example_data()
folders = crud.show_user_folders(1)
self.assertIsNotNone(folders[0].folder_id)
self.assertEqual(folders[0].folder_title, 'Entrees')
self.assertEqual(folders[1].folder_title, 'Drinks')
def test_show_recipe_by_folder(self):
"""Can we look up recipes for a given folder"""
example_data()
recipes = crud.show_recipe_by_folder(1)
self.assertEqual(recipes[0]['recipe_title'], 'Fried Bananas')
self.assertNotEqual(recipes[0]['folder_id'], 2)
self.assertNotIn(recipes[0]['recipe_title'], 'Marinated Cucumbers')
def test_update_recipes_folder(self):
"""Can we re-assign a recipe to a different folder"""
example_data()
recipe = Recipe.query.get(2)
self.assertEqual(recipe.folder_id, 1)
crud.update_recipes_folder(2, 3)
self.assertEqual(recipe.folder_id, 3)
def test_delete_folder_and_contents(self):
"""Can we delete a given folder"""
example_data()
folder = Folder.query.get(1)
self.assertEqual(folder.folder_title, 'Entrees')
self.assertIsNotNone(folder.folder_id)
crud.delete_folder_and_contents(1)
new_folder = Folder.query.get(1)
self.assertIsNone(new_folder)
def test_delete_recipe(self):
"""Can we delete a given recipe"""
example_data()
recipe = Recipe.query.get(4)
self.assertEqual(recipe.recipe_title, 'Marinated Beets')
self.assertIsNotNone(recipe.recipe_id)
crud.delete_recipe(4)
new_recipe = Recipe.query.get(4)
self.assertIsNone(new_recipe)
def test_search_for_recipe(self):
"""Can we find a recipe by its title||ingredients||directions"""
example_data()
recipes = crud.search_for_recipe(1, "Marinated")
self.assertEqual(recipes[0].recipe_ingred, 'apples, sugar')
self.assertEqual(recipes[0].recipe_id, 2)
# SQL query for quering in psql -- select folders.folder_id, folders.user_id, users.fname, users.lname, folders.folder_title, recipes.recipe_title FROM folders JOIN users on folders.user_id = users.user_id JOIN recipes ON folders.folder_id = recipes.folder_id ORDER BY users.user_id;
class ServerTestBasic(TestCase):
"""Test server functions"""
def setUp(self):
"""Run before every test"""
self.client = app.test_client()
app.config['TESTING'] = True
connect_to_db(app, "postgresql:///testdb")
db.create_all()
example_data()
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_homepage(self):
"""Test homepage page"""
result = self.client.get("/")
self.assertIn(b"Your favorite recipes. Organized.", result.data)
class ServerTestLogInSignUp(TestCase):
"""Test log in and sign up pages"""
def setUp(self):
"""Run before every test"""
self.client = app.test_client()
app.config['TESTING'] = True
connect_to_db(app, "postgresql:///testdb")
db.create_all()
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_login(self):
"""Test login page"""
example_data()
result = self.client.post("/login",
data={"email": "testemail@email.test", "password": "12345678"},
follow_redirects=True)
self.assertIn(b"Successfully logged in as", result.data)
result2 = self.client.post("/login",
data={"email": "testemail@email.test", "password": "87654321"},
follow_redirects=True)
self.assertIn(b"Incorrect password", result2.data)
def test_signup(self):
"""Test signup page"""
result = self.client.post("/signup",
data={"email": "unittest_1@unittest.test", "password": "87654321", "fname" : "Anthony", "lname" :"Kiedis"},
follow_redirects=True)
self.assertIn(b"Account created! Yay!", result.data)
result2 = self.client.post("/signup",
data={"email": "unittest_1@unittest.test", "password": "87654321", "fname" : "Anthony", "lname" :"Kiedis"},
follow_redirects=True)
self.assertIn(b"Account with this email already exists!", result2.data)
class ServerTestsLoggedIn(TestCase):
"""Server tests with user logged in to session."""
def setUp(self):
"""Stuff to do before every test."""
app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'key'
self.client = app.test_client()
connect_to_db(app, "postgresql:///testdb")
db.create_all()
example_data()
with self.client as c:
with c.session_transaction() as sess:
sess['userid'] = 1
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_myrecipes_page(self):
"""Test page with user's recipes"""
result = self.client.get("/myfolders")
self.assertIn(b"NEW FOLDER", result.data)
self.assertNotIn(b"Your favorite recipes. Organized.", result.data)
class ServerTestsLoggedOut(TestCase):
"""Server tests with user logged in to session."""
def setUp(self):
"""Stuff to do before every test."""
app.config['TESTING'] = True
self.client = app.test_client()
def test_myfolders_page(self):
"""Test that user can't see important page when logged out."""
result = self.client.get("/myfolders", follow_redirects=True)
self.assertNotIn(b"NEW FOLDER", result.data)
self.assertIn(b"Collect and save your favorite recipes,", result.data)
class ServerTestsLogInLogOut(TestCase): # Bonus example. Not in lecture.
"""Test log in and log out."""
def setUp(self):
"""Before every test"""
app.config['TESTING'] = True
self.client = app.test_client()
connect_to_db(app, "postgresql:///testdb")
db.create_all()
example_data()
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_login(self):
"""Test log in form."""
with self.client as c:
result = c.post('/login',
data={"email": "testemail@email.test", "password": "12345678"},
follow_redirects=True
)
self.assertEqual(session['userid'], 1)
self.assertIn(b"NEW FOLDER", result.data)
self.assertIn(b"Successfully logged in as", result.data)
def test_logout(self):
"""Test logout route."""
with self.client as c:
with c.session_transaction() as sess:
sess['userid'] = 1
result = self.client.get('/logout', follow_redirects=True)
self.assertNotIn(b'userid', session)
self.assertIn(b'Your favorite recipes. Organized', result.data)