forked from brendan-w/python-OBD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_commands.py
More file actions
82 lines (59 loc) · 2.56 KB
/
test_commands.py
File metadata and controls
82 lines (59 loc) · 2.56 KB
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
import obd
from obd.decoders import pid
def test_list_integrity():
for mode, cmds in enumerate(obd.commands.modes):
for pid, cmd in enumerate(cmds):
# make sure the command tables are in mode & PID order
assert mode == cmd.get_mode_int(), "Command is in the wrong mode list: %s" % cmd.name
assert pid == cmd.get_pid_int(), "The index in the list must also be the PID: %s" % cmd.name
# make sure all the fields are set
assert cmd.name != "", "Command names must not be null"
assert cmd.name.isupper(), "Command names must be upper case"
assert ' ' not in cmd.name, "No spaces allowed in command names"
assert cmd.desc != "", "Command description must not be null"
assert (mode >= 1) and (mode <= 9), "Mode must be in the range [1, 9] (decimal)"
assert (pid >= 0) and (pid <= 196), "PID must be in the range [0, 196] (decimal)"
assert cmd.bytes >= 0, "Number of return bytes must be >= 0"
assert hasattr(cmd.decode, '__call__'), "Decode is not callable"
def test_unique_names():
# make sure no two commands have the same name
names = {}
for cmds in obd.commands.modes:
for cmd in cmds:
assert not names.__contains__(cmd.name), "Two commands share the same name: %s" % cmd.name
names[cmd.name] = True
def test_getitem():
# ensure that __getitem__ works correctly
for cmds in obd.commands.modes:
for cmd in cmds:
# by [mode][pid]
mode = cmd.get_mode_int()
pid = cmd.get_pid_int()
assert cmd == obd.commands[mode][pid], "mode %d, PID %d could not be accessed through __getitem__" % (mode, pid)
# by [name]
assert cmd == obd.commands[cmd.name], "command name %s could not be accessed through __getitem__" % (cmd.name)
def test_contains():
for cmds in obd.commands.modes:
for cmd in cmds:
# by (command)
assert obd.commands.has_command(cmd)
# by (mode, pid)
mode = cmd.get_mode_int()
pid = cmd.get_pid_int()
assert obd.commands.has_pid(mode, pid)
# by (name)
assert obd.commands.has_name(cmd.name)
# by `in`
assert cmd.name in obd.commands
# test things NOT in the tables, or invalid parameters
assert 'modes' not in obd.commands
assert not obd.commands.has_pid(-1, 0)
assert not obd.commands.has_pid(1, -1)
assert not obd.commands.has_command("I'm a string, not an OBDCommand")
def test_pid_getters():
# ensure that all pid getters are found
pid_getters = obd.commands.pid_getters()
for cmds in obd.commands.modes:
for cmd in cmds:
if cmd.decode == pid:
assert cmd in pid_getters