|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from lfx.components.perplexity.perplexity_search import PerplexitySearchComponent |
| 5 | +from lfx.schema import Data, DataFrame |
| 6 | + |
| 7 | +from tests.base import ComponentTestBaseWithoutClient |
| 8 | + |
| 9 | + |
| 10 | +class TestPerplexitySearchComponent(ComponentTestBaseWithoutClient): |
| 11 | + @pytest.fixture |
| 12 | + def component_class(self): |
| 13 | + return PerplexitySearchComponent |
| 14 | + |
| 15 | + @pytest.fixture |
| 16 | + def default_kwargs(self): |
| 17 | + return { |
| 18 | + "api_key": "test-key", |
| 19 | + "query": "what is langflow", |
| 20 | + "max_results": 3, |
| 21 | + } |
| 22 | + |
| 23 | + @pytest.fixture |
| 24 | + def file_names_mapping(self): |
| 25 | + return [] |
| 26 | + |
| 27 | + @pytest.fixture |
| 28 | + def fake_search_response(self): |
| 29 | + return { |
| 30 | + "results": [ |
| 31 | + { |
| 32 | + "title": "Result One", |
| 33 | + "url": "https://example.com/one", |
| 34 | + "snippet": "First snippet.", |
| 35 | + "date": "2025-01-01", |
| 36 | + "last_updated": "2025-01-02", |
| 37 | + }, |
| 38 | + { |
| 39 | + "title": "Result Two", |
| 40 | + "url": "https://example.com/two", |
| 41 | + "snippet": "Second snippet.", |
| 42 | + "date": "2025-02-01", |
| 43 | + "last_updated": "2025-02-02", |
| 44 | + }, |
| 45 | + ], |
| 46 | + "id": "abc", |
| 47 | + "server_time": "2025-03-01T00:00:00Z", |
| 48 | + } |
| 49 | + |
| 50 | + def test_component_initialization(self, component_class): |
| 51 | + component = component_class() |
| 52 | + frontend_node = component.to_frontend_node() |
| 53 | + node_data = frontend_node["data"]["node"] |
| 54 | + |
| 55 | + assert node_data["display_name"] == "Perplexity Search API" |
| 56 | + assert node_data["icon"] == "Perplexity" |
| 57 | + template = node_data["template"] |
| 58 | + for input_name in ("api_key", "query", "max_results", "search_recency_filter", "country", "search_mode"): |
| 59 | + assert input_name in template |
| 60 | + |
| 61 | + @patch("lfx.components.perplexity.perplexity_search.httpx.Client") |
| 62 | + def test_fetch_content_success(self, mock_client_cls, component_class, default_kwargs, fake_search_response): |
| 63 | + mock_response = MagicMock() |
| 64 | + mock_response.json.return_value = fake_search_response |
| 65 | + mock_response.raise_for_status.return_value = None |
| 66 | + mock_client = MagicMock() |
| 67 | + mock_client.post.return_value = mock_response |
| 68 | + mock_client_cls.return_value.__enter__.return_value = mock_client |
| 69 | + |
| 70 | + component = component_class(**default_kwargs) |
| 71 | + results = component.fetch_content() |
| 72 | + |
| 73 | + assert isinstance(results, list) |
| 74 | + assert len(results) == 2 |
| 75 | + assert isinstance(results[0], Data) |
| 76 | + assert results[0].data["title"] == "Result One" |
| 77 | + assert results[0].data["url"] == "https://example.com/one" |
| 78 | + assert results[0].text == "First snippet." |
| 79 | + |
| 80 | + # Verify the request was sent with the correct attribution header and payload. |
| 81 | + call_args = mock_client.post.call_args |
| 82 | + assert call_args.args[0] == "https://api.perplexity.ai/search" |
| 83 | + sent_headers = call_args.kwargs["headers"] |
| 84 | + assert sent_headers["Authorization"] == "Bearer test-key" |
| 85 | + assert sent_headers["Content-Type"] == "application/json" |
| 86 | + assert "X-Pplx-Integration" in sent_headers |
| 87 | + assert sent_headers["X-Pplx-Integration"].startswith("langflow/") |
| 88 | + sent_payload = call_args.kwargs["json"] |
| 89 | + assert sent_payload == {"query": "what is langflow", "max_results": 3} |
| 90 | + |
| 91 | + @patch("lfx.components.perplexity.perplexity_search.httpx.Client") |
| 92 | + def test_fetch_content_optional_filters(self, mock_client_cls, component_class, fake_search_response): |
| 93 | + mock_response = MagicMock() |
| 94 | + mock_response.json.return_value = fake_search_response |
| 95 | + mock_response.raise_for_status.return_value = None |
| 96 | + mock_client = MagicMock() |
| 97 | + mock_client.post.return_value = mock_response |
| 98 | + mock_client_cls.return_value.__enter__.return_value = mock_client |
| 99 | + |
| 100 | + component = component_class( |
| 101 | + api_key="test-key", |
| 102 | + query="langflow", |
| 103 | + max_results=10, |
| 104 | + search_recency_filter="week", |
| 105 | + country="US", |
| 106 | + search_mode="academic", |
| 107 | + ) |
| 108 | + component.fetch_content() |
| 109 | + |
| 110 | + sent_payload = mock_client.post.call_args.kwargs["json"] |
| 111 | + assert sent_payload["query"] == "langflow" |
| 112 | + assert sent_payload["max_results"] == 10 |
| 113 | + assert sent_payload["search_recency_filter"] == "week" |
| 114 | + assert sent_payload["country"] == "US" |
| 115 | + assert sent_payload["search_mode"] == "academic" |
| 116 | + |
| 117 | + @patch("lfx.components.perplexity.perplexity_search.httpx.Client") |
| 118 | + def test_max_results_clamped(self, mock_client_cls, component_class, fake_search_response): |
| 119 | + mock_response = MagicMock() |
| 120 | + mock_response.json.return_value = fake_search_response |
| 121 | + mock_response.raise_for_status.return_value = None |
| 122 | + mock_client = MagicMock() |
| 123 | + mock_client.post.return_value = mock_response |
| 124 | + mock_client_cls.return_value.__enter__.return_value = mock_client |
| 125 | + |
| 126 | + component = component_class(api_key="test-key", query="langflow", max_results=999) |
| 127 | + component.fetch_content() |
| 128 | + |
| 129 | + sent_payload = mock_client.post.call_args.kwargs["json"] |
| 130 | + assert sent_payload["max_results"] == 20 |
| 131 | + |
| 132 | + def test_missing_query_returns_error(self, component_class): |
| 133 | + component = component_class(api_key="test-key", query="") |
| 134 | + results = component.fetch_content() |
| 135 | + assert len(results) == 1 |
| 136 | + assert "error" in results[0].data |
| 137 | + |
| 138 | + @patch("lfx.components.perplexity.perplexity_search.httpx.Client") |
| 139 | + def test_fetch_content_dataframe(self, mock_client_cls, component_class, default_kwargs, fake_search_response): |
| 140 | + mock_response = MagicMock() |
| 141 | + mock_response.json.return_value = fake_search_response |
| 142 | + mock_response.raise_for_status.return_value = None |
| 143 | + mock_client = MagicMock() |
| 144 | + mock_client.post.return_value = mock_response |
| 145 | + mock_client_cls.return_value.__enter__.return_value = mock_client |
| 146 | + |
| 147 | + component = component_class(**default_kwargs) |
| 148 | + df = component.fetch_content_dataframe() |
| 149 | + assert isinstance(df, DataFrame) |
| 150 | + assert len(df) == 2 |
| 151 | + |
| 152 | + @pytest.mark.asyncio |
| 153 | + async def test_latest_version(self, component_class, default_kwargs): |
| 154 | + """Override to skip network call.""" |
| 155 | + component = component_class(**default_kwargs) |
| 156 | + assert component is not None |
0 commit comments