haystack
442 строки · 27.3 Кб
1import os
2from unittest.mock import Mock, patch
3from haystack.utils.auth import Secret
4
5import pytest
6from requests import Timeout, RequestException, HTTPError
7
8from haystack import Document
9from haystack.components.websearch.searchapi import SearchApiError, SearchApiWebSearch
10
11
12EXAMPLE_SEARCHAPI_RESPONSE = {
13"search_metadata": {
14"id": "search_Y16dWXw4JOrIwNjjvqoKNGlE",
15"status": "Success",
16"created_at": "2023-11-22T16:10:56Z",
17"request_time_taken": 1.98,
18"parsing_time_taken": 0.16,
19"total_time_taken": 2.15,
20"request_url": "https://www.google.com/search?q=Who+is+CEO+of+Microsoft%3F&oq=Who+is+CEO+of+Microsoft%3F&gl=us&hl=en&ie=UTF-8",
21"html_url": "https://www.searchapi.io/api/v1/searches/search_Y16dWXw4JOrIwNjjvqoKNGlE.html",
22"json_url": "https://www.searchapi.io/api/v1/searches/search_Y16dWXw4JOrIwNjjvqoKNGlE",
23},
24"search_parameters": {
25"engine": "google",
26"q": "Who is CEO of Microsoft?",
27"device": "desktop",
28"google_domain": "google.com",
29"hl": "en",
30"gl": "us",
31},
32"search_information": {
33"query_displayed": "Who is CEO of Microsoft?",
34"total_results": 429000000,
35"time_taken_displayed": 0.48,
36},
37"answer_box": {
38"type": "organic_result",
39"title": "Microsoft Corporation/CEO",
40"answer": "Satya Nadella",
41"answer_date": "Feb 4, 2014–",
42"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Satya+Nadella&stick=H4sIAAAAAAAAAONgVuLSz9U3KDQxqMjKesRoyi3w8sc9YSmdSWtOXmNU4-IKzsgvd80rySypFJLgYoOy-KR4uJC08Sxi5Q1OLKlMVPBLTEnNyUkEALvb1RBWAAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQzIcDKAB6BAgyEAE",
43"snippet": "Microsoft CEO Satya Nadella speaks during the OpenAI DevDay event on November 06, 2023 in San Francisco, California.",
44"date": "1 day ago",
45"organic_result": {
46"title": "Microsoft CEO Satya Nadella's response to the OpenAI board ...",
47"link": "https://fortune.com/2023/11/21/microsoft-ceo-satya-nadella-openai-ceo-sam-altman-move-fast-fix-things/#:~:text=Microsoft%20CEO%20Satya%20Nadella%20speaks,2023%20in%20San%20Francisco%2C%20California.",
48"source": "Fortune",
49"domain": "fortune.com",
50"displayed_link": "https://fortune.com › 2023/11/21 › microsoft-ceo-satya-...",
51},
52"people_also_search_for": [
53{
54"title": "Sundar Pichai",
55"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Sundar+Pichai&stick=H4sIAAAAAAAAAONgFuLQz9U3MCkuM1HiArEs01OKzU20-AJSi4rz84IzU1LLEyuLF7HyBpfmpSQWKQRkJmckZgIAJfaYezgAAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQxA16BAgnEAQ",
56},
57{
58"title": "Steve Ballmer",
59"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Steve+Ballmer&stick=H4sIAAAAAAAAAONgFuLQz9U3MCkuM1ECs8yTssu0-AJSi4rz84IzU1LLEyuLF7HyBpeklqUqOCXm5OSmFgEA31ogfDYAAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQxA16BAgnEAY",
60},
61{
62"title": "Anupama Nadella",
63"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Anupama+Nadella&stick=H4sIAAAAAAAAAONgFuLQz9U3MCkuM1Hi1U_XNzRMMjPMzTHMMtHiC0gtKs7PC85MSS1PrCxexMrvmFdakJibqOCXmJKak5MIAEx0yhM9AAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQxA16BAgnEAg",
64},
65{
66"title": "Zain Nadella",
67"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Zain+Nadella&stick=H4sIAAAAAAAAAONgFuLQz9U3MCkuM1Hi1U_XNzRMMjMyKCgsj9fiC0gtKs7PC85MSS1PrCxexMoTlZiZp-CXmJKak5MIANDRqOs6AAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQxA16BAgnEAo",
68},
69{
70"title": "Bill Gates",
71"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Bill+Gates&stick=H4sIAAAAAAAAAONgFuLQz9U3MCkuM1ECswzN80q0-AJSi4rz84IzU1LLEyuLF7FyOWXm5Ci4J5akFgMAF5_u-TMAAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQxA16BAgnEAw",
72},
73{
74"title": "Shantanu Narayen",
75"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Shantanu+Narayen&stick=H4sIAAAAAAAAAONgFuLQz9U3MCkuM1HiArGMzC0ts5O0-AJSi4rz84IzU1LLEyuLF7EKBGck5pUk5pUq-CUWJVam5gEA2xdRszsAAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQxA16BAgnEA4",
76},
77{
78"title": "Paul Allen",
79"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Paul+Allen&stick=H4sIAAAAAAAAAONgFuLQz9U3MCkuM1ECs0xLsnO1-AJSi4rz84IzU1LLEyuLF7FyBSSW5ig45uSk5gEA_4-yKDMAAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQxA16BAgnEBA",
80},
81],
82},
83"knowledge_graph": {
84"kgmid": "/m/0q40xjj",
85"knowledge_graph_type": "People",
86"title": "Satya Nadella",
87"type": "CEO of Microsoft",
88"description": "Satya Narayana Nadella is an Indian-American business executive. He is the executive chairman and CEO of Microsoft, succeeding Steve Ballmer in 2014 as CEO and John W. Thompson in 2021 as chairman.",
89"source": {"name": "Wikipedia", "link": "https://en.wikipedia.org/wiki/Satya_Nadella"},
90"born": "August 19, 1967 (age 56 years), Hyderabad, India",
91"born_links": [
92{
93"text": "Hyderabad, India",
94"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Hyderabad&si=ALGXSlZS0YT-iRe81F2cKC9lM9KWTK4y0m5Atx8g9YliNNw2meVELJr66A46Jmr2L7YaEMWXarsN12T-Vg9bXBeu7mCHCG-SpT-gWQmluIDs5SvdST1r6rBUhcAOclNosjy4RgkGlWnecyHsBen2Ttz-NbCqTmTwwPK9ro0lfOFPb0CUDvLAkTbBXx4xNX7WWUJ19n0EWeuA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAHoECGUQAg",
95}
96],
97"awards": "Padma Bhushan, CNN-IBN Indian of the Year Global Indian",
98"awards_links": [
99{
100"text": "Padma Bhushan",
101"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Padma+Bhushan&si=ALGXSlYh1-GEPndq7qMo--O-TPixQtNN4JMroSxgItz5kq0stCyOa5BGWGIYt20KbMd-zdQdvwREsU7qSkWcyv0yzHS195H46le5meMq90to5z-nIHo4evgG3koKwps5uC-gu8Huemxmq6P1usjVEj5YR9okGopoUaOxuuyZP-isnQAmC6otzjnjf1O9jMuQObZmAnl2HH7coBXCHbIx1QvAHw1KZOYyJKPnYhWaYgqfQo7yF5BOVVLXvtr_8FhnFIxxl7f_V2B6&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAHoECF8QAg",
102},
103{
104"text": "CNN-IBN Indian of the Year Global Indian",
105"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=CNN-IBN+Indian+of+the+Year+Global+Indian&si=ALGXSlZZLz93Q5j8HVkpXyxpTaoqXw8cocmoi-DFAGsSj5diF8YzT48GvLer52UWWyGCjf3yeWD9YQzPqUV-LEVPLmirdkrJ_7HPexciHWOKnyaMVi0vXdKPSwvc8pE4fD3qmgVyw7qAFoNmy-T-U6OlosYKKVbf9CZnaOonmPhLRRFHGEEmKVtb_0FdKkXeUE2RIDgUJ1n1LWZoTeporPHOj4JfKSJADc-hymzzDEb5-uW3KxQtTdv_GJNMOoleFxqH9cvObQvW0_NvpfHZcThW9b_9g1BXjLfozVqh6hjRTbb40p5vu5e9Oi4sNqxtACf4Xoys_QX5&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAXoECF8QAw",
106},
107],
108"nominations": "CNN-IBN Indian of the Year Global Indian",
109"nominations_links": [
110{
111"text": "CNN-IBN Indian of the Year Global Indian",
112"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=CNN-IBN+Indian+of+the+Year+Global+Indian&si=ALGXSlZZLz93Q5j8HVkpXyxpTaoqXw8cocmoi-DFAGsSj5diF8YzT48GvLer52UWWyGCjf3yeWD9YQzPqUV-LEVPLmirdkrJ_7HPexciHWOKnyaMVlh5LgokSYRM8a-Dib-kzfIaD6Uw_x_3lxo6j3NNKQbuBs4v4kkSCjL68joimLMo16eCX83PFrnvSsVKsgu6aFRoBYQt5p5NRofNfBXtVt2jzFVAWh23VsBHyyAxOuC2aQmgvKp-FGYymourIbHCdJ3rcx-Z&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAHoECGIQAg",
113}
114],
115"books": "Hit Refresh: The Quest to Rediscover Microsoft's Soul and Imagine a Better Future for Everyone",
116"books_links": [
117{
118"text": "Hit Refresh: The Quest to Rediscover Microsoft's Soul and Imagine a Better Future for Everyone",
119"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Hit+Refresh&si=ALGXSlZZLz93Q5j8HVkpXyxpTaoqXw8cocmoi-DFAGsSj5diFzM3kSV8cu0gYZuy4n6At7XJ8qKh8mnRaXfDbxUaZoS_kPW87tGFHpw6B9zAS2a52vwJDx-fkzytheyPXaMQENZSl3bwqC9Nz3bqn7-Pglqh0Bik5Ow9AdVr2XI8mdVktN4SkCIaPE4qQfjAurt8rjUVyQzu3OFQx04nfPH3Gv7vP8aKqg%3D%3D&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAHoECGEQAg",
120}
121],
122"children": "Zain Nadella, Divya Nadella, Tara Nadella",
123"children_links": [
124{
125"text": "Zain Nadella",
126"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Zain+Nadella&si=ALGXSlZZLz93Q5j8HVkpXyxpTaoqXw8cocmoi-DFAGsSj5diFxtguEvrMR1GmF2uy_-DLcVXwF5gosIuQudQPkad9bBUZxVKOG9PFdHXdEGQHrfXekG0E0x_raEKuDnHD6kk8_HfD3LZ57PWZ3Zyz0uhKPE15DfvA42IpAByWbms0fsgRw5IFCWwB5XMd3WM5U8KKsgeb_DmdoooQ_k3RrxO57jTcm5ZwgDlpBpGq0wj2Ksc2A65RQvA8NPJtpEqDcvEpJ4xWQ_tM_rHduCXRfsv9XFr84DzwA%3D%3D&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAHoECGQQAg",
127},
128{
129"text": "Divya Nadella",
130"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Divya+Nadella&si=ALGXSlZZLz93Q5j8HVkpXyxpTaoqXw8cocmoi-DFAGsSj5diFwYr_pFPi4_6apkHPz96V-E6wDawAGH_i6kZL7ZB-ETzV3LLESN1a8BgFguu3LOpz1qAQypmcVosQxCFWSJVexciDel34yrgWJmUu5bY2zzEmu1h95LQ35yUDkf6Mqcn-TiwyLu7OzGYkw6D9P4kNkS2D3gNPnRZb6vQJbqdayQg-wgn-LG2BmwR-RntneXFgSSZgotziGaY96UzeZ0zgRWYp6LAKlRqlTbeDeCbDDY2_VIWjQ%3D%3D&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAXoECGQQAw",
131},
132{
133"text": "Tara Nadella",
134"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Tara+Nadella&si=ALGXSlZZLz93Q5j8HVkpXyxpTaoqXw8cocmoi-DFAGsSj5diF465A_RPTnaELE1D-l5XgaKmBEpoAyayrOAdoXqBSLZ8Qu5UB1hBz6xLN4I1DdUSzqN0G0e9_8lfDbD_Qnx2uLJL_3XUNJ3gPrjCNvCyYeR9a9wkCnMBLchfUhVji9EHiobO4WgdWkxKd44YXHxfMBIYEek8OfbdUx9tplETPYtu7X1HRtGzqp8lXsQ6Vacj-aT7K6Xw0psbP4NXwHRQ71MYjLS-A5_VpSnitGScPsP-1m41Kg%3D%3D&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAnoECGQQBA",
135},
136],
137"education": "University of Wisconsin-Milwaukee (1990), MORE",
138"education_links": [
139{
140"text": "University of Wisconsin-Milwaukee",
141"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=University+of+Wisconsin+Milwaukee&si=ALGXSlYh1-GEPndq7qMo--O-TPixQtNN4JMroSxgItz5kq0stDerMl6ZWDVLeoc_9LMxC6poOvWKyPlaxlQHHC7l9sV2e_sYZ2w92bas10emnFKqvF8PcMhCIIHCiTbdtg6nHIA-ihu0l0dNJtl3ZXuRejodvwikfjAsz-cGgFCLkxoi_eMM95SSZ77VXB0gP7fPTA6q__pIRK7T6ZfiSyM2xTbDt3YUvrWFmx5LBSJwRd2K1f0DK6sGaIa3ozdQOGvGXZkTOTLEG_a2ssbGBTX4MyU4cHmLsvW-Gfpq-makl3esSS7fQTc%3D&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQmxMoAHoECGAQAg",
142},
143{
144"text": "MORE",
145"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=satya+nadella+education&stick=H4sIAAAAAAAAAOPgE-LSz9U3KDQxqMjK0pLOTrbSL0jNL8hJBVJFxfl5VqkppcmJJZn5eYtYxYsTSyoTFfISU1JzchIV4DIAcrWm-UUAAAA&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ44YBKAF6BAhgEAM",
146},
147],
148"full_name": "Satya Narayana Nadella",
149"profiles": [
150{"name": "LinkedIn", "link": "https://www.linkedin.com/in/satyanadella"},
151{"name": "Twitter", "link": "https://twitter.com/satyanadella"},
152],
153},
154"organic_results": [
155{
156"position": 1,
157"title": "Satya Nadella - Stories",
158"link": "https://news.microsoft.com/exec/satya-nadella/",
159"source": "Microsoft",
160"domain": "news.microsoft.com",
161"displayed_link": "https://news.microsoft.com › exec › satya-nadella",
162"snippet": "Satya Nadella is Chairman and Chief Executive Officer of Microsoft. Before being named CEO in February 2014, Nadella held leadership roles in both ...",
163"snippet_highlighted_words": ["Satya Nadella"],
164"cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:jTiZ69Cck7EJ:https://news.microsoft.com/exec/satya-nadella/&hl=en&gl=us",
165},
166{
167"position": 2,
168"title": "Satya Nadella",
169"link": "https://en.wikipedia.org/wiki/Satya_Nadella",
170"source": "Wikipedia",
171"domain": "en.wikipedia.org",
172"displayed_link": "https://en.wikipedia.org › wiki › Satya_Nadella",
173"snippet": "Satya Narayana Nadella is an Indian-American business executive. He is the executive chairman and CEO of Microsoft, succeeding Steve Ballmer in 2014 as CEO ...",
174"snippet_highlighted_words": ["Satya Narayana Nadella"],
175"sitelinks": {
176"inline": [
177{
178"title": "Manipal Institute of Technology",
179"link": "https://en.wikipedia.org/wiki/Manipal_Institute_of_Technology",
180},
181{
182"title": "University of Wisconsin",
183"link": "https://en.wikipedia.org/wiki/University_of_Wisconsin%E2%80%93Milwaukee",
184},
185{"title": "S. Somasegar", "link": "https://en.wikipedia.org/wiki/S._Somasegar"},
186]
187},
188"cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:Tgw93hG0PnoJ:https://en.wikipedia.org/wiki/Satya_Nadella&hl=en&gl=us",
189},
190{
191"position": 3,
192"title": "Satya Nadella",
193"link": "https://www.linkedin.com/in/satyanadella",
194"source": "LinkedIn · Satya Nadella",
195"domain": "www.linkedin.com",
196"displayed_link": "10.5M+ followers",
197"snippet": "As chairman and CEO of Microsoft, I define my mission and that of my company as empowering… | Learn more about Satya Nadella's work experience, education, ...",
198"snippet_highlighted_words": ["Satya Nadella's"],
199},
200{
201"position": 4,
202"title": "Who is Satya Nadella, Family, Salary, Education, Net Worth ...",
203"link": "https://www.business-standard.com/about/who-is-satya-nadella",
204"source": "Business Standard",
205"domain": "www.business-standard.com",
206"displayed_link": "https://www.business-standard.com › about › who-is-s...",
207"snippet": "Satya Narayana Nadella is the chief executive officer (CEO) of Microsoft. Under him, Microsoft has more cloud computing revenue than Google, more subscribers ...",
208"snippet_highlighted_words": ["Satya Narayana Nadella"],
209"cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:yQ0bmLSmP8gJ:https://www.business-standard.com/about/who-is-satya-nadella&hl=en&gl=us",
210},
211{
212"position": 5,
213"title": "Satya Nadella (@satyanadella) / X",
214"link": "https://twitter.com/satyanadella",
215"source": "Twitter · satyanadella",
216"domain": "twitter.com",
217"displayed_link": "3.1M+ followers",
218"snippet": "Chairman and CEO of Microsoft Corporation.",
219"snippet_highlighted_words": ["CEO of Microsoft"],
220"cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:dEJiGKzwLfkJ:https://twitter.com/satyanadella&hl=en&gl=us",
221},
222{
223"position": 6,
224"title": "Satya Nadella | Biography & Facts",
225"link": "https://www.britannica.com/biography/Satya-Nadella",
226"source": "Britannica",
227"domain": "www.britannica.com",
228"displayed_link": "https://www.britannica.com › biography › Satya-Nadella",
229"snippet": "Satya Nadella (born August 19, 1967, Hyderabad, India) Indian-born business executive who was CEO of the computer software company Microsoft (2014– ).",
230"snippet_highlighted_words": ["Satya Nadella"],
231"cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:a0S8ke4I9qgJ:https://www.britannica.com/biography/Satya-Nadella&hl=en&gl=us",
232},
233{
234"position": 7,
235"title": "Satya Nadella",
236"link": "https://www.forbes.com/profile/satya-nadella/",
237"source": "Forbes",
238"domain": "www.forbes.com",
239"displayed_link": "https://www.forbes.com › profile › satya-nadella",
240"snippet": "Satya Nadella replaced billionaire Steve Ballmer as Microsoft CEO in 2014. Prior to that, Nadella was Microsoft EVP of the cloud and enterprise group.",
241"snippet_highlighted_words": ["Satya Nadella"],
242"cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:q_CXTYNnHSMJ:https://www.forbes.com/profile/satya-nadella/&hl=en&gl=us",
243},
244{
245"position": 8,
246"title": "5 Facts You Didn't Know About Microsoft CEO Satya Nadella",
247"link": "https://in.benzinga.com/content/35911756/5-facts-you-didnt-know-about-microsoft-ceo-satya-nadella",
248"source": "Benzinga",
249"domain": "in.benzinga.com",
250"displayed_link": "https://in.benzinga.com › content › 5-facts-you-didnt-...",
251"snippet": "Satya Nadella's journey at Microsoft underscores the importance of diverse experiences in shaping effective and empathetic leadership in the ...",
252"snippet_highlighted_words": ["Satya Nadella's"],
253"date": "8 hours ago",
254"cached_page_link": "https://webcache.googleusercontent.com/search?q=cache:hCbtJUTgvEQJ:https://in.benzinga.com/content/35911756/5-facts-you-didnt-know-about-microsoft-ceo-satya-nadella&hl=en&gl=us",
255},
256{
257"position": 9,
258"title": "Microsoft CEO Satya Nadella: Q&A - The Wall Street Journal",
259"link": "https://www.wsj.com/video/microsoft-ceo-satya-nadella-qa/41D02815-935C-421D-8021-5E1BFD3DDE84",
260"source": "Wall Street Journal",
261"domain": "www.wsj.com",
262"displayed_link": "https://www.wsj.com › video › microsoft-ceo-satya-nadel...",
263"snippet": "Microsoft CEO Satya Nadella talks about his biggest accomplishment, how to make successful acquisitions and how the tech industry could improve its image ...",
264"snippet_highlighted_words": ["Microsoft CEO"],
265"video": {"source": "The Wall Street Journal", "channel": "The Wall Street Journal", "date": "Feb 1, 2019"},
266},
267],
268"related_questions": [
269{
270"question": "Who is the real CEO of Microsoft?",
271"answer": "Satya Nadella is Chairman and Chief Executive Officer of Microsoft.",
272"answer_highlight": "Satya Nadella",
273"source": {
274"title": "Satya Nadella - Stories - Microsoft News",
275"link": "https://news.microsoft.com/exec/satya-nadella/#:~:text=Satya%20Nadella%20is%20Chairman%20and%20Chief%20Executive%20Officer%20of%20Microsoft.",
276"source": "Microsoft",
277"domain": "news.microsoft.com",
278"displayed_link": "https://news.microsoft.com › exec › satya-nadella",
279},
280"search": {
281"title": "Search for: Who is the real CEO of Microsoft?",
282"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Who+is+the+real+CEO+of+Microsoft%3F&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQzmd6BAgeEAY",
283},
284},
285{
286"question": "Who is the CEO of Microsoft 2023?",
287"answer": "Microsoft Corp. chief executive officer Satya Nadella signaled that he'd be open to Sam Altman going back to OpenAI, rather than joining his company as part of a surprise move announced over the weekend.",
288"date": "2 days ago",
289"source": {
290"title": "Microsoft CEO Satya Nadella signals willingness to have Sam Altman ...",
291"link": "https://economictimes.indiatimes.com/tech/technology/microsoft-ceo-satya-nadella-signals-willingness-to-have-sam-altman-rejoin-openai/articleshow/105370026.cms#:~:text=Microsoft%20Corp.%20chief%20executive%20officer,move%20announced%20over%20the%20weekend.",
292"source": "indiatimes.com",
293"domain": "economictimes.indiatimes.com",
294"displayed_link": "https://economictimes.indiatimes.com › tech › articleshow",
295},
296"search": {
297"title": "Search for: Who is the CEO of Microsoft 2023?",
298"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Who+is+the+CEO+of+Microsoft+2023%3F&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQzmd6BAgcEAY",
299},
300},
301{
302"question": "How many degrees does Satya Nadella have?",
303"answer": "He earned a bachelor's degree in electrical engineering from Mangalore University, a master's degree in computer science from the University of Wisconsin – Milwaukee and a master's degree in business administration from the University of Chicago.",
304"source": {
305"title": "Satya Nadella - Institutional - BlackRock",
306"link": "https://www.blackrock.com/institutions/en-zz/biographies/satya-nadella#:~:text=He%20earned%20a%20bachelor's%20degree,from%20the%20University%20of%20Chicago.",
307"source": "blackrock.com",
308"domain": "www.blackrock.com",
309"displayed_link": "https://www.blackrock.com › en-zz › biographies › satya...",
310},
311"search": {
312"title": "Search for: How many degrees does Satya Nadella have?",
313"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=How+many+degrees+does+Satya+Nadella+have%3F&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQzmd6BAgdEAY",
314},
315},
316{
317"question": "How old is Satya Nadella?",
318"answer_highlight": "56 years (August 19, 1967)",
319"entity": {"subject": "Satya Nadella", "attribute": "Age", "value": "56 years (August 19, 1967)"},
320"search": {
321"title": "Search for: How old is Satya Nadella?",
322"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=How+old+is+Satya+Nadella%3F&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQzmd6BAgREAY",
323},
324},
325],
326"related_searches": [
327{
328"query": "Who is ceo of microsoft wife",
329"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Who+is+ceo+of+microsoft+wife&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ1QJ6BAhWEAE",
330},
331{
332"query": "Who is ceo of microsoft and microsoft",
333"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Who+is+ceo+of+microsoft+and+microsoft&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ1QJ6BAhVEAE",
334},
335{
336"query": "Who is ceo of microsoft wikipedia",
337"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Who+is+ceo+of+microsoft+wikipedia&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ1QJ6BAhUEAE",
338},
339{
340"query": "microsoft founder",
341"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Microsoft+founder&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ1QJ6BAhSEAE",
342},
343{
344"query": "Who is ceo of microsoft 2020",
345"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Who+is+ceo+of+microsoft+2020&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ1QJ6BAhTEAE",
346},
347{
348"query": "satya nadella net worth",
349"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=Satya+Nadella+net+worth&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ1QJ6BAhREAE",
350},
351{
352"query": "ceo of microsoft salary",
353"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=CEO+of+Microsoft+salary&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ1QJ6BAhQEAE",
354},
355{
356"query": "ceo of apple",
357"link": "https://www.google.com/search?sca_esv=584620230&gl=us&hl=en&q=CEO+of+Apple&sa=X&ved=2ahUKEwi89re3_9eCAxU4IUQIHfHeB6MQ1QJ6BAhXEAE",
358},
359],
360}
361
362
363@pytest.fixture
364def mock_searchapi_search_result():
365with patch("haystack.components.websearch.searchapi.requests.get") as mock_get:
366mock_get.return_value = Mock(status_code=200, json=lambda: EXAMPLE_SEARCHAPI_RESPONSE)
367yield mock_get
368
369
370class TestSearchApiSearchAPI:
371def test_init_fail_wo_api_key(self, monkeypatch):
372monkeypatch.delenv("SEARCHAPI_API_KEY", raising=False)
373with pytest.raises(ValueError, match="None of the .* environment variables are set"):
374SearchApiWebSearch()
375
376def test_to_dict(self, monkeypatch):
377monkeypatch.setenv("SEARCHAPI_API_KEY", "test-api-key")
378component = SearchApiWebSearch(
379top_k=10, allowed_domains=["testdomain.com"], search_params={"param": "test params"}
380)
381data = component.to_dict()
382assert data == {
383"type": "haystack.components.websearch.searchapi.SearchApiWebSearch",
384"init_parameters": {
385"api_key": {"env_vars": ["SEARCHAPI_API_KEY"], "strict": True, "type": "env_var"},
386"top_k": 10,
387"allowed_domains": ["testdomain.com"],
388"search_params": {"param": "test params"},
389},
390}
391
392@pytest.mark.parametrize("top_k", [1, 5, 7])
393def test_web_search_top_k(self, mock_searchapi_search_result, top_k: int):
394ws = SearchApiWebSearch(api_key=Secret.from_token("test-api-key"), top_k=top_k)
395results = ws.run(query="Who is CEO of Microsoft?")
396documents = results["documents"]
397links = results["links"]
398assert len(documents) == len(links) == top_k
399assert all(isinstance(doc, Document) for doc in documents)
400assert all(isinstance(link, str) for link in links)
401assert all(link.startswith("http") for link in links)
402
403@patch("requests.get")
404def test_timeout_error(self, mock_get):
405mock_get.side_effect = Timeout
406ws = SearchApiWebSearch(api_key=Secret.from_token("test-api-key"))
407
408with pytest.raises(TimeoutError):
409ws.run(query="Who is CEO of Microsoft?")
410
411@patch("requests.get")
412def test_request_exception(self, mock_get):
413mock_get.side_effect = RequestException
414ws = SearchApiWebSearch(api_key=Secret.from_token("test-api-key"))
415
416with pytest.raises(SearchApiError):
417ws.run(query="Who is CEO of Microsoft?")
418
419@patch("requests.get")
420def test_bad_response_code(self, mock_get):
421mock_response = mock_get.return_value
422mock_response.status_code = 404
423mock_response.raise_for_status.side_effect = HTTPError
424ws = SearchApiWebSearch(api_key=Secret.from_token("test-api-key"))
425
426with pytest.raises(SearchApiError):
427ws.run(query="Who is CEO of Microsoft?")
428
429@pytest.mark.skipif(
430not os.environ.get("SEARCHAPI_API_KEY", None),
431reason="Export an env var called SEARCHAPI_API_KEY containing the SearchApi API key to run this test.",
432)
433@pytest.mark.integration
434def test_web_search(self):
435ws = SearchApiWebSearch(top_k=10)
436results = ws.run(query="Who is CEO of Microsoft?")
437documents = results["documents"]
438links = results["links"]
439assert len(documents) == len(links) == 10
440assert all(isinstance(doc, Document) for doc in results)
441assert all(isinstance(link, str) for link in links)
442assert all(link.startswith("http") for link in links)
443