forked from ace-jc/netflix-tests
/
ai3845-rea764-TestNetflix.py
273 lines (244 loc) · 8.96 KB
/
ai3845-rea764-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
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
#!/usr/bin/env python3
# https://docs.python.org/3.4/reference/simple_stmts.html#grammar-token-assert_stmt
"""
Unit tests for Netflix.py functions
"""
# -------
# imports
# -------
from io import StringIO
from unittest import main, TestCase
from Netflix import netflix_eval, netflix_print, netflix_solve
# -----------
# Testnetflix
# -----------
class TestNetflix(TestCase):
"""
All test cases
"""
# -----
# eval
# -----
def test_netflix_eval_1(self):
"""
Test 1 netflix_eval to see if it
makes the correct prediction
"""
movie_cache = {'1': 3.7, '2': 1, '3': 1}
movie_year_cache = {1: 2000, 2: 1999, 3: 1995}
cust_year_cache = {1: {1999: 1}, 2: {2000: 3.7}, 3: {1995: 1}}
self.assertEqual(
netflix_eval(1, movie_cache, 2, movie_year_cache, cust_year_cache), 3.7)
def test_netflix_eval_2(self):
"""
Test 2 netflix_eval to see if it
makes the correct prediction
"""
movie_cache = {'1': 4.0, '2': 1, '3': 1}
movie_year_cache = {1: 2005, 2: 1999, 3: 1995}
cust_year_cache = {1: {2010: 1.5}, 2: {2005: 5}, 3: {1995: 4}}
self.assertEqual(
netflix_eval(1, movie_cache, 2, movie_year_cache, cust_year_cache), 5)
def test_netflix_eval_3(self):
"""
Test 3 netflix_eval to see if it
makes the correct prediction
"""
movie_cache = {'1': 5, '2': 1.9, '3': 3.2}
movie_year_cache = {1: 2005, 2: 1999, 3: 1995}
cust_year_cache = {1: {2010: 1.5}, 2: {2005: 5}, 3: {1995: 4}}
self.assertEqual(
netflix_eval(3, movie_cache, 3, movie_year_cache, cust_year_cache), 3.5)
def test_netflix_eval_4(self):
"""
Test 4 netflix_eval to see if it
makes the correct prediction
"""
movie_cache = {'1': 3.7, '2': 1, '3': 4}
movie_year_cache = {1: 2005, 2: 1999, 3: 1995}
cust_year_cache = {1: {2010: 1.5}, 2: {2005: 5}, 3: {1995: 2.0}}
self.assertEqual(
netflix_eval(3, movie_cache, 3, movie_year_cache, cust_year_cache), 2.3)
def test_netflix_eval_5(self):
"""
Test 5 netflix_eval to see if it
makes the correct prediction
"""
movie_cache = {'1': 3.7, '2': 1, '3': 4}
movie_year_cache = {1: 2005, 2: 1999, 3: 1995}
cust_year_cache = {1: {2010: 1.5}, 2: {1999: 1.0}, 3: {1995: 2.0}}
self.assertEqual(
netflix_eval(2, movie_cache, 2, movie_year_cache, cust_year_cache), 1)
def test_netflix_eval_6(self):
"""
Test 6 netflix_eval to see if it
makes the correct prediction
"""
movie_cache = {'1': 3.7, '2': 3.5, '3': 4}
movie_year_cache = {1: 2005, 2: 1999, 3: 1995}
cust_year_cache = {1: {2010: 1.5}, 2: {1999: 1.9}, 3: {2005: 2.0}}
self.assertEqual(
netflix_eval(1, movie_cache, 3, movie_year_cache, cust_year_cache), 2.0)
# -----
# print
# -----
def test_netflix_print_1(self):
"""
Test 1 netflix_print to see if
movies are formatted correctly
"""
write = StringIO()
netflix_print(write, '123:', 'movie')
self.assertEqual(write.getvalue(), '123:\n')
def test_netflix_print_2(self):
"""
Test 2 netflix_print to see if
movies are formatted correctly
"""
write = StringIO()
netflix_print(write, '199999:', 'movie')
self.assertEqual(write.getvalue(), '199999:\n')
def test_netflix_print_3(self):
"""
Test 3 netflix_print to see if
ratings are formatted correctly
"""
write = StringIO()
netflix_print(write, 3.5456465, 'rating')
self.assertEqual(write.getvalue(), '3.5\n')
def test_netflix_print_4(self):
"""
Test 4 netflix_print to see if
ratings are formatted correctly
"""
write = StringIO()
netflix_print(write, 1, 'rating')
self.assertEqual(write.getvalue(), '1.0\n')
def test_netflix_print_5(self):
"""
Test 5 netflix_print to see if the
rmse is formatted correctly
"""
write = StringIO()
netflix_print(write, .9849846, 'rmse')
self.assertEqual(write.getvalue(), 'RMSE: 0.98\n')
def test_netflix_print_6(self):
"""
Test 6 netflix_print to see if the
rmse is formatted correctly
"""
write = StringIO()
# NOTE: We round instead of truncate. Is this permissable?
netflix_print(write, .555555, 'rmse')
self.assertEqual(write.getvalue(), 'RMSE: 0.56\n')
# -----
# solve
# -----
def test_netflix_solve_1(self):
"""
Test 1 to see if
solving correctly
"""
read = StringIO(
"10002:\n1450941\n1213181\n308502\n2581993\n10003:\n1515111")
write = StringIO()
actual_ratings = {
10002: {1450941: 2, 1213181: 3, 308502: 4, 2581993: 3}, 10003: {1515111: 1}}
movie_cache = {'10002': 3.7, '10003': 1.75}
movie_year_cache = {10002: 2005, 10003: 1999}
cust_year_cache = {1450941: {2005: 3.5}, 1213181: {2005: 4.987},
308502: {2005: 5}, 2581993: {2005: 3}, 1515111: {1999: 3}}
netflix_solve(read, write, actual_ratings,
movie_cache, movie_year_cache, cust_year_cache)
self.assertEqual(
write.getvalue(), "10002:\n3.5\n5.0\n5.0\n3.0\n10003:\n1.0\nRMSE: 1.20\n")
def test_netflix_solve_2(self):
"""
Test 2 to see if
solving correctly
"""
read = StringIO(
"1:\n1\n2\n3\n4\n2:\n5")
write = StringIO()
actual_ratings = {1: {1: 2, 2: 3, 3: 4, 4: 3}, 2: {5: 1}}
movie_cache = {'1': 3.7, '2': 1.75}
movie_year_cache = {1: 1988, 2: 1975}
cust_year_cache = {
1: {1988: 1}, 2: {1988: 4.987}, 3: {1988: 5}, 4: {1988: 3}, 5: {1975: 3}}
netflix_solve(read, write, actual_ratings,
movie_cache, movie_year_cache, cust_year_cache)
self.assertEqual(
write.getvalue(), "1:\n1.0\n5.0\n5.0\n3.0\n2:\n1.0\nRMSE: 1.09\n")
def test_netflix_solve_3(self):
"""
Test 3 to see if
solving correctly
"""
read = StringIO(
"1:\n1\n2\n3\n4\n2:\n5")
write = StringIO()
actual_ratings = {1: {1: 2, 2: 3, 3: 4, 4: 3}, 2: {5: 1}}
movie_cache = {'1': 4.5, '2': 2.1525}
movie_year_cache = {1: 1988, 2: 1975}
cust_year_cache = {
1: {1988: 1}, 2: {1988: 4.987}, 3: {1988: 5}, 4: {1988: 3}, 5: {1975: 3}}
netflix_solve(read, write, actual_ratings,
movie_cache, movie_year_cache, cust_year_cache)
self.assertEqual(
write.getvalue(), "1:\n1.8\n5.0\n5.0\n3.8\n2:\n1.5\nRMSE: 1.08\n")
def test_netflix_solve_4(self):
"""
Test 4 to see if
solving correctly
"""
read = StringIO(
"1:\n1\n2\n3\n4\n2:\n5")
write = StringIO()
actual_ratings = {1: {1: 2, 2: 3, 3: 4, 4: 3}, 2: {5: 1}}
movie_cache = {'1': 3.7, '2': 1.75}
movie_year_cache = {1: 1988, 2: 1975}
cust_year_cache = {1: {1988: 1.5}, 2: {1988: 2.5}, 3: {
1988: 4.6}, 4: {1988: 2.9}, 5: {1975: 3.6}}
netflix_solve(read, write, actual_ratings,
movie_cache, movie_year_cache, cust_year_cache)
self.assertEqual(
write.getvalue(), "1:\n1.5\n2.5\n4.6\n2.9\n2:\n1.6\nRMSE: 0.51\n")
def test_netflix_solve_5(self):
"""
Test 5 to see if
solving correctly
"""
read = StringIO(
"1:\n1\n2\n3\n4\n2:\n5")
write = StringIO()
actual_ratings = {1: {1: 4, 2: 5, 3: 5, 4: 5}, 2: {5: 5}}
movie_cache = {'1': 3.7, '2': 1.75}
movie_year_cache = {1: 1988, 2: 1975}
cust_year_cache = {1: {1988: 1.5}, 2: {1988: 2.5}, 3: {
1988: 4.6}, 4: {1988: 2.9}, 5: {1975: 3.6}}
netflix_solve(read, write, actual_ratings,
movie_cache, movie_year_cache, cust_year_cache)
self.assertEqual(
write.getvalue(), "1:\n1.5\n2.5\n4.6\n2.9\n2:\n1.6\nRMSE: 2.38\n")
def test_netflix_solve_6(self):
"""
Test 6 to see if
solving correctly
"""
read = StringIO(
"1:\n1\n2\n3\n4\n2:\n5")
write = StringIO()
actual_ratings = {1: {1: 4, 2: 5, 3: 5, 4: 5}, 2: {5: 5}}
movie_cache = {'1': 4.5, '2': 2.1525}
movie_year_cache = {1: 1988, 2: 1975}
cust_year_cache = {1: {1988: 2.5}, 2: {1988: 2.0}, 3: {
1988: 4.3}, 4: {1988: 4.9}, 5: {1975: 3.9}}
netflix_solve(read, write, actual_ratings,
movie_cache, movie_year_cache, cust_year_cache)
self.assertEqual(
write.getvalue(), "1:\n3.3\n2.8\n5.0\n5.0\n2:\n2.4\nRMSE: 1.57\n")
# ----
# main
# ----
if __name__ == "__main__":
main()