def test_micropython_mode_find_device_no_ports(): """ There are no connected devices so return None. """ editor = mock.MagicMock() view = mock.MagicMock() mm = MicroPythonMode(editor, view) with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts', return_value=[]): assert mm.find_device() == (None, None)
def test_micropython_mode_find_device_no_ports(): """ There are no connected devices so return None. """ editor = mock.MagicMock() view = mock.MagicMock() mm = MicroPythonMode(editor, view) with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts', return_value=[]): assert mm.find_device() is None
def test_micropython_mode_add_plotter_exception(): """ Ensure that any non-IOError based exceptions are logged. """ editor = mock.MagicMock() view = mock.MagicMock() ex = Exception('BOOM') view.add_micropython_plotter = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value='COM0') with mock.patch('mu.modes.base.logger', return_value=None) as logger: mm.add_plotter() logger.error.assert_called_once_with(ex)
def test_micropython_mode_find_device_but_no_device(): """ None of the connected devices is a valid board so return None. """ editor = mock.MagicMock() view = mock.MagicMock() mm = MicroPythonMode(editor, view) mock_port = mock.MagicMock() mock_port.productIdentifier = mock.MagicMock(return_value=666) mock_port.vendorIdentifier = mock.MagicMock(return_value=999) with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts', return_value=[mock_port, ]): assert mm.find_device() is None
def test_micropython_mode_add_plotter_exception(): """ Ensure that any non-IOError based exceptions are logged. """ editor = mock.MagicMock() view = mock.MagicMock() ex = Exception('BOOM') view.add_micropython_plotter = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=('COM0', '12345')) with mock.patch('mu.modes.base.logger', return_value=None) as logger: mm.add_plotter() logger.error.assert_called_once_with(ex)
def test_micropython_mode_add_repl_exception(): """ Ensure that any non-IOError based exceptions are logged. """ editor = mock.MagicMock() view = mock.MagicMock() ex = Exception("BOOM") view.add_micropython_repl = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=("COM0", "12345")) with mock.patch("mu.modes.base.logger", return_value=None) as logger: mm.add_repl() logger.error.assert_called_once_with(ex)
def test_micropython_mode_add_plotter_no_port(): """ If it's not possible to find a connected micro:bit then ensure a helpful message is enacted. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=(None, None)) mm.add_plotter() assert view.show_message.call_count == 1 message = 'Could not find an attached device.' assert view.show_message.call_args[0][0] == message
def test_micropython_mode_add_plotter_no_port(): """ If it's not possible to find a connected micro:bit then ensure a helpful message is enacted. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=False) mm.add_plotter() assert view.show_message.call_count == 1 message = 'Could not find an attached device.' assert view.show_message.call_args[0][0] == message
def test_micropython_mode_find_microbit_no_device(): """ None of the connected devices is a valid board so return None. """ editor = mock.MagicMock() view = mock.MagicMock() mm = MicroPythonMode(editor, view) mock_port = mock.MagicMock() mock_port = mock.MagicMock() mock_port.productIdentifier = mock.MagicMock(return_value=666) mock_port.vendorIdentifier = mock.MagicMock(return_value=999) with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts', return_value=[mock_port, ]): assert mm.find_device() is None
def test_micropython_mode_add_plotter(): """ Nothing goes wrong so check the _view.add_micropython_plotter gets the expected argument. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() view.add_micropython_plotter = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value='COM0') with mock.patch('os.name', 'nt'): mm.add_plotter() assert view.show_message.call_count == 0 assert view.add_micropython_plotter.call_args[0][0] == 'COM0'
def test_micropython_mode_add_plotter(): """ Nothing goes wrong so check the _view.add_micropython_plotter gets the expected argument. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() view.add_micropython_plotter = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=('COM0', '12345')) with mock.patch('os.name', 'nt'): mm.add_plotter() assert view.show_message.call_count == 0 assert view.add_micropython_plotter.call_args[0][0] == 'COM0'
def test_micropython_mode_add_repl(): """ Nothing goes wrong so check the _view.add_micropython_repl gets the expected argument. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() view.add_micropython_repl = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=("COM0", "12345")) with mock.patch("os.name", "nt"): mm.add_repl() assert view.show_message.call_count == 0 assert view.add_micropython_repl.call_args[0][0] == "COM0"
def test_micropython_mode_add_plotter_ioerror(): """ Sometimes when attempting to connect to the device there is an IOError because it's still booting up or connecting to the host computer. In this case, ensure a useful message is displayed. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() ex = IOError('BOOM') view.add_micropython_plotter = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value=('COM0', '123456')) mm.add_plotter() assert view.show_message.call_count == 1 assert view.show_message.call_args[0][0] == str(ex)
def test_micropython_mode_add_plotter_ioerror(): """ Sometimes when attempting to connect to the device there is an IOError because it's still booting up or connecting to the host computer. In this case, ensure a useful message is displayed. """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() ex = IOError('BOOM') view.add_micropython_plotter = mock.MagicMock(side_effect=ex) mm = MicroPythonMode(editor, view) mm.find_device = mock.MagicMock(return_value='COM0') mm.add_plotter() assert view.show_message.call_count == 1 assert view.show_message.call_args[0][0] == str(ex)
def test_micropython_mode_add_repl_no_force_interrupt(): """ Nothing goes wrong so check the _view.add_micropython_repl gets the expected arguments (including the flag so no keyboard interrupt is called). """ editor = mock.MagicMock() view = mock.MagicMock() view.show_message = mock.MagicMock() view.add_micropython_repl = mock.MagicMock() mm = MicroPythonMode(editor, view) mm.force_interrupt = False mm.find_device = mock.MagicMock(return_value=('COM0', '12345')) with mock.patch('os.name', 'nt'): mm.add_repl() assert view.show_message.call_count == 0 assert view.add_micropython_repl.call_args[0][0] == 'COM0' assert view.add_micropython_repl.call_args[0][2] is False
def test_micropython_mode_find_device(): """ Ensure it's possible to detect a device and return the expected port. """ editor = mock.MagicMock() view = mock.MagicMock() mm = MicroPythonMode(editor, view) mock_port = mock.MagicMock() for vid, pid in mm.valid_boards: mock_port.vid = vid mock_port.productIdentifier = mock.MagicMock() mock_port.productIdentifier.return_value = pid mock_port.vendorIdentifier = mock.MagicMock() mock_port.vendorIdentifier.return_value = vid mock_port.portName = mock.MagicMock(return_value='COM0') with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts', return_value=[mock_port, ]): assert mm.find_device() == 'COM0'
def test_micropython_mode_find_device(): """ Ensure it's possible to detect a device and return the expected port. """ editor = mock.MagicMock() view = mock.MagicMock() mm = MicroPythonMode(editor, view) mock_port = mock.MagicMock() for vid, pid in mm.valid_boards: mock_port.vid = vid mock_port.productIdentifier = mock.MagicMock() mock_port.productIdentifier.return_value = pid mock_port.vendorIdentifier = mock.MagicMock() mock_port.vendorIdentifier.return_value = vid mock_port.portName = mock.MagicMock(return_value='COM0') mock_os = mock.MagicMock() mock_os.name = 'nt' with mock.patch('mu.modes.base.QSerialPortInfo.availablePorts', return_value=[mock_port, ]), \ mock.patch('mu.modes.base.os', mock_os): assert mm.find_device() == 'COM0'
def test_micropython_mode_find_device(): """ Ensure it's possible to detect a device and return the expected port. """ editor = mock.MagicMock() view = mock.MagicMock() mm = MicroPythonMode(editor, view) mock_port = mock.MagicMock() for vid, pid in mm.valid_boards: mock_port.vid = vid mock_port.productIdentifier = mock.MagicMock() mock_port.productIdentifier.return_value = pid mock_port.vendorIdentifier = mock.MagicMock() mock_port.vendorIdentifier.return_value = vid mock_port.portName = mock.MagicMock(return_value="COM0") mock_port.serialNumber = mock.MagicMock(return_value="12345") mock_os = mock.MagicMock() mock_os.name = "nt" with mock.patch( "mu.modes.base.QSerialPortInfo.availablePorts", return_value=[mock_port], ), mock.patch("mu.modes.base.os", mock_os): assert mm.find_device() == ("COM0", "12345")