From 2d9119a4be087d030d7cd7d8445237d982f65a00 Mon Sep 17 00:00:00 2001 From: Maximilian Fajnberg Date: Sun, 28 Nov 2021 15:20:28 +0100 Subject: [PATCH] company financials returned as separate dicts; test fix --- src/mwfin/functions.py | 15 ++++++++------- tests/test_functions.py | 18 ++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/mwfin/functions.py b/src/mwfin/functions.py index 840fd85..75b5317 100644 --- a/src/mwfin/functions.py +++ b/src/mwfin/functions.py @@ -1,5 +1,5 @@ import logging -from typing import Union, List +from typing import Union, List, Dict from aiohttp.client import ClientSession from bs4 import BeautifulSoup @@ -63,7 +63,7 @@ def extract_row_data(tr: Tag) -> tuple[str, tuple[int]]: item_name = str(tr.td.div.string).strip() data_div = tr.find_all('td')[-1].div.div values_str: str = data_div.attrs['data-chart-data'] - values = tuple(int(float(s)) for s in values_str.split(',')) + values = tuple(int(float(s if s != '' else 0)) for s in values_str.split(',')) return item_name, values @@ -115,11 +115,12 @@ async def get_cash_flow_statement(ticker_symbol: str, quarterly: bool = False, async def get_company_financials(ticker_symbol: str, quarterly: bool = False, - session: ClientSession = None) -> ResultDict: + session: ClientSession = None) -> Dict[str, ResultDict]: """ Returns all fundamentals (balance sheet, income statement and cash flow statement) of the specified company. """ - financials = await get_balance_sheet(ticker_symbol, quarterly, session) - financials.update(await get_income_statement(ticker_symbol, quarterly, session)) - financials.update(await get_cash_flow_statement(ticker_symbol, quarterly, session)) - return financials + return { + constants.BS: await get_balance_sheet(ticker_symbol, quarterly, session), + constants.IS: await get_income_statement(ticker_symbol, quarterly, session), + constants.CF: await get_cash_flow_statement(ticker_symbol, quarterly, session) + } diff --git a/tests/test_functions.py b/tests/test_functions.py index c5379e9..cf84aa8 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -74,7 +74,7 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): def test_extract_row_data(self): test_row = self.test_soup.find('div', attrs={'class': 'financials'}).tbody.tr - expected_output = ('Item_1', (11000000, -22000000)) + expected_output = ('Cash & Short Term Investments', (11000000, -22000000)) output = functions.extract_row_data(test_row) self.assertTupleEqual(expected_output, output) @@ -155,10 +155,9 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): 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 = { - END_DATE: mock_end_dates, - 'a': (1, 2), - 'b': (2, 3), - 'c': (3, 4) + 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)} } symbol, quarterly, mock_session = 'foo', False, MagicMock() output = await functions.get_company_financials(symbol, quarterly, mock_session) @@ -171,12 +170,11 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): 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 we mock the web request and always receive the same HTML markup, - # and the function essentially does 3 separate requests always updating the output dictionary with the same - # data, we expect it to remain unchanged and only having one item. + # Since the web request is mocked we always receive the same HTML markup. expected_output = { - END_DATE: ('End_Date_1', 'End_Date_2'), - 'Cash & Short Term Investments': (11000000, -22000000), + 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)} } output = await functions.get_company_financials(symbol, session=mock_session_obj) self.assertDictEqual(expected_output, output)