[tests/battery] Add tests for battery module

This commit is contained in:
Tobi-wan Kenobi 2017-03-05 09:35:56 +01:00
parent 728cb73189
commit 8724af2906
5 changed files with 179 additions and 0 deletions

View file

@ -52,6 +52,9 @@ class Module(object):
"""By default, update() is a NOP""" """By default, update() is a NOP"""
pass pass
def update_all(self):
self.update(self._widgets)
def parameter(self, name, default=None): def parameter(self, name, default=None):
"""Return the config parameter 'name' for this module""" """Return the config parameter 'name' for this module"""
name = "{}.{}".format(self.name, name) name = "{}.{}".format(self.name, name)

0
tests/__init__.py Normal file
View file

View file

66
tests/modules/backup.py Normal file
View file

@ -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

View file

@ -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