function for checking tr relevance

This commit is contained in:
Maximilian Fajnberg 2021-11-24 22:28:33 +01:00
parent 9adad5dba1
commit 1e0410a254
2 changed files with 17 additions and 13 deletions

View File

@ -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

View File

@ -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 = '<tr><td><div> Cash & Short Term Investments </div></td></tr>'
test_soup = BeautifulSoup(test_html, functions.HTML_PARSER)
self.assertTrue(functions.is_relevant_table_row(test_soup.tr))
test_html = '<tr><td><div> Cash & Short Term Investments Growth </div></td></tr>'
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