Пример #1
0
    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))
Пример #2
0
    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)
Пример #3
0
    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))
Пример #4
0
 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"))
Пример #5
0
    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)
Пример #6
0
    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
Пример #7
0
    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))
Пример #8
0
    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))
Пример #9
0
    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)
Пример #10
0
    def test_init(self):
        newsapi = NewsAPI(self.license_keys)
        self.assertIsNotNone(newsapi)

        articles = newsapi.get_headlines(NewsAPI.BBC_NEWS)
        self.assertIsNotNone(articles)
Пример #11
0
    def test_init(self):
        newsapi = NewsAPI(self.license_keys)
        self.assertIsNotNone(newsapi)

        articles = newsapi.get_headlines(NewsAPI.BBC_NEWS)
        self.assertIsNotNone(articles)
Пример #12
0
 def test_gaming(self):
     response = NewsAPI.gaming("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #13
0
 def test_science_and_nature(self):
     response = NewsAPI.science_and_nature("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #14
0
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
Пример #15
0
 def test_missing_license_keys(self):
     with self.assertRaises(Exception):
         _ = NewsAPI(None)
Пример #16
0
 def test_bbc_sport(self):
     response = NewsAPI.bbc_sport("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #17
0
 def test_associated_press(self):
     response = NewsAPI.associated_press("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #18
0
 def test_ars_technica(self):
     response = NewsAPI.ars_technica("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #19
0
 def test_al_jazeera_english(self):
     response = NewsAPI.al_jazeera_english("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #20
0
 def test_abc_news_au(self):
     response = NewsAPI.abc_news_au("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #21
0
 def test_uk_newspapers(self):
     response = NewsAPI.uk_newspapers("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #22
0
 def test_technology(self):
     response = NewsAPI.technology("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #23
0
 def test_entertainment(self):
     response = NewsAPI.entertainment("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #24
0
 def get_news_api_api(self, bot, clientid):
     return NewsAPI(bot.license_keys)
Пример #25
0
 def test_usa_today(self):
     response = NewsAPI.usa_today("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #26
0
 def test_buzzfeed(self):
     response = NewsAPI.buzzfeed("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #27
0
    def test_missing_keys(self):
        client = TestClient()

        with self.assertRaises(Exception):
            _ = NewsAPI(client.license_keys)
Пример #28
0
 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)
Пример #29
0
 def test_bloomberg(self):
     response = NewsAPI.bloomberg("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #30
0
 def test_music(self):
     response = NewsAPI.music("testservice", 10, False, False)
     self.assertIsNotNone(response)
Пример #31
0
 def get_news_api_api(self, context):
     return NewsAPI(context.client.license_keys)
Пример #32
0
 def test_business_insider_uk(self):
     response = NewsAPI.business_insider_uk("testservice", 10, False, False)
     self.assertIsNotNone(response)