-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_scand.py
108 lines (94 loc) · 4.14 KB
/
test_scand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""Test the scand (as best as we can)"""
# pylint: disable=missing-docstring,invalid-name,unnecessary-lambda
from collections import namedtuple
import mock
from nose.tools import eq_, raises
import scand
InputDeviceMock = namedtuple("InputDeviceMock", "name")
EventMock = namedtuple("EventMock", "type keystate scancode")
EventMock.__new__.__defaults__ = (0, 0, 42)
@mock.patch("graphiteudp.init")
def test_init_graphite(graphite_init):
scand.init_graphite("testserver", "testprefix")
graphite_init.assert_called_with("testserver", prefix="testprefix")
@mock.patch("scand.ConfigParser")
def test_parse_config(config_mock):
instance = config_mock.return_value
scand.parse_config()
instance.read.assert_called_with("scand.cfg")
@mock.patch("os.path.exists")
@mock.patch("sqlite3.connect")
def test_init_database_default_exists(connect_function, path_exists):
conn_mock = connect_function.return_value
path_exists.return_value = True
scand.init_database()
path_exists.assert_called_with('scans.sqlite3')
connect_function.assert_called_with('scans.sqlite3')
eq_(conn_mock.execute.called, False)
@mock.patch("os.path.exists")
@mock.patch("sqlite3.connect")
def test_init_database_default_missing(connect_function, path_exists):
conn_mock = connect_function.return_value
path_exists.return_value = False
scand.init_database()
path_exists.assert_called_with('scans.sqlite3')
connect_function.assert_called_with('scans.sqlite3')
eq_(conn_mock.execute.called, True)
@mock.patch("os.path.exists")
@mock.patch("sqlite3.connect")
def test_init_database_custom(connect_function, path_exists):
conn_mock = connect_function.return_value
path_exists.return_value = False
scand.init_database('TESTSCAN')
path_exists.assert_called_with('TESTSCAN')
connect_function.assert_called_with('TESTSCAN')
eq_(conn_mock.execute.called, True)
@mock.patch("scand.list_devices")
@mock.patch("scand.InputDevice")
def test_get_input_device_default_present(input_device, list_devices_function):
list_devices_function.return_value = ['TEST1', scand.SCANNER_NAME]
input_device.side_effect = lambda name: InputDeviceMock(name)
device = scand.get_input_device()
assert isinstance(device, InputDeviceMock)
eq_(device.name, scand.SCANNER_NAME)
@raises(IndexError)
@mock.patch("scand.list_devices")
@mock.patch("scand.InputDevice")
def test_get_input_device_default_missing(input_device, list_devices_function):
list_devices_function.return_value = ['TEST1', 'TEST2']
input_device.side_effect = lambda name: InputDeviceMock(name)
scand.get_input_device()
@mock.patch("scand.list_devices")
@mock.patch("scand.InputDevice")
def test_get_input_device_nondefault_present(input_device, list_devices_function):
list_devices_function.return_value = ['TEST1', 'TEST2', scand.SCANNER_NAME]
input_device.side_effect = lambda name: InputDeviceMock(name)
device = scand.get_input_device('TEST2')
assert isinstance(device, InputDeviceMock)
eq_(device.name, 'TEST2')
@mock.patch("scand.init_graphite")
@mock.patch("scand.init_database")
@mock.patch("scand.get_input_device")
@mock.patch("scand.categorize")
def test_main_no_input(categorize, get_input, init_db, init_graphite):
SAMPLE_CONFIG = {"graphite": None} # Doesn't emulate the config, but gets the job done
dev = get_input.return_value
dev.read_loop.return_value = [EventMock(0), EventMock(0)]
scand.main(SAMPLE_CONFIG)
eq_(dev.grab.called, True)
eq_(dev.read_loop.called, True)
eq_(categorize.called, False)
@mock.patch("scand.init_graphite")
@mock.patch("scand.init_database")
@mock.patch("scand.get_input_device")
@mock.patch("scand.categorize")
def test_main_input_invalid_keystate(categorize, get_input, init_db, init_graphite):
SAMPLE_CONFIG = {"graphite": None} # Doesn't emulate the config, but gets the job done
dev = get_input.return_value
dev.read_loop.return_value = [EventMock(scand.ecodes.EV_KEY, 0), EventMock(scand.ecodes.EV_KEY)]
data = categorize.return_value
data.keystate.return_val
scand.main(SAMPLE_CONFIG)
eq_(dev.grab.called, True)
eq_(dev.read_loop.called, True)
eq_(categorize.called, True)