def test_v1_single_channel_error(): transceiver = MagicMock() transceiver.API_VERSION = 1 transceiver.channel_count = None transceiver.transceive.return_value = (3, Exception("timeout"), b"") connection = I2cConnection(transceiver) with pytest.raises(I2cTimeoutError): connection.execute(0x42, I2cCommand(b"\x55", 3, 0.1, 0.2))
def test_v1_single_channel_execute_wait_post_process_false(): transceiver = MagicMock() transceiver.API_VERSION = 1 transceiver.channel_count = None transceiver.transceive.return_value = (0, None, b"\x11\x22\x33") connection = I2cConnection(transceiver) response = connection.execute(0x42, I2cCommand(b"\x55", 3, 0.1, 0.2, 0.1), wait_post_process=False) assert response == b"\x11\x22\x33"
def test_single_channel_with_connection(): transceiver = MagicMock() transceiver.API_VERSION = 1 transceiver.channel_count = None transceiver.transceive.return_value = (0, None, b"\x11\x22") connection = I2cConnection(transceiver) device = I2cDevice(connection, 0x42) assert device.connection == connection assert device.slave_address == 0x42 assert device.execute(I2cCommand(b"\x55", 3, 0.1, 0.2)) == b"\x11\x22"
def test_v1_single_channel_execute(): transceiver = MagicMock() transceiver.API_VERSION = 1 transceiver.channel_count = None transceiver.transceive.return_value = (0, None, b"\x11\x22\x33") connection = I2cConnection(transceiver) response = connection.execute(0x42, I2cCommand(b"\x55", 3, 0.1, 0.2)) args = [kwargs for args, kwargs in transceiver.transceive.call_args_list] assert args == [ { "slave_address": 0x42, "tx_data": b"\x55", "rx_length": 3, "read_delay": 0.1, "timeout": 0.2, }, ] assert response == b"\x11\x22\x33"
def test_v1_multi_channel_execute(): transceiver = MagicMock() transceiver.API_VERSION = 1 transceiver.channel_count = 2 transceiver.transceive.return_value = [ (0, None, b"\x11\x22\x33"), (2, Exception("NACK"), b""), ] connection = I2cConnection(transceiver) response = connection.execute(0x42, I2cCommand(b"\x55", 3, 0.1, 0.2)) args = [kwargs for args, kwargs in transceiver.transceive.call_args_list] assert args == [ { "slave_address": 0x42, "tx_data": b"\x55", "rx_length": 3, "read_delay": 0.1, "timeout": 0.2, }, ] assert len(response) == 2 assert response[0] == b"\x11\x22\x33" assert type(response[1]) is I2cNackError
def test_multi_channel(): connection = MagicMock() connection.read.return_value = ["foo1", "foo2"] connection.execute.return_value = ["bar1", "bar2"] device = I2cDevice(connection, 0x42) assert device.execute(I2cCommand(b"\x55", 3, 0.1, 0.2)) == ["bar1", "bar2"]
def test_interpret_response_empty(): cmd = I2cCommand(b"\x11", 42, 0.1, 0.2, 0.0) response = cmd.interpret_response(b"") assert response is None
def test_post_processing_time_float(): cmd = I2cCommand(b"\x11", 42, 0.1, 0.2, 2.3) assert type(cmd.post_processing_time) is float assert cmd.post_processing_time == 2.3
def test_post_processing_time_int(): cmd = I2cCommand(b"\x11", 42, 1, 2, 5) assert type(cmd.post_processing_time) is float assert cmd.post_processing_time == 5.0
def test_post_processing_time_defaults_to_zero(): cmd = I2cCommand(b"\x11", 42, 1, 2) # post processing time not passed assert type(cmd.post_processing_time) is float assert cmd.post_processing_time == 0.0
def test_tx_data_bytearray(): cmd = I2cCommand(bytearray([0x11, 0x22]), 42, 0.1, 0.2, 0.0) assert type(cmd.tx_data) is bytes assert cmd.tx_data == b"\x11\x22"
def test_timeout_int(): cmd = I2cCommand(b"\x11", 42, 1, 2, 0.0) assert type(cmd.timeout) is float assert cmd.timeout == 2.0
def test_read_delay_float(): cmd = I2cCommand(b"\x11", 42, 0.1, 0.2, 0.0) assert type(cmd.read_delay) is float assert cmd.read_delay == 0.1
def test_rx_length_int(): cmd = I2cCommand(b"\x11", 42, 0.1, 0.2, 0.0) assert type(cmd.rx_length) is int assert cmd.rx_length == 42
def test_rx_length_none(): cmd = I2cCommand(b"\x11", None, 0.1, 0.2, 0.0) assert cmd.rx_length is None
def test_tx_data_list(): cmd = I2cCommand([0x11, 0x22], 42, 0.1, 0.2, 0.0) assert type(cmd.tx_data) is bytes assert cmd.tx_data == b"\x11\x22"
def test_tx_data_empty_bytes(): cmd = I2cCommand(b"", 42, 0.1, 0.2, 0.0) assert type(cmd.tx_data) is bytes assert cmd.tx_data == b""
def test_tx_data_none(): cmd = I2cCommand(None, 42, 0.1, 0.2, 0.0) assert cmd.tx_data is None
def test_tx_data_bytes(): cmd = I2cCommand(b"\x11\x22", 42, 0.1, 0.2, 0.0) assert type(cmd.tx_data) is bytes assert cmd.tx_data == b"\x11\x22"
def test_timeout_float(): cmd = I2cCommand(b"\x11", 42, 0.1, 0.2, 0.0) assert type(cmd.timeout) is float assert cmd.timeout == 0.2
def test_interpret_response_bytes(): cmd = I2cCommand(b"\x11", 42, 0.1, 0.2, 0.0) response = cmd.interpret_response(b"\x55\x66") assert type(response) is bytes assert response == b"\x55\x66"