Compare commits

...

2 Commits

3 changed files with 50 additions and 29 deletions

View File

@ -11,6 +11,8 @@ from bs4.element import ResultSet, Tag
# the end dates of the reporting periods as strings (either years or quarters).
ResultDict = dict[str, Union[tuple[int], tuple[str]]]
HTML_PARSER = 'html.parser'
async def soup_from_url(url: str, session: ClientSession = None) -> BeautifulSoup:
"""

View File

@ -1,3 +1,4 @@
from pathlib import Path
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, MagicMock, AsyncMock, call
@ -5,14 +6,21 @@ from bs4 import BeautifulSoup
from mwfin import functions
# 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 = ''
TEST_SOUP = BeautifulSoup(TEST_HTML, 'html.parser')
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):
@ -33,7 +41,7 @@ class FunctionsTestCase(IsolatedAsyncioTestCase):
def test_extract_end_dates(self):
expected_output = ('End_Date_1', 'End_Date_2')
output = functions.extract_end_dates(TEST_SOUP)
output = functions.extract_end_dates(self.test_soup)
self.assertTupleEqual(expected_output, output)
def test_find_relevant_table_rows(self):

View File

@ -5,28 +5,39 @@
<title>Title</title>
</head>
<body>
<div><table aria-label="(something something) data table">
<thead>
<tr>
<th><div> 'Item' </div><div> 'Item' </div></th>
<th><div> End_Date_1 </div></th>
<th><div> End_Date_2 </div></th>
</tr>
</thead>
<tbody>
<tr>
<td><div> Item_1 </div><div class="other"> Item_1 </div></td>
<td><div><span class=""> 11M </span></div></td>
<td><div><span class="negative"> (22M) </span></div></td>
<td><div> <div data-chart-data="11000000.0,-22000000.0"><div></div></td>
</tr>
<tr>
<td><div> Item_2 </div><div class="other"> Item_2 </div></td>
<td><div><span class="positive"> 12% </span></div></td>
<td><div><span class="negative"> 13% </span></div></td>
<td><div> <div data-chart-data="0.12bazbazbaz,-0.13bazbazbaz"> <div></div></td>
</tr>
</tbody>
</table></div>
<div class="financials">
<header>
<h2><span>Foo table</span></h2>
<small> All values USD.</small>
</header>
<div>
<div>
<table>
<thead>
<tr>
<th><div> 'Item' </div><div> 'Item' </div></th>
<th><div> End_Date_1 </div></th>
<th><div> End_Date_2 </div></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td><div> Item_1 </div><div> Item_1 </div></td>
<td></td>
<td></td>
<td><div> <div data-chart-data="11000000.0,-22000000.0"><div></div></td>
</tr>
<tr>
<td><div> Item_2 </div><div> Item_2 </div></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>