| 1 |
"""Tests for UtilSource with mocked psutil.""" |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from s_tui.sources.util_source import UtilSource |
| 6 |
|
| 7 |
|
| 8 |
class TestUtilSourceInit: |
| 9 |
def test_name(self, mock_cpu_count, mock_cpu_percent): |
| 10 |
src = UtilSource() |
| 11 |
assert src.get_source_name() == "Util" |
| 12 |
|
| 13 |
def test_measurement_unit(self, mock_cpu_count, mock_cpu_percent): |
| 14 |
src = UtilSource() |
| 15 |
assert src.get_measurement_unit() == "%" |
| 16 |
|
| 17 |
def test_is_available(self, mock_cpu_count, mock_cpu_percent): |
| 18 |
src = UtilSource() |
| 19 |
assert src.get_is_available() is True |
| 20 |
|
| 21 |
def test_sensor_list_length(self, mock_cpu_count, mock_cpu_percent): |
| 22 |
src = UtilSource() |
| 23 |
|
| 24 |
assert len(src.get_sensor_list()) == 5 |
| 25 |
|
| 26 |
def test_sensor_list_names(self, mock_cpu_count, mock_cpu_percent): |
| 27 |
src = UtilSource() |
| 28 |
sensors = src.get_sensor_list() |
| 29 |
assert sensors[0] == "Avg" |
| 30 |
assert sensors[1] == "Core 0" |
| 31 |
assert sensors[4] == "Core 3" |
| 32 |
|
| 33 |
def test_initial_measurement_zeros(self, mock_cpu_count, mock_cpu_percent): |
| 34 |
src = UtilSource() |
| 35 |
assert src.get_reading_list() == [0, 0, 0, 0, 0] |
| 36 |
|
| 37 |
def test_get_top(self, mock_cpu_count, mock_cpu_percent): |
| 38 |
src = UtilSource() |
| 39 |
assert src.get_top() == 100 |
| 40 |
|
| 41 |
def test_pallet(self, mock_cpu_count, mock_cpu_percent): |
| 42 |
src = UtilSource() |
| 43 |
assert "util" in src.get_pallet()[0] |
| 44 |
|
| 45 |
|
| 46 |
class TestUtilSourceUpdate: |
| 47 |
def test_update_populates_values(self, mock_cpu_count, mock_cpu_percent): |
| 48 |
src = UtilSource() |
| 49 |
src.update() |
| 50 |
readings = src.get_reading_list() |
| 51 |
|
| 52 |
assert readings[0] == pytest.approx(22.5) |
| 53 |
assert readings[1] == 25.0 |
| 54 |
assert readings[2] == 30.0 |
| 55 |
assert readings[3] == 20.0 |
| 56 |
assert readings[4] == 15.0 |
| 57 |
|
| 58 |
def test_update_summary(self, mock_cpu_count, mock_cpu_percent): |
| 59 |
src = UtilSource() |
| 60 |
src.update() |
| 61 |
summary = src.get_sensors_summary() |
| 62 |
assert "Avg" in summary |
| 63 |
assert "Core 0" in summary |
| 64 |
|
| 65 |
def test_update_full_summary(self, mock_cpu_count, mock_cpu_percent): |
| 66 |
src = UtilSource() |
| 67 |
src.update() |
| 68 |
summary = src.get_summary() |
| 69 |
assert summary["Util"] == "[%]" |