def test_read_serial_line_no_data() -> None: """Check that a communication error is thrown if we get no data.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) serial = cast(MotorSerial, backend._serial) serial.check_data_sent_by_constructor() with pytest.raises(CommunicationError): backend.read_serial_line()
def test_read_serial_line() -> None: """Test that we can we lines from the serial interface.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) serial = cast(MotorSerial, backend._serial) serial.check_data_sent_by_constructor() serial.append_received_data(b"Green Beans", newline=True) data = backend.read_serial_line() assert data == "Green Beans"
def test_backend_discover() -> None: """Test that the backend can discover boards.""" found_boards = SRV4MotorBoardHardwareBackend.discover( mock_comports, MotorSerial) assert len(found_boards) == 2 assert all(type(board) is MotorBoard for board in found_boards) assert all((int(board.serial_number[6:])) < 2 for board in found_boards)
def test_backend_initialisation() -> None: """Test that we can initialise a backend.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) assert type(backend) is SRV4MotorBoardHardwareBackend assert type(backend._serial) is MotorSerial assert len(backend._state) == 2 assert all(state == MotorSpecialState.BRAKE for state in backend._state)
def test_brake_motors_at_deletion() -> None: """Test that both motors are set to BRAKE when the backend is garbage collected.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) serial = cast(MotorSerial, backend._serial) serial.check_data_sent_by_constructor() del backend serial.check_sent_data( b'\x02\x02' # Brake motor 0 b'\x03\x02', # Brake motor 1 )
def test_backend_send_command_bad_write() -> None: """Test that an error is thrown if we can't write bytes.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) bad_serial_driver = MotorSerialBadWrite("COM0", baudrate=1000000, timeout=0.25) backend._serial = bad_serial_driver with pytest.raises(CommunicationError): backend.send_command(4)
def test_backend_send_command() -> None: """Test that the backend can send commands.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) serial = cast(MotorSerial, backend._serial) serial.check_data_sent_by_constructor() backend.send_command(4) serial.check_sent_data(b"\x04") backend.send_command(2, 100) serial.check_sent_data(b'\x02d')
def test_safe_shutdown_serial_start_crash() -> None: """Test that the _shutdown on the board doesn't break during start.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) # This line simulates the backend never initialising the state del backend._state # Check it worked. assert not hasattr(backend, "_state") # Now force a deconstruction event. del backend
def test_get_firmware_version() -> None: """Test that we can get the firmware version from the serial interface.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) serial = cast(MotorSerial, backend._serial) serial.check_data_sent_by_constructor() assert backend.firmware_version == "3" serial.check_sent_data(b'\x01') serial.append_received_data(b'PBV4C:5', newline=True) with pytest.raises(CommunicationError): backend.firmware_version serial.check_sent_data(b'\x01')
def test_backend_send_command_bad_write() -> None: """Test that an error is thrown if we can't write bytes.""" # Use a good serial driver for the initialisation backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) good_serial_driver = backend._serial # Swap it for a bad one. bad_serial_driver = MotorSerialBadWrite("COM0", baudrate=1000000, timeout=0.25) backend._serial = bad_serial_driver with pytest.raises(CommunicationError): backend.send_command(4) # Use the good serial driver for a graceful shutdown backend._serial = good_serial_driver
def test_get_set_motor_state() -> None: """Test that we can get and set the motor state.""" backend = SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerial) serial = cast(MotorSerial, backend._serial) serial.check_data_sent_by_constructor() assert backend.get_motor_state(0) == MotorSpecialState.BRAKE assert backend.get_motor_state(1) == MotorSpecialState.BRAKE backend.set_motor_state(0, 0.65) serial.check_sent_data(b'\x02\xd1') assert backend.get_motor_state(0) == 0.65 backend.set_motor_state(0, 1.0) serial.check_sent_data(b'\x02\xfd') assert backend.get_motor_state(0) == 1.0 backend.set_motor_state(0, -1.0) serial.check_sent_data(b'\x02\x03') assert backend.get_motor_state(0) == -1.0 backend.set_motor_state(0, MotorSpecialState.BRAKE) serial.check_sent_data(b'\x02\x02') assert backend.get_motor_state(0) == MotorSpecialState.BRAKE backend.set_motor_state(0, MotorSpecialState.COAST) serial.check_sent_data(b'\x02\x01') assert backend.get_motor_state(0) == MotorSpecialState.COAST with pytest.raises(ValueError): backend.set_motor_state(0, 20.0) serial.check_sent_data(b'') with pytest.raises(ValueError): backend.set_motor_state(2, 0.0) serial.check_sent_data(b'')
def test_backend_bad_firmware_version() -> None: """Test that we can detect a bad firmware version.""" with pytest.raises(CommunicationError): SRV4MotorBoardHardwareBackend("COM0", serial_class=MotorSerialBadFirmware)