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
|
|
|
|
2021-12-26 18:40:29 +01:00
|
|
|
def test_get_row_indent(self):
|
|
|
|
trs = self.test_soup.find_all('tr')
|
|
|
|
expected_output = 0
|
|
|
|
output = functions.get_row_indent(trs[0])
|
|
|
|
self.assertEqual(expected_output, output)
|
|
|
|
for i, tr in enumerate(trs[1:], start=1):
|
|
|
|
output = functions.get_row_indent(tr)
|
|
|
|
self.assertEqual(i, output)
|
|
|
|
|
|
|
|
@patch.object(functions, 'get_row_indent')
|
|
|
|
def test_extract_end_dates(self, mock_get_row_indent):
|
|
|
|
mock_get_row_indent.return_value = 0
|
|
|
|
expected_output = (0, '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-12-26 18:40:29 +01:00
|
|
|
mock_get_row_indent.assert_called_once_with(self.test_soup.tr)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-12-26 17:34:59 +01:00
|
|
|
def test_get_all_table_rows(self):
|
2021-11-27 15:11:42 +01:00
|
|
|
expected_output = self.test_soup.find('div', attrs={'class': 'financials'}).tbody.find_all('tr')
|
2021-12-26 17:34:59 +01:00
|
|
|
output = functions.get_all_table_rows(self.test_soup)
|
|
|
|
self.assertSequenceEqual(expected_output, output)
|
2021-12-03 16:20:04 +01:00
|
|
|
|
2021-12-26 18:40:29 +01:00
|
|
|
@patch.object(functions, 'get_row_indent')
|
|
|
|
def test_extract_row_data(self, mock_get_row_indent):
|
|
|
|
mock_get_row_indent.return_value = 1
|
2021-11-27 15:36:51 +01:00
|
|
|
test_row = self.test_soup.find('div', attrs={'class': 'financials'}).tbody.tr
|
2021-12-26 18:08:25 +01:00
|
|
|
expected_output = ('foo', (1, 1., -2.))
|
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-12-26 18:40:29 +01:00
|
|
|
mock_get_row_indent.assert_called_once_with(test_row)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-11-26 12:47:58 +01:00
|
|
|
@patch.object(functions, 'extract_row_data')
|
2021-12-26 17:55:34 +01:00
|
|
|
@patch.object(functions, 'get_all_table_rows')
|
2021-11-26 12:47:58 +01:00
|
|
|
@patch.object(functions, 'extract_end_dates')
|
2021-12-26 17:55:34 +01:00
|
|
|
def test_extract_all_data(self, mock_extract_end_dates, mock_get_all_table_rows, mock_extract_row_data):
|
2021-11-26 12:47:58 +01:00
|
|
|
test_end_dates = ('foo', 'bar')
|
|
|
|
mock_extract_end_dates.return_value = test_end_dates
|
|
|
|
test_relevant_rows = ['tr1', 'tr2']
|
2021-12-26 17:55:34 +01:00
|
|
|
mock_get_all_table_rows.return_value = test_relevant_rows
|
2021-11-26 12:47:58 +01:00
|
|
|
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)
|
2021-12-26 17:55:34 +01:00
|
|
|
mock_get_all_table_rows.assert_called_once_with(self.test_soup)
|
2021-11-26 12:47:58 +01:00
|
|
|
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')
|
2021-12-03 14:17:49 +01:00
|
|
|
async def test__get_single_company_fin_stmt(self, mock_soup_from_url, mock_extract_all_data):
|
2021-11-26 12:47:58 +01:00
|
|
|
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
|
2021-12-03 14:17:49 +01:00
|
|
|
output = await functions._get_single_company_fin_stmt(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-12-03 14:17:49 +01:00
|
|
|
output = await functions._get_single_company_fin_stmt(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-12-03 14:17:49 +01:00
|
|
|
@patch.object(functions, '_get_single_company_fin_stmt')
|
|
|
|
async def test__get_multi_companies_fin_stmt(self, mock__get_single_company_fin_stmt):
|
2021-12-03 15:19:21 +01:00
|
|
|
statement, sym1, sym2, quarterly, mock_session = 'xyz', 'foo', 'bar', False, MagicMock()
|
2021-12-03 14:17:49 +01:00
|
|
|
mock__get_single_company_fin_stmt.return_value = expected_output = 'baz'
|
2021-12-03 15:19:21 +01:00
|
|
|
output = await functions._get_multi_companies_fin_stmt(statement, sym1,
|
2021-12-03 14:17:49 +01:00
|
|
|
quarterly=quarterly, session=mock_session)
|
|
|
|
self.assertEqual(expected_output, output)
|
2021-12-03 15:19:21 +01:00
|
|
|
mock__get_single_company_fin_stmt.assert_called_once_with(statement, sym1, quarterly, mock_session)
|
2021-12-03 14:17:49 +01:00
|
|
|
mock__get_single_company_fin_stmt.reset_mock()
|
|
|
|
|
2021-12-03 15:19:21 +01:00
|
|
|
expected_output = {sym1: expected_output, sym2: expected_output}
|
|
|
|
output = await functions._get_multi_companies_fin_stmt(statement, sym1, sym2,
|
2021-12-03 14:17:49 +01:00
|
|
|
quarterly=quarterly, session=mock_session)
|
|
|
|
self.assertDictEqual(expected_output, output)
|
|
|
|
mock__get_single_company_fin_stmt.assert_has_calls([
|
2021-12-03 15:19:21 +01:00
|
|
|
call(statement, sym1, quarterly, mock_session),
|
|
|
|
call(statement, sym2, quarterly, mock_session)
|
2021-12-03 14:17:49 +01:00
|
|
|
])
|
|
|
|
|
2021-12-03 15:19:21 +01:00
|
|
|
async def _helper_test_get_any_statement(self, stmt: str, mock__get_multi_companies_fin_stmt):
|
|
|
|
sym1, sym2, quarterly, batch_size, mock_session = 'foo', 'bar', False, 2, MagicMock()
|
2021-12-03 14:32:52 +01:00
|
|
|
mock__get_multi_companies_fin_stmt.return_value = expected_output = 'baz'
|
2021-12-03 15:19:21 +01:00
|
|
|
if stmt == BS:
|
2021-11-28 16:52:16 +01:00
|
|
|
function = functions.get_balance_sheet
|
2021-12-03 15:19:21 +01:00
|
|
|
elif stmt == IS:
|
2021-11-28 16:52:16 +01:00
|
|
|
function = functions.get_income_statement
|
2021-12-03 15:19:21 +01:00
|
|
|
elif stmt == CF:
|
2021-11-28 16:52:16 +01:00
|
|
|
function = functions.get_cash_flow_statement
|
|
|
|
else:
|
|
|
|
raise ValueError
|
2021-12-03 15:19:21 +01:00
|
|
|
output = await function(sym1, sym2, quarterly=quarterly, concurrent_batch_size=batch_size, session=mock_session)
|
2021-12-03 14:32:52 +01:00
|
|
|
self.assertEqual(expected_output, output)
|
2021-12-03 15:19:21 +01:00
|
|
|
mock__get_multi_companies_fin_stmt.assert_called_once_with(
|
|
|
|
stmt, sym1, sym2, quarterly=quarterly, concurrent_batch_size=batch_size, session=mock_session
|
|
|
|
)
|
2021-11-28 16:52:16 +01:00
|
|
|
|
2021-12-03 14:32:52 +01:00
|
|
|
@patch.object(functions, '_get_multi_companies_fin_stmt')
|
|
|
|
async def test_get_balance_sheet(self, mock__get_multi_companies_fin_stmt):
|
|
|
|
await self._helper_test_get_any_statement(BS, mock__get_multi_companies_fin_stmt)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-12-03 14:32:52 +01:00
|
|
|
@patch.object(functions, '_get_multi_companies_fin_stmt')
|
|
|
|
async def test_get_income_statement(self, mock__get_multi_companies_fin_stmt):
|
|
|
|
await self._helper_test_get_any_statement(IS, mock__get_multi_companies_fin_stmt)
|
2021-11-21 22:45:13 +01:00
|
|
|
|
2021-12-03 14:32:52 +01:00
|
|
|
@patch.object(functions, '_get_multi_companies_fin_stmt')
|
|
|
|
async def test_get_cash_flow_statement(self, mock__get_multi_companies_fin_stmt):
|
|
|
|
await self._helper_test_get_any_statement(CF, mock__get_multi_companies_fin_stmt)
|
2021-11-26 12:47:58 +01:00
|
|
|
|
2021-12-03 14:17:49 +01:00
|
|
|
@patch.object(functions, '_get_single_company_fin_stmt')
|
2021-12-03 15:38:21 +01:00
|
|
|
async def test__get_single_company_all_financials(self, mock__get_single_company_fin_stmt):
|
2021-11-26 21:38:10 +01:00
|
|
|
symbol, quarterly, mock_session = 'foo', False, MagicMock()
|
2021-12-03 14:17:49 +01:00
|
|
|
mock__get_single_company_fin_stmt.return_value = bar = 'bar'
|
2021-12-03 11:46:39 +01:00
|
|
|
expected_output = {BS: bar, IS: bar, CF: bar}
|
2021-12-03 15:38:21 +01:00
|
|
|
output = await functions._get_single_company_all_financials(symbol, quarterly, mock_session)
|
2021-11-26 21:38:10 +01:00
|
|
|
self.assertDictEqual(expected_output, output)
|
2021-12-03 14:17:49 +01:00
|
|
|
mock__get_single_company_fin_stmt.assert_has_calls([
|
2021-12-03 11:46:39 +01:00
|
|
|
call(BS, symbol, quarterly, mock_session),
|
|
|
|
call(IS, symbol, quarterly, mock_session),
|
|
|
|
call(CF, symbol, quarterly, mock_session)
|
|
|
|
])
|
2021-11-28 16:39:53 +01:00
|
|
|
|
2021-12-03 15:38:21 +01:00
|
|
|
@patch.object(functions, '_get_single_company_all_financials')
|
|
|
|
async def test_get_company_financials(self, mock__get_single_company_all_financials):
|
|
|
|
mock__get_single_company_all_financials.return_value = expected_output = 'baz'
|
2021-12-03 11:46:39 +01:00
|
|
|
symbol, quarterly, mock_session = 'foo', False, MagicMock()
|
2021-12-03 15:38:21 +01:00
|
|
|
output = await functions.get_all_financials(symbol, quarterly=quarterly, session=mock_session)
|
2021-12-03 11:46:39 +01:00
|
|
|
self.assertEqual(expected_output, output)
|
2021-12-03 15:38:21 +01:00
|
|
|
mock__get_single_company_all_financials.assert_called_once_with(symbol, quarterly, mock_session)
|
|
|
|
mock__get_single_company_all_financials.reset_mock()
|
2021-12-03 11:46:39 +01:00
|
|
|
|
2021-12-03 15:19:21 +01:00
|
|
|
test_sym1, test_sym2 = 'x', 'y'
|
|
|
|
expected_output = {test_sym1: expected_output, test_sym2: expected_output}
|
2021-12-03 15:38:21 +01:00
|
|
|
output = await functions.get_all_financials(test_sym1, test_sym2,
|
|
|
|
quarterly=quarterly, session=mock_session)
|
2021-11-28 16:39:53 +01:00
|
|
|
self.assertDictEqual(expected_output, output)
|
2021-12-03 15:38:21 +01:00
|
|
|
mock__get_single_company_all_financials.assert_has_calls([
|
2021-12-03 15:19:21 +01:00
|
|
|
call(test_sym1, quarterly, mock_session),
|
|
|
|
call(test_sym2, quarterly, 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-12-26 18:08:25 +01:00
|
|
|
BS: {
|
2021-12-26 19:04:23 +01:00
|
|
|
END_DATE: (0, 'End_Date_1', 'End_Date_2'),
|
2021-12-26 18:08:25 +01:00
|
|
|
'foo': (1, 1., -2.),
|
|
|
|
'bar': (2, 2., -3.),
|
|
|
|
'baz': (3, 3., -4.)
|
|
|
|
},
|
|
|
|
IS: {
|
2021-12-26 19:04:23 +01:00
|
|
|
END_DATE: (0, 'End_Date_1', 'End_Date_2'),
|
2021-12-26 18:08:25 +01:00
|
|
|
'foo': (1, 1., -2.),
|
|
|
|
'bar': (2, 2., -3.),
|
|
|
|
'baz': (3, 3., -4.)
|
|
|
|
},
|
|
|
|
CF: {
|
2021-12-26 19:04:23 +01:00
|
|
|
END_DATE: (0, 'End_Date_1', 'End_Date_2'),
|
2021-12-26 18:08:25 +01:00
|
|
|
'foo': (1, 1., -2.),
|
|
|
|
'bar': (2, 2., -3.),
|
|
|
|
'baz': (3, 3., -4.)
|
|
|
|
}
|
2021-11-27 18:35:12 +01:00
|
|
|
}
|
2021-12-03 15:38:21 +01:00
|
|
|
output = await functions.get_all_financials(symbol, session=mock_session_obj)
|
2021-11-27 18:35:12 +01:00
|
|
|
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]}'),
|
|
|
|
])
|