From f9984ba386754889e3d4c5e2aa8482434d8a75e1 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 5 Mar 2017 13:16:13 +0100 Subject: [PATCH] [tests] Add unit tests for load module --- bumblebee/modules/load.py | 2 +- tests/modules/test_load.py | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 tests/modules/test_load.py diff --git a/bumblebee/modules/load.py b/bumblebee/modules/load.py index c44ae5a..4d94ee1 100644 --- a/bumblebee/modules/load.py +++ b/bumblebee/modules/load.py @@ -22,7 +22,7 @@ class Module(bumblebee.engine.Module): self._load = [0, 0, 0] try: self._cpus = multiprocessing.cpu_count() - except multiprocessing.NotImplementedError as e: + except NotImplementedError as e: self._cpus = 1 engine.input.register_callback(self, button=bumblebee.input.LEFT_MOUSE, cmd="gnome-system-monitor") diff --git a/tests/modules/test_load.py b/tests/modules/test_load.py new file mode 100644 index 0000000..e57bc2b --- /dev/null +++ b/tests/modules/test_load.py @@ -0,0 +1,57 @@ +# pylint: disable=C0103,C0111 + +import json +import mock +import unittest + +import tests.mocks as mocks + +from bumblebee.input import LEFT_MOUSE +from bumblebee.modules.load import Module + +class TestLoadModule(unittest.TestCase): + def setUp(self): + mocks.setup_test(self, Module) + + self._mp = mock.patch("bumblebee.modules.load.multiprocessing") + self._os = mock.patch("bumblebee.modules.load.os") + + self.mp = self._mp.start() + self.os = self._os.start() + + self.mp.cpu_count.return_value = 1 + + def tearDown(self): + self._mp.stop() + self._os.stop() + mocks.teardown_test(self) + + def test_leftclick(self): + mocks.mouseEvent(stdin=self.stdin, button=LEFT_MOUSE, inp=self.input, module=self.module) + self.popen.assert_call("gnome-system-monitor") + + def test_load_format(self): + self.os.getloadavg.return_value = [ 5.9, 1.2, 0.8 ] + self.module.update_all() + self.assertEquals(self.module.load(self.anyWidget), "5.90/1.20/0.80") + + def test_warning(self): + self.config.set("load.critical", "1") + self.config.set("load.warning", "0.8") + self.os.getloadavg.return_value = [ 0.9, 0, 0 ] + self.module.update_all() + self.assertTrue("warning" in self.module.state(self.anyWidget)) + + def test_critical(self): + self.config.set("load.critical", "1") + self.config.set("load.warning", "0.8") + self.os.getloadavg.return_value = [ 1.1, 0, 0 ] + self.module.update_all() + self.assertTrue("critical" in self.module.state(self.anyWidget)) + + def test_assume_single_core(self): + self.mp.cpu_count.side_effect = NotImplementedError + module = Module(engine=self.engine, config={"config": mock.Mock() }) + self.assertEquals(1, module._cpus) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4