/
test.py
281 lines (246 loc) · 9.86 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
""" The tests for the app"""
import unittest
import flask
from app import APP
from app.models.recipe import Recipe
from app.models.category import Category
from app.models.users import User
class BasicTestCase(unittest.TestCase):
"""
These are tests for the url enpoints for the app.
The url endpoints are;
1. / (get)
2. /index (get)
3. /addcategory (get, post)
4. /addrecipe/<categoryid> (get, post)
5. /category/<categoryid> (get)
6. /editcategory/<id> (get, post)
7. /editrecipe/<categoryid>/<recipeid> (get, post)
8. /viewcategory (get)
9. /viewrecipe (get)
10. /deletecategory/<id> (get)
11. /deleterecipe/<categoryid>/<recipeid> (get)
12. /profile (get)
13. /recipe/<categoryid>/<recipeid> (get)
14. /signin (get, post)
15. /signup (get, post)
16. /logout
"""
def setUp(self):
"""
Sets up tests Category object :
Category(categorytitle, categorydescription)
The constructor of the Category class is;
self.categorytitle = categorytitle
self.categorydescription = categorydescription
"""
self.new_user = User('sylvance', 'sylvance@mail',
'I am good', 'Scifi4u*@')
self.categoryid = self.new_user.create_category('Chinese dishes',
'Dishes made in China')
self.sample_category = Category('Kenyan dishes',
'Dishes made in Kenya')
self.recipeid = self.sample_category.add_recipe('not chapati',
'not round or brown or good')
APP.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
def test_index(self):
"""
A test for loading of the landing page
The url endpoint is;
=> / (get)
"""
tester = APP.test_client(self)
response = tester.get('/', content_type='html/text')
self.assertEqual(response.status_code, 200)
def test_home(self):
"""
A test for loading of the home page
The url endpoint is;
=> /index (get)
"""
tester = APP.test_client(self)
response = tester.get('/index', content_type='html/text')
self.assertEqual(response.status_code, 200)
def test_addcategory(self):
"""
A test for loading of the addcategory page
The url endpoint is;
=> /addcategory (get, post)
"""
tester = APP.test_client(self)
response = tester.get('/addcategory', content_type='html/text')
self.assertEqual(response.status_code, 302)
def test_addrecipe(self):
"""
A test for loading of the addrecipe page
The url endpoint is;
=> /addrecipe/<categoryid> (get, post)
"""
with APP.test_request_context('/addrecipe/?categoryid=Kenyan'):
assert flask.request.path == '/addrecipe/'
assert flask.request.args['categoryid'] == 'Kenyan'
def test_category(self):
"""
A test for loading of the category page
The url endpoint is;
=> /category/<categoryid> (get)
"""
with APP.test_request_context('/category/?categoryid=Kenyan'):
assert flask.request.path == '/category/'
assert flask.request.args['categoryid'] == 'Kenyan'
def test_editcategory(self):
"""
A test for loading of the editcategory page
The url endpoint is;
=> /editcategory/<id> (get, post)
"""
with APP.test_request_context('/editcategory/?categoryid=Kenyan'):
assert flask.request.path == '/editcategory/'
assert flask.request.args['categoryid'] == 'Kenyan'
def test_profile(self):
"""
A test for loading of the profile page
The url endpoint is;
=> /profile (get)
"""
tester = APP.test_client(self)
response = tester.get('/profile', content_type='html/text')
self.assertEqual(response.status_code, 302)
def test_signin(self):
"""
A test for loading of the signin page
The url endpoint is;
=> /signin (get)
"""
tester = APP.test_client(self)
response = tester.get('/signin', content_type='html/text')
self.assertEqual(response.status_code, 200)
def test_signup(self):
"""
A test for loading of the signup page
The url endpoint is;
=> /signup (get, post)
"""
tester = APP.test_client(self)
response = tester.get('/signup', content_type='html/text')
self.assertEqual(response.status_code, 200)
def test_logout(self):
"""
A test for loading of the editrecipe page
The url endpoint is;
=> /logout (get)
"""
r = APP.test_client(self)
return r.get('/logout', follow_redirects=True)
def test_viewcategory(self):
"""
A test for loading of the editrecipe page
The url endpoint is;
=> /viewcategory (get)
"""
tester = APP.test_client(self)
response = tester.get('/viewcategory', content_type='html/text', follow_redirects=True)
self.assertEqual(response.status_code, 200)
def test_viewrecipe(self):
"""
A test for loading of the editrecipe page
The url endpoint is;
=> /viewrecipe (get)
"""
tester = APP.test_client(self)
response = tester.get('/viewrecipe', content_type='html/text', follow_redirects=True)
self.assertEqual(response.status_code, 200)
class TestCategory(unittest.TestCase):
"""
These are tests for Category methods.
The Category methods are;
1. Add recipe
2. Edit recipe
3. Delete recipe
"""
def setUp(self):
"""
Sets up tests Category object :
Category(categorytitle, categorydescription)
The constructor of the Category class is;
self.categorytitle = categorytitle
self.categorydescription = categorydescription
"""
self.sample_category = Category('Kenyan dishes',
'Dishes made in Kenya')
def test_add_recipe(self):
""" Test whether a recipe has been saved """
before_add = len(self.sample_category.recipes)
self.sample_category.add_recipe('chapati',
'round brown and good')
after_add = len(self.sample_category.recipes)
self.assertEqual(after_add - before_add, 1)
def test_edit_recipe(self):
""" Test whether an recipe has been edited """
id = self.sample_category.add_recipe('not chapati',
'not round or brown or good')
self.sample_category.edit_recipe(id,
'chapati',
'round brown and good')
recipe = self.sample_category.recipes[id]
self.assertEqual(
recipe.recipetitle,
'chapati')
def test_delete_recipe(self):
"""Test whether a recipe has been deleted"""
id = self.sample_category.add_recipe('not chapati',
'not round or brown or good')
size_before = len(self.sample_category.recipes)
self.sample_category.delete_recipe(id)
size_after = len(self.sample_category.recipes)
self.assertTrue(size_before - size_after, 1)
class TestUser(unittest.TestCase):
"""
These are tests for User methods.
The User methods are (what a user is able to do);
1. Add category
2. Edit category
3. Delete category
"""
def setUp(self):
"""
Sets up tests User object :
User(username, email, bio, password)
The constructor of the Category class is;
self.username = username
self.email = email
self.bio = bio
self.password = password
"""
self.new_user = User('sylvance', 'sylvance@mail',
'I am good', 'Scifi4u*@')
def test_create_category(self):
""" Test whether category has been created """
result = len(self.new_user.categories)
self.new_user.create_category('Kenyan dishes',
'Dishes made in Kenya')
after_add = len(self.new_user.categories)
self.assertEqual(after_add - result, 1)
def test_update_category(self):
""" Test whether category has been edited"""
id = self.new_user.create_category('Chinese dishes',
'Dishes made in China')
self.new_user.update_category(id,
'Chinese dishes reloaded',
'Made in China')
category = self.new_user.categories[id]
self.assertEqual(category.categorytitle,
'Chinese dishes reloaded'
)
self.assertEqual(category.categorydescription,
'Made in China')
def test_delete_category(self):
""" Test whether category has been deleted """
id = self.new_user.create_category('Indian dishes',
'Dishes made in India')
result = len(self.new_user.categories)
self.new_user.delete_category(id)
after_delete = len(self.new_user.categories)
self.assertTrue(result, after_delete)
if __name__ == '__main__':
unittest.main()