Skip to content

Commit c0f0c9b

Browse files
author
Brendan Whitfield
committed
wrote initial tests for OBDCommand objects
1 parent 31e50bc commit c0f0c9b

6 files changed

Lines changed: 60 additions & 7 deletions

File tree

obd/commands.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def compute(self, _data):
8282

8383
# constrain number of bytes in response
8484
if (self.bytes > 0): # zero bytes means flexible response
85-
constrainHex(_data, self.bytes)
85+
_data = constrainHex(_data, self.bytes)
8686

8787
# decoded value into the response object
8888
r.set(self.decode(_data))

obd/decoders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def noop(_hex):
5252

5353
# hex in, bitstring out
5454
def pid(_hex):
55-
v = bitstring(_hex)
55+
v = bitstring(_hex, len(_hex) * 4)
5656
return (v, Unit.NONE)
5757

5858
'''

obd/obd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def load_commands(self):
9898

9999
response = self.query(get) # ask nicely
100100

101-
if response.isEmpty():
101+
if response.isNull():
102102
continue
103103

104104
supported = response.value # string of binary 01010101010101

obd/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def __init__(self, raw_data=""):
6363
self.unit = Unit.NONE
6464
self.raw_data = raw_data
6565

66-
def isEmpty(self):
67-
return (self.value == None) or (len(self.raw_data) == 0)
66+
def isNull(self):
67+
return (self.value == "No Data") or (len(self.raw_data) == 0)
6868

6969
def set(self, decode):
7070
self.value = decode[0]

tests/test_OBDCommand.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
from obd.commands import OBDCommand
3+
from obd.decoders import noop
4+
5+
6+
def test_basic_OBDCommand():
7+
# name description mode cmd bytes decoder
8+
cmd = OBDCommand("Test", "example OBD command", "01", "23", 2, noop)
9+
assert cmd.name == "Test"
10+
assert cmd.desc == "example OBD command"
11+
assert cmd.mode == "01"
12+
assert cmd.pid == "23"
13+
assert cmd.bytes == 2
14+
assert cmd.decode == noop
15+
assert cmd.supported == False
16+
17+
assert cmd.get_command() == "0123"
18+
assert cmd.get_mode_int() == 1
19+
assert cmd.get_pid_int() == 35
20+
21+
cmd = OBDCommand("Test", "example OBD command", "01", "23", 2, noop, True)
22+
assert cmd.supported == True
23+
24+
25+
def test_data_stripping():
26+
# name description mode cmd bytes decoder
27+
cmd = OBDCommand("Test", "example OBD command", "01", "00", 2, noop)
28+
r = cmd.compute("41 00 01 01\r\n")
29+
assert not r.isNull()
30+
assert r.value == "0101"
31+
32+
33+
def test_data_not_hex():
34+
# name description mode cmd bytes decoder
35+
cmd = OBDCommand("Test", "example OBD command", "01", "00", 2, noop)
36+
r = cmd.compute("41 00 wx yz\r\n")
37+
assert r.isNull()
38+
39+
40+
def test_data_length():
41+
# name description mode cmd bytes decoder
42+
cmd = OBDCommand("Test", "example OBD command", "01", "00", 2, noop)
43+
r = cmd.compute("41 00 01 23 45\r\n")
44+
assert r.value == "0123"
45+
r = cmd.compute("41 00 01\r\n")
46+
assert r.value == "0100"

tests/test_decoders.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import obd.decoders as d
44

55

6+
def test_noop():
7+
assert d.noop("No Operation") == ("No Operation", Unit.NONE)
8+
9+
def test_pid():
10+
assert d.pid("00000000") == ("00000000000000000000000000000000", Unit.NONE)
11+
assert d.pid("F00AA00F") == ("11110000000010101010000000001111", Unit.NONE)
12+
613
def test_count():
714
assert d.count("0") == (0, Unit.COUNT)
815
assert d.count("F") == (15, Unit.COUNT)
@@ -64,8 +71,8 @@ def test_evap_pressure():
6471
#assert d.evap_pressure("0000") == (0.0, Unit.PA)
6572

6673
def test_abs_evap_pressure():
67-
assert d.abs_evap_pressure("0000") == (0, Unit.KPA)
68-
assert d.abs_evap_pressure("FFFF") == (327, Unit.KPA)
74+
assert d.abs_evap_pressure("0000") == (0, Unit.KPA)
75+
assert d.abs_evap_pressure("FFFF") == (327.675, Unit.KPA)
6976

7077
def test_evap_pressure_alt():
7178
assert d.evap_pressure_alt("0000") == (-32767, Unit.PA)

0 commit comments

Comments
 (0)