def test_set_retries(self): """ Test set_retries """ con = HnApi() con.set_max_retries(12) self.assertEqual(con.max_retries, 12)
def test_is_dead_true(self): """ Test that a dead item is determined to be dead """ con = HnApi() item = con.get_item(8937830) self.assertTrue(con.is_dead_item(item))
def test_is_dead_false(self): """ Test that a non-dead item is determined to be not dead """ con = HnApi() item = con.get_item(2549) self.assertFalse(con.is_dead_item(item))
def test_set_timeout(self): """ Test set_timeout """ con = HnApi() con.set_timeout(4) self.assertEqual(con.timeout, 4)
def test_get_comment(self): """ Test retrieval of a comment """ con = HnApi() comment = con.get_item(15) byline = comment.get('by') self.assertEqual(byline, 'sama')
def test_get_item_by(self): """ Test item retrieval and 'by' field """ con = HnApi() item = con.get_item(8863) byline = item.get('by') self.assertEqual(byline, 'dhouston')
def test_get_poll_item(self): """ Test retrieval of 'poll' """ con = HnApi() item = con.get_item(7059569) self.assertTrue(con.is_valid_item(item)) self.assertEqual(item.get('type'), 'poll')
def test_get_max_item(self): """ Test retrieval of the max item without error """ con = HnApi() max_item_id = con.get_max_item() max_item = con.get_item(max_item_id) self.assertTrue(max_item.get('id') > 0)
def test_get_updates_users(self): """ Test retrieval of new users """ con = HnApi() updates = con.get_updates() self.assertTrue(len(updates.get('profiles')) > 1) user = con.get_user(updates.get('profiles')[0]) year_2001 = 1000000000 self.assertTrue(user.get('created') > year_2001)
def test_get_surrogate_item(self): """ Test retrieval of item that isn't really an item """ con = HnApi() item = con.get_item(8847790) self.assertTrue(con.is_valid_item(item)) byline = item.get('by') self.assertEqual(byline, '')
def test_get_updates_item(self): """ Test retrieval of new items """ con = HnApi() updates = con.get_updates() self.assertTrue(len(updates.get('items')) > 1) item = con.get_item(updates.get('items')[0]) year_2001 = 1000000000 self.assertTrue(item.get('time') > year_2001)
def get_hackernews_stories(): con = HnApi() stories = [] limit = 10000 for i in range(1,limit): print("parsing story: " + str(i) + " of " + str(limit)) try: item = con.get_item(i) title = item.get('title') stories.append(title) except: pass return stories
def test_bad_api_request(self): """ Test that api fails with appropriate error """ con = HnApi() self.assertRaises(NetworkError, \ con._request, "http://hacker-news.firebaseio.com/v0/foobar")
def test_request_new(self): """Test request of new stories""" con = HnApi() new = con.get_new() self.assertTrue(len(new) > 100) item_0 = con.get_item(new[0]) self.assertTrue(con.is_api_item(item_0)) item_last = con.get_item(new[-1]) self.assertTrue(con.is_api_item(item_last))
def test_get_top(self): """ Test retrieval of first and last items from /top endpoint """ con = HnApi() top = con.get_top() self.assertTrue(len(top) > 100) item_0 = con.get_item(top[0]) self.assertTrue(con.is_api_item(item_0)) item_100 = con.get_item(top[-1]) self.assertTrue(con.is_api_item(item_100))
def test_set_timeout_error(self): """ Test that set_timeout throws a RuntimeError """ con = HnApi() self.assertRaises(RuntimeError, con.set_timeout, -1)
def test_set_retries_error(self): """ Test set_retries throws RuntimeError """ con = HnApi() self.assertRaises(RuntimeError, con.set_max_retries, 0)
def test_request_retry(self): """Test that the retry occurs""" con = HnApi() self.assertRaises(NetworkError, \ con._request, 'https://hacker-news.firebaseio.com/v0/foobar/1.json')