-
Notifications
You must be signed in to change notification settings - Fork 22
/
test.py
281 lines (210 loc) · 8.64 KB
/
test.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
#!/usr/bin/env python
"""Unit tests for the `rottentomatoes.py` file."""
import unittest
try:
from urlparse import urlparse, parse_qs
except ImportError: # pragma: no cover
# For older versions of Python.
from urlparse import urlparse
from cgi import parse_qs
from mock import Mock
from rottentomatoes import rottentomatoes
from rottentomatoes import RT
def set_up():
"""
Mock both and json.loads' return value. Makes for fast unit tests.
"""
rottentomatoes.urlopen = Mock()
movies_dict = {'movies': ['first_result', 'second_result'],
'total': 2}
rottentomatoes.json.loads = Mock(return_value=movies_dict)
rottentomatoes.zlib.decompress = Mock()
rottentomatoes.API_KEY = 'my_api_key'
def call_args(kind='query'):
"""Find out what urlopen called while mocking."""
call = rottentomatoes.urlopen.call_args[0][0]
parsed_call = urlparse(call)
if kind == 'query':
return parse_qs(parsed_call.query)
elif kind == 'path':
return parsed_call.path
class RTClassInitTest(unittest.TestCase):
def setUp(self):
set_up()
def test_uninitialized_api_key(self):
self.assertEqual(RT().api_key, 'my_api_key')
def test_initialized_api_key(self):
self.assertEqual(RT('called_api_key').api_key, 'called_api_key')
def test_version_argument_with_float(self):
self.assertEqual(RT(version=2.5).version, '2.5')
def test_version_argument_with_string(self):
self.assertEqual(RT(version='2.5').version, '2.5')
class SearchMethodTest(unittest.TestCase):
def setUp(self):
set_up()
def test_nonempty_search_url_path(self):
RT().search('some movie')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/movies')
def test_empty_search_url_keys(self):
RT().search('')
movie = call_args()
self.assertEqual(movie.keys(), ['apikey'])
def test_nonempty_search_url_keys(self):
RT().search('some movie')
movie = call_args()
self.assertEqual(movie.keys(), ['q', 'apikey'])
def test_search_url_keys_with_page_arg(self):
RT().search('some movie', page=2)
movie = call_args()
self.assertEqual(movie.keys(), ['q', 'apikey', 'page'])
def test_search_url_keys_with_page_limit_arg(self):
RT().search('some movie', page_limit=5)
movie = call_args()
self.assertEqual(movie.keys(), ['q', 'apikey', 'page_limit'])
def test_search_url_keys_with_multiple_kwargs(self):
RT().search('some movie', page=2, page_limit=5)
movie = call_args()
self.assertEqual(movie.keys(), ['q', 'apikey', 'page', 'page_limit'])
def test_search_url_keys_for_lion_king(self):
RT().search('the lion king')
movie = call_args()
assert 'my_api_key' in movie['apikey']
assert 'the lion king' in movie['q']
def test_search_url_keys_for_ronin(self):
RT().search('ronin')
movie = call_args()
assert 'my_api_key' in movie['apikey']
assert 'ronin' in movie['q']
def test_search_results_for_standard_datatype(self):
results = RT().search('some movie')
self.assertEqual(results, ['first_result', 'second_result'])
def test_search_results_for_movies_datatype(self):
results = RT().search('some movie', 'movies')
self.assertEqual(results, ['first_result', 'second_result'])
def test_search_results_for_total_datatype(self):
results = RT().search('some movie', 'total')
self.assertEqual(results, 2)
class ListsMethodTest(unittest.TestCase):
def setUp(self):
set_up()
def test_empty_lists_url_path(self):
RT().lists()
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/lists.json')
def test_lists_url_path_for_dvds(self):
RT().lists('dvds')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/lists/dvds.json')
def test_lists_url_path_for_dvds_sub_new_releases(self):
RT().lists('dvds', 'new_releases')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/lists/dvds/new_releases.json')
def test_lists_url_path_for_movies(self):
RT().lists('movies')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/lists/movies.json')
def test_lists_url_path_for_movies_sub_opening(self):
RT().lists('movies', 'opening')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/lists/movies/opening.json')
def test_lists_url_keys_for_extra_kwargs(self):
RT().lists('movies', 'in_theaters', page_limit=5)
parsed_query = call_args()
assert 'my_api_key' in parsed_query['apikey']
assert '5' in parsed_query['page_limit']
class InfoMethodTest(unittest.TestCase):
def setUp(self):
set_up()
def test_empty_info_method_call_fails(self):
self.assertRaises(TypeError, RT().info)
def test_id_num_as_string(self):
fight_club = '13153'
RT().info(fight_club)
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/movies/13153.json')
def test_id_num_as_int(self):
fight_club = 13153
RT().info(fight_club)
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/movies/13153.json')
def test_specific_info_for_cast(self):
fight_club = '13153'
RT().info(fight_club, 'cast')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/movies/13153/cast.json')
def test_specific_info_for_reviews(self):
fight_club = '13153'
RT().info(fight_club, 'reviews')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/movies/13153/reviews.json')
class NewMethodTest(unittest.TestCase):
def setUp(self):
set_up()
def test_new_url_path_for_dvds(self):
RT().new('dvds')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/lists/dvds/new_releases.json')
def test_new_url_keys_for_dvds_with_kwargs(self):
RT().new('dvds', page=2)
parsed_query = call_args()
assert '2' in parsed_query['page']
def test_new_url_path_for_movies(self):
RT().new('movies')
path = call_args('path')
self.assertEqual(path, '/api/public/v1.0/lists/movies/opening.json')
def test_new_url_keys_for_movies_with_kwargs(self):
RT().new('movies', page_limit=5)
parsed_query = call_args()
assert '5' in parsed_query['page_limit']
class MoviesMethodTest(unittest.TestCase):
def setUp(self):
set_up()
def test_empty_movies_call_reverts_to_in_theaters(self):
RT().movies()
path = call_args('path')
expected_path = '/api/public/v1.0/lists/movies/in_theaters.json'
self.assertEqual(path, expected_path)
def test_movies_in_theaters(self):
RT().movies('in_theaters')
path = call_args('path')
expected_path = '/api/public/v1.0/lists/movies/in_theaters.json'
self.assertEqual(path, expected_path)
def test_movies_upcoming(self):
RT().movies('upcoming')
path = call_args('path')
expected_path = '/api/public/v1.0/lists/movies/upcoming.json'
self.assertEqual(path, expected_path)
def test_movies_opening(self):
RT().movies('opening')
path = call_args('path')
expected_path = '/api/public/v1.0/lists/movies/opening.json'
self.assertEqual(path, expected_path)
def test_movies_box_office(self):
RT().movies('box_office')
path = call_args('path')
expected_path = '/api/public/v1.0/lists/movies/box_office.json'
self.assertEqual(path, expected_path)
class DvdsMethodTest(unittest.TestCase):
def setUp(self):
set_up()
def test_empty_dvds_call_reverts_to_new_releases(self):
RT().dvds()
path = call_args('path')
expected_path = '/api/public/v1.0/lists/dvds/new_releases.json'
self.assertEqual(path, expected_path)
def test_dvds_new_releases(self):
RT().dvds('new_releases')
path = call_args('path')
expected_path = '/api/public/v1.0/lists/dvds/new_releases.json'
self.assertEqual(path, expected_path)
class FeelingLuckyMethodTest(unittest.TestCase):
def setUp(self):
set_up()
def test_empty_feeling_lucky_method_fails(self):
self.assertRaises(TypeError, RT().feeling_lucky)
def test_first_json_loads_movies_result_is_returned(self):
data = RT().feeling_lucky('some movie')
self.assertEqual(data, 'first_result')
if __name__ == '__main__':
unittest.main()