|
3 | 3 | import time
|
4 | 4 | import asyncio
|
5 | 5 | import unittest
|
6 |
| -from unittest.mock import patch, AsyncMock |
| 6 | +from unittest.mock import patch, AsyncMock, MagicMock |
7 | 7 |
|
8 | 8 | sys.path.append(os.path.abspath('docs/3. Handling CPU and IO Bound Tasks'))
|
9 | 9 | from ex_3_1 import fetch_data as fetch_data_ex_1, main as main_ex_1
|
| 10 | +from ex_3_2 import fetch_data as fetch_data_ex_2, main as main_ex_2 |
| 11 | +from ex_3_3 import fetch_data as fetch_data_ex_3, main as main_ex_3 |
| 12 | +from ex_3_4 import fetch_data as fetch_data_ex_4, main as main_ex_4 |
| 13 | +from ex_3_5 import cpu_bound_task, without_multiprocessing, with_multiprocessing, main as main_ex_5 |
10 | 14 |
|
11 | 15 |
|
12 | 16 | class TestEx1AsyncHttpx(unittest.IsolatedAsyncioTestCase):
|
@@ -47,5 +51,129 @@ async def test_main_success(self, mock_fetch):
|
47 | 51 | mocked_print.assert_any_call(f"response is: {mock_fetch.return_value}")
|
48 | 52 |
|
49 | 53 |
|
| 54 | +class TestEx2AsyncHttpx(unittest.IsolatedAsyncioTestCase): |
| 55 | + @patch('httpx.AsyncClient.get') |
| 56 | + async def test_fetch_data_success(self, mock_get): |
| 57 | + """Test successful fetching of data.""" |
| 58 | + mock_response = mock_get.return_value |
| 59 | + mock_response.status_code = 200 |
| 60 | + mock_response.text = 'Success' |
| 61 | + |
| 62 | + url = 'https://www.example.com/' |
| 63 | + response1, response2, response3 = await fetch_data_ex_2(url) |
| 64 | + mock_get.assert_called_with(url) |
| 65 | + assert mock_get.call_count == 3 |
| 66 | + |
| 67 | + self.assertEqual(response1.status_code, 200) |
| 68 | + self.assertEqual(response2.status_code, 200) |
| 69 | + self.assertEqual(response3.status_code, 200) |
| 70 | + self.assertEqual(response1.text, 'Success') |
| 71 | + self.assertEqual(response2.text, 'Success') |
| 72 | + self.assertEqual(response3.text, 'Success') |
| 73 | + |
| 74 | + @patch('ex_3_2.fetch_data', new_callable=AsyncMock) |
| 75 | + async def test_main_success(self, mock_fetch): |
| 76 | + """Test main function when fetch_data is successful.""" |
| 77 | + mock_fetch.return_value.status_code = 200 |
| 78 | + mock_fetch.return_value = '<Response [200 OK]>', '<Response [200 OK]>', '<Response [200 OK]>' |
| 79 | + |
| 80 | + with patch('builtins.print') as mocked_print: |
| 81 | + await main_ex_2() |
| 82 | + mocked_print.assert_any_call(f"response1: {mock_fetch.return_value[0]}, " |
| 83 | + f"response2: {mock_fetch.return_value[1]}, " |
| 84 | + f"response3: {mock_fetch.return_value[2]}") |
| 85 | + |
| 86 | + |
| 87 | +class TestEx3AsyncHttpx(unittest.IsolatedAsyncioTestCase): |
| 88 | + @patch('httpx.AsyncClient.get') |
| 89 | + async def test_fetch_data_success(self, mock_get): |
| 90 | + """Test successful fetching of data.""" |
| 91 | + mock_response = mock_get.return_value |
| 92 | + mock_response.status_code = 200 |
| 93 | + mock_response.text = 'Success' |
| 94 | + |
| 95 | + url = 'https://www.example.com/' |
| 96 | + response1, response2, response3 = await fetch_data_ex_3(url) |
| 97 | + mock_get.assert_called_with(url) |
| 98 | + assert mock_get.call_count == 3 |
| 99 | + |
| 100 | + self.assertEqual(response1.status_code, 200) |
| 101 | + self.assertEqual(response2.status_code, 200) |
| 102 | + self.assertEqual(response3.status_code, 200) |
| 103 | + self.assertEqual(response1.text, 'Success') |
| 104 | + self.assertEqual(response2.text, 'Success') |
| 105 | + self.assertEqual(response3.text, 'Success') |
| 106 | + |
| 107 | + @patch('ex_3_3.fetch_data', new_callable=AsyncMock) |
| 108 | + async def test_main_success(self, mock_fetch): |
| 109 | + """Test main function when fetch_data is successful.""" |
| 110 | + mock_fetch.return_value.status_code = 200 |
| 111 | + mock_fetch.return_value = ['<Response [200 OK]>', '<Response [200 OK]>', '<Response [200 OK]>'] |
| 112 | + |
| 113 | + with patch('builtins.print') as mocked_print: |
| 114 | + await main_ex_3() |
| 115 | + mocked_print.assert_any_call(f'obj: {mock_fetch.return_value}, obj type: {type(mock_fetch.return_value)}') |
| 116 | + |
| 117 | + |
| 118 | +class TestEx4AsyncHttpx(unittest.TestCase): |
| 119 | + @patch('httpx.get') |
| 120 | + def test_fetch_data_success(self, mock_get): |
| 121 | + """Test successful fetching of data.""" |
| 122 | + mock_response = mock_get.return_value |
| 123 | + mock_response.status_code = 200 |
| 124 | + mock_response.text = 'Success' |
| 125 | + |
| 126 | + url = 'https://www.example.com/' |
| 127 | + response1, response2, response3 = fetch_data_ex_4(url) |
| 128 | + mock_get.assert_called_with(url) |
| 129 | + assert mock_get.call_count == 3 |
| 130 | + |
| 131 | + self.assertEqual(response1.status_code, 200) |
| 132 | + self.assertEqual(response2.status_code, 200) |
| 133 | + self.assertEqual(response3.status_code, 200) |
| 134 | + self.assertEqual(response1.text, 'Success') |
| 135 | + self.assertEqual(response2.text, 'Success') |
| 136 | + self.assertEqual(response3.text, 'Success') |
| 137 | + |
| 138 | + @patch('ex_3_4.fetch_data', new_callable=MagicMock) |
| 139 | + def test_main_success(self, mock_fetch): |
| 140 | + """Test main function when fetch_data is successful.""" |
| 141 | + mock_fetch.return_value.status_code = 200 |
| 142 | + mock_fetch.return_value = '<Response [200 OK]>', '<Response [200 OK]>', '<Response [200 OK]>' |
| 143 | + |
| 144 | + with patch('builtins.print') as mocked_print: |
| 145 | + main_ex_4() |
| 146 | + mocked_print.assert_any_call(f"response1: {mock_fetch.return_value[0]}, " |
| 147 | + f"response2: {mock_fetch.return_value[1]}, " |
| 148 | + f"response3: {mock_fetch.return_value[2]}") |
| 149 | + |
| 150 | + |
| 151 | +class TestMultiprocessingPerformance(unittest.TestCase): |
| 152 | + |
| 153 | + def setUp(self): |
| 154 | + self.step = 2 |
| 155 | + self.value = 100000000 |
| 156 | + |
| 157 | + def test_with_and_without_mp(self): |
| 158 | + value1, value2, time_taken = without_multiprocessing(self.step, self.value) |
| 159 | + self.assertEqual(value1, value2) |
| 160 | + self.assertEqual(value1, self.value * self.step) |
| 161 | + |
| 162 | + value1_mp, value2_mp, time_taken_mp = with_multiprocessing(self.step, self.value) |
| 163 | + self.assertEqual(value1_mp, value2_mp) |
| 164 | + self.assertEqual(value1_mp, self.value * self.step) |
| 165 | + self.assertEqual(value2_mp, self.value * self.step) |
| 166 | + self.assertLessEqual(time_taken_mp * 1.7, time_taken, |
| 167 | + msg='time taken without mp is less than 1.7 times of with mp!') |
| 168 | + |
| 169 | + def test_main(self): |
| 170 | + with patch('builtins.print') as mocked_print: |
| 171 | + main_ex_5() |
| 172 | + value = self.value * self.step |
| 173 | + mocked_print.assert_any_call(f'Without multiprocessing, value1: {value}, value2: {value}') |
| 174 | + mocked_print.assert_any_call(f'======================================') |
| 175 | + mocked_print.assert_any_call(f'With multiprocessing, value1: {value}, value2: {value}') |
| 176 | + |
| 177 | + |
50 | 178 | if __name__ == '__main__':
|
51 | 179 | unittest.main()
|
0 commit comments