-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_artannounce.py
237 lines (154 loc) · 7.05 KB
/
test_artannounce.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
import unittest
from unittest import TestCase
awq
from server import app
from model import User, AppUser, Address, Artist, Patron, Fan, Artwork, db
from flask_sqlalchemy import SQLAlchemy
class TestArtAnnounce(TestCase):
""" test harness to test ArtAnnounce"""
def example_data(self):
"""Create sample data"""
# populate users table
uk = User(first_name='Kushlani', last_name='Jayasinha', email='kushi@att.net',
address_id=1)
uk.app_user = AppUser(login='kushij', password='1234')
uk.address = Address(address_line1='myhome')
uc = User(first_name='Chris', last_name='Lane', email='chris@lane-jayasinha.com',
address_id=2)
uc.app_user = AppUser(login='chrisl', password='1234')
uc.address = Address(address_line1='myhome')
uv = User(first_name='Vidharshi', last_name='Dharmasens', email='V@gmail.com',
address_id=3)
uv.app_user = AppUser(login='v', password='1234')
uv.address = Address(address_line1='myhome')
ua = User(first_name='Alex', last_name='Hall', email='alex@gmail.com',
address_id=4)
ua.app_user = AppUser(login='alexh', password='1234')
ua.address = Address(address_line1='myhome')
# populate artists table
a1 = Artist(bio='I am an artist.', statement='I love art!', website='http://KushlaniFineArt.com')
# populate patrons table
p1 = Patron(patron_info='I love Chris....')
# populate fans table
f1 = Fan(fan_info='I love Alex.')
# populate artworks table
aw1 = Artwork(title='Mendocino', year_created='2015', medium='oil', substrate='canvas',
genre='abstracts',length='40"', height='30"', depth='1.5"', url='https://fasoimages-4cde.kxcdn.com/25287_1438386l+v=201609181617c201609181617error/mendocino.jpg')
aw2 = Artwork(title='Autumn', year_created='2015', medium='oil', substrate='canvas',
genre='abstracts',length='8"', height='8"', depth='1.5"', url='https://fasoimages-4cde.kxcdn.com/25287_1322110l+v=201609181617c201609181617error/autumn.jpg')
#db.session.add_all([uk, uk.app_user, uk.address, uc, uc.app_user, uc.address, ua, ua.app_user, ua.address, uv, uv.app_user, uv.address, a1, p1, f1, aw1, aw2])
db.session.add_all([uk, uc, ua, uv, a1, p1, f1, aw1, aw2])
db.session.commit()
def setUp(self):
"""Stuff to do before every test"""
#import pdb; pdb.set_trace()
print "set up is done"
self.client = app.test_client()
app.config['TESTING'] = True
db.create_all()
self.example_data()
def tearDown(self):
"""do at end of every test"""
#import pdb; pdb.set_trace()
print "tear down "
db.session.close()
db.drop_all()
def test_homepage_route(self):
"""Test homepage"""
result = self.client.get("/")
self.assertEqual(result.status_code, 200) # if working code should be 200
self.assertIn('login', result.data)
self.assertIn('registered', result.data)
def test_login_get(self):
"""Test if gets to login page"""
result = self.client.get("/login")
self.assertIn('login', result.data) # if working code should be 200
def test_login_correct(self):
"""Test if login correct shows Welcome page"""
result = self.client.post("/login", data={'login':'kushij', 'pwd':'1234'},
follow_redirects=True)
try:
user_rec = AppUser.query.filter_by(login='kushij').first()
except:
print "db query for user failed."
self.assertEqual(user_rec.login, 'kushij')
self.assertIn('Welcome to ArtAnnounce', result.data)
def test_login_wrong(self):
"""Test if it goes to registration page user doesn't exist"""
result = self.client.post("/login", data={'login':'wrong_user', 'pwd':'1234'},
follow_redirects=True)
self.assertIn('Zip code:', result.data) # should get to registration page
def test_registration_get(self):
"""Test if in registrstion page"""
result = self.client.get("/register")
self.assertIn('Zip code:', result.data) # should get to registration page
def test_registration_incorrect(self):
"""Test if redirects to register if using invalid login name"""
result = self.client.post("/register", data={'login':'kushij', 'pwd':'1234',
'first_name':'valid_firstname','last_name':'valid_lastname'},
follow_redirects=True)
self.assertIn('Zip code', result.data)
def test_registration_correct(self):
"""Test if redirects to login if using invalid login name"""
result = self.client.post("/register", data={'login':'valid_login', 'pwd':'1234',
'first_name':'valid_firstname','last_name':'valid_lastname'},
follow_redirects=True)
self.assertIn('If you have already registered, please login', result.data)
def helper_user_type_db(self, user_ID, user_type, db_field, db_data):
"""
Test if users and user_type (eg. artist)
tables get updated
"""
route_string = "/%sInfo" % user_type
# check if artist is put in artists database. use user_id from user
result_use = self.client.post(route_string, data={'user_id':user_ID,
db_field:db_data}, follow_redirects=True)
User_type_class = globals()[user_type.title()]
#import pdb; pdb.set_trace()
user_rec = User_type_class.query.filter_by(user_id=user_ID).first()
self.assertEqual(getattr(user_rec, db_field), db_data)
def helper_user_type_all(self, usertype, str_in_user_type_page, db_field, db_data):
""" Test for user type: routes to specific
user info page, if users and user_type (eg. artist)
tables get updated
"""
# create a user who is an user_type
result = self.client.post("/register", data={'login':'anji', 'pwd':'1234',
'first_name':'Anjana','last_name':'Jayasinha', 'user_type':usertype},
follow_redirects=True)
# check if it goes to user_type info page
self.assertIn(str_in_user_type_page, result.data)
# check if user put in users database
user_type_rec = User.query.filter_by(login='anji').first()
# check if user_type table gets updated
self.helper_user_type_db(user_type_rec.user_id, usertype, db_field, db_data)
def test_register_artist(self):
""" Test if user type is an artist,
routes to collecting artist info page,
if users and artist tables get updated"""
self.helper_user_type_all('artist', 'Artist Info', 'website', 'mywebsite')
def test_register_patron(self):
""" Test if user type is an patron,
routes to collecting patron info page,
if users and patron tables get updated"""
self.helper_user_type_all('patron', 'Patron Info', 'patron_info', 'I am a patron')
def test_register_fan(self):
""" Test if user type is an fan,
routes to collecting fan info page,
if users and fan tables get updated"""
self.helper_user_type_all('fan', 'Fan Info', 'fan_info', 'I am a fan')
##############################################################################
### Helper Functions:
##############################################################################
def connect_to_test_db(app):
"""Connect the database to our Flask app."""
# Configure to use our database.
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgres:///testdb'
app.config['SQLALCHEMY_ECHO'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
if __name__ == "__main__":
connect_to_test_db(app)
print "Connected to DB."
unittest.main()