mwfin/tests/test_functions.py

82 lines
3.0 KiB
Python

from pathlib import Path
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, MagicMock, AsyncMock, call
from bs4 import BeautifulSoup
from mwfin import functions
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')
@classmethod
def setUpClass(cls) -> None:
with open(cls.TEST_HTML_FILE_PATH, 'r') as f:
test_html = f.read()
cls.test_soup = BeautifulSoup(test_html, functions.HTML_PARSER)
@patch.object(functions, 'ClientSession')
async def test_soup_from_url(self, mock_session_cls):
test_html = '<b>foo</b>'
mock_response = MagicMock()
mock_response.text = AsyncMock(return_value=test_html)
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)
mock_session_cls.return_value = mock_session_obj
expected_output = BeautifulSoup(test_html, 'html.parser')
output = await functions.soup_from_url('baz')
self.assertEqual(expected_output, output)
output = await functions.soup_from_url('baz', mock_session_obj)
self.assertEqual(expected_output, output)
def test_extract_end_dates(self):
expected_output = ('End_Date_1', 'End_Date_2')
output = functions.extract_end_dates(self.test_soup)
self.assertTupleEqual(expected_output, output)
def test_find_relevant_table_rows(self):
test_table = self.test_soup.find('div', attrs={'class': 'financials'}).div.div.table
expected_output = [test_table.thead.tr, test_table.tbody.tr]
# print(expected_output) # debug
output = functions.find_relevant_table_rows(self.test_soup)
self.assertListEqual(expected_output, output)
def test_convert_number(self):
# likely not necessary if data-chart-data is used
# e.g. floats may be converted on extraction
pass
def test_extract_row_data(self):
test_table = self.test_soup.find('div', attrs={'class': 'financials'}).div.div.table
expected_output = ('Item_1', (11000000.0, -22000000.0))
output = functions.extract_row_data(test_table.tbody.tr)
# print(test_table.tbody.tr) # debug
self.assertTupleEqual(expected_output, output)
def test_extract_all_data(self):
expected_output = {'Item_1': (11000000.0, -22000000.0)}
output = functions.extract_row_data(self.test_soup)
self.assertDictEqual(expected_output, output)
async def test_get_balance_sheet(self):
pass
async def test_get_income_statement(self):
pass
async def test_get_cash_flow_statement(self):
pass
async def test_get_company_financials(self):
pass