From 8724af2906f73ab9236603b0297ded7d0a387328 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 5 Mar 2017 09:35:56 +0100 Subject: [PATCH] [tests/battery] Add tests for battery module --- bumblebee/engine.py | 3 + tests/__init__.py | 0 tests/modules/__init__.py | 0 tests/modules/backup.py | 66 ++++++++++++++++++++ tests/modules/test_battery.py | 110 ++++++++++++++++++++++++++++++++++ 5 files changed, 179 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/modules/__init__.py create mode 100644 tests/modules/backup.py create mode 100644 tests/modules/test_battery.py diff --git a/bumblebee/engine.py b/bumblebee/engine.py index 9a9c35f..67234fe 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -52,6 +52,9 @@ class Module(object): """By default, update() is a NOP""" pass + def update_all(self): + self.update(self._widgets) + def parameter(self, name, default=None): """Return the config parameter 'name' for this module""" name = "{}.{}".format(self.name, name) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/modules/__init__.py b/tests/modules/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/modules/backup.py b/tests/modules/backup.py new file mode 100644 index 0000000..45b76c4 --- /dev/null +++ b/tests/modules/backup.py @@ -0,0 +1,66 @@ +# pylint: disable=C0103,C0111 + +import sys +import json +import unittest +import mock + +from contextlib import contextmanager + +from bumblebee.input import I3BarInput +from bumblebee.modules.battery import Module + +class MockOpen(object): + def __init__(self): + self._value = "" + + def returns(self, value): + self._value = value + + def __enter__(self): + return self + + def __exit__(self, a, b, c): + pass + + def read(self): + return self._value + +class TestBatteryModule(unittest.TestCase): + def setUp(self): + self.engine = MockEngine() + self.config = MockConfig() + self.module = Module(engine=self.engine, config={ "config": self.config }) + for widget in self.module.widgets(): + widget.link_module(self.module) + + @mock.patch("sys.stdout") + def test_format(self, mock_output): + for widget in self.module.widgets(): + self.assertEquals(len(widget.full_text()), len("100%")) + + @mock.patch("os.path.exists") + @mock.patch("{}.open".format("__builtin__" if sys.version_info[0] < 3 else "builtins")) + @mock.patch("subprocess.Popen") + def test_critical(self, mock_output, mock_open, mock_exists): + mock_open.return_value = MockOpen() + mock_open.return_value.returns("19") + mock_exists.return_value = True + self.config.set("battery.critical", "20") + self.config.set("battery.warning", "25") + self.module.update(self.module.widgets()) + self.assertTrue("critical" in self.module.widgets()[0].state()) + + @mock.patch("os.path.exists") + @mock.patch("{}.open".format("__builtin__" if sys.version_info[0] < 3 else "builtins")) + @mock.patch("subprocess.Popen") + def test_warning(self, mock_output, mock_open, mock_exists): + mock_open.return_value = MockOpen() + mock_exists.return_value = True + mock_open.return_value.returns("22") + self.config.set("battery.critical", "20") + self.config.set("battery.warning", "25") + self.module.update(self.module.widgets()) + self.assertTrue("warning" in self.module.widgets()[0].state()) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/modules/test_battery.py b/tests/modules/test_battery.py new file mode 100644 index 0000000..6f31b16 --- /dev/null +++ b/tests/modules/test_battery.py @@ -0,0 +1,110 @@ +# pylint: disable=C0103,C0111 + +import sys +import mock +import unittest + +try: + from StringIO import StringIO +except ImportError: + from io import StringIO + +import tests.mocks as mocks + +from bumblebee.modules.battery import Module +from bumblebee.config import Config + +class TestBatteryModule(unittest.TestCase): + def setUp(self): + self._stdout = mock.patch("sys.stdout", new_callable=StringIO) + self._exists = mock.patch("bumblebee.modules.battery.os.path.exists") + self._open = mock.patch("bumblebee.modules.battery.open") + + self.stdout = self._stdout.start() + self.exists = self._exists.start() + self.open = self._open.start() + self.file = mock.Mock() + self.file.__enter__ = lambda x: self.file + self.file.__exit__ = lambda x, a, b, c: "" + self.open.return_value = self.file + + self.exists.return_value = True + self.engine = mock.Mock() + self.config = Config() + self.module = Module(engine=self.engine, config={"config":self.config}) + self.popen = mocks.MockPopen() + + self.config.set("battery.critical", "20") + self.config.set("battery.warning", "25") + self.criticalValue = "19" + self.warningValue = "21" + self.normalValue = "26" + self.chargedValue = "96" + + for widget in self.module.widgets(): + widget.link_module(self.module) + self.anyWidget = widget + + def tearDown(self): + self._stdout.stop() + self._exists.stop() + self._open.stop() + self.popen.cleanup() + + def test_format(self): + for widget in self.module.widgets(): + self.assertEquals(len(widget.full_text()), len("100%")) + + def test_critical(self): + self.file.read.return_value = self.criticalValue + self.module.update_all() + self.assertTrue("critical" in self.module.state(self.anyWidget)) + + def test_warning(self): + self.file.read.return_value = self.warningValue + self.module.update_all() + self.assertTrue("warning" in self.module.state(self.anyWidget)) + + def test_normal(self): + self.file.read.return_value = self.normalValue + self.module.update_all() + self.assertTrue(not "warning" in self.module.state(self.anyWidget)) + self.assertTrue(not "critical" in self.module.state(self.anyWidget)) + + def test_overload(self): + self.file.read.return_value = "120" + self.module.update_all() + self.assertTrue(not "warning" in self.module.state(self.anyWidget)) + self.assertTrue(not "critical" in self.module.state(self.anyWidget)) + self.assertEquals(self.module.capacity(self.anyWidget), "100%") + + def test_ac(self): + self.exists.return_value = False + self.module.update_all() + self.assertEquals(self.module.capacity(self.anyWidget), "ac") + self.assertTrue("AC" in self.module.state(self.anyWidget)) + + def test_error(self): + self.file.read.side_effect = IOError("failed to read") + self.module.update_all() + self.assertEquals(self.module.capacity(self.anyWidget), "n/a") + self.assertTrue("critical" in self.module.state(self.anyWidget)) + self.assertTrue("unknown" in self.module.state(self.anyWidget)) + + def test_charging(self): + self.file.read.return_value = self.chargedValue + self.module.update_all() + self.assertTrue("charged" in self.module.state(self.anyWidget)) + self.file.read.return_value = self.normalValue + self.module.update_all() + self.assertTrue("charging" in self.module.state(self.anyWidget)) + + def test_discharging(self): + for limit in [ 10, 25, 50, 80, 100 ]: + value = limit - 1 + self.file.read.return_value = str(value) + self.module.update_all() + self.file.read.return_value = "Discharging" + self.assertTrue("discharging-{}".format(limit) in self.module.state(self.anyWidget)) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4