test for new function to get any statement from many companies; renamed functions

This commit is contained in:
2021-12-03 14:17:49 +01:00
parent 1f8104b1a0
commit ca360413c3
3 changed files with 59 additions and 32 deletions

View File

@ -101,7 +101,7 @@ class FunctionsTestCase(IsolatedAsyncioTestCase):
@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):
async def test__get_single_company_fin_stmt(self, mock_soup_from_url, mock_extract_all_data):
mock_session = MagicMock()
test_ticker, statement = 'bar', BS
test_url = f'{BASE_URL}/{test_ticker}/financials{FIN_STMT_URL_SUFFIX[statement]}'
@ -109,7 +109,7 @@ class FunctionsTestCase(IsolatedAsyncioTestCase):
mock_extract_all_data.return_value = expected_output = {'foo': 'bar'}
quarterly = False
output = await functions._get_financial_statement(statement, test_ticker, quarterly, mock_session)
output = await functions._get_single_company_fin_stmt(statement, test_ticker, quarterly, mock_session)
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)
@ -117,14 +117,33 @@ class FunctionsTestCase(IsolatedAsyncioTestCase):
mock_extract_all_data.reset_mock()
quarterly = True
output = await functions._get_financial_statement(statement, test_ticker, quarterly, mock_session)
output = await functions._get_single_company_fin_stmt(statement, test_ticker, quarterly, mock_session)
self.assertDictEqual(expected_output, output)
mock_soup_from_url.assert_called_once_with(test_url + '/quarter', mock_session)
mock_extract_all_data.assert_called_once_with(mock_soup)
async def _helper_test_get_any_statement(self, statement: str, mock__get_financial_statement):
@patch.object(functions, '_get_single_company_fin_stmt')
async def test__get_multi_companies_fin_stmt(self, mock__get_single_company_fin_stmt):
statement, symbol1, symbol2, 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, symbol1,
quarterly=quarterly, session=mock_session)
self.assertEqual(expected_output, output)
mock__get_single_company_fin_stmt.assert_called_once_with(statement, symbol1, quarterly, mock_session)
mock__get_single_company_fin_stmt.reset_mock()
expected_output = {symbol1: expected_output, symbol2: expected_output}
output = await functions._get_multi_companies_fin_stmt(symbol1, symbol2,
quarterly=quarterly, session=mock_session)
self.assertDictEqual(expected_output, output)
mock__get_single_company_fin_stmt.assert_has_calls([
call(statement, symbol1, quarterly, mock_session),
call(statement, symbol2, quarterly, mock_session)
])
async def _helper_test_get_any_statement(self, statement: str, mock__get_single_company_fin_stmt):
symbol, quarterly, mock_session = 'foo', False, MagicMock()
mock__get_financial_statement.return_value = expected_output = 'bar'
mock__get_single_company_fin_stmt.return_value = expected_output = 'bar'
if statement == BS:
function = functions.get_balance_sheet
elif statement == IS:
@ -135,38 +154,38 @@ class FunctionsTestCase(IsolatedAsyncioTestCase):
raise ValueError
output = await function(symbol, quarterly=quarterly, session=mock_session)
self.assertEqual(expected_output, output)
mock__get_financial_statement.assert_called_once_with(statement, symbol, quarterly, mock_session)
mock__get_financial_statement.reset_mock()
mock__get_single_company_fin_stmt.assert_called_once_with(statement, symbol, quarterly, mock_session)
mock__get_single_company_fin_stmt.reset_mock()
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([
mock__get_single_company_fin_stmt.assert_has_calls([
call(statement, symbol1, quarterly, mock_session),
call(statement, symbol2, quarterly, mock_session),
])
@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)
@patch.object(functions, '_get_single_company_fin_stmt')
async def test_get_balance_sheet(self, mock__get_single_company_fin_stmt):
await self._helper_test_get_any_statement(BS, mock__get_single_company_fin_stmt)
@patch.object(functions, '_get_financial_statement')
async def test_get_income_statement(self, mock__get_financial_statement):
await self._helper_test_get_any_statement(IS, mock__get_financial_statement)
@patch.object(functions, '_get_single_company_fin_stmt')
async def test_get_income_statement(self, mock__get_single_company_fin_stmt):
await self._helper_test_get_any_statement(IS, mock__get_single_company_fin_stmt)
@patch.object(functions, '_get_financial_statement')
async def test_get_cash_flow_statement(self, mock__get_financial_statement):
await self._helper_test_get_any_statement(CF, mock__get_financial_statement)
@patch.object(functions, '_get_single_company_fin_stmt')
async def test_get_cash_flow_statement(self, mock__get_single_company_fin_stmt):
await self._helper_test_get_any_statement(CF, mock__get_single_company_fin_stmt)
@patch.object(functions, '_get_financial_statement')
async def test__get_single_company_financials(self, mock__get_financial_statement):
@patch.object(functions, '_get_single_company_fin_stmt')
async def test__get_single_company_financials(self, mock__get_single_company_fin_stmt):
symbol, quarterly, mock_session = 'foo', False, MagicMock()
mock__get_financial_statement.return_value = bar = 'bar'
mock__get_single_company_fin_stmt.return_value = bar = 'bar'
expected_output = {BS: bar, IS: bar, CF: bar}
output = await functions._get_single_company_financials(symbol, quarterly, mock_session)
self.assertDictEqual(expected_output, output)
mock__get_financial_statement.assert_has_calls([
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)