Skip to content

Commit

Permalink
Expand Search test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvan2006 committed Jan 2, 2025
1 parent 4ded157 commit e3aae8a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
5 changes: 4 additions & 1 deletion doc/source/reference/examples/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
quotes = yf.Search("AAPL", max_results=10).quotes

# get list of news
news = yf.Search("Google", news_count=10).news
news = yf.Search("Google", news_count=10).news

# get list of related research
research = yf.Search("apple", include_research=True).research
25 changes: 18 additions & 7 deletions tests/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@


class TestSearch(unittest.TestCase):
def test_valid_query(self):
search = yf.Search(query="AAPL", max_results=5, news_count=3)

self.assertEqual(len(search.quotes), 5)
self.assertEqual(len(search.news), 3)
self.assertIn("AAPL", search.quotes[0]['symbol'])

def test_invalid_query(self):
search = yf.Search(query="XYZXYZ")

self.assertEqual(len(search.quotes), 0)
self.assertEqual(len(search.news), 0)
self.assertEqual(len(search.lists), 0)
self.assertEqual(len(search.nav), 0)
self.assertEqual(len(search.research), 0)

def test_empty_query(self):
search = yf.Search(query="")
Expand All @@ -29,3 +25,18 @@ def test_fuzzy_query(self):
# Check if the fuzzy search retrieves relevant results despite the typo
self.assertGreater(len(search.quotes), 0)
self.assertIn("AAPL", search.quotes[0]['symbol'])

def test_quotes(self):
search = yf.Search(query="AAPL", max_results=5)

self.assertEqual(len(search.quotes), 5)
self.assertIn("AAPL", search.quotes[0]['symbol'])

def test_news(self):
search = yf.Search(query="AAPL", news_count=3)

self.assertEqual(len(search.news), 3)

def test_research_reports(self):
search = yf.Search(query="AAPL", include_research=True)
self.assertEqual(len(search.research), 3)
17 changes: 9 additions & 8 deletions yfinance/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@


class Search:
def __init__(self, query, max_results=8, news_count=8, lists_count=8, include_cb=True, include_nav_links=False, include_research=False, include_cultural_assets=False, enable_fuzzy_query=False, recommended=8,
def __init__(self, query, max_results=8, news_count=8, lists_count=8, include_cb=True, include_nav_links=False,
include_research=False, include_cultural_assets=False, enable_fuzzy_query=False, recommended=8,
session=None, proxy=None, timeout=30, raise_errors=True):
"""
Fetches and organizes search results from Yahoo Finance, including stock quotes and news articles.
Expand Down Expand Up @@ -116,11 +117,11 @@ def search(self) -> 'Search':
self._research = data.get("researchReports", [])
self._nav = data.get("nav", [])

self._all = {"quotes": self._quotes, "news": self._news, "lists": self._lists, "research": self._research, "nav": self._nav}
self._all = {"quotes": self._quotes, "news": self._news, "lists": self._lists, "research": self._research,
"nav": self._nav}

return self


@property
def quotes(self) -> 'list':
"""Get the quotes from the search results."""
Expand All @@ -130,27 +131,27 @@ def quotes(self) -> 'list':
def news(self) -> 'list':
"""Get the news from the search results."""
return self._news

@property
def lists(self) -> 'list':
"""Get the lists from the search results."""
return self._lists

@property
def research(self) -> 'list':
"""Get the research reports from the search results."""
return self._research

@property
def nav(self) -> 'list':
"""Get the navigation links from the search results."""
return self._nav

@property
def all(self) -> 'dict[str,list]':
"""Get all the results from the search results: filtered down version of response."""
return self._all

@property
def response(self) -> 'dict':
"""Get the raw response from the search results."""
Expand Down

0 comments on commit e3aae8a

Please sign in to comment.