Adding a unit test for the Seeking Alpha earnings calendar model

This commit is contained in:
Artem Veremey
2021-03-30 11:31:06 -07:00
parent b6a776d455
commit c3372ac073
3 changed files with 1611 additions and 9 deletions

View File

@@ -9,6 +9,26 @@ from pandas.core.frame import DataFrame
from gamestonk_terminal.helper_funcs import get_user_agent
def get_earnings_html(url_next_earnings: str) -> str:
"""Wraps HTTP requests.get for testibility
Parameters
----------
url_next_earnings : str
Next earnings URL
Returns
-------
str
HTML page of next earnings
"""
earnings_html = requests.get(
url_next_earnings, headers={"User-Agent": get_user_agent()}
).text
return earnings_html
def get_next_earnings(pages: int) -> DataFrame:
"""Returns a DataFrame with upcoming earnings
@@ -24,17 +44,11 @@ def get_next_earnings(pages: int) -> DataFrame:
"""
earnings = list()
url_next_earnings = "https://seekingalpha.com/earnings/earnings-calendar"
for idx in range(0, pages):
if idx == 0:
url_next_earnings = "https://seekingalpha.com/earnings/earnings-calendar"
else:
url_next_earnings = (
f"https://seekingalpha.com/earnings/earnings-calendar/{idx+1}"
)
text_soup_earnings = BeautifulSoup(
requests.get(
url_next_earnings, headers={"User-Agent": get_user_agent()}
).text,
get_earnings_html(url_next_earnings),
"lxml",
)
@@ -44,6 +58,10 @@ def get_next_earnings(pages: int) -> DataFrame:
stocks.append(a_stock.text)
earnings.append(stocks)
url_next_earnings = (
f"https://seekingalpha.com/earnings/earnings-calendar/{idx+1}"
)
df_earnings = pd.DataFrame(earnings, columns=["Ticker", "Name", "Date"])
df_earnings["Date"] = pd.to_datetime(df_earnings["Date"])
df_earnings = df_earnings.set_index("Date")

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,31 @@
""" discovery/seeking_alpha_model.py tests """
import unittest
from unittest import mock
# pylint: disable=unused-import
from gamestonk_terminal.test_helper import ( # noqa: F401
parameterize_from_file,
pytest_generate_tests,
)
from gamestonk_terminal.discovery.seeking_alpha_model import get_next_earnings
assertions = unittest.TestCase("__init__")
class TestDiscoverySeekingAlphaModel:
@mock.patch("gamestonk_terminal.discovery.seeking_alpha_model.get_earnings_html")
@parameterize_from_file(
"test_get_next_earnings",
"../tests/data/discovery_seeking_alpha_model.yaml",
)
# pylint: disable=unused-argument
def test_get_next_earnings(
self, mock_get_earnings_html, pages, mock_next_earnings_rets, expected_result
):
mock_get_earnings_html.side_effect = mock_next_earnings_rets
ret = get_next_earnings(pages)
assertions.assertEqual(
ret.to_csv().replace("\r\n", "\n"), expected_result.replace("\r\n", "\n")
)