[tests] Added engine and module tests

Added those two unit tests together, as they are tightly linked together
anyhow.
This commit is contained in:
Tobi-wan Kenobi 2017-03-04 13:44:51 +01:00
parent f65ab6bcae
commit 64523119af
3 changed files with 234 additions and 0 deletions

View file

@ -4,6 +4,8 @@ import mock
import shlex import shlex
import subprocess import subprocess
from bumblebee.output import Widget
class MockPopen(object): class MockPopen(object):
def __init__(self, module): def __init__(self, module):
self._patch = mock.patch("{}.subprocess.Popen".format(module)) self._patch = mock.patch("{}.subprocess.Popen".format(module))
@ -24,4 +26,64 @@ class MockPopen(object):
def cleanup(self): def cleanup(self):
self._patch.stop() self._patch.stop()
class MockInput(object):
def __init__(self):
self._callbacks = {}
def start(self):
pass
def stop(self):
pass
def get_callback(self, uid):
return self._callbacks.get(uid, None)
def register_callback(self, obj, button, cmd):
if not obj:
return
self._callbacks[obj.id] = {
"button": button,
"command": cmd,
}
class MockOutput(object):
def start(self):
pass
def stop(self):
pass
def draw(self, widget, engine, module):
engine.stop()
def begin(self):
pass
def flush(self):
pass
def end(self):
pass
class MockEngine(object):
def __init__(self):
self.input = MockInput()
class MockWidget(Widget):
def __init__(self, text):
super(MockWidget, self).__init__(text)
self._text = text
self.module = None
self.attr_state = ["state-default"]
self.id = "none"
def state(self):
return self.attr_state
def update(self, widgets):
pass
def full_text(self):
return self._text
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

83
tests/test_engine.py Normal file
View file

@ -0,0 +1,83 @@
# pylint: disable=C0103,C0111,W0703,W0212
import shlex
import unittest
from bumblebee.error import ModuleLoadError
from bumblebee.engine import Engine
from bumblebee.config import Config
import bumblebee.input
from mocks import MockOutput, MockInput
class TestEngine(unittest.TestCase):
def setUp(self):
self.engine = Engine(config=Config(), output=MockOutput(), inp=MockInput())
self.singleWidgetModule = [{"module": "test", "name": "a"}]
self.testModule = "test"
self.invalidModule = "no-such-module"
self.testModuleSpec = "bumblebee.modules.{}".format(self.testModule)
self.testModules = [
{"module": "test", "name": "a"},
{"module": "test", "name": "b"},
]
def test_stop(self):
self.assertTrue(self.engine.running())
self.engine.stop()
self.assertFalse(self.engine.running())
def test_load_module(self):
module = self.engine._load_module(self.testModule)
self.assertEquals(module.__module__, self.testModuleSpec)
def test_load_invalid_module(self):
with self.assertRaises(ModuleLoadError):
self.engine._load_module(self.invalidModule)
def test_load_none(self):
with self.assertRaises(ModuleLoadError):
self.engine._load_module(None)
def test_load_modules(self):
modules = self.engine.load_modules(self.testModules)
self.assertEquals(len(modules), len(self.testModules))
self.assertEquals(
[module.__module__ for module in modules],
[self.testModuleSpec for module in modules]
)
def test_run(self):
self.engine.load_modules(self.singleWidgetModule)
try:
self.engine.run()
except Exception as e:
self.fail(e)
def test_custom_cmd(self):
testmodules = [
{ "name": "test", "button": "test.left-click", "action": "echo" },
{ "name": "test:alias", "button": "alias.right-click", "action": "echo2" },
]
cmd = "-m"
for test in testmodules:
cmd += " " + test["name"]
cmd += " -p"
for test in testmodules:
cmd += " " + test["button"] + "=" + test["action"]
cfg = Config(shlex.split(cmd))
inp = MockInput()
engine = Engine(config=cfg, output=MockOutput(), inp=inp)
i = 0
for module in engine.modules():
callback = inp.get_callback(module.id)
self.assertTrue(callback is not None)
self.assertEquals(callback["command"], testmodules[i]["action"])
if "left" in testmodules[i]["button"]:
self.assertTrue(callback["button"], bumblebee.input.LEFT_MOUSE)
if "right" in testmodules[i]["button"]:
self.assertTrue(callback["button"], bumblebee.input.RIGHT_MOUSE)
i += 1
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

