-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
259 lines (197 loc) · 8.74 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
import unittest
from datetime import datetime
from bs4 import BeautifulSoup
import json
from py_zap.constants import BASE_URL, DATE_FMT
import py_zap.utils as u
from py_zap.search import SearchDaily
from py_zap import Cable, Broadcast
class TestCableRatings(unittest.TestCase):
def setUp(self):
self.date = 'July 25 2017'
self.ratings = Cable(self.date)
def test_correct_fields(self):
"""Test Cable ratings object has correct arguments"""
self.assertEqual(self.date, self.date)
self.assertTrue(len(self.ratings) > 0)
def test_valid_entries(self):
"""Test Cable rating entries have valid fields"""
for entry in self.ratings:
self.assertTrue(entry.viewers > 0)
self.assertTrue(entry.rating > 0)
self.assertTrue(len(entry.show) > 0)
self.assertTrue(len(entry.net) > 0)
self.assertTrue(len(entry.time) > 0)
def test_valid_json(self):
"""Test Cable ratings produces valid JSON"""
self.assertTrue(json.loads(self.ratings.get_json()))
class TestBroadcastRatings(unittest.TestCase):
def setUp(self):
self.date = 'July 25 2017'
self.ratings = Broadcast(self.date)
def test_correct_fields(self):
"""Test Broadcast ratings object has correct arguments"""
self.assertEqual(self.date, self.date)
self.assertTrue(len(self.ratings) > 0)
def test_valid_entries(self):
"""Test Broadcast rating entries have valid fields"""
for entry in self.ratings:
self.assertTrue(entry.viewers > 0)
self.assertTrue(entry.rating > 0)
self.assertTrue(entry.share > 0)
self.assertTrue(len(entry.show) > 0)
self.assertTrue(len(entry.net) > 0)
self.assertTrue(len(entry.time) > 0)
def test_get_averages(self):
"""Test getting network averages for Broadcast ratings"""
networks = ['NBC', 'ABC', 'CBS', 'CW', 'FOX']
averages = self.ratings.get_averages()
self.assertTrue(isinstance(averages, dict))
self.assertTrue(len(averages) == 5)
for network in networks:
self.assertTrue(network in averages)
def test_valid_json(self):
"""Test Broadcast ratings produces valid JSON"""
self.assertTrue(json.loads(self.ratings.get_json()))
class TestInvalidDate(unittest.TestCase):
def test_page_not_found_error(self):
"""Test assertion of page not found error with invalid date"""
date = 'July 25 ' + str(datetime.today().year + 1)
self.assertRaises(u.PageNotFoundError, Broadcast, date)
class TestRatingsLimit(unittest.TestCase):
def setUp(self):
self.date = 'July 25 2017'
self.limit = 6
self.ratings = Broadcast(self.date, limit=self.limit)
def test_has_6_ratings(self):
"""Test that the ratings is only limited to 6 results"""
self.assertEqual(len(self.ratings), self.limit)
class TestBroadcastShowFilter(unittest.TestCase):
def setUp(self):
self.date = 'October 27 2016'
shows = ['Big Bang Theory', 'Supernatural']
self.ratings = Broadcast(self.date, show=shows)
def test_broadcast_show_filter(self):
"""Test that Broadcast shows are filtered in results"""
self.assertEqual(len(self.ratings), 2)
class TestCableShowFilter(unittest.TestCase):
def setUp(self):
self.date = 'May 15 2016'
shows = ['Game of Thrones', 'Silicon Valley', 'Kardashians']
self.ratings = Cable(self.date, show=shows)
def test_cable_show_filter(self):
"""Test that Cable shows are filtered in results"""
self.assertEqual(len(self.ratings), 3)
class TestBroadcastNetFilter(unittest.TestCase):
def setUp(self):
self.date = 'October 27 2016'
self.networks = ['CBS', 'ABC']
self.ratings = Broadcast(self.date, network=self.networks)
def test_broadcast_network_filter(self):
"""Test that Broadcast networks are filtered in results"""
for entry in self.ratings:
self.assertTrue(u.match_list(self.networks, entry.net))
class TestCableNetFilter(unittest.TestCase):
def setUp(self):
self.date = 'July 23 2017'
self.networks = ['HBO', 'Starz']
self.ratings = Cable(self.date, network=self.networks)
def test_cable_network_filter(self):
"""Test that Cable networks are filtered in results"""
for entry in self.ratings:
self.assertTrue(u.match_list(self.networks, entry.net))
class TestUtils(unittest.TestCase):
def test_convert_string(self):
"""Test that string returns string with specific characters"""
string = 'July-25/2017'
clean_string = u.convert_string(string, ['-', '/'])
self.assertEqual(clean_string, 'July 25 2017')
def test_convert_time(self):
"""Test that time string is converted to 24-hour time"""
time = '8:30 PM'
military_time = u.convert_time(time)
self.assertEqual(military_time, '20:30 PM')
def test_convert_month_shorten(self):
"""Test shortening a month name from a date string"""
short_month = u.convert_month('January 15')
self.assertEqual(short_month, 'jan 15')
def test_convert_month_lengthen(self):
"""Test lengthening a month name from a date string"""
long_month = u.convert_month('Jan 15', shorten=False)
self.assertEqual(long_month, 'january 15')
def test_convert_date(self):
"""Test string date converts to a datetime object"""
date = 'July 16 2017'
date_obj = u.convert_date(date)
self.assertTrue(isinstance(date_obj, datetime))
def test_convert_date_fail(self):
"""Test that wrong date format raises ValueError"""
date = '2017 July 16'
self.assertRaises(ValueError, u.convert_date, date)
def test_get_soup(self):
"""Test url converts to BeautifulSoup objects"""
url = 'http://tvbythenumbers.zap2it.com/daily-ratings/tv-ratings-tuesday-july-25-2017/'
soup = u.get_soup(url)
self.assertTrue(isinstance(soup, BeautifulSoup))
def test_get_day(self):
"""Test date objects return correct day string"""
date = 'July 25 2017'
date_obj = u.convert_date(date)
day = u.get_day(date_obj)
self.assertEqual(day, 'Tuesday')
def test_date_in_range(self):
"""Test two date strings are within specific range span"""
date1 = 'July 25 2017'
date2 = 'July 28 2017'
self.assertTrue(u.date_in_range(date1, date2, 5))
def test_date_not_in_range(self):
"""Test two date strings are not within specific range span"""
date1 = 'July 25 2017'
date2 = 'July 31 2017'
self.assertFalse(u.date_in_range(date1, date2, 5))
def test_inc_date(self):
"""Test date is incremented a number of days"""
date = 'July 25 2017'
date_obj = u.convert_date(date)
inc_date = u.inc_date(date_obj, 4, DATE_FMT)
self.assertEqual(inc_date, 'July 29 2017')
def test_match_words_success(self):
"""Test if a phrase successfully matches a word list"""
phrase = 'It\'s Always Sunny in Philadelphia'
query = 'always sunny philadelphia'
self.assertTrue(u.match_list(query, phrase))
def test_match_words_fail(self):
"""Test if a phrase unsuccessfully matches a word list"""
phrase = 'Survivor'
query = 'designated survivor'
self.assertFalse(u.match_list(query, phrase))
class TestSearchDaily(unittest.TestCase):
def test_build_url(self):
"""Test SearchDaily returns correct search url"""
category = 'Broadcast'
date = 'July 25 2017'
search = SearchDaily(category, date)
url = search.get_url()
expected_url = '/?s=Broadcast ratings+Tuesday&year=2017&monthnum=7&day&category=daily-ratings'
self.assertEqual(url, BASE_URL + expected_url)
def test_category_assertion(self):
"""Test invalid category raises assertion"""
category = 'None'
date = 'July 25 2017'
self.assertRaises(AssertionError, SearchDaily, category, date)
def test_result_found(self):
"""Test if successful result is found"""
category = 'Broadcast'
date = 'July 25 2017'
search = SearchDaily(category, date)
result = search.fetch_result()
self.assertTrue(isinstance(result, BeautifulSoup))
def test_result_not_found(self):
"""Test unsuccessful result returns PageNotFoundError"""
category = 'Broadcast'
year = datetime.today().year + 1
date = 'July 25 ' + str(year)
search = SearchDaily(category, date)
self.assertRaises(u.PageNotFoundError, search.fetch_result)
if __name__ == '__main__':
unittest.main()