def test_get_news_feed_articles_none_200_response(self): client = TestClient() client.add_license_keys_store() mock_api = MockNewsApiApi() newsapi = NewsAPI(client.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 401, {"content-type": "application/json"}, json.loads(""" { "articles": [ { "title": "test title", "description": "test description", "publishedAt": "test publishedAt", "author": "test author", "url": "test url", "urlToImage": "test urlToImage" } ] } """)) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles))
def test_abc_news_au(self): client = TestClient() client.add_license_keys_store() mock_api = MockNewsApiApi() newsapi = NewsAPI(client.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 200, {"content-type": "application/json"}, json.loads(""" { "articles": [ { "title": "test title", "description": "test description", "publishedAt": "test publishedAt", "author": "test author", "url": "test url", "urlToImage": "test urlToImage" } ] } """)) response = NewsAPI.abc_news_au("testservice", 10, False, False) self.assertIsNotNone(response)
def test_get_news_feed_articles_none_200_response(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + "/test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 401, {"content-type": "application/json"}, json.loads(""" { "articles": [ { "title": "test title", "description": "test description", "publishedAt": "test publishedAt", "author": "test author", "url": "test url", "urlToImage": "test urlToImage" } ] } """)) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles))
def test_format_url(self): self.assertEqual( "https://newsapi.org/v1/articles?source=testservice&sortBy=top&apiKey=key", NewsAPI._format_url("testservice", "key")) self.assertEqual( "https://newsapi.org/v1/articles?source=testservice&sortBy=bottom&apiKey=key", NewsAPI._format_url("testservice", "key", sort_by="bottom"))
def test_get_headlines(self): client = TestClient() client.add_license_keys_store() mock_api = MockNewsApiApi() mock_api.response = MockResponse( 200, {"content-type": "application/json"}, json.loads(""" { "articles": [ { "title": "test title", "description": "test description", "publishedAt": "test publishedAt", "author": "test author", "url": "test url", "urlToImage": "test urlToImage" } ] } """)) newsapi = NewsAPI(client.license_keys, mock_api) self.assertIsNotNone(newsapi) headlines = newsapi.get_headlines(NewsAPI.UK_NEWS) self.assertIsNotNone(headlines) headlines = newsapi.get_headlines("Other") self.assertIsNotNone(headlines) self.assertEquals([], headlines)
def execute(self, bot, clientid, data): splits = data.split() source = splits[0] max = 10 # int(splits[1]) sort = False # bool(splits[2]) reverse = False # bool(splits[3]) newsapi = NewsAPI(bot.license_keys) results = newsapi.to_program_y_text( newsapi.get_headlines(source, max, sort, reverse)) return results
def test_get_news_feed_articles_content_not_json(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file(os.path.dirname(__file__)+ os.sep + "test.keys") mock_api = MockNewsApiApi() newsapi = NewsAPI(self.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse(200, {"content-type": "application/xml"}, None) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles))
def test_get_news_feed_articles_content_not_json(self): client = TestClient() client.add_license_keys_store() mock_api = MockNewsApiApi() newsapi = NewsAPI(client.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse(200, {"content-type": "application/xml"}, None) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 10, True, False) self.assertIsNotNone(articles) self.assertEqual(0, len(articles))
def test_get_news_feed_articles_max_articles_99(self): client = TestClient() client.add_license_keys_store() mock_api = MockNewsApiApi() newsapi = NewsAPI(client.license_keys, mock_api) self.assertIsNotNone(newsapi) mock_api.response = MockResponse( 200, {"content-type": "application/json"}, json.loads(""" { "articles": [ { "title": "test title1", "description": "test description1", "publishedAt": "test publishedAt1", "author": "test author1", "url": "test url1", "urlToImage": "test urlToImage1" }, { "title": "test title2", "description": "test description2", "publishedAt": "test publishedAt2", "author": "test author2", "url": "test url2", "urlToImage": "test urlToImage2" } ] } """)) mock_url = NewsAPI._format_url("testservice", "key") articles = newsapi._get_news_feed_articles(mock_url, 99, True, False) self.assertIsNotNone(articles) self.assertEqual(2, len(articles)) self.assertEqual("test title1", articles[0].title) self.assertEqual("test description1", articles[0].description) self.assertEqual("test publishedAt1", articles[0].published_at) self.assertEqual("test author1", articles[0].author) self.assertEqual("test url1", articles[0].url) self.assertEqual("test urlToImage1", articles[0].url_to_image)
def test_init(self): newsapi = NewsAPI(self.license_keys) self.assertIsNotNone(newsapi) articles = newsapi.get_headlines(NewsAPI.BBC_NEWS) self.assertIsNotNone(articles)
def test_gaming(self): response = NewsAPI.gaming("testservice", 10, False, False) self.assertIsNotNone(response)
def test_science_and_nature(self): response = NewsAPI.science_and_nature("testservice", 10, False, False) self.assertIsNotNone(response)
import os from programy.utils.license.keys import LicenseKeys from programy.utils.newsapi.newsapi import NewsAPI if __name__ == '__main__': # Running these tools drops test files into the newapi test folder app_license_keys = LicenseKeys() app_license_keys.load_license_key_file(os.path.dirname(__file__) + '/../../../../bots/y-bot/config/license.keys') news_api = NewsAPI(app_license_keys) results = news_api.get_headlines(NewsAPI.BBC_NEWS) json_data = NewsAPI.to_json(results) NewsAPI.json_to_file('../../../test/utils/newsapi/newsapi.json', json_data) # Running these tools drops test files into the geocode test folder
def test_missing_license_keys(self): with self.assertRaises(Exception): _ = NewsAPI(None)
def test_bbc_sport(self): response = NewsAPI.bbc_sport("testservice", 10, False, False) self.assertIsNotNone(response)
def test_associated_press(self): response = NewsAPI.associated_press("testservice", 10, False, False) self.assertIsNotNone(response)
def test_ars_technica(self): response = NewsAPI.ars_technica("testservice", 10, False, False) self.assertIsNotNone(response)
def test_al_jazeera_english(self): response = NewsAPI.al_jazeera_english("testservice", 10, False, False) self.assertIsNotNone(response)
def test_abc_news_au(self): response = NewsAPI.abc_news_au("testservice", 10, False, False) self.assertIsNotNone(response)
def test_uk_newspapers(self): response = NewsAPI.uk_newspapers("testservice", 10, False, False) self.assertIsNotNone(response)
def test_technology(self): response = NewsAPI.technology("testservice", 10, False, False) self.assertIsNotNone(response)
def test_entertainment(self): response = NewsAPI.entertainment("testservice", 10, False, False) self.assertIsNotNone(response)
def get_news_api_api(self, bot, clientid): return NewsAPI(bot.license_keys)
def test_usa_today(self): response = NewsAPI.usa_today("testservice", 10, False, False) self.assertIsNotNone(response)
def test_buzzfeed(self): response = NewsAPI.buzzfeed("testservice", 10, False, False) self.assertIsNotNone(response)
def test_missing_keys(self): client = TestClient() with self.assertRaises(Exception): _ = NewsAPI(client.license_keys)
def test_missing_keys(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + "/bad_test.keys") with self.assertRaises(Exception): newsapi = NewsAPI(self.license_keys)
def test_bloomberg(self): response = NewsAPI.bloomberg("testservice", 10, False, False) self.assertIsNotNone(response)
def test_music(self): response = NewsAPI.music("testservice", 10, False, False) self.assertIsNotNone(response)
def get_news_api_api(self, context): return NewsAPI(context.client.license_keys)
def test_business_insider_uk(self): response = NewsAPI.business_insider_uk("testservice", 10, False, False) self.assertIsNotNone(response)