def test_data_length(): # name description mode cmd bytes decoder cmd = OBDCommand("Test", "example OBD command", "01", "00", 2, noop) r = cmd.compute("41 00 01 23 45\r\n") assert r.value == "0123" r = cmd.compute("41 00 01\r\n") assert r.value == "0100"
def test_clone(): # name description mode cmd bytes decoder cmd = OBDCommand("Test", "example OBD command", "01", "23", 2, noop) other = cmd.clone() assert cmd.name == other.name assert cmd.desc == other.desc assert cmd.mode == other.mode assert cmd.pid == other.pid assert cmd.bytes == other.bytes assert cmd.decode == other.decode assert cmd.supported == cmd.supported
def test_clone(): # name description mode cmd bytes decoder cmd = OBDCommand("", "", "01", "23", 2, noop) other = cmd.clone() assert cmd.name == other.name assert cmd.desc == other.desc assert cmd.mode == other.mode assert cmd.pid == other.pid assert cmd.bytes == other.bytes assert cmd.decode == other.decode assert cmd.supported == cmd.supported
def test_constructor(): # name description mode cmd bytes decoder cmd = OBDCommand("Test", "example OBD command", "01", "23", 2, noop) assert cmd.name == "Test" assert cmd.desc == "example OBD command" assert cmd.mode == "01" assert cmd.pid == "23" assert cmd.bytes == 2 assert cmd.decode == noop assert cmd.supported == False assert cmd.get_command() == "0123" assert cmd.get_mode_int() == 1 assert cmd.get_pid_int() == 35 cmd = OBDCommand("Test", "example OBD command", "01", "23", 2, noop, True) assert cmd.supported == True
def test_call(): p = SAE_J1850_PWM() m = p(["48 6B 10 41 00 BE 1F B8 11 AA"]) # parse valid data into response object # valid response size cmd = OBDCommand("", "", "01", "23", 4, noop) r = cmd(m[0]) assert r.value == "BE1FB811" # response too short (pad) cmd = OBDCommand("", "", "01", "23", 5, noop) r = cmd(m[0]) assert r.value == "BE1FB81100" # response too long (clip) cmd = OBDCommand("", "", "01", "23", 3, noop) r = cmd(m[0]) assert r.value == "BE1FB8"
def test_data_not_hex(): # name description mode cmd bytes decoder cmd = OBDCommand("Test", "example OBD command", "01", "00", 2, noop) r = cmd.compute("41 00 wx yz\r\n") assert r.is_null()
def test_data_stripping(): # name description mode cmd bytes decoder cmd = OBDCommand("Test", "example OBD command", "01", "00", 2, noop) r = cmd.compute("41 00 01 01\r\n") assert not r.is_null() assert r.value == "0101"
def test_get_pid_int(): cmd = OBDCommand("", "", "01", "23", 4, noop) assert cmd.get_pid_int() == 0x23 cmd = OBDCommand("", "", "01", "", 4, noop) assert cmd.get_pid_int() == 0
def test_get_mode_int(): cmd = OBDCommand("", "", "01", "23", 4, noop) assert cmd.get_mode_int() == 0x01 cmd = OBDCommand("", "", "", "23", 4, noop) assert cmd.get_mode_int() == 0
def test_get_command(): cmd = OBDCommand("", "", "01", "23", 4, noop) assert cmd.get_command() == "0123" # simple concat of mode and PID
def test_query(): # we don't need an actual serial connection o = obd.OBD("/dev/null") # forge our own command, to control the output cmd = OBDCommand("TEST", "Test command", "01", "23", 2, noop, False) # forge IO from the car by overwriting the read/write functions # buffers toCar = [ "" ] # needs to be inside mutable object to allow assignment in closure fromCar = "" def write(cmd): toCar[0] = cmd o.is_connected = lambda *args: True o.port.is_connected = lambda *args: True o.port._ELM327__protocol = SAE_J1850_PWM() o.port._ELM327__primary_ecu = 0x10 o.port._ELM327__write = write o.port._ELM327__read = lambda *args: fromCar # make sure unsupported commands don't write ------------------------------ fromCar = ["48 6B 10 41 23 AB CD 10"] r = o.query(cmd) assert toCar[0] == "" assert r.is_null() # a correct command transaction ------------------------------------------- fromCar = ["48 6B 10 41 23 AB CD 10"] # preset the response r = o.query(cmd, force=True) # run assert toCar[0] == "0123" # verify that the command was sent correctly assert not r.is_null() assert r.value == "ABCD" # verify that the response was parsed correctly # response of greater length ---------------------------------------------- fromCar = ["48 6B 10 41 23 AB CD EF 10"] r = o.query(cmd, force=True) assert toCar[0] == "0123" assert r.value == "ABCD" # response of lesser length ----------------------------------------------- fromCar = ["48 6B 10 41 23 AB 10"] r = o.query(cmd, force=True) assert toCar[0] == "0123" assert r.value == "AB00" # NO DATA response -------------------------------------------------------- fromCar = ["NO DATA"] r = o.query(cmd, force=True) assert r.is_null() # malformed response ------------------------------------------------------ fromCar = ["totaly not hex!@#$"] r = o.query(cmd, force=True) assert r.is_null() # no response ------------------------------------------------------------- fromCar = [""] r = o.query(cmd, force=True) assert r.is_null() # reject responses from other ECUs --------------------------------------- fromCar = ["48 6B 12 41 23 AB CD 10"] r = o.query(cmd, force=True) assert toCar[0] == "0123" assert r.is_null() # filter for primary ECU -------------------------------------------------- fromCar = ["48 6B 12 41 23 AB CD 10", "48 6B 10 41 23 AB CD 10"] r = o.query(cmd, force=True) assert toCar[0] == "0123" assert r.value == "ABCD" '''