/
camper_tests.py
413 lines (323 loc) · 16.7 KB
/
camper_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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from unittest import TestCase
import os
from mock import patch
from datetime import datetime
from server import app
from model import connect_to_db, db
from seed import load_regions, load_users, load_bestuses, load_categories
from seed import load_brands, load_products, load_tents, load_filltypes
from seed import load_gendertypes, load_sleepingbags, load_padtypes
from seed import load_sleepingpads, load_ratings, load_histories, load_test_postalcodes
from seed import clear_data
from model import User, Brand, Product, Tent, SleepingBag, Category, Rating
from model import SleepingPad, PostalCode
from make_update_helpers import check_brand, make_brand, get_brand_id
from make_update_helpers import make_postalcode
from search_helpers import get_users_in_area, filter_products, convert_string_to_datetime
from search_helpers import get_products_within_dates, categorize_products, calc_Haversine_distance
from make_update_helpers import calc_avg_star_rating
from search_helpers import search_radius, calc_default_dates
class IntegrationTestCase(TestCase):
def setUp(self):
print "\n\n\n\n (1) DOING AN INTEGRATION TEST \n\n\n\n"
self.client = app.test_client()
app.config['TESTING'] = True
postgrespassword = os.environ['POSTGRES_PASSWORD']
db_uri = 'postgresql://' + postgrespassword + '/test'
connect_to_db(app, db_uri)
db.create_all()
load_regions()
load_users()
load_bestuses()
load_categories()
load_brands()
load_products()
load_tents()
load_filltypes()
load_gendertypes()
load_sleepingbags()
load_padtypes()
load_sleepingpads()
load_ratings()
load_histories()
load_test_postalcodes()
def tearDown(self):
db.session.close_all()
clear_data()
# meta = db.metadata
# for table in reversed(meta.sorted_tables):
# if table.name == 'postalcodes':
# continue
# print '\n**Dropping table %s**\n' % table
# db.engine.execute('DROP TABLE ' + table.name + ' CASCADE')
# db.session.commit()
# # can put in here the python (os) to remove a file r
# db.drop_all()
# db.session.remove()
# postgrespassword = os.environ['POSTGRES_PASSWORD']
# db_uri = 'postgresql://' + postgrespassword + '/test'
# os.remove(db_uri)
def test_homepage(self):
result = self.client.get('/')
self.assertEqual(result.status_code, 200)
self.assertIn('Create one here', result.data)
def test_handle_successful_login(self):
result = self.client.post('/handle-login', data={'email': 'phar@fignewton.com',
'password': 'abc'}, follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Find Stuff', result.data)
def test_handle_failed_login(self):
result = self.client.post('/handle-login', data={'email': 'the@godpigeon.com',
'password': 'abc'}, follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Create one here', result.data)
def test_logout(self):
result = self.client.get('/logout', follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Don\'t have an account yet?', result.data)
def test_create_new_user(self):
user1 = User(fname='Michelle', lname='Tanner', street='1709 Broderick Street',
city='San Francisco', region_id=1, postalcode='94115',
phone=4155556666, email='michelle@tanner.com', password='123')
user2 = User(fname='Grumpy', lname='Grandpa', street='54 Elizabeth Street #31',
city='New York', region_id=2, postalcode='10013',
phone=2125556666, email='grumpy@grandpa.com', password='123')
self.assertEqual(user1.email, 'michelle@tanner.com')
self.assertEqual(user2.email, 'grumpy@grandpa.com')
self.assertEqual(user1.region_id, 1)
self.assertEqual(user2.region_id, 2)
def test_handle_successful_create_account(self):
result = self.client.post('handle-create-account', data={'pword': '123',
'confirm_pword': '123', 'firstname': 'The',
'lastname': 'Brain', 'staddress': '2525 Blueberry Rd #106',
'cty': 'Anchorage', 'state': 'AK',
'zipcode': '99503', 'phonenumber': '9075630220',
'username': 'the@brain.com'}, follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Find Stuff', result.data)
user = User.query.filter(User.email == 'the@brain.com').one()
self.assertEqual(user.street, '2525 Blueberry Rd #106')
postalcode = PostalCode.query.get(99503)
self.assertEqual(int(postalcode.longitude), -149)
def test_handle_failed_create_account(self):
result = self.client.post('handle-create-account', data={'pword': '123',
'confirm_pword': 'abc'}, follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('<h3>Contact Info</h3>', result.data)
def test_show_account_info(self):
with self.client as c:
with c.session_transaction() as sess:
sess['user'] = 'franken@berry.com'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
result = self.client.get('/account-info')
self.assertEqual(result.status_code, 200)
def test_handle_tent_listing(self):
with self.client as c:
with c.session_transaction() as sess:
sess['user'] = 'phar@fignewton.com'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
result = c.post('/handle-listing/1', data={'category_id': 1,
'brand_id': 3, 'modelname': 'Kaiju 6',
'desc': 'Guaranteeing campground fun for the family, blah blah',
'cond': 'Excellente', 'avail_start': '2015-11-20',
'avail_end': '2015-12-31', 'pricing': 4.5, 'image': None,
'user': User.query.get(1), 'bestuse': 2, 'sleep': 6,
'seasoncat': 3, 'weight': 200, 'length': 80, 'width': 25,
'doors': 3, 'poles': 3}, follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Listing successfully posted!', result.data)
product = Product.query.filter(Product.model == 'Kaiju 6').one()
self.assertEqual(product.model, 'Kaiju 6')
tent = Tent.query.get(product.prod_id)
self.assertEqual(tent.sleep_capacity, 6)
def test_handle_sleepingbag_listing(self):
with self.client as c:
with c.session_transaction() as sess:
sess['user'] = 'count@chocula.com'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
result = c.post('/handle-listing/2', data={'category_id': 2,
'brand_id': 2, 'modelname': 'Arrow Rock 15',
'desc': 'By the time you clean up dinner and organize, blah blah',
'cond': 'Lost some feathers', 'avail_start': '2016-03-01',
'avail_end': '2016-03-31', 'pricing': 3.0, 'image': None,
'user': User.query.get(2), 'filltype': 'D', 'temp': 15,
'bag_weight': 45, 'length': 43, 'gender': 'U'},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Listing successfully posted!', result.data)
product = Product.query.filter(Product.model == 'Arrow Rock 15').one()
self.assertEqual(product.brand_id, 2)
sleepingbag = SleepingBag.query.get(product.prod_id)
self.assertEqual(sleepingbag.fill_code, 'D')
def test_handle_sleepingpad_listing(self):
with self.client as c:
with c.session_transaction() as sess:
sess['user'] = 'trix@rabbit.com'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
result = c.post('/handle-listing/3', data={'category_id': 3,
'brand_id': -1, 'new_brand_name': 'Exped', 'modelname': 'Mega Mat 10',
'desc': 'mega big and mega warm',
'cond': 'mega good', 'avail_start': '2017-03-01',
'avail_end': '2017-06-15', 'pricing': 2.50, 'image': None,
'user': User.query.get(3), 'padtype': 'F', 'bestuse': 2,
'r_val': 9.5, 'pad_weight': 38, 'pad_length': '78'},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Listing successfully posted!', result.data)
product = Product.query.filter(Product.model == 'Mega Mat 10').one()
self.assertEqual(product.brand.brand_name, 'Exped')
sleepingpad = SleepingPad.query.get(product.prod_id)
self.assertEqual(sleepingpad.type_code, 'F')
def test_submit_renter_rating(self):
result = self.client.post('handle-user-rating', data={'hist_id': 7,
'num_stars': 3, 'is_owner': 0, 'comments': 'abcdefg'},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('History id=7', result.data)
def test_submit_owner_rating(self):
result = self.client.post('handle-user-rating', data={'hist_id': 7,
'num_stars': 3, 'is_owner': 1, 'comments': 'abcdefg'},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('History id=7', result.data)
def test_submit_product_rating(self):
result = self.client.post('handle-product-rating', data={'hist_id': 7,
'num_stars': 3, 'comments': 'abcdefg'},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('history_id=7', result.data)
def test_delist_product(self):
with self.client as c:
with c.session_transaction() as sess:
sess['user'] = 'franken@berry.com'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
result = self.client.post('handle-delist-product', data={'prod_id': 1},
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('<li>This product has been delisted.</li>', result.data)
self.assertEqual(Product.query.get(1).available, False)
def test_deactivate_account(self):
with self.client as c:
with c.session_transaction() as sess:
sess['user'] = 'spuds@mackenzie.com'
c.set_cookie('localhost', 'MYCOOKIE', 'cookie_value')
result = self.client.post('handle-deactivate-account',
follow_redirects=True)
self.assertEqual(result.status_code, 200)
self.assertIn('Your account has been deactivated. Thank you for using Happy Camper!',
result.data)
self.assertEqual(User.query.get(5).active, False)
def test_find_users(self):
franken = User.query.filter(User.fname == 'Franken').one()
self.assertEqual(franken.fname, 'Franken')
self.assertEqual(franken.email, 'franken@berry.com')
def test_find_product(self):
product = Product.query.filter(Product.model == 'Sugar Shack 2').one()
self.assertEqual(product.condition, 'Good. Used twice.')
tent = Tent.query.get(product.prod_id)
self.assertEqual(tent.sleep_capacity, 2)
def test_get_users_in_area(self):
users_in_area = get_users_in_area(['94612'], 1)
users_names = []
for user in users_in_area:
users_names.append(user.fname)
self.assertEqual(sorted(users_names), ['Count', 'Trix'])
def test_filter_products(self):
filtered_products = filter_products(Product.query.all(), 1, 1)
self.assertEqual(filtered_products[0].model, 'Passage 2')
def test_check_brand(self):
self.assertEqual(check_brand(3), 3)
def test_make_brand(self):
make_brand("ABC")
self.assertEqual(Brand.query.filter(Brand.brand_name == "ABC").one().brand_name, "ABC")
# brand = Brand.query.filter(Brand.brand_name == "ABC").one()
def test_get_brand_id(self):
self.assertEqual(get_brand_id("REI"), 1)
def test_get_products_within_dates(self):
start_date = convert_string_to_datetime('2015-10-31')
end_date = convert_string_to_datetime('2015-11-01')
products = get_products_within_dates(start_date, end_date, User.query.all())
self.assertEqual(products[0].prod_id, 1)
def test_categorize_products(self):
categories = [Category.query.get(1), Category.query.get(2)]
products = [Product.query.get(1), Product.query.get(2), Product.query.get(5)]
inventory = categorize_products(categories, products)
self.assertEqual(inventory['Tents'][0].prod_id, 1)
self.assertEqual(inventory['Sleeping Bags'][0].prod_id, 5)
class SearchHelpersTestCase(TestCase):
def setUp(self):
print "\n\n\n\n (2) DOING A SEARCH TEST \n\n\n\n"
self.client = app.test_client()
app.config['TESTING'] = True
postgrespassword = os.environ['POSTGRES_PASSWORD']
db_uri = 'postgresql://' + postgrespassword + '/test'
connect_to_db(app, db_uri)
db.create_all()
load_regions()
load_users()
load_bestuses()
load_categories()
load_brands()
load_products()
load_tents()
load_filltypes()
load_gendertypes()
load_sleepingbags()
load_padtypes()
load_sleepingpads()
load_ratings()
load_histories()
load_test_postalcodes()
def tearDown(self):
db.session.close_all()
clear_data()
def test_Haversine_formula(self):
lat1 = 36.12
lng1 = -86.67
lat2 = 33.94
lng2 = -118.40
distance = calc_Haversine_distance(lat1, lng1, lat2, lng2)
self.assertEqual(int(distance), 1794)
def test_get_postalcode(self):
zipcode = PostalCode.query.get(94109)
self.assertEqual(zipcode.latitude, 37.803893)
self.assertEqual(zipcode.longitude, -122.42364)
def test_make_postalcode(self):
make_postalcode('98570')
a = PostalCode.query.get(98570)
self.assertEqual(int(a.latitude), 46)
self.assertEqual(int(a.longitude), -122)
def test_search_radius(self):
searchcenter = '94607'
postalcodes = [('94608',), ('94102',), ('94040',), ('95376',), ('95451',),
('92277',), ('10013',), ('02139',)]
within20 = search_radius(searchcenter, postalcodes, 20)
within60 = search_radius(searchcenter, postalcodes, 60)
within200 = search_radius(searchcenter, postalcodes, 200)
self.assertEqual(sorted(within20), sorted(['94608', '94102']))
self.assertEqual(sorted(within60), sorted(['94608', '94102', '94040',
'95376']))
self.assertEqual(sorted(within200), sorted(['94608', '94102', '94040',
'95376', '95451']))
# http://www.robotswillkillusall.org/posts/how-to-mock-datetime-in-python/
# https://pypi.python.org/pypi/mock
@patch('search_helpers.datetime')
def test_calc_default_dates(self, mock_dt):
expected = {'future': datetime(2015, 12, 18),
'today_string': '2015-11-18',
'today': datetime(2015, 11, 18),
'future_string': '2015-12-18'}
mock_dt.today.return_value = datetime(2015, 11, 18)
self.assertEqual(calc_default_dates(30), expected)
def test_convert_string_to_datetime(self):
test_date = convert_string_to_datetime("2015-11-18")
self.assertEqual(test_date, datetime(2015, 11, 18))
def test_calc_avg_star_rating(self):
rating1 = Rating(rating_id=1, stars=4, comments="abc")
rating2 = Rating(rating_id=3, stars=2, comments="def")
rating3 = Rating(rating_id=3, stars=3, comments="ghi")
ratings = [rating1, rating2, rating3]
self.assertEqual(calc_avg_star_rating(ratings), 3)
if __name__ == "__main__":
import unittest
unittest.main()