-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_app.py
212 lines (144 loc) · 7 KB
/
test_app.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
import os
import unittest
import doctest
from server import app, index
from flask import session, request
from model import (Recipe, User, Ingredient, RecipeStep, Category, RecipeUser,
Expence, ShoppingList, RecipeIngredient, Meals, connect_to_db, db)
# -*- coding: utf-8 -*-
############################ DOCTESTS ##########################
def load_tests(loader, tests, ignore):
""" It runs the doctest"""
# tests.addTests(doctest.DocTestSuite(server))
# tests.addTests(doctest.DocFileSuite("tests.txt"))
return tests
class MealPlannerTests(unittest.TestCase):
############################ UNIT TESTS ##########################
"""Test for functions that don't require sessions."""
def setUp(self):
# set up fake test browser
self.client = app.test_client()
# connect to a test database
connect_to_db(app, "sqlite:///test.db")
# This line makes a 500 error in a route raise an error in a test
app.config['TESTING'] = True
app.secret_key = "ABC"
# coding: utf-8
# create tables and add sample data
# db.create_all()
######################## INTEGRATION TESTS #############################
# LOGIN/LOGOUT
def test_load_homepage(self):
"""Tests to see if the homepage comes up"""
result = self.client.get('/')
# print dir(result) to see what methods are available for result
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Store Recipes', result.data)
def test_signup_old_user(self):
"""Tests signup function when the user has already an account"""
result = self.client.post('/register-confirm',
data={"email":"[email protected]","password":"semagna"})
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('You already have an account', result.data)
def test_signup_new_user(self):
"""Tests signup function when the user is new"""
result = self.client.post('/register-confirm',
data={"email":"[email protected]","password":"muntobe", "name":"Sofia"})
self.assertEqual(result.status_code, 302)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Redirecting', result.data)
def test_signin_old_user(self):
"""Tests to see if the signup function works."""
result = self.client.post('/login_confirm',
data={"email":"[email protected]","password":"semagna"})
self.assertEqual(result.status_code, 302)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Redirecting', result.data)
def test_log_out(self):
""" Test the log out function"""
with self.client as c:
with c.session_transaction() as sess:
sess['User'] = '1'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
# sess['_fresh'] = True
result = self.client.get('/logout')
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Log In', result.data)
# RECIPE
def test_search_recipe_load_by_user(self):
"""Tests search recipe list"""
with self.client as c:
with c.session_transaction() as sess:
sess['User'] = '1'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
# sess['_fresh'] = True
result = self.client.get('/recipes')
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Cuisine', result.data)
# def test_search_recipe_data(self):
# """ Tests search recipe list by filters"""
# with self.client as c:
# resp = c.post('/changeFilters.json',
# data={"title":"Roasted Veggie Tarts","cuisine":"",
# "level":"","cat":"","source":""})
# data = json.loads(resp.data)
# self.assert_equal(data['titles'], 'Roasted Veggie Tarts')
# IMPORT RECIPE FORM
def test_import_recipe_form(self):
""" Tests import form """
result = self.client.get('/addRecipesForm')
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('New York Times', result.data)
def test_scrape_old_recipe(self):
""" Test the scraping function when recipe is already in DB """
Recipe.deleteRecipeById(recipe_id=39)
db.session.commit()
result = self.client.post('/importRecipe',
data={"url":"http://cooking.nytimes.com/recipes/1015348-spicy-red-pepper-cranberry-relish"},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Error: This recipe has already been loaded', result.data)
# def test_scrape_new_recipe(self):
# """ Test the scraping function when recipe is not in DB """
# result = self.client.post('/importRecipe',
# data={"url":"http://cooking.nytimes.com/recipes/1017780-whole-roasted-stuffed-delicata-squash"},
# follow_redirects=True)
# self.assertEqual(result.status_code, 200)
# self.assertIn('text/html', result.headers['Content-Type'])
# self.assertIn('Whole-Roasted', result.data)
# RECIPE PAGE
def test_recipe_page(self):
""" Tests if recipe page comes up """
result = self.client.get('/recipe_page/28')
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Preparation', result.data)
# def test_delete_recipe(self):
# """ Test recipe delete function """
# result = self.client.get('/deleteRecipe/39', follow_redirects=True)
# self.assertEqual(result.status_code, 200)
# self.assertIn('text/html', result.headers['Content-Type'])
# self.assertNotIn('Whole-Roasted Stuffed Delicata Squash', result.data)
def tearDown(self):# It runs after each test
print 'doing my teardown'
User.query.filter_by(user_id=2).delete()
Recipe.deleteRecipeById(recipe_id=39)
db.session.commit()
# MEAL PLANNER
def test_planner_page(self):
""" Tests if planner comes up """
with self.client as c:
with c.session_transaction() as sess:
sess['User'] = '1'
result = self.client.get('/planner')
self.assertEqual(result.status_code, 200)
self.assertIn('text/html', result.headers['Content-Type'])
self.assertIn('Breakfast', result.data)
if __name__ == "__main__":
unittest.main()