101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
"""
|
|
Test for the NewsAPI handler.
|
|
"""
|
|
|
|
import os
|
|
import unittest
|
|
import asyncio
|
|
from dotenv import load_dotenv
|
|
|
|
from execution.api_handlers.news_handler import NewsSearchHandler
|
|
from config.config import get_config
|
|
|
|
|
|
class TestNewsHandler(unittest.TestCase):
|
|
"""Test cases for the NewsAPI handler."""
|
|
|
|
def setUp(self):
|
|
"""Set up the test environment."""
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
# Initialize the handler
|
|
self.handler = NewsSearchHandler()
|
|
|
|
def test_handler_initialization(self):
|
|
"""Test that the handler initializes correctly."""
|
|
self.assertEqual(self.handler.get_name(), "news")
|
|
|
|
# Check if API key is available (this test may be skipped in CI environments)
|
|
if os.environ.get("NEWSAPI_API_KEY"):
|
|
self.assertTrue(self.handler.is_available())
|
|
|
|
# Check rate limit info
|
|
rate_limit_info = self.handler.get_rate_limit_info()
|
|
self.assertIn("requests_per_minute", rate_limit_info)
|
|
self.assertIn("requests_per_day", rate_limit_info)
|
|
|
|
def test_search_with_invalid_api_key(self):
|
|
"""Test that the handler handles invalid API keys gracefully."""
|
|
# Temporarily set the API key to an invalid value
|
|
original_api_key = self.handler.api_key
|
|
self.handler.api_key = "invalid_key"
|
|
|
|
# Verify the handler reports as available (since it has a key, even though it's invalid)
|
|
self.assertTrue(self.handler.is_available())
|
|
|
|
# Try to search with the invalid key
|
|
results = self.handler.search("test", num_results=1)
|
|
|
|
# Verify that we get an empty result set
|
|
self.assertEqual(len(results), 0)
|
|
|
|
# Restore the original API key
|
|
self.handler.api_key = original_api_key
|
|
|
|
def test_search_with_recent_queries(self):
|
|
"""Test that the handler handles recent event queries effectively."""
|
|
# Skip this test if no API key is available
|
|
if not self.handler.is_available():
|
|
self.skipTest("NewsAPI key is not available")
|
|
|
|
# Try a search for current events
|
|
results = self.handler.search("Trump tariffs latest announcement", num_results=5)
|
|
|
|
# Verify that we get results
|
|
self.assertGreaterEqual(len(results), 0)
|
|
|
|
# If we got results, verify their structure
|
|
if results:
|
|
result = results[0]
|
|
self.assertIn("title", result)
|
|
self.assertIn("url", result)
|
|
self.assertIn("snippet", result)
|
|
self.assertIn("source", result)
|
|
self.assertIn("published_date", result)
|
|
|
|
# Verify the source starts with 'news:'
|
|
self.assertTrue(result["source"].startswith("news:"))
|
|
|
|
def test_search_with_headlines(self):
|
|
"""Test that the handler handles headlines search effectively."""
|
|
# Skip this test if no API key is available
|
|
if not self.handler.is_available():
|
|
self.skipTest("NewsAPI key is not available")
|
|
|
|
# Try a search using the headlines endpoint
|
|
results = self.handler.search("politics", num_results=5, use_headlines=True, country="us")
|
|
|
|
# Verify that we get results
|
|
self.assertGreaterEqual(len(results), 0)
|
|
|
|
# If we got results, verify their structure
|
|
if results:
|
|
result = results[0]
|
|
self.assertIn("title", result)
|
|
self.assertIn("url", result)
|
|
self.assertIn("source", result)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main() |