forked from 13lheytens/Netflix-Prize
/
TestNetflix.py
executable file
·242 lines (206 loc) · 6.67 KB
/
TestNetflix.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
#!/usr/bin/env python3
""" NETFLIX TESTS
"""
# -------
# imports
# -------
import pickle
from io import StringIO
from unittest import main, TestCase
from Netflix import netflix_read, netflix_print, \
netflix_predict, netflix_rmse, netflix_solve
# -----------
# TestNetflix
# -----------
class TestNetflix(TestCase):
# ----
# read
# ----
def test_read_1(self):
""" tests reading movie_id:
"""
line = "2043:\n"
val, ind = netflix_read(line)
self.assertEqual(val, 2043)
self.assertEqual(ind, 1)
def test_read_2(self):
""" tests reading customer_id
"""
line = "1417435\n"
val, ind = netflix_read(line)
self.assertEqual(val, 1417435)
self.assertEqual(ind, 0)
def test_read_3(self):
""" test reading empty line
"""
line = "\n"
val, ind = netflix_read(line)
self.assertEqual(val, -1)
self.assertEqual(ind, -1)
# -----
# print
# -----
def test_print_1(self):
""" tests printing rating
"""
rating = 1.0
writer = StringIO()
netflix_print(writer, rating)
self.assertEqual(writer.getvalue(), "1.0\n")
def test_print_2(self):
""" tests printing movie_id
"""
rating = 2043
writer = StringIO()
netflix_print(writer, rating)
self.assertEqual(writer.getvalue(), "2043:\n")
def test_print_3(self):
""" tests printing RMSE
"""
rating = "RMSE: 0.93"
writer = StringIO()
netflix_print(writer, rating)
self.assertEqual(writer.getvalue(), "RMSE: 0.93\n")
# ----
# cache
# ----
def test_cache_1(self):
"""
tests opening movYear cache
tests movYear cache vals
"""
with open("caches/movYear.p", "rb") as cache:
mov_year_cache = pickle.load(cache)
cache.close()
self.assertEqual(mov_year_cache[94], 2000)
self.assertEqual(mov_year_cache[12711], 1952)
self.assertEqual(mov_year_cache[12755], 1995)
def test_cache_2(self):
"""
tests opening tAnswers cache
tests tAnswers cache vals
"""
with open("caches/tAnswers.p", "rb") as cache:
answers_cache = pickle.load(cache)
cache.close()
self.assertEqual(answers_cache[2043][814483], 4)
self.assertEqual(answers_cache[10][1952305], 3)
self.assertEqual(answers_cache[10017][2280428], 3)
def test_cache_3(self):
"""
tests opening tAvgCust cache
tests tAvgCust cache vals
"""
with open("caches/tAvgCust.p", "rb") as cache:
avg_cust_cache = pickle.load(cache)
cache.close()
self.assertEqual(avg_cust_cache[814483], 3.6666666666666665)
self.assertEqual(avg_cust_cache[1952305], 3.409340659340659)
self.assertEqual(avg_cust_cache[2280428], 3.72)
def test_cache_4(self):
"""
tests opening tAvgMovie cache
tests tAvgMovie cache vals
"""
with open("caches/tAvgMovie.p", "rb") as cache:
avg_movie_cache = pickle.load(cache)
cache.close()
self.assertEqual(avg_movie_cache[2043], 3.7776648456358783)
self.assertEqual(avg_movie_cache[10], 3.180722891566265)
self.assertEqual(avg_movie_cache[10017], 2.982142857142857)
def test_cache_5(self):
"""
tests opening tYearsSinceRelease cache
tests tYearsSinceRelease cache vals
"""
with open("caches/tYearsSinceRelease.p", "rb") as cache:
years_since_release_cache = pickle.load(cache)
cache.close()
self.assertEqual(years_since_release_cache[2043][716091], 50)
self.assertEqual(years_since_release_cache[1000][2326571], 2)
self.assertEqual(years_since_release_cache[10001][262828], 11)
#-----
# rmse
# -----
def test_solve_1(self):
""" test solves input 1"""
inp = StringIO("2043:\n1417435\n2312054\n462685\n")
writer = StringIO()
netflix_solve(inp, writer)
self.assertEqual(
writer.getvalue(), "2043:\n3.7\n4.6\n4.0\nRMSE: 2.1283858730975753\n")
def test_solve_2(self):
""" test solves input 2"""
inp = StringIO("1:\n30878\n2043:\n462685\n")
writer = StringIO()
netflix_solve(inp, writer)
self.assertEqual(
writer.getvalue(), "1:\n3.7\n2043:\n4.0\nRMSE: 0.18432622758011358\n")
def test_solve_3(self):
""" test solves input 3"""
inp = StringIO("2043:\n2312054\n1:\n30878\n")
writer = StringIO()
netflix_solve(inp, writer)
self.assertEqual(
writer.getvalue(), "2043:\n4.6\n1:\n3.7\nRMSE: 2.5588590159754325\n")
def test_solve_4(self):
""" test solves invalid input 4"""
inp = StringIO("2043:\n2312054\n1:\n\n")
writer = StringIO()
with self.assertRaises(AssertionError):
netflix_solve(inp, writer)
# -------
# predict
# -------
def test_predict_1(self):
""" test predicts movie and customer"""
movie_id = 1
customer_id = 30878
res = netflix_predict(movie_id, customer_id)
self.assertEqual(res, 3.7394321383123703)
def test_predict_2(self):
""" test predicts movie and customer"""
movie_id = 10
customer_id = 1952305
res = netflix_predict(movie_id, customer_id)
self.assertEqual(res, 3.020308046060214)
def test_predict_3(self):
""" test predicts movie and customer"""
movie_id = 1002
customer_id = 2174660
res = netflix_predict(movie_id, customer_id)
self.assertEqual(res, 3.2379678434646615)
# -----
# rmse
# -----
def test_rmse_1(self):
""" tests rmse function"""
ans = [2, 3, 4]
pre = [2, 3, 4]
res = netflix_rmse(ans, pre)
self.assertEqual(res, 0.0)
def test_rmse_2(self):
""" tests rmse function"""
ans = [2, 3, 4]
pre = [3, 2, 3]
res = netflix_rmse(ans, pre)
self.assertEqual(res, 1.0)
def test_rmse_3(self):
""" tests rmse function"""
ans = [2, 3, 4]
pre = [4, 3, 2]
res = netflix_rmse(ans, pre)
self.assertEqual(res, 1.632993161855452)
def test_rmse_4(self):
""" tests rmse function on dictionaries"""
ans_cust = {123: 3, 234: 4}
pre_cust = {123: 4, 234: 5}
ans = {2034: ans_cust, 2345: ans_cust}
pre = {2034: pre_cust, 2345: pre_cust}
res = netflix_rmse(ans, pre)
self.assertEqual(res, 1.0)
# ----
# main
# ----
if __name__ == "__main__":
main()