forked from brendan-w/python-OBD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_OBD.py
More file actions
180 lines (130 loc) · 4.51 KB
/
test_OBD.py
File metadata and controls
180 lines (130 loc) · 4.51 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"""
Tests for the API layer
"""
import obd
from obd import ECU
from obd.OBDCommand import OBDCommand
from obd.decoders import noop
from obd.protocols.protocol import Message
from obd.utils import OBDStatus
class FakeELM:
"""
Fake ELM327 driver class for intercepting the commands from the API
"""
def __init__(self, port_name):
self._port_name = port_name
self._status = OBDStatus.CAR_CONNECTED
self._last_command = None
def port_name(self):
return self._port_name
def status(self):
return self._status
@staticmethod
def ecus():
return [ECU.ENGINE, ECU.UNKNOWN]
@staticmethod
def protocol_name():
return "ISO 15765-4 (CAN 11/500)"
@staticmethod
def protocol_id():
return "6"
def close(self):
pass
def send_and_parse(self, cmd):
# stow this, so we can check that the API made the right request
print(cmd)
self._last_command = cmd
# all commands succeed
message = Message([])
message.data = bytearray(b'response data')
message.ecu = ECU.ENGINE # picked engine so that simple commands like RPM will work
return [message]
def _test_last_command(self, expected):
r = self._last_command == expected
self._last_command = None
return r
# a toy command to test with
command = OBDCommand("Test_Command",
"A test command",
b"0123456789ABCDEF",
0,
noop,
ECU.ALL,
True)
def test_is_connected():
o = obd.OBD("/dev/null")
assert not o.is_connected()
# our fake ELM class always returns success for connections
o.interface = FakeELM("/dev/null")
assert o.is_connected()
def test_status():
"""
Make sure that the API's status() function reports the
same values as the underlying ELM327 class.
"""
o = obd.OBD("/dev/null")
assert o.status() == OBDStatus.NOT_CONNECTED
o.interface = None
assert o.status() == OBDStatus.NOT_CONNECTED
# we can manually set our fake ELM class to test
# the other values
o.interface = FakeELM("/dev/null")
o.interface._status = OBDStatus.ELM_CONNECTED
assert o.status() == OBDStatus.ELM_CONNECTED
o.interface._status = OBDStatus.CAR_CONNECTED
assert o.status() == OBDStatus.CAR_CONNECTED
def test_supports():
o = obd.OBD("/dev/null")
# since we haven't actually connected,
# no commands should be marked as supported
assert not o.supports(obd.commands.RPM)
o.supported_commands.add(obd.commands.RPM)
assert o.supports(obd.commands.RPM)
# commands that aren't in python-OBD's tables are unsupported by default
assert not o.supports(command)
def test_port_name():
"""
Make sure that the API's port_name() function reports the
same values as the underlying ELM327 class.
"""
o = obd.OBD("/dev/null")
o.interface = FakeELM("/dev/null")
assert o.port_name() == o.interface._port_name
o.interface = FakeELM("A different port name")
assert o.port_name() == o.interface._port_name
def test_protocol_name():
o = obd.OBD("/dev/null")
o.interface = None
assert o.protocol_name() == ""
o.interface = FakeELM("/dev/null")
assert o.protocol_name() == o.interface.protocol_name()
def test_protocol_id():
o = obd.OBD("/dev/null")
o.interface = None
assert o.protocol_id() == ""
o.interface = FakeELM("/dev/null")
assert o.protocol_id() == o.interface.protocol_id()
"""
The following tests are for the query() function
"""
def test_force():
o = obd.OBD("/dev/null", fast=False) # disable the trailing response count byte
o.interface = FakeELM("/dev/null")
r = o.query(obd.commands.RPM)
assert r.is_null()
assert o.interface._test_last_command(None)
r = o.query(obd.commands.RPM, force=True)
assert not r.is_null()
assert o.interface._test_last_command(obd.commands.RPM.command)
# a command that isn't in python-OBD's tables
r = o.query(command)
assert r.is_null()
assert o.interface._test_last_command(None)
o.query(command, force=True)
assert o.interface._test_last_command(command.command)
def test_fast():
o = obd.OBD("/dev/null", fast=False)
o.interface = FakeELM("/dev/null")
assert command.fast
o.query(command, force=True) # force since this command isn't in the tables
# assert o.interface._test_last_command(command.command)