[tests] Minor refactoring

Draw some commonly-used assertion logic into common functions.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-10 19:08:29 +01:00
parent e15147fe10
commit 16a4613e57
4 changed files with 44 additions and 56 deletions

View file

@ -7,7 +7,7 @@ import mock
import bumblebee.input import bumblebee.input
from bumblebee.input import I3BarInput from bumblebee.input import I3BarInput
from bumblebee.modules.brightness import Module from bumblebee.modules.brightness import Module
from tests.util import MockEngine, MockConfig, assertPopen from tests.util import MockEngine, MockConfig, assertPopen, assertMouseEvent
class TestBrightnessModule(unittest.TestCase): class TestBrightnessModule(unittest.TestCase):
def setUp(self): def setUp(self):
@ -28,31 +28,19 @@ class TestBrightnessModule(unittest.TestCase):
@mock.patch("subprocess.Popen") @mock.patch("subprocess.Popen")
@mock.patch("sys.stdin") @mock.patch("sys.stdin")
def test_wheel_up(self, mock_input, mock_output, mock_select): def test_wheel_up(self, mock_input, mock_output, mock_select):
mock_input.readline.return_value = json.dumps({ assertMouseEvent(mock_input, mock_output, mock_select, self.engine,
"name": self.module.id, self.module, bumblebee.input.WHEEL_UP,
"button": bumblebee.input.WHEEL_UP, "xbacklight +2%"
"instance": None )
})
mock_select.return_value = (1,2,3)
self.engine.input.start()
self.engine.input.stop()
mock_input.readline.assert_any_call()
assertPopen(mock_output, "xbacklight +2%")
@mock.patch("select.select") @mock.patch("select.select")
@mock.patch("subprocess.Popen") @mock.patch("subprocess.Popen")
@mock.patch("sys.stdin") @mock.patch("sys.stdin")
def test_wheel_down(self, mock_input, mock_output, mock_select): def test_wheel_down(self, mock_input, mock_output, mock_select):
mock_input.readline.return_value = json.dumps({ assertMouseEvent(mock_input, mock_output, mock_select, self.engine,
"name": self.module.id, self.module, bumblebee.input.WHEEL_DOWN,
"button": bumblebee.input.WHEEL_DOWN, "xbacklight -2%"
"instance": None )
})
mock_select.return_value = (1,2,3)
self.engine.input.start()
self.engine.input.stop()
mock_input.readline.assert_any_call()
assertPopen(mock_output, "xbacklight -2%")
@mock.patch("select.select") @mock.patch("select.select")
@mock.patch("subprocess.Popen") @mock.patch("subprocess.Popen")
@ -60,13 +48,9 @@ class TestBrightnessModule(unittest.TestCase):
def test_custom_step(self, mock_input, mock_output, mock_select): def test_custom_step(self, mock_input, mock_output, mock_select):
self.config.set("brightness.step", "10") self.config.set("brightness.step", "10")
module = Module(engine=self.engine, config={ "config": self.config }) module = Module(engine=self.engine, config={ "config": self.config })
mock_input.readline.return_value = json.dumps({ assertMouseEvent(mock_input, mock_output, mock_select, self.engine,
"name": module.id, module, bumblebee.input.WHEEL_DOWN,
"button": bumblebee.input.WHEEL_DOWN, "xbacklight -10%"
"instance": None )
})
mock_select.return_value = (1,2,3) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
self.engine.input.start()
self.engine.input.stop()
mock_input.readline.assert_any_call()
assertPopen(mock_output, "xbacklight -10%")

View file

