Debian Codegraph @

tests/test_coverup_32.py

raw symbols SCIP
1 # file scalene/scalene_nvidia_gpu.py:133-139
2 # lines [133, 135, 136, 137, 138, 139]
3 # branches ['135->136', '135->139']
4
5 import pytest
6 from unittest.mock import patch, MagicMock
7
8 # Assuming ScaleneNVIDIAGPU is in the scalene_nvidia_gpu.py file within the scalene package
9 # and that the pynvml module is not available, we mock the entire scalene_nvidia_gpu module.
10 # We also assume that the __pid attribute is set somewhere within ScaleneNVIDIAGPU.
11
12 # Mocking the entire scalene_nvidia_gpu module
13 with patch.dict("sys.modules", {"pynvml": MagicMock()}):
14 from scalene import scalene_nvidia_gpu
15
16
17 @pytest.fixture(scope="function")
18 def mock_gpu_stats():
19 with patch.object(
20 scalene_nvidia_gpu.ScaleneNVIDIAGPU, "has_gpu", return_value=True
21 ), patch.object(
22 scalene_nvidia_gpu.ScaleneNVIDIAGPU, "gpu_utilization", return_value=50.0
23 ), patch.object(
24 scalene_nvidia_gpu.ScaleneNVIDIAGPU, "gpu_memory_usage", return_value=1024.0
25 ):
26 yield
27
28
29 @pytest.fixture(scope="function")
30 def mock_no_gpu():
31 with patch.object(
32 scalene_nvidia_gpu.ScaleneNVIDIAGPU, "has_gpu", return_value=False
33 ):
34 yield
35
36
37 def test_get_stats_with_gpu(mock_gpu_stats):
38 gpu = scalene_nvidia_gpu.ScaleneNVIDIAGPU()
39 gpu._ScaleneNVIDIAGPU__pid = 1234 # Mocking the __pid attribute
40 total_load, mem_used = gpu.get_stats()
41 assert total_load == 50.0
42 assert mem_used == 1024.0
43
44
45 def test_get_stats_without_gpu(mock_no_gpu):
46 gpu = scalene_nvidia_gpu.ScaleneNVIDIAGPU()
47 gpu._ScaleneNVIDIAGPU__pid = 1234 # Mocking the __pid attribute
48 total_load, mem_used = gpu.get_stats()
49 assert total_load == 0.0
50 assert mem_used == 0.0