first two unit tests

This commit is contained in:
Maximilian Fajnberg 2021-11-22 19:52:36 +01:00
parent 33e1fca03f
commit 525d56ef63
2 changed files with 38 additions and 3 deletions

View File

@ -11,6 +11,9 @@ 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]]]
DOMAIN = 'www.marketwatch.com'
HTML_PARSER = 'html.parser'
async def soup_from_url(url: str, session: ClientSession = None) -> BeautifulSoup:
"""

View File

@ -1,13 +1,45 @@
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch, MagicMock, AsyncMock, call
from bs4 import BeautifulSoup
from mwfin import functions
class FunctionsTestCase(IsolatedAsyncioTestCase):
def test_soup_from_url(self):
pass
@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):
pass
test_html = '<tr>' \
'<th><div> ITEM </div></th>' \
'<th><div> 30-SEP-2020 </div></th>' \
'<th><div> 31-DEC-2020 </div></th>' \
'<th><div> 31-MAR-2021 </div></th>' \
'<th><div> 30-JUN-2021 </div></th>' \
'<th><div> 30-SEP-2021 </div></th>' \
'</tr>'
test_soup = BeautifulSoup(test_html, 'html.parser')
expected_output = ('30-SEP-2020', '31-DEC-2020', '31-MAR-2021',
'30-JUN-2021', '30-SEP-2021')
output = functions.extract_end_dates(test_soup)
self.assertTupleEqual(expected_output, output)
def test_find_relevant_table_rows(self):
pass