forked from brendan-w/python-OBD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_OBDCommand.py
More file actions
82 lines (61 loc) · 2.58 KB
/
test_OBDCommand.py
File metadata and controls
82 lines (61 loc) · 2.58 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
from obd.OBDCommand import OBDCommand
from obd.decoders import noop
from obd.protocols import *
def test_constructor():
# default constructor
# name description cmd bytes decoder ECU
cmd = OBDCommand("Test", "example OBD command", b"0123", 2, noop, ECU.ENGINE)
assert cmd.name is "Test"
assert cmd.desc is "example OBD command"
assert cmd.command == b"0123"
assert cmd.bytes == 2
assert cmd.decode == noop
assert cmd.ecu == ECU.ENGINE
assert cmd.fast is False
assert cmd.mode == 1
assert cmd.pid == 35
# a case where "fast", and "supported" were set explicitly
# name description cmd bytes decoder ECU fast
cmd = OBDCommand("Test 2", "example OBD command", b"0123", 2, noop, ECU.ENGINE, True)
assert cmd.fast is True
def test_clone():
# name description mode cmd bytes decoder
cmd = OBDCommand("", "", b"0123", 2, noop, ECU.ENGINE)
other = cmd.clone()
assert cmd.name == other.name
assert cmd.desc == other.desc
assert cmd.command == other.command
assert cmd.bytes == other.bytes
assert cmd.decode == other.decode
assert cmd.ecu == other.ecu
assert cmd.fast == cmd.fast
def test_call():
p = SAE_J1850_PWM(["48 6B 10 41 00 FF FF FF FF AA"]) # train the ecu_map to identify the engine
messages = p(["48 6B 10 41 00 BE 1F B8 11 AA"]) # parse valid data into response object
print(messages[0].data)
# valid response size
cmd = OBDCommand("", "", b"0123", 6, noop, ECU.ENGINE)
r = cmd(messages)
assert r.value == bytearray([0x41, 0x00, 0xBE, 0x1F, 0xB8, 0x11])
# response too short (pad)
cmd = OBDCommand("", "", b"0123", 7, noop, ECU.ENGINE)
r = cmd(messages)
assert r.value == bytearray([0x41, 0x00, 0xBE, 0x1F, 0xB8, 0x11, 0x00])
# response too long (clip)
cmd = OBDCommand("", "", b"0123", 5, noop, ECU.ENGINE)
r = cmd(messages)
assert r.value == bytearray([0x41, 0x00, 0xBE, 0x1F, 0xB8])
def test_get_mode():
cmd = OBDCommand("", "", b"0123", 4, noop, ECU.ENGINE)
assert cmd.mode == 0x01
cmd = OBDCommand("", "", b"", 4, noop, ECU.ENGINE)
assert cmd.mode == None
cmd = OBDCommand("", "", b"totally not hex", 4, noop, ECU.ENGINE)
assert cmd.mode == None
def test_pid():
cmd = OBDCommand("", "", b"0123", 4, noop, ECU.ENGINE)
assert cmd.pid == 0x23
cmd = OBDCommand("", "", b"01", 4, noop, ECU.ENGINE)
assert cmd.pid == None
cmd = OBDCommand("", "", b"totally not hex", 4, noop, ECU.ENGINE)
assert cmd.mode == None