コード例 #1
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"))
コード例 #2
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))
コード例 #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_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))
コード例 #5
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))
コード例 #6
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)