-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
executable file
·254 lines (216 loc) · 7.98 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
#!/usr/bin/python3
import os
from unittest import main, TestCase
from server import app, db
# Maybe not the below one just yet
# from app.models import User
from server.models import Game, Company, Year, get_game_genre_table, Platform, Genre
class TestCase(TestCase):
# -------------------
# setup test server
# -------------------
def setUp(self):
app.config['TESTING'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:password@localhost/swe_test'
self.app = app.test_client()
self.endpoints = []
self.endpoints.append('/games')
self.endpoints.append('/games/1')
self.endpoints.append('/companies')
self.endpoints.append('/companies/1234')
self.endpoints.append('/years')
self.endpoints.append('/years/1995')
self.api_endpoints = ['/api'+x for x in self.endpoints]
# Below two added after making api_endpoints because these
# are not api endpoints
self.endpoints.append('/')
self.endpoints.append('/about')
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
# -------------------
# test splash page
# -------------------
# Make sure connection to each page is successful
def test_page_connection(self):
for endpoint in self.endpoints:
with self.subTest():
res = self.app.get(endpoint)
self.assertEqual('200 OK', res.status)
# Make sure connection to each API endpoint is successful
def test_api_page_connection1(self):
for endpoint in self.api_endpoints:
with self.subTest():
res = self.app.get(endpoint)
self.assertEqual('200 OK', res.status)
# Make sure JSON is returned from a GET on each API endpoint
def test_api_page_connection2(self):
for endpoint in self.api_endpoints:
with self.subTest():
res = self.app.get(endpoint)
self.assertTrue('json' in res.mimetype, 'culprit: {}'.format(endpoint))
# TESTS FOR MODELS
# id, name, image-url, rating, release_year
def test_games_1(self):
y = Year(2008)
db.session.add(y)
g = Game(1, "Fallout 3", rating = 87, release_year = 2008)
db.session.add(g)
db.session.commit()
q = Game.query.all()
self.assertEqual(len(q), 1)
db.session.remove()
def test_games_2(self):
y = Year(2008)
db.session.add(y)
g = Game(1, "Fallout 3", rating = 87, release_year = 2008)
db.session.add(g)
db.session.commit()
self.assertTrue(g in db.session) # https://pythonhosted.org/Flask-Testing/
db.session.remove()
def test_games_3(self):
y = Year(2008)
db.session.add(y)
g = Game(1,"Fallout 3", rating = 87, release_year = 2008)
y2 = Year(2004)
db.session.add(y2)
g2 = Game(2,"TES IV: Oblivion", rating = 100, release_year = 2004)
db.session.add(g)
db.session.add(g2)
db.session.commit()
q = Game.query.all()
self.assertEqual(len(q), 2)
db.session.remove()
#(self, company_id, name=None, num_developed=None, image_url=None, num_published=None, avg_rating=None, year_founded=None)
def test_company_1(self):
y = Year(1986)
db.session.add(y)
c = Company(1,"Bethesda Softworks", 11, None, 66, 8.9, 1986)
db.session.add(c)
db.session.commit()
self.assertTrue(c in db.session)
db.session.remove()
def test_company_2(self):
y = Year(1986)
db.session.add(y)
c = Company(1, "Bethesda Softworks", 11, None, 66, 8.9, 1986)
y2 = Year(1994)
db.session.add(y2)
c2 = Company(2, "Blizzard Entertainment", 20, None, 40, 9, 1994)
y3 = Year(1980)
db.session.add(y3)
c3 = Company(3, "Activision", 10, None, 1000, 6, 1980)
db.session.add(c)
db.session.add(c2)
db.session.add(c3)
db.session.commit()
q = Company.query.all()
self.assertEqual(len(q), 3)
db.session.remove()
def test_company_3(self):
y = Year(1986)
db.session.add(y)
c = Company(1, "Bethesda Softworks", 11, None, 66, 8.9, 1986)
c2 = Company(2, "Bethesda Softworks", 11, None, 66, 8.9, 1986)
db.session.add(c)
self.assertTrue(c in db.session)
self.assertFalse(c2 in db.session)
db.session.remove()
#Years year_id = None, num_games = None, most_popular_genre = None, avg_rating = None, num_companies_founded = None
def test_year_1(self):
y = Year(1997, 2000, "FPS", 9, 100)
db.session.add(y)
db.session.commit()
q = Year.query.all()
self.assertEqual(y, q[0])
db.session.remove()
def test_year_2(self):
y = Year(1997, 2000, "FPS", 9, 100)
y2 = Year(1990, 200, "Platformer", 10, 50)
db.session.add(y)
db.session.add(y2)
db.session.commit()
self.assertTrue(y2 in db.session)
db.session.remove()
# q is a list sorted in asc order by primary key (year_id in this case)
def test_year_3(self):
y = Year(2007, 5000, "FPS", 9.6, 1000)
y2 = Year(2008, 2000, "FPS", 9.5, 500)
y3 = Year(1999, 1000, "Fighting", 8.0, 400)
db.session.add(y)
db.session.add(y2)
db.session.add(y3)
q = Year.query.all()
self.assertEqual(q[0].year_id, y3.year_id)
db.session.remove()
#Years year_id = None, num_games = None, most_popular_genre = None, avg_rating = None, num_companies_founded = None
def test_year_api_1(self):
y = Year(1997, 2000, "FPS", 9, 100)
db.session.add(y)
db.session.commit()
q = Year.query.all()
self.assertEqual(y, q[0])
db.session.remove()
def test_year_api_2(self):
y = Year(1997, 2000, "FPS", 9, 100)
y2 = Year(1990, 200, "Platformer", 10, 50)
db.session.add(y)
db.session.add(y2)
db.session.commit()
self.assertTrue(y2 in db.session)
db.session.remove()
# q is a list sorted in asc order by primary key (year_api_id in this case)
def test_year_api_3(self):
y = Year(2007, 5000, "FPS", 9.6, 1000)
y2 = Year(2008, 2000, "FPS", 9.5, 500)
y3 = Year(1999, 1000, "Fighting", 8.0, 400)
db.session.add(y)
db.session.add(y2)
db.session.add(y3)
q = Year.query.all()
self.assertEqual(q[0].year_id, y3.year_id)
db.session.remove()
def test_platform_1(self):
p = Platform("Xbox 360")
db.session.add(p)
db.session.commit()
q = Platform.query.all()
self.assertEqual(q[0].platform_name, "Xbox 360")
db.session.remove()
def test_platform_2(self):
p = Platform("Xbox 360")
db.session.add(p)
p2 = Platform("PC")
db.session.add(p2)
db.session.commit()
q = Platform.query.filter_by(platform_id=2).all()
self.assertEqual(q[0].platform_name, "PC")
db.session.remove()
def test_platform_3(self):
p = Platform("Xbox 360")
db.session.add(p)
p2 = Platform("PC")
db.session.add(p2)
p3 = Platform("PS3")
db.session.add(p3)
q = Platform.query.filter_by(platform_name="PS3").all()
self.assertEqual(q[0].platform_id, 3)
db.session.remove()
def test_platform_4(self):
p = Platform("Xbox 360")
db.session.add(p)
p2 = Platform("PC")
db.session.add(p2)
p3 = Platform("PS3")
db.session.add(p3)
q = Platform.query.all()
self.assertEqual(len(q), 3)
db.session.remove()
def test_get_genre_table_1(self):
game_genre_table = get_game_genre_table()
q = db.session.query(game_genre_table).all()
self.assertEqual(len(q), 0)
db.session.remove()
if __name__ == '__main__':
main()