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__) + os.sep + "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_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 get_news_api_api(self, context): return NewsAPI(context.client.license_keys)
def test_init(self): newsapi = NewsAPI(self.license_keys) self.assertIsNotNone(newsapi) articles = newsapi.get_headlines(NewsAPI.BBC_NEWS) self.assertIsNotNone(articles)
import os from programr.utils.license.keys import LicenseKeys from programr.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_keys(self): self.license_keys = LicenseKeys() self.license_keys.load_license_key_file( os.path.dirname(__file__) + os.sep + "bad_test.keys") with self.assertRaises(Exception): newsapi = NewsAPI(self.license_keys)
def test_missing_license_keys(self): with self.assertRaises(Exception): newsapi = NewsAPI(None)