From 07f0b7e34ab1c2b1f800acb7014851d0c6f5ba3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thayn=C3=A3=20Moretti?= Date: Tue, 1 Sep 2020 20:54:10 -0300 Subject: [PATCH] Add CPU module tests --- tests/modules/core/test_cpu.py | 68 +++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 2 deletions(-) diff --git a/tests/modules/core/test_cpu.py b/tests/modules/core/test_cpu.py index 9ea85ed..eca2362 100644 --- a/tests/modules/core/test_cpu.py +++ b/tests/modules/core/test_cpu.py @@ -1,7 +1,71 @@ import pytest +from unittest import TestCase, mock + +import core.config +import core.widget +import modules.core.cpu pytest.importorskip("psutil") -def test_load_module(): - __import__("modules.core.cpu") +def build_module(): + config = core.config.Config([]) + return modules.core.cpu.Module(config=config, theme=None) + +def cpu_widget(module): + return module.widgets()[0] + +class TestCPU(TestCase): + def test_load_module(self): + __import__("modules.core.cpu") + + @mock.patch('psutil.cpu_percent') + def test_cpu_percent(self, cpu_percent_mock): + cpu_percent_mock.return_value = 5 + module = build_module() + + assert cpu_widget(module).full_text() == '5.0%' + + @mock.patch('psutil.cpu_percent') + def test_cpu_percent_update(self, cpu_percent_mock): + cpu_percent_mock.return_value = 10 + module = build_module() + + assert cpu_widget(module).full_text() == '10.0%' + + cpu_percent_mock.return_value = 20 + module.update() + + assert cpu_widget(module).full_text() == '20.0%' + + @mock.patch('psutil.cpu_percent') + def test_healthy_state(self, cpu_percent_mock): + cpu_percent_mock.return_value = 50 + module = build_module() + + assert module.state(None) == None + + @mock.patch('psutil.cpu_percent') + def test_warning_state(self, cpu_percent_mock): + cpu_percent_mock.return_value = 75 + module = build_module() + + assert module.state(None) == 'warning' + + @mock.patch('psutil.cpu_percent') + def test_critical_state(self, cpu_percent_mock): + cpu_percent_mock.return_value = 82 + module = build_module() + + assert module.state(None) == 'critical' + + @mock.patch('core.input.register') + def test_register_left_mouse_action(self, input_register_mock): + module = build_module() + + input_register_mock.assert_called_with( + module, + button=core.input.LEFT_MOUSE, + cmd='gnome-system-monitor' + ) +