@ -7,7 +7,7 @@ import mock
import bumblebee.input import bumblebee.input
from bumblebee.input import I3BarInput from bumblebee.input import I3BarInput
from bumblebee.modules.cpu import Module from bumblebee.modules.cpu import Module
from tests.util import MockEngine, MockConfig, assertPopen from tests.util import MockEngine, MockConfig, assertPopen, assertMouseEvent, assertStateContains
class TestCPUModule(unittest.TestCase): class TestCPUModule(unittest.TestCase):
def setUp(self): def setUp(self):
@ -16,8 +16,6 @@ class TestCPUModule(unittest.TestCase):
self.engine.input.need_event = True self.engine.input.need_event = True
self.config = MockConfig() self.config = MockConfig()
self.module = Module(engine=self.engine, config={ "config": self.config }) 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") @mock.patch("sys.stdout")
def test_format(self, mock_output): def test_format(self, mock_output):
@ -28,31 +26,23 @@ class TestCPUModule(unittest.TestCase):
@mock.patch("subprocess.Popen") @mock.patch("subprocess.Popen")
@mock.patch("sys.stdin") @mock.patch("sys.stdin")
def test_leftclick(self, mock_input, mock_output, mock_select): def test_leftclick(self, mock_input, mock_output, mock_select):
mock_input.readline.return_value = json.dumps({ assertMouseEvent(mock_input, mock_output, mock_select, self.engine,
"name": self.module.id, self.module, bumblebee.input.LEFT_MOUSE,
"button": bumblebee.input.LEFT_MOUSE, "gnome-system-monitor"
"instance": None )
})
mock_select.return_value = (1,2,3)
self.engine.input.start()
self.engine.input.stop()
mock_input.readline.assert_any_call()
assertPopen(mock_output, "gnome-system-monitor")
@mock.patch("psutil.cpu_percent") @mock.patch("psutil.cpu_percent")
def test_warning(self, mock_psutil): def test_warning(self, mock_psutil):
self.config.set("cpu.critical", "20") self.config.set("cpu.critical", "20")
self.config.set("cpu.warning", "18") self.config.set("cpu.warning", "18")
mock_psutil.return_value = 19.0 mock_psutil.return_value = 19.0
self.module.update(self.module.widgets()) assertStateContains(self, self.module, "warning")
self.assertEquals(self.module.widgets()[0].state(), ["warning"])
@mock.patch("psutil.cpu_percent") @mock.patch("psutil.cpu_percent")
def test_critical(self, mock_psutil): def test_critical(self, mock_psutil):
self.config.set("cpu.critical", "20") self.config.set("cpu.critical", "20")
self.config.set("cpu.warning", "19") self.config.set("cpu.warning", "19")
mock_psutil.return_value = 21.0 mock_psutil.return_value = 21.0
self.module.update(self.module.widgets()) assertStateContains(self, self.module, "critical")
self.assertEquals(self.module.widgets()[0].state(), ["critical"])
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -7,7 +7,7 @@ import mock
import bumblebee.input import bumblebee.input
from bumblebee.input import I3BarInput from bumblebee.input import I3BarInput
from bumblebee.modules.disk import Module from bumblebee.modules.disk import Module
from tests.util import MockEngine, MockConfig, assertPopen from tests.util import MockEngine, MockConfig, assertPopen, assertStateContains
class MockVFS(object): class MockVFS(object):
def __init__(self, perc): def __init__(self, perc):
@ -23,8 +23,6 @@ class TestDiskModule(unittest.TestCase):
self.config = MockConfig() self.config = MockConfig()
self.config.set("disk.path", "somepath") self.config.set("disk.path", "somepath")
self.module = Module(engine=self.engine, config={"config": self.config}) self.module = Module(engine=self.engine, config={"config": self.config})
for widget in self.module.widgets():
widget.link_module(self.module)
@mock.patch("select.select") @mock.patch("select.select")
@mock.patch("subprocess.Popen") @mock.patch("subprocess.Popen")
@ -46,16 +44,13 @@ class TestDiskModule(unittest.TestCase):
self.config.set("disk.critical", "80") self.config.set("disk.critical", "80")
self.config.set("disk.warning", "70") self.config.set("disk.warning", "70")
mock_stat.return_value = MockVFS(75.0) mock_stat.return_value = MockVFS(75.0)
self.module.update(self.module.widgets()) assertStateContains(self, self.module, "warning")
self.assertEquals(self.module.widgets()[0].state(), ["warning"])
@mock.patch("os.statvfs") @mock.patch("os.statvfs")
def test_warning(self, mock_stat): def test_warning(self, mock_stat):
self.config.set("disk.critical", "80") self.config.set("disk.critical", "80")
self.config.set("disk.warning", "70") self.config.set("disk.warning", "70")
mock_stat.return_value = MockVFS(85.0) mock_stat.return_value = MockVFS(85.0)
self.module.update(self.module.widgets()) assertStateContains(self, self.module, "critical")
self.assertEquals(self.module.widgets()[0].state(), ["critical"])
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -1,5 +1,6 @@
# pylint: disable=C0103,C0111,W0613 # pylint: disable=C0103,C0111,W0613
import json
import shlex import shlex
import subprocess import subprocess
@ -16,6 +17,24 @@ def assertPopen(output, cmd):
stderr=subprocess.STDOUT stderr=subprocess.STDOUT
) )
def assertStateContains(test, module, state):
for widget in module.widgets():
widget.link_module(module)
module.update(module.widgets())
test.assertTrue(state in module.widgets()[0].state())
def assertMouseEvent(mock_input, mock_output, mock_select, engine, module, button, cmd):
mock_input.readline.return_value = json.dumps({
"name": module.id,
"button": button,
"instance": None
})
mock_select.return_value = (1,2,3)
engine.input.start()
engine.input.stop()
mock_input.readline.assert_any_call()
assertPopen(mock_output, cmd)
class MockInput(object): class MockInput(object):
def start(self): def start(self):
pass pass