89
tests/test_module.py Normal file
View file

@ -0,0 +1,89 @@
# pylint: disable=C0103,C0111,W0703
import unittest
from bumblebee.engine import Module
from bumblebee.config import Config
from mocks import MockWidget
class TestModule(unittest.TestCase):
def setUp(self):
self.widgetName = "foo"
self.widget = MockWidget(self.widgetName)
self.config = Config()
self.anyWidgetName = "random-widget-name"
self.noSuchModule = "this-module-does-not-exist"
self.moduleWithoutWidgets = Module(engine=None, widgets=None)
self.moduleWithOneWidget = Module(engine=None, widgets=self.widget, config={"config": self.config})
self.moduleWithMultipleWidgets = Module(engine=None,
widgets=[self.widget, self.widget, self.widget]
)
self.anyConfigName = "cfg"
self.anotherConfigName = "cfg2"
self.anyModule = Module(engine=None, widgets=self.widget, config={
"name": self.anyConfigName, "config": self.config
})
self.anotherModule = Module(engine=None, widgets=self.widget, config={
"name": self.anotherConfigName, "config": self.config
})
self.anyKey = "some-parameter"
self.anyValue = "value"
self.anotherValue = "another-value"
self.emptyKey = "i-do-not-exist"
self.config.set("{}.{}".format(self.anyConfigName, self.anyKey), self.anyValue)
self.config.set("{}.{}".format(self.anotherConfigName, self.anyKey), self.anotherValue)
def test_empty_widgets(self):
self.assertEquals(self.moduleWithoutWidgets.widgets(), [])
def test_single_widget(self):
self.assertEquals(self.moduleWithOneWidget.widgets(), [self.widget])
def test_multiple_widgets(self):
for widget in self.moduleWithMultipleWidgets.widgets():
self.assertEquals(widget, self.widget)
def test_retrieve_widget_by_name(self):
widget = MockWidget(self.anyWidgetName)
widget.name = self.anyWidgetName
module = Module(engine=None, widgets=[self.widget, widget, self.widget])
retrievedWidget = module.widget(self.anyWidgetName)
self.assertEquals(retrievedWidget, widget)
def test_retrieve_widget_by_id(self):
widget = MockWidget(self.anyWidgetName)
widget.id = self.anyWidgetName
module = Module(engine=None, widgets=[self.widget, widget, self.widget])
retrievedWidget = module.widget_by_id(self.anyWidgetName)
self.assertEquals(retrievedWidget, widget)
def test_retrieve_missing_widget(self):
module = self.moduleWithMultipleWidgets
widget = module.widget(self.noSuchModule)
self.assertEquals(widget, None)
widget = module.widget_by_id(self.noSuchModule)
self.assertEquals(widget, None)
def test_threshold(self):
module = self.moduleWithOneWidget
module.name = self.widgetName
self.config.set("{}.critical".format(self.widgetName), 10.0)
self.config.set("{}.warning".format(self.widgetName), 8.0)
self.assertEquals("critical", module.threshold_state(10.1, 0, 0))
self.assertEquals("warning", module.threshold_state(10.0, 0, 0))
self.assertEquals(None, module.threshold_state(8.0, 0, 0))
self.assertEquals(None, module.threshold_state(7.9, 0, 0))
def test_parameters(self):
self.assertEquals(self.anyModule.parameter(self.anyKey), self.anyValue)
self.assertEquals(self.anotherModule.parameter(self.anyKey), self.anotherValue)
def test_default_parameters(self):
self.assertEquals(self.anyModule.parameter(self.emptyKey), None)
self.assertEquals(self.anyModule.parameter(self.emptyKey, self.anyValue), self.anyValue)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4