-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.py
269 lines (206 loc) · 10.3 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
#!/usr/bin/env python
"""Unit tests for `espn.py` file."""
import unittest
import datetime
from mock import Mock
import espn
from espn import (daterange, _format_scoreboard_url, scrape_links,
adjust_game, _league_time, _calc_overall_time,
_play_as_dict, _adjust_time)
class DateRangeTest(unittest.TestCase):
def test_week_has_gone_by(self):
now = datetime.date(2010, 2, 27)
week_ago = now - datetime.timedelta(days=7)
days = [day for day in daterange(week_ago, now)]
self.assertEqual(len(days), 7)
class FormatScoreboardUrlTest(unittest.TestCase):
def setUp(self):
self.date = datetime.date(2010, 2, 26)
self.date_string = '20100226'
self.correct_nba_link = ('http://scores.espn.go.com/nba/scoreboard?'
'date=20100226')
self.correct_ncb_link = ('http://scores.espn.go.com/ncb/scoreboard?'
'date=20100226&confId=50')
def test_format_nba_link_with_datetime(self):
self.assertEqual(_format_scoreboard_url(self.date),
self.correct_nba_link)
def test_format_nba_link_with_date_string(self):
self.assertEqual(_format_scoreboard_url(self.date_string),
self.correct_nba_link)
def test_format_ncb_link_with_datetime(self):
self.assertEqual(_format_scoreboard_url(self.date, league='ncb'),
self.correct_ncb_link)
def test_format_ncb_link_with_date_string(self):
self.assertEqual(_format_scoreboard_url(self.date_string, league='ncb'),
self.correct_ncb_link)
def test_format_link_with_capital_case(self):
self.assertEqual(_format_scoreboard_url(self.date, league='NBA'),
self.correct_nba_link)
def test_format_link_with_mixed_case(self):
self.assertEqual(_format_scoreboard_url(self.date, league='NbA'),
self.correct_nba_link)
class ScrapeLinksTest(unittest.TestCase):
def setUp(self):
"""This set up is a little tricky since we're mocking urllib2."""
espn.urllib2 = Mock()
self.mock_read = Mock()
self.url = 'http://scores.espn.go.com/nba/scoreboard?date=20100226'
self.one_link = ('<div class="span-4">'
'<a href="nba/playbyplay?gameId=300226001">'
'Play-By-Play</a></div>')
self.one_game = [u'gameId=300226001']
self.multi_link = ('<div class="span-4">'
'<a href="nba/playbyplay?gameId=300226001">'
'Play-By-Play</a>'
'<a href="nba/playbyplay?gameId=300226002">'
'Play-By-Play</a></div>')
self.multi_game = [u'gameId=300226001', u'gameId=300226002']
def test_scrape_fails(self):
self.mock_read.read.return_value = 'Nothing here. You should fail.'
espn.urllib2.urlopen.return_value = self.mock_read
self.assertRaises(AttributeError, scrape_links, self.url)
def test_scrape_works_on_one_link(self):
self.mock_read.read.return_value = self.one_link
espn.urllib2.urlopen.return_value = self.mock_read
self.assertEqual(scrape_links(self.url), self.one_game)
def test_scrape_works_on_multi_link(self):
self.mock_read.read.return_value = self.multi_link
espn.urllib2.urlopen.return_value = self.mock_read
self.assertEqual(scrape_links(self.url), self.multi_game)
class AdjustTimeTest(unittest.TestCase):
def setUp(self):
self.adjusted = _adjust_time('12:00', 1, True, 'nba')
def test_dictionary_output(self):
time = self.adjusted[0]
self.assertEqual(time, {'quarter': 2, 'quarter_time': '12:00',
'overall_time': '0:12:00'})
def test_quarter_increases(self):
quarter = self.adjusted[1]
self.assertEqual(quarter, 2)
def test_end_of_quarter_becomes_false(self):
end_of_quarter = self.adjusted[2]
self.assertEqual(end_of_quarter, False)
def test_end_of_quarter_becomes_true(self):
new_adjusted = _adjust_time('0:55', 1, False, 'nba')
end_of_quarter = new_adjusted[2]
self.assertEqual(end_of_quarter, True)
class LeagueTimeTest(unittest.TestCase):
def test_nba_league_time(self):
num_quarters, regulation_time, regular_quarter = _league_time('nba')
self.assertEqual(num_quarters, 4)
self.assertEqual(regulation_time, 48)
self.assertEqual(regular_quarter, 12)
def test_ncb_league_time(self):
num_quarters, regulation_time, regular_quarter = _league_time('ncb')
self.assertEqual(num_quarters, 2)
self.assertEqual(regulation_time, 40)
self.assertEqual(regular_quarter, 20)
class CalcOverallTimeTest(unittest.TestCase):
def test_beginning_of_game_nba(self):
time = _calc_overall_time(0, 12, 1, 'nba')
self.assertEqual(time, '0:00:00')
def test_beginning_of_second_quarter_nba(self):
time = _calc_overall_time(0, 12, 2, 'nba')
self.assertEqual(time, '0:12:00')
def test_five_minutes_into_third_quarter_nba(self):
time = _calc_overall_time(0, 7, 3, 'nba')
self.assertEqual(time, '0:29:00')
def test_last_five_minutes_of_fourth_quarter_nba(self):
time = _calc_overall_time(0, 5, 4, 'nba')
self.assertEqual(time, '0:43:00')
def test_last_minute_of_overtime_nba(self):
time = _calc_overall_time(0, 1, 5, 'nba')
self.assertEqual(time, '0:52:00')
def test_beginning_of_triple_overtime_nba(self):
time = _calc_overall_time(0, 5, 7, 'nba')
self.assertEqual(time, '0:58:00')
def test_beginning_of_game_ncb(self):
time = _calc_overall_time(0, 20, 1, 'ncb')
self.assertEqual(time, '0:00:00')
def test_five_minutes_into_first_half_ncb(self):
time = _calc_overall_time(0, 15, 1, 'ncb')
self.assertEqual(time, '0:05:00')
def test_beginning_of_second_half_ncb(self):
time = _calc_overall_time(0, 20, 2, 'ncb')
self.assertEqual(time, '0:20:00')
def test_ten_minutes_into_second_half_ncb(self):
time = _calc_overall_time(0, 10, 2, 'ncb')
self.assertEqual(time, '0:30:00')
def test_beginning_of_overtime_ncb(self):
time = _calc_overall_time(0, 5, 3, 'ncb')
self.assertEqual(time, '0:40:00')
def test_last_minute_of_second_overtime_ncb(self):
time = _calc_overall_time(0, 1, 4, 'ncb')
self.assertEqual(time, '0:49:00')
def test_beginning_of_third_overtime_ncb(self):
time = _calc_overall_time(0, 5, 5, 'ncb')
self.assertEqual(time, '0:50:00')
class PlayAsDictTest(unittest.TestCase):
def test_start_of_game_play(self):
start_play = ['6:33', 'Start of Game']
self.assertEqual(_play_as_dict(start_play),
{'official_play': 'Start of Game', 'home_play': None,
'away_play':None})
def test_home_team_play(self):
home_play = ['3:44', ' ', '0-0', 'Test comes through!']
self.assertEqual(_play_as_dict(home_play),
{'official_play': None,
'home_play': 'Test comes through!',
'away_play': None})
def test_away_team_play(self):
away_play = ['2:44', 'Away test wins!', '1-0', ' ']
self.assertEqual(_play_as_dict(away_play),
{'official_play': None, 'home_play': None,
'away_play': 'Away test wins!'})
class AdjustGameTest(unittest.TestCase):
def setUp(self):
self.game = [['12:00', 'Start of Game'],
['6:00', 'Away team scores!', '2-0', ' '],
['0:01', ' ', '2-2', 'Home team scores!'],
['12:00', 'Start of new quarter']]
def test_away_play_equals_none(self):
plays = [['2:00', ' ', '0-0', 'Home team play!']]
adjusted_play = adjust_game(plays)[0]
self.assertEqual(adjusted_play['away_play'], None)
def test_home_play_equals_none(self):
plays = [['2:00', 'Away team play!', '0-0', ' ']]
adjusted_play = adjust_game(plays)[0]
self.assertEqual(adjusted_play['home_play'], None)
def test_last_play_has_home_score(self):
adjusted_game = adjust_game(self.game)
last_play = adjusted_game[-1]
self.assertTrue(last_play['home_score'])
self.assertEqual(last_play['home_score'], 2)
def test_last_play_has_away_score(self):
adjusted_game = adjust_game(self.game)
last_play = adjusted_game[-1]
self.assertTrue(last_play['away_score'])
self.assertEqual(last_play['away_score'], 2)
def test_last_play_quarter_equals_2(self):
adjusted_game = adjust_game(self.game)
last_play = adjusted_game[-1]
self.assertEqual(last_play['quarter'], 2)
def test_expected_output(self):
expected_output = [{'away_score': 0, 'quarter_time': '12:00',
'overall_time': '0:00:00', 'away_play': None,
'home_play': None,
'official_play': 'Start of Game',
'quarter': 1, 'home_score': 0},
{'away_score': 2, 'quarter_time': '6:00',
'overall_time': '0:06:00',
'away_play': 'Away team scores!',
'home_play': None, 'official_play': None,
'quarter': 1, 'home_score': 0},
{'away_score': 2, 'quarter_time': '0:01',
'overall_time': '0:11:59', 'away_play': None,
'home_play': 'Home team scores!',
'official_play': None,
'quarter': 1, 'home_score': 2},
{'away_score': 2, 'quarter_time': '12:00',
'overall_time': '0:12:00', 'away_play': None,
'home_play': None,
'official_play': 'Start of new quarter',
'quarter': 2, 'home_score': 2}]
self.assertEqual(adjust_game(self.game), expected_output)
if __name__ == '__main__':
unittest.main()