|
20 | 20 |
|
21 | 21 |
|
22 | 22 | class TestCPU(unittest.TestCase): |
23 | | - def test_is_psutil_available(self): |
24 | | - @mock.patch("psutil.cpu_times") |
25 | | - def test_is_psutil_available(self, mock_cpu_times): |
26 | | - # Test when psutil is available |
27 | | - mock_cpu_times.return_value = mock.Mock() |
28 | | - self.assertTrue(is_psutil_available()) # noqa: E712 |
29 | | - |
30 | | - @mock.patch("psutil.cpu_times", side_effect=AttributeError) |
31 | | - def test_is_psutil_not_available(self, mock_cpu_times): |
32 | | - # Test when psutil is not available |
33 | | - self.assertFalse(is_psutil_available()) # noqa: E712 |
| 23 | + @mock.patch("psutil.cpu_times") |
| 24 | + def test_is_psutil_available_with_nice(self, mock_cpu_times): |
| 25 | + # Create a mock with 'nice' attribute |
| 26 | + mock_times = mock.Mock() |
| 27 | + mock_times.nice = 0.1 |
| 28 | + mock_cpu_times.return_value = mock_times |
| 29 | + self.assertTrue(is_psutil_available()) |
| 30 | + |
| 31 | + @mock.patch("psutil.cpu_times") |
| 32 | + def test_is_psutil_available_without_nice(self, mock_cpu_times): |
| 33 | + # Create a mock without 'nice' attribute (like Windows) |
| 34 | + mock_times = mock.Mock(spec=[]) # Empty spec = no attributes |
| 35 | + mock_cpu_times.return_value = mock_times |
| 36 | + with mock.patch("psutil.cpu_percent") as mock_cpu_percent: |
| 37 | + self.assertTrue(is_psutil_available()) |
| 38 | + mock_cpu_percent.assert_called_once_with(interval=0.0, percpu=False) |
| 39 | + |
| 40 | + @mock.patch("psutil.cpu_times", side_effect=Exception("Test error")) |
| 41 | + def test_is_psutil_not_available_on_exception(self, mock_cpu_times): |
| 42 | + self.assertFalse(is_psutil_available()) |
34 | 43 |
|
35 | 44 |
|
36 | 45 | class TestIntelPowerGadget(unittest.TestCase): |
|
0 commit comments