mwfin/tests/test_functions.py

241 lines
12 KiB
Python
Raw Normal View History

2021-11-27 15:11:42 +01:00
import logging
from pathlib import Path
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
THIS_DIR = Path(__file__).parent
class FunctionsTestCase(IsolatedAsyncioTestCase):
# 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
@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-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-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')
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)
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')
output = functions.get_all_table_rows(self.test_soup)
self.assertSequenceEqual(expected_output, output)
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
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-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')
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
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-26 12:47:58 +01:00
quarterly = True
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
@patch.object(functions, '_get_single_company_fin_stmt')
async def test__get_multi_companies_fin_stmt(self, mock__get_single_company_fin_stmt):
statement, sym1, sym2, quarterly, mock_session = 'xyz', 'foo', 'bar', False, MagicMock()
mock__get_single_company_fin_stmt.return_value = expected_output = 'baz'
output = await functions._get_multi_companies_fin_stmt(statement, sym1,
quarterly=quarterly, session=mock_session)
self.assertEqual(expected_output, output)
mock__get_single_company_fin_stmt.assert_called_once_with(statement, sym1, quarterly, mock_session)
mock__get_single_company_fin_stmt.reset_mock()
expected_output = {sym1: expected_output, sym2: expected_output}
output = await functions._get_multi_companies_fin_stmt(statement, sym1, sym2,
quarterly=quarterly, session=mock_session)
self.assertDictEqual(expected_output, output)
mock__get_single_company_fin_stmt.assert_has_calls([
call(statement, sym1, quarterly, mock_session),
call(statement, sym2, quarterly, mock_session)
])
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()
mock__get_multi_companies_fin_stmt.return_value = expected_output = 'baz'
if stmt == BS:
2021-11-28 16:52:16 +01:00
function = functions.get_balance_sheet
elif stmt == IS:
2021-11-28 16:52:16 +01:00
function = functions.get_income_statement
elif stmt == CF:
2021-11-28 16:52:16 +01:00
function = functions.get_cash_flow_statement
else:
raise ValueError
output = await function(sym1, sym2, quarterly=quarterly, concurrent_batch_size=batch_size, session=mock_session)
self.assertEqual(expected_output, output)
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
@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)
@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)
@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
@patch.object(functions, '_get_single_company_fin_stmt')
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()
mock__get_single_company_fin_stmt.return_value = bar = 'bar'
expected_output = {BS: bar, IS: bar, CF: bar}
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)
mock__get_single_company_fin_stmt.assert_has_calls([
call(BS, symbol, quarterly, mock_session),
call(IS, symbol, quarterly, mock_session),
call(CF, symbol, quarterly, mock_session)
])
@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'
symbol, quarterly, mock_session = 'foo', False, MagicMock()
output = await functions.get_all_financials(symbol, quarterly=quarterly, session=mock_session)
self.assertEqual(expected_output, output)
mock__get_single_company_all_financials.assert_called_once_with(symbol, quarterly, mock_session)
mock__get_single_company_all_financials.reset_mock()
test_sym1, test_sym2 = 'x', 'y'
expected_output = {test_sym1: expected_output, test_sym2: expected_output}
output = await functions.get_all_financials(test_sym1, test_sym2,
quarterly=quarterly, session=mock_session)
self.assertDictEqual(expected_output, output)
mock__get_single_company_all_financials.assert_has_calls([
call(test_sym1, quarterly, mock_session),
call(test_sym2, quarterly, mock_session)
])
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'
# Since the web request is mocked we always receive the same HTML markup.
2021-11-27 18:35:12 +01:00
expected_output = {
BS: {
2021-12-26 19:04:23 +01:00
END_DATE: (0, 'End_Date_1', 'End_Date_2'),
'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'),
'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'),
'foo': (1, 1., -2.),
'bar': (2, 2., -3.),
'baz': (3, 3., -4.)
}
2021-11-27 18:35:12 +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]}'),
])