feat: News Search Component (#8190)

* Create news_search.py

* add tests

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
Edwin Jose 2025-05-23 15:36:31 -04:00 committed by GitHub
commit 9b5cfa58b5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 254 additions and 0 deletions

View file

@ -0,0 +1,88 @@
from unittest.mock import Mock, patch
import pytest
import requests
from langflow.components.data.news_search import NewsSearchComponent
from langflow.schema import DataFrame
from tests.base import ComponentTestBaseWithoutClient
class TestNewsSearchComponent(ComponentTestBaseWithoutClient):
@pytest.fixture
def component_class(self):
return NewsSearchComponent
@pytest.fixture
def default_kwargs(self):
return {"query": "OpenAI"}
@pytest.fixture
def file_names_mapping(self):
return []
def test_successful_news_search(self):
# Mock Google News RSS feed content
mock_rss_content = """
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<item>
<title>Test News 1</title>
<link>https://example.com/1</link>
<pubDate>2024-03-20</pubDate>
<description>Summary 1</description>
</item>
<item>
<title>Test News 2</title>
<link>https://example.com/2</link>
<pubDate>2024-03-21</pubDate>
<description>Summary 2</description>
</item>
</channel>
</rss>
"""
mock_response = Mock()
mock_response.content = mock_rss_content.encode("utf-8")
mock_response.raise_for_status = Mock()
with patch("requests.get", return_value=mock_response):
component = NewsSearchComponent(query="OpenAI")
result = component.search_news()
assert isinstance(result, DataFrame)
df = result
assert len(df) == 2
assert list(df.columns) == ["title", "link", "published", "summary"]
assert df.iloc[0]["title"] == "Test News 1"
assert df.iloc[1]["title"] == "Test News 2"
def test_news_search_error(self):
with patch("requests.get", side_effect=requests.RequestException("Network error")):
component = NewsSearchComponent(query="OpenAI")
result = component.search_news()
assert isinstance(result, DataFrame)
df = result
assert len(df) == 1
assert df.iloc[0]["title"] == "Error"
assert "Network error" in df.iloc[0]["summary"]
def test_empty_news_results(self):
# Mock empty RSS feed
mock_rss_content = """
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
</channel>
</rss>
"""
mock_response = Mock()
mock_response.content = mock_rss_content.encode("utf-8")
mock_response.raise_for_status = Mock()
with patch("requests.get", return_value=mock_response):
component = NewsSearchComponent(query="OpenAI")
result = component.search_news()
assert isinstance(result, DataFrame)
df = result
assert len(df) == 1
assert df.iloc[0]["title"] == "No articles found"