From 1e0410a2540b15194ae516fdd6c3f639d397bba7 Mon Sep 17 00:00:00 2001 From: Maximilian Fajnberg Date: Wed, 24 Nov 2021 22:28:33 +0100 Subject: [PATCH] function for checking tr relevance --- src/mwfin/functions.py | 8 ++++---- tests/test_functions.py | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/mwfin/functions.py b/src/mwfin/functions.py index c7a1240..e22133f 100644 --- a/src/mwfin/functions.py +++ b/src/mwfin/functions.py @@ -29,16 +29,16 @@ def extract_end_dates(soup: BeautifulSoup) -> tuple[str]: pass -def find_relevant_table_rows(soup: BeautifulSoup) -> ResultSet: +def is_relevant_table_row(tr: Tag) -> bool: """ - Returns the table rows containing the data of interest. + Returns True if the item in the table row is marked as relevant. Additionally warns when an item is unknown. """ pass -def convert_number(num_str: str) -> int: +def find_relevant_table_rows(soup: BeautifulSoup) -> ResultSet: """ - Takes a string like e.g. "420.69M" and returns 42069000000. + Returns the table rows containing the data of interest. """ pass diff --git a/tests/test_functions.py b/tests/test_functions.py index 3560138..ad4b223 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -44,17 +44,21 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): 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 + def test_is_relevant_table_row(self): + test_html = '
Cash & Short Term Investments
' + test_soup = BeautifulSoup(test_html, functions.HTML_PARSER) + self.assertTrue(functions.is_relevant_table_row(test_soup.tr)) + test_html = '
Cash & Short Term Investments Growth
' + test_soup = BeautifulSoup(test_html, functions.HTML_PARSER) + self.assertFalse(functions.is_relevant_table_row(test_soup.tr)) + + @patch.object(functions, 'is_relevant_table_row') + def test_find_relevant_table_rows(self, mock_is_relevant_table_row): + mock_is_relevant_table_row.return_value = True + expected_output = self.test_soup.find('div', attrs={'class': 'financials'}).div.div.table.tbody.find_all('tr') 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 + mock_is_relevant_table_row.assert_has_calls([call(expected_output[0]), call(expected_output[1])]) def test_extract_row_data(self): test_table = self.test_soup.find('div', attrs={'class': 'financials'}).div.div.table