-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
281 lines (212 loc) · 11.3 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
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
import server
import unittest
from server import *
from helper_functions import *
from seed import *
from model import *
import os
class ServerTestsWithSession(unittest.TestCase):
"""Tests for the game recommendations site that require session"""
def setUp(self):
"""Code to run before every test."""
self.client = server.app.test_client()
server.app.config['TESTING'] = True
app.config['SECRET_KEY'] = 'key'
# Connect to test database
connect_to_db(app, "postgresql:///testdb")
# Create tables and add sample data
db.create_all()
example_data()
with self.client as c:
with c.session_transaction() as sess:
sess['user_id'] = 1
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
def test_logout(self):
"""Can we log out a user that's been logged in?"""
result = self.client.get("/logout", follow_redirects=True)
self.assertIn("Goodbye!", result.data)
def test_homepage(self):
"""Can we log in and reach the homepage?"""
result = self.client.get("/homepage")
self.assertIn('Get a game recommendation', result.data)
self.assertIn('Filter by system', result.data)
self.assertIn('Filter by genre', result.data)
def test_login_fail_pw(self):
"""Can we reject a user with the wrong password?"""
result = self.client.post("/login", data={"email": "[email protected]",
"password": "jfsghjkdfhgh"},
follow_redirects=True)
self.assertIn("Incorrect password.", result.data)
def test_login_fail_email(self):
"""Can we reject a user with the wrong email?"""
result = self.client.post("/login", data={"email": "[email protected]",
"password": "jfsghjkdfhgh"},
follow_redirects=True)
self.assertIn("Incorrect email.", result.data)
def test_search(self):
"""Can we search for a game and reach the game details page?"""
result = self.client.get("/games/7346")
self.assertIn("The Legend of Zelda: Breath of the Wild", result.data)
self.assertIn("Rating:", result.data)
self.assertIn("Systems:", result.data)
self.assertIn("Nintendo Switch", result.data)
self.assertIn("Genres:", result.data)
self.assertIn("Role-playing (RPG)", result.data)
self.assertIn('<img src="http://www.cutestpaw.com/wp-content/uploads/2016/02/Yawn..jpeg" style="width:100px;">', result.data)
self.assertIn("Its a cool game with Zelda and Link", result.data)
self.assertIn("Waste your life playing this game", result.data)
self.assertIn('<iframe width="420" height="315" allowfullscreen', result.data)
self.assertIn('src="http://www.youtube.com/embed/Z6BeAtdoELY ">', result.data)
self.assertIn('<img src="http://www.cutestpaw.com/wp-content/uploads/2016/02/Yawn..jpeg" style="width:1920px height:1080;">', result.data)
def test_search_not_found(self):
"""Can we see the right page when a game isn't found?"""
result = self.client.get("/games/0")
self.assertIn("No game found by that name. Please try again.", result.data)
def test_recommendation(self):
"""Can we get a game recommendation?"""
result = self.client.get("/recommendation", data={"systems": [130],
"genres": [12]})
self.assertIn("Zelda", result.data)
def test_profile(self):
"""Can we get to the user's profile page?"""
result = self.client.get("/user/11")
self.assertIn("hellboy", result.data)
self.assertIn("[email protected]", result.data)
self.assertIn("User Since:", result.data)
self.assertIn("Systems:", result.data)
self.assertIn("Nintendo Switch", result.data)
self.assertIn("Games I've Rated:", result.data)
class ServerTestsWithoutSession(unittest.TestCase):
"""Tests for the game recommendations site"""
def setUp(self):
"""Code to run before every test."""
connect_to_db(app, "postgresql:///testdb")
self.client = server.app.test_client()
server.app.config['TESTING'] = True
# os.system("createdb testdb")
# Connect to test database
# import pdb; pdb.set_trace()
# Create tables and add sample data
db.create_all()
example_data()
def mock_get_user_rating(users, user, games):
pass
self.old_function = server.get_user_rating
server.get_user_rating = mock_get_user_rating
server.users = server.games = []
def tearDown(self):
"""Do at end of every test."""
db.session.close()
db.drop_all()
server.get_user_rating = self.old_function
# os.system("dropdb testdb")
def test_welcome_page(self):
"""Can we reach the welcome page?"""
result = self.client.get("/")
self.assertIn('Login or <a href="/register">Register</a>', result.data)
def test_registration_page(self):
"""Can we reach the registration page?"""
result = self.client.get("/register")
self.assertIn('Registration Form', result.data)
def test_registration(self):
"""Can we register a new user?"""
result = self.client.post("/register", data={"username": "cree",
"email": "[email protected]",
"password": "cree",
"systems": ["Nintendo Switch"],
}, follow_redirects=True)
self.assertIn("/login", result.data)
def test_login_success(self):
"""Can we log in a registered user?"""
result = self.client.post("/login", data={"email": "[email protected]",
"password": "hellboy"},
follow_redirects=True)
self.assertIn("/homepage", result.data)
self.assertIn("Successfully logged in!", result.data)
class RecommendationAlgorithmTests(unittest.TestCase):
"""Tests all functions that generate the recommendations"""
def setUp(self):
"""Code to run before every test."""
# global games
# global users
self.games = [38983, 10232, 27081, 21063, 11667, 17000, 7346, 26764, 19550, 19554]
self.users = {
10: {38983: 5, 10232: 4, 27081: 4, 21063: 2, 11667: 1,
17000: 4, 7346: 1, 26764: 3, 19550: 2, 19554:4},
11: {38983: 5, 10232: 4, 27081: 4, 21063: 2, 11667: 1,
17000: 4, 7346: 1, 26764: 3, 19550: 2, 19554:4},
12: {38983: 5, 10232: 4, 27081: 4, 21063: 2, 11667: 1,
17000: 4, 7346: 1, 26764: 3, 19550: 2, 19554:4},
13: {38983: 5, 10232: 4, 27081: 4, 21063: 2, 11667: 1,
17000: 4, 7346: 1, 26764: 3, 19550: 2, 19554:4},
14: {38983: 5, 10232: 4, 27081: 4, 21063: 2, 11667: 1,
17000: 4, 7346: 1, 26764: 3, 19550: 2, 19554:4}
}
def tearDown(self):
"""Do at end of every test."""
del self.games
del self.users
def test_get_all_similarities(self):
"""Do we get the expected result?"""
target_game = 38983
user_id = 12
# import pdb; pdb.set_trace()
assert get_all_similarities(target_game, self.users, self.games, user_id) == {10: 1.0, 11: 1.0, 13: 1.0, 14: 1.0}
def test_filt_all(self):
user1 = 11
user2 = 12
# import pdb; pdb.set_trace()
assert filt_all(user1, user2, self.games, self.users) == [(1, 1, 38983),
(4, 4, 10232),
(1, 1, 27081),
(4, 4, 21063),
(2, 2, 11667),
(4, 4, 17000),
(2, 2, 7346),
(3, 3, 26764),
(4, 4, 19550),
(5, 5, 19554)]
def test_pearson(self):
pairs = [(1, 4), (4, 2), (2, 4), (4, 1), (3, 3), (4, 2), (3, 2), (2, 4),
(1, 5), (5, 4), (4, 2), (1, 3), (5, 1), (5, 4), (4, 5), (4, 2),
(2, 1), (1, 1), (5, 2), (2, 4), (2, 1), (2, 2), (2, 4), (4, 3),
(2, 1), (3, 5), (1, 3), (1, 2), (1, 4), (2, 1), (2, 5), (5, 1),
(4, 1), (5, 2), (2, 5), (2, 3), (4, 2), (2, 5), (4, 5), (3, 3),
(5, 5), (5, 2), (2, 3), (3, 4), (2, 3), (5, 5), (2, 1), (2, 4),
(5, 4), (2, 2), (4, 3), (4, 2), (1, 4), (2, 5), (2, 4), (5, 4),
(3, 5), (4, 4), (1, 3), (5, 1), (2, 1), (3, 2), (1, 4), (5, 1),
(3, 1), (4, 3), (5, 5), (4, 4), (3, 2), (1, 2), (5, 2), (5, 4),
(2, 5), (2, 5), (1, 4), (4, 4), (4, 3), (1, 1), (5, 2), (3, 3),
(2, 3), (3, 3), (3, 2), (2, 4), (4, 5), (1, 3), (3, 3), (1, 4),
(3, 5), (5, 1), (3, 3), (4, 4), (5, 4), (1, 1), (5, 2), (1, 3),
(5, 1), (1, 4), (2, 1), (3, 5), (5, 3), (5, 5), (1, 4), (1, 1),
(3, 4), (5, 3), (5, 2), (4, 4), (3, 3), (3, 3), (3, 2), (1, 4),
(1, 1), (3, 3), (1, 4), (4, 1), (5, 2), (5, 5), (2, 4), (5, 2),
(4, 4), (2, 1), (1, 1), (5, 5), (5, 4), (4, 2), (1, 2), (3, 5),
(1, 5), (4, 3), (5, 3), (2, 3), (3, 4), (1, 5), (3, 1), (5, 2),
(2, 3), (2, 4), (3, 2), (1, 3), (4, 5), (3, 2), (1, 3), (4, 3),
(1, 3), (2, 5), (1, 3), (5, 2), (1, 4), (2, 1), (3, 3), (5, 1),
(2, 2), (2, 2), (4, 3), (1, 3), (3, 1), (1, 4), (2, 5), (5, 4),
(5, 5), (2, 3), (4, 5), (4, 5), (2, 5), (4, 2), (2, 3), (5, 4),
(4, 5), (1, 2), (2, 3), (3, 3), (3, 4), (4, 3), (1, 2), (5, 4),
(4, 1), (4, 3), (2, 3), (5, 3), (1, 3), (5, 1), (3, 5), (4, 3),
(2, 4), (5, 1), (3, 4), (2, 5), (3, 3), (3, 2), (2, 3), (1, 5),
(2, 2), (1, 4), (2, 1), (2, 4), (2, 4), (5, 2), (1, 3), (1, 4),
(5, 4), (4, 5), (3, 4), (5, 3), (5, 2), (3, 3), (2, 5), (5, 2),
(1, 5), (2, 1), (4, 1), (4, 3), (2, 4), (3, 3), (5, 2), (4, 3),
(2, 2), (2, 5), (3, 4), (3, 1), (3, 4), (1, 5), (3, 5), (3, 2),
(2, 3)]
# import pdb; pdb.set_trace()
assert pearson(pairs) == -0.09100648402162553
def test_predict(self):
sims = {10: 0.02383173173628074, 11: -0.06863014222129703,
12: -0.031168143681458316, 13: -0.0627253940114878,
14: 0.05444267283832687}
target_game = 38983
# import pdb; pdb.set_trace()
assert predict(sims, self.users, target_game) == 2.30024962184833
if __name__ == "__main__":
unittest.main()