2021-11-27 15:11:42 +01:00
|
|
|
import logging
|
2021-11-23 19:57:37 +01:00
|
|
|
from pathlib import Path
|
2021-11-21 22:45:13 +01:00
|
|
|
from unittest import IsolatedAsyncioTestCase
|
2021-11-22 19:52:36 +01:00
|
|
|
from unittest.mock import patch, MagicMock, AsyncMock, call
|
|
|
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
from mwfin import functions
|
2021-11-26 21:38:10 +01:00
|
|
|
from mwfin.constants import HTML_PARSER, BASE_URL, FIN_STMT_URL_SUFFIX, IS, BS, CF, END_DATE
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-11-23 19:57:37 +01:00
|
|
|
|
|
|
|
THIS_DIR = Path(__file__).parent
|
2021-11-23 14:51:29 +01:00
|
|
|
|
2021-11-21 22:45:13 +01:00
|
|
|
|
|
|
|
class FunctionsTestCase(IsolatedAsyncioTestCase):
|
2021-11-23 19:57:37 +01:00
|
|
|
# boiled down & accurate structure of a relevant data table
|
|
|
|
# https://www.marketwatch.com/investing/stock/aapl/financials/cash-flow
|
|
|
|
# view page source @ line 2055
|
|
|
|
TEST_HTML_FILE_PATH = Path(THIS_DIR, 'test_structure.html')
|
|
|
|
|
2021-11-27 15:11:42 +01:00
|
|
|
log_lvl: int
|
2021-11-27 18:35:12 +01:00
|
|
|
test_html: str
|
2021-11-27 15:11:42 +01:00
|
|
|
|
2021-11-26 12:47:58 +01:00
|
|
|
@staticmethod
|
|
|
|
def get_mock_session(response_text: str = None) -> MagicMock:
|
|
|
|
mock_response = MagicMock()
|
|
|
|
mock_response.text = AsyncMock(return_value=response_text)
|
|
|
|
mock_get_return = MagicMock()
|
|
|
|
mock_get_return.__aenter__ = AsyncMock(return_value=mock_response)
|
|
|
|
mock_session_obj = MagicMock()
|
|
|
|
mock_session_obj.get = MagicMock(return_value=mock_get_return)
|
|
|
|
return mock_session_obj
|
|
|
|
|
2021-11-23 19:57:37 +01:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls) -> None:
|
|
|
|
with open(cls.TEST_HTML_FILE_PATH, 'r') as f:
|
2021-11-27 18:35:12 +01:00
|
|
|
cls.test_html = f.read()
|
|
|
|
cls.test_soup = BeautifulSoup(cls.test_html, HTML_PARSER)
|
2021-11-27 15:11:42 +01:00
|
|
|
cls.log_lvl = functions.log.level
|
|
|
|
functions.log.setLevel(logging.CRITICAL)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls) -> None:
|
|
|
|
functions.log.setLevel(cls.log_lvl)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-11-22 19:52:36 +01:00
|
|
|
@patch.object(functions, 'ClientSession')
|
|
|
|
async def test_soup_from_url(self, mock_session_cls):
|
|
|
|
test_html = '<b>foo</b>'
|
2021-11-26 12:47:58 +01:00
|
|
|
mock_session_cls.return_value = mock_session_obj = self.get_mock_session(test_html)
|
2021-11-22 19:52:36 +01:00
|
|
|
expected_output = BeautifulSoup(test_html, 'html.parser')
|
|
|
|
output = await functions.soup_from_url('baz', mock_session_obj)
|
|
|
|
self.assertEqual(expected_output, output)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
|
|
|
def test_extract_end_dates(self):
|
2021-11-23 14:51:29 +01:00
|
|
|
expected_output = ('End_Date_1', 'End_Date_2')
|
2021-11-23 19:57:37 +01:00
|
|
|
output = functions.extract_end_dates(self.test_soup)
|
2021-11-22 19:52:36 +01:00
|
|
|
self.assertTupleEqual(expected_output, output)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-11-24 22:28:33 +01:00
|
|
|
def test_is_relevant_table_row(self):
|
2021-11-27 15:11:42 +01:00
|
|
|
test_soup = BeautifulSoup('<tr><td><div> Cash & Short Term Investments </div></td></tr>', HTML_PARSER)
|
2021-11-24 22:28:33 +01:00
|
|
|
self.assertTrue(functions.is_relevant_table_row(test_soup.tr))
|
2021-11-27 15:11:42 +01:00
|
|
|
test_soup = BeautifulSoup('<tr><td><div> Cash & Short Term Investments Growth </div></td></tr>', HTML_PARSER)
|
|
|
|
self.assertFalse(functions.is_relevant_table_row(test_soup.tr))
|
|
|
|
test_soup = BeautifulSoup('<tr><td><div> baz </div></td></tr>', HTML_PARSER)
|
2021-11-24 22:28:33 +01:00
|
|
|
self.assertFalse(functions.is_relevant_table_row(test_soup.tr))
|
|
|
|
|
|
|
|
@patch.object(functions, 'is_relevant_table_row')
|
|
|
|
def test_find_relevant_table_rows(self, mock_is_relevant_table_row):
|
|
|
|
mock_is_relevant_table_row.return_value = True
|
2021-11-27 15:11:42 +01:00
|
|
|
expected_output = self.test_soup.find('div', attrs={'class': 'financials'}).tbody.find_all('tr')
|
2021-11-24 15:28:34 +01:00
|
|
|
output = functions.find_relevant_table_rows(self.test_soup)
|
|
|
|
self.assertListEqual(expected_output, output)
|
2021-11-27 15:11:42 +01:00
|
|
|
mock_is_relevant_table_row.assert_has_calls([call(expected_output[0]), call(expected_output[1])])
|
2021-11-21 22:45:13 +01:00
|
|
|
|
|
|
|
def test_extract_row_data(self):
|
2021-11-27 15:36:51 +01:00
|
|
|
test_row = self.test_soup.find('div', attrs={'class': 'financials'}).tbody.tr
|
2021-11-28 15:20:28 +01:00
|
|
|
expected_output = ('Cash & Short Term Investments', (11000000, -22000000))
|
2021-11-27 15:36:51 +01:00
|
|
|
output = functions.extract_row_data(test_row)
|
2021-11-24 15:28:34 +01:00
|
|
|
self.assertTupleEqual(expected_output, output)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-11-26 12:47:58 +01:00
|
|
|
@patch.object(functions, 'extract_row_data')
|
|
|
|
@patch.object(functions, 'find_relevant_table_rows')
|
|
|
|
@patch.object(functions, 'extract_end_dates')
|
|
|
|
def test_extract_all_data(self, mock_extract_end_dates, mock_find_relevant_table_rows, mock_extract_row_data):
|
|
|
|
test_end_dates = ('foo', 'bar')
|
|
|
|
mock_extract_end_dates.return_value = test_end_dates
|
|
|
|
test_relevant_rows = ['tr1', 'tr2']
|
|
|
|
mock_find_relevant_table_rows.return_value = test_relevant_rows
|
|
|
|
test_row_data = ('item_name', (123, 456))
|
|
|
|
mock_extract_row_data.return_value = test_row_data
|
|
|
|
expected_output = {
|
2021-11-26 21:38:10 +01:00
|
|
|
END_DATE: test_end_dates,
|
2021-11-26 12:47:58 +01:00
|
|
|
test_row_data[0]: test_row_data[1],
|
|
|
|
test_row_data[0]: test_row_data[1],
|
|
|
|
}
|
|
|
|
output = functions.extract_all_data(self.test_soup)
|
2021-11-24 15:28:34 +01:00
|
|
|
self.assertDictEqual(expected_output, output)
|
2021-11-26 12:47:58 +01:00
|
|
|
mock_extract_end_dates.assert_called_once_with(self.test_soup)
|
|
|
|
mock_find_relevant_table_rows.assert_called_once_with(self.test_soup)
|
|
|
|
mock_extract_row_data.assert_has_calls([call(test_relevant_rows[0]), call(test_relevant_rows[1])])
|
|
|
|
|
|
|
|
@patch.object(functions, 'extract_all_data')
|
|
|
|
@patch.object(functions, 'soup_from_url')
|
|
|
|
async def test__get_financial_statement(self, mock_soup_from_url, mock_extract_all_data):
|
|
|
|
mock_session = MagicMock()
|
2021-11-26 21:38:10 +01:00
|
|
|
test_ticker, statement = 'bar', BS
|
2021-11-27 18:11:16 +01:00
|
|
|
test_url = f'{BASE_URL}/{test_ticker}/financials{FIN_STMT_URL_SUFFIX[statement]}'
|
2021-11-26 12:47:58 +01:00
|
|
|
mock_soup_from_url.return_value = mock_soup = MagicMock()
|
2021-11-26 21:38:10 +01:00
|
|
|
mock_extract_all_data.return_value = expected_output = {'foo': 'bar'}
|
2021-11-26 12:47:58 +01:00
|
|
|
|
2021-11-26 21:38:10 +01:00
|
|
|
quarterly = False
|
|
|
|
output = await functions._get_financial_statement(statement, test_ticker, quarterly, mock_session)
|
2021-11-26 12:47:58 +01:00
|
|
|
self.assertDictEqual(expected_output, output)
|
|
|
|
mock_soup_from_url.assert_called_once_with(test_url, mock_session)
|
|
|
|
mock_extract_all_data.assert_called_once_with(mock_soup)
|
|
|
|
mock_soup_from_url.reset_mock()
|
|
|
|
mock_extract_all_data.reset_mock()
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-11-26 12:47:58 +01:00
|
|
|
quarterly = True
|
2021-11-26 21:38:10 +01:00
|
|
|
output = await functions._get_financial_statement(statement, test_ticker, quarterly, mock_session)
|
2021-11-26 12:47:58 +01:00
|
|
|
self.assertDictEqual(expected_output, output)
|
2021-11-26 21:38:10 +01:00
|
|
|
mock_soup_from_url.assert_called_once_with(test_url + '/quarter', mock_session)
|
|
|
|
mock_extract_all_data.assert_called_once_with(mock_soup)
|
2021-11-26 12:47:58 +01:00
|
|
|
|
2021-11-28 16:52:16 +01:00
|
|
|
async def _helper_test_get_any_statement(self, statement: str, mock__get_financial_statement):
|
2021-11-26 21:38:10 +01:00
|
|
|
symbol, quarterly, mock_session = 'foo', False, MagicMock()
|
2021-11-26 12:47:58 +01:00
|
|
|
mock__get_financial_statement.return_value = expected_output = 'bar'
|
2021-11-28 16:52:16 +01:00
|
|
|
if statement == BS:
|
|
|
|
function = functions.get_balance_sheet
|
|
|
|
elif statement == IS:
|
|
|
|
function = functions.get_income_statement
|
|
|
|
elif statement == CF:
|
|
|
|
function = functions.get_cash_flow_statement
|
|
|
|
else:
|
|
|
|
raise ValueError
|
2021-11-28 16:58:21 +01:00
|
|
|
output = await function(symbol, quarterly=quarterly, session=mock_session)
|
2021-11-26 12:47:58 +01:00
|
|
|
self.assertEqual(expected_output, output)
|
2021-11-28 16:52:16 +01:00
|
|
|
mock__get_financial_statement.assert_called_once_with(statement, symbol, quarterly, mock_session)
|
2021-11-28 16:58:21 +01:00
|
|
|
mock__get_financial_statement.reset_mock()
|
2021-11-28 17:07:23 +01:00
|
|
|
|
2021-11-28 16:58:21 +01:00
|
|
|
symbol1, symbol2 = 'x', 'y'
|
|
|
|
expected_output = {symbol1: expected_output, symbol2: expected_output}
|
|
|
|
output = await function(symbol1, symbol2, quarterly=quarterly, session=mock_session)
|
|
|
|
self.assertDictEqual(expected_output, output)
|
|
|
|
mock__get_financial_statement.assert_has_calls([
|
|
|
|
call(statement, symbol1, quarterly, mock_session),
|
|
|
|
call(statement, symbol2, quarterly, mock_session),
|
|
|
|
])
|
2021-11-28 16:52:16 +01:00
|
|
|
|
|
|
|
@patch.object(functions, '_get_financial_statement')
|
|
|
|
async def test_get_balance_sheet(self, mock__get_financial_statement):
|
|
|
|
await self._helper_test_get_any_statement(BS, mock__get_financial_statement)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-11-26 12:47:58 +01:00
|
|
|
@patch.object(functions, '_get_financial_statement')
|
|
|
|
async def test_get_income_statement(self, mock__get_financial_statement):
|
2021-11-28 16:52:16 +01:00
|
|
|
await self._helper_test_get_any_statement(IS, mock__get_financial_statement)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-11-26 12:47:58 +01:00
|
|
|
@patch.object(functions, '_get_financial_statement')
|
|
|
|
async def test_get_cash_flow_statement(self, mock__get_financial_statement):
|
2021-11-28 16:52:16 +01:00
|
|
|
await self._helper_test_get_any_statement(CF, mock__get_financial_statement)
|
2021-11-26 12:47:58 +01:00
|
|
|
|
|
|
|
@patch.object(functions, 'get_cash_flow_statement')
|
|
|
|
@patch.object(functions, 'get_income_statement')
|
|
|
|
@patch.object(functions, 'get_balance_sheet')
|
|
|
|
async def test_get_company_financials(self, mock_get_bs, mock_get_is, mock_get_cf):
|
2021-11-26 21:38:10 +01:00
|
|
|
mock_end_dates = ('bar', 'baz')
|
|
|
|
mock_get_bs.return_value = {END_DATE: mock_end_dates, 'a': (1, 2)}
|
|
|
|
mock_get_is.return_value = {END_DATE: mock_end_dates, 'b': (2, 3)}
|
|
|
|
mock_get_cf.return_value = {END_DATE: mock_end_dates, 'c': (3, 4)}
|
|
|
|
expected_output = {
|
2021-11-28 15:20:28 +01:00
|
|
|
BS: {END_DATE: mock_end_dates, 'a': (1, 2)},
|
|
|
|
IS: {END_DATE: mock_end_dates, 'b': (2, 3)},
|
|
|
|
CF: {END_DATE: mock_end_dates, 'c': (3, 4)}
|
2021-11-26 21:38:10 +01:00
|
|
|
}
|
|
|
|
symbol, quarterly, mock_session = 'foo', False, MagicMock()
|
2021-11-28 16:39:53 +01:00
|
|
|
output = await functions.get_company_financials(symbol, quarterly=quarterly, session=mock_session)
|
2021-11-26 21:38:10 +01:00
|
|
|
self.assertDictEqual(expected_output, output)
|
2021-11-28 17:07:23 +01:00
|
|
|
mock_get_bs.assert_called_once_with(symbol, quarterly=quarterly, session=mock_session)
|
|
|
|
mock_get_is.assert_called_once_with(symbol, quarterly=quarterly, session=mock_session)
|
|
|
|
mock_get_cf.assert_called_once_with(symbol, quarterly=quarterly, session=mock_session)
|
2021-11-28 16:39:53 +01:00
|
|
|
mock_get_bs.reset_mock()
|
|
|
|
mock_get_is.reset_mock()
|
|
|
|
mock_get_cf.reset_mock()
|
|
|
|
|
|
|
|
test_symbol1, test_symbol2 = 'x', 'y'
|
|
|
|
expected_output = {test_symbol1: expected_output, test_symbol2: expected_output}
|
|
|
|
output = await functions.get_company_financials(test_symbol1, test_symbol2,
|
|
|
|
quarterly=quarterly, session=mock_session)
|
|
|
|
self.assertDictEqual(expected_output, output)
|
|
|
|
mock_get_bs.assert_has_calls([
|
2021-11-28 17:07:23 +01:00
|
|
|
call(test_symbol1, quarterly=quarterly, session=mock_session),
|
|
|
|
call(test_symbol2, quarterly=quarterly, session=mock_session)
|
2021-11-28 16:39:53 +01:00
|
|
|
])
|
|
|
|
mock_get_is.assert_has_calls([
|
2021-11-28 17:07:23 +01:00
|
|
|
call(test_symbol1, quarterly=quarterly, session=mock_session),
|
|
|
|
call(test_symbol2, quarterly=quarterly, session=mock_session)
|
2021-11-28 16:39:53 +01:00
|
|
|
])
|
|
|
|
mock_get_cf.assert_has_calls([
|
2021-11-28 17:07:23 +01:00
|
|
|
call(test_symbol1, quarterly=quarterly, session=mock_session),
|
|
|
|
call(test_symbol2, quarterly=quarterly, session=mock_session)
|
2021-11-28 16:39:53 +01:00
|
|
|
])
|
2021-11-27 18:35:12 +01:00
|
|
|
|
|
|
|
@patch.object(functions, 'ClientSession')
|
|
|
|
async def test_integration_get_company_financials(self, mock_session_cls):
|
|
|
|
mock_session_cls.return_value = mock_session_obj = self.get_mock_session(self.test_html)
|
|
|
|
symbol = 'foo'
|
2021-11-28 15:20:28 +01:00
|
|
|
# Since the web request is mocked we always receive the same HTML markup.
|
2021-11-27 18:35:12 +01:00
|
|
|
expected_output = {
|
2021-11-28 15:20:28 +01:00
|
|
|
BS: {END_DATE: ('End_Date_1', 'End_Date_2'), 'Cash & Short Term Investments': (11000000, -22000000)},
|
|
|
|
IS: {END_DATE: ('End_Date_1', 'End_Date_2'), 'Cash & Short Term Investments': (11000000, -22000000)},
|
|
|
|
CF: {END_DATE: ('End_Date_1', 'End_Date_2'), 'Cash & Short Term Investments': (11000000, -22000000)}
|
2021-11-27 18:35:12 +01:00
|
|
|
}
|
|
|
|
output = await functions.get_company_financials(symbol, session=mock_session_obj)
|
|
|
|
self.assertDictEqual(expected_output, output)
|
|
|
|
mock_session_obj.get.assert_has_calls([
|
|
|
|
call(f'{BASE_URL}/{symbol}/financials{FIN_STMT_URL_SUFFIX[BS]}'),
|
|
|
|
call(f'{BASE_URL}/{symbol}/financials{FIN_STMT_URL_SUFFIX[IS]}'),
|
|
|
|
call(f'{BASE_URL}/{symbol}/financials{FIN_STMT_URL_SUFFIX[CF]}'),
|
|
|
|
])
|