From 326b956be4a29e22984dc62f80bb5f8b768e26a9 Mon Sep 17 00:00:00 2001 From: Daniil Fajnberg Date: Sat, 27 Nov 2021 18:35:12 +0100 Subject: [PATCH] added small integration test --- tests/test_functions.py | 24 ++++++++++++++++++++++-- tests/test_structure.html | 4 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tests/test_functions.py b/tests/test_functions.py index fec2326..c5379e9 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -19,6 +19,7 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): TEST_HTML_FILE_PATH = Path(THIS_DIR, 'test_structure.html') log_lvl: int + test_html: str @staticmethod def get_mock_session(response_text: str = None) -> MagicMock: @@ -33,8 +34,8 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): @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, HTML_PARSER) + cls.test_html = f.read() + cls.test_soup = BeautifulSoup(cls.test_html, HTML_PARSER) cls.log_lvl = functions.log.level functions.log.setLevel(logging.CRITICAL) @@ -165,3 +166,22 @@ class FunctionsTestCase(IsolatedAsyncioTestCase): mock_get_bs.assert_called_once_with(symbol, quarterly, mock_session) mock_get_is.assert_called_once_with(symbol, quarterly, mock_session) mock_get_cf.assert_called_once_with(symbol, quarterly, mock_session) + + @patch.object(functions, 'ClientSession') + async def test_integration_get_company_financials(self, mock_session_cls): + mock_session_cls.return_value = mock_session_obj = self.get_mock_session(self.test_html) + symbol = 'foo' + # Since we mock the web request and always receive the same HTML markup, + # and the function essentially does 3 separate requests always updating the output dictionary with the same + # data, we expect it to remain unchanged and only having one item. + expected_output = { + END_DATE: ('End_Date_1', 'End_Date_2'), + 'Cash & Short Term Investments': (11000000, -22000000), + } + output = await functions.get_company_financials(symbol, session=mock_session_obj) + self.assertDictEqual(expected_output, output) + mock_session_obj.get.assert_has_calls([ + call(f'{BASE_URL}/{symbol}/financials{FIN_STMT_URL_SUFFIX[BS]}'), + call(f'{BASE_URL}/{symbol}/financials{FIN_STMT_URL_SUFFIX[IS]}'), + call(f'{BASE_URL}/{symbol}/financials{FIN_STMT_URL_SUFFIX[CF]}'), + ]) diff --git a/tests/test_structure.html b/tests/test_structure.html index b0b7ce5..7e59e1b 100644 --- a/tests/test_structure.html +++ b/tests/test_structure.html @@ -23,13 +23,13 @@ -
Item_1
Item_1
+
Cash & Short Term Investments
Cash & Short Term Investments
-
Item_2
Item_2
+
Cash & Short Term Investments Growth
Cash & Short Term Investments Growth