diff --git a/functions/funcs.py b/functions/funcs.py index b54447e..ea6cdb9 100644 --- a/functions/funcs.py +++ b/functions/funcs.py @@ -380,6 +380,8 @@ async def get_reports(reportname, ffrom, to): async def helper_function_exam(): c = 0 users_data = fetch(c) + if users_data == []: + return {"msg":"error"} q = int(users_data['total']) file_path = os.path.join(os.getcwd(), "log_exam.txt") @@ -400,8 +402,12 @@ async def helper_function_exam(): c += len(users_data['data']) users_data = fetch(c) + return "success" + async def get_uuser_list_exam(): - await helper_function_exam() + res = await helper_function_exam() + if res != "success": + return {"err": "some error"} with open('log_exam.txt', 'r') as file: file_contents = file.read() diff --git a/tests/e2e/e2e_tests.py b/tests/e2e/e2e_tests.py index 18db1bd..4b2e9cb 100644 --- a/tests/e2e/e2e_tests.py +++ b/tests/e2e/e2e_tests.py @@ -103,6 +103,26 @@ async def test_get_reports_V2_200(self): except json.JSONDecodeError: self.fail("Response is not valid JSON") + async def test_list_exam(self): + async with httpx.AsyncClient() as client: + response = await client.get( + "http://127.0.0.1:8000/api/users/list") + if response.status_code == 307: + location = response.headers.get("Location") + response = await client.get(location) + + data = response.text + + if response.status_code != 200: + self.assertEqual(response.status_code, 404) + else: + try: + parsed_data = json.loads(data) + self.assertIsInstance(parsed_data, list) + self.assertTrue(len(parsed_data)>0) + except json.JSONDecodeError: + self.fail("Response is not valid JSON") + def test_start_get200_fail(self): asyncio.run(self.test_get_reports_307_then_200_then_fail()) @@ -112,5 +132,8 @@ def test_start_get200(self): def test_start_get200V2(self): asyncio.run(self.test_get_reports_V2_200()) + def test_start_exam_list(self): + asyncio.run(self.test_list_exam()) + if __name__ == '__main__': unittest.main() diff --git a/tests/integration/exam.py b/tests/integration/exam.py new file mode 100644 index 0000000..2bac043 --- /dev/null +++ b/tests/integration/exam.py @@ -0,0 +1,34 @@ +import unittest +from collections import defaultdict +from unittest.mock import patch +import asyncio + +from functions.funcs import helper_function_exam, get_uuser_list_exam + +class TestIntegration(unittest.TestCase): + + @patch('functions.funcs.fetch') + def test_helper_func_fetch_empty(self, mock_fetch): + async def async_test(): + mock_fetch.return_value = [] + res = await helper_function_exam() + + self.assertIsInstance(res, dict) + self.assertTrue(res == {"msg":"error"}) + + asyncio.run(async_test()) + + @patch('functions.funcs.helper_function_exam') + def test_exam_return_endpoint(self, mock_helper): + async def async_test(): + mock_helper.return_value = "failure" + + res = await get_uuser_list_exam() + self.assertIsInstance(res, dict) + self.assertTrue(res == {"err": "some error"}) + + asyncio.run(async_test()) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file