def test_base_mode_remove_plotter(): """ Ensure the plotter is removed and data is saved as a CSV file in the expected directory. """ editor = mock.MagicMock() view = mock.MagicMock() view.plotter_pane.raw_data = [1, 2, 3] bm = BaseMode(editor, view) bm.plotter = mock.MagicMock() mock_mkdir = mock.MagicMock() mock_open = mock.mock_open() mock_csv_writer = mock.MagicMock() mock_csv = mock.MagicMock() mock_csv.writer.return_value = mock_csv_writer with mock.patch('mu.modes.base.os.path.exists', return_value=False), \ mock.patch('mu.modes.base.os.makedirs', mock_mkdir), \ mock.patch('builtins.open', mock_open), \ mock.patch('mu.modes.base.csv', mock_csv): bm.remove_plotter() assert bm.plotter is None view.remove_plotter.assert_called_once_with() dd = os.path.join(bm.workspace_dir(), 'data_capture') mock_mkdir.assert_called_once_with(dd) mock_csv_writer.writerows.\ assert_called_once_with(view.plotter_pane.raw_data)
def test_base_mode_open_file(): """ Ensure the the base class returns None to indicate it can't open the file. """ editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.open_file('unused/path') is None
def test_base_mode_add_plotter(): """ Ensure the child classes need to implement this. """ editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.add_plotter() == NotImplemented
def test_base_mode_open_file(): """ Ensure the the base class returns None to indicate it can't open the file. """ editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) text, newline = bm.open_file("unused/path") assert text is None assert newline is None
def test_base_on_data_flood(): """ Ensure the plotter is removed and a helpful message is displayed to the user. """ editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) bm.on_data_flood() view.remove_plotter.assert_called_once_with() assert view.show_message.call_count == 1
def test_base_mode_workspace_dir(): """ Return settings file workspace value. """ # read from our demo settings.json with mock.patch('mu.modes.base.get_settings_path', return_value='tests/settings.json'), \ mock.patch('os.path.isdir', return_value=True): editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == '/home/foo/mycode'
def test_base_mode_workspace_not_present(): """ No workspace key in settings file, return default folder. """ default_workspace = os.path.join(mu.logic.HOME_DIRECTORY, mu.logic.WORKSPACE_NAME) with mock.patch('mu.modes.base.get_settings_path', return_value='tests/settingswithoutworkspace.json'): editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == default_workspace
def test_base_mode_workspace_not_present(): """ No workspace key in settings file, return default folder. """ default_workspace = os.path.join(mu.config.HOME_DIRECTORY, mu.config.WORKSPACE_NAME) mocked_settings = mu.settings.UserSettings() assert "workspace" not in mocked_settings with mock.patch.object(mu.settings, "settings", mocked_settings): editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == default_workspace
def test_base_mode_set_buttons(): """ Ensure only buttons for existing actions have their "Enabled" states updates. """ editor = mock.MagicMock() view = mock.MagicMock() view.button_bar.slots = {'foo': mock.MagicMock(), 'bar': mock.MagicMock()} bm = BaseMode(editor, view) bm.set_buttons(foo=True, bar=False, baz=True) view.button_bar.slots['foo'].setEnabled.assert_called_once_with(True) view.button_bar.slots['bar'].setEnabled.assert_called_once_with(False) assert 'baz' not in view.button_bar.slots
def test_base_mode_workspace_no_settings_file(): """ Invalid settings file, return default folder. NB most of the work here is done in the settings.py module so we're just testing that we get a suitable default back """ default_workspace = os.path.join(mu.config.HOME_DIRECTORY, mu.config.WORKSPACE_NAME) mocked_settings = mu.settings.UserSettings() with mock.patch.object(mu.settings, "settings", mocked_settings): editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == default_workspace
def test_base_mode_write_csv(tmp_path): """When the plotter is removed the resulting csv should represent the data -- an should not not include interspersed blank lines """ csv_filepath = str(tmp_path / "plotter.csv") editor = mock.MagicMock() view = mock.MagicMock() view.plotter_pane.raw_data = [[1, 2, 3], [4, 5, 6]] bm = BaseMode(editor, view) bm.write_plotter_data_to_csv(csv_filepath) expected_output = ["1,2,3", "4,5,6"] with open(csv_filepath, "r") as f: output = f.read().splitlines() assert output == expected_output
def test_base_mode_workspace_invalid_json(): """ Invalid workspace key in settings file, return default folder. """ default_workspace = os.path.join(mu.logic.HOME_DIRECTORY, mu.logic.WORKSPACE_NAME) mock_open = mock.mock_open(read_data='{"workspace": invalid}') with mock.patch('mu.modes.base.get_settings_path', return_value='a.json'), \ mock.patch('builtins.open', mock_open), \ mock.patch('mu.modes.base.logger', return_value=None) as logger: editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == default_workspace assert logger.error.call_count == 1
def test_base_mode_workspace_invalid_value(): """ Invalid workspace key in settings file, return default folder. """ default_workspace = os.path.join(mu.logic.HOME_DIRECTORY, mu.logic.WORKSPACE_NAME) # read from our demo settings.json with mock.patch('mu.modes.base.get_settings_path', return_value='tests/settings.json'), \ mock.patch('os.path.isdir', return_value=False), \ mock.patch('mu.modes.base.logger', return_value=None) as logger: editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == default_workspace assert logger.error.call_count == 1
def test_base_mode(): """ Sanity check for the parent class of all modes. """ editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.name == 'UNNAMED MODE' assert bm.description == 'DESCRIPTION NOT AVAILABLE.' assert bm.icon == 'help' assert bm.is_debugger is False assert bm.editor == editor assert bm.view == view assert bm.actions() == NotImplemented assert bm.workspace_dir() assert bm.api() == NotImplemented
def test_base_mode_workspace_no_settings_file(): """ Invalid settings file, return default folder. """ default_workspace = os.path.join(mu.logic.HOME_DIRECTORY, mu.logic.WORKSPACE_NAME) mock_open = mock.MagicMock(side_effect=FileNotFoundError()) with mock.patch('mu.modes.base.get_settings_path', return_value='tests/settings.json'), \ mock.patch('builtins.open', mock_open), \ mock.patch('mu.modes.base.logger', return_value=None) as logger: editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == default_workspace assert logger.error.call_count == 1
def test_base_mode_set_buttons(): """ Ensure only buttons for existing actions have their "Enabled" states updates. """ editor = mock.MagicMock() view = mock.MagicMock() view.button_bar.slots = { 'foo': mock.MagicMock(), 'bar': mock.MagicMock() } bm = BaseMode(editor, view) bm.set_buttons(foo=True, bar=False, baz=True) view.button_bar.slots['foo'].setEnabled.assert_called_once_with(True) view.button_bar.slots['bar'].setEnabled.assert_called_once_with(False) assert 'baz' not in view.button_bar.slots
def test_base_mode(): """ Sanity check for the parent class of all modes. """ editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.name == 'UNNAMED MODE' assert bm.description == 'DESCRIPTION NOT AVAILABLE.' assert bm.icon == 'help' assert bm.is_debugger is False assert bm.editor == editor assert bm.view == view assert bm.actions() == NotImplemented assert bm.workspace_dir() assert bm.api() == NotImplemented assert bm.builtins is None
def test_base_mode_workspace_invalid_value(): """ Invalid workspace key in settings file, return default folder. """ default_workspace = os.path.join(mu.config.HOME_DIRECTORY, mu.config.WORKSPACE_NAME) mocked_settings = mu.settings.UserSettings() mocked_settings["workspace"] = "*invalid*" with mock.patch.object( mu.settings, "settings", mocked_settings), mock.patch("mu.modes.base.logger", return_value=None) as logger: editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == default_workspace assert logger.warn.call_count == 1
def test_base_mode_activate_deactivate_change(microbit): """ Dummy test of no-operation base methods only meant for overriding. """ editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) bm.activate() bm.deactivate() bm.device_changed(microbit)
def test_base_mode_workspace_invalid_json(tmp_path): """ Invalid workspace key in settings file, return default folder. NB most of the work here is done in the settings.py module so we're just testing that we get a suitable default back """ default_workspace = os.path.join(mu.config.HOME_DIRECTORY, mu.config.WORKSPACE_NAME) mocked_settings = mu.settings.UserSettings() settings_filepath = os.path.join(str(tmp_path), "settings.json") with open(settings_filepath, "w") as f: f.write("*invalid JSON*") mocked_settings.load(settings_filepath) with mock.patch.object(mu.settings, "settings", mocked_settings): editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.workspace_dir() == default_workspace
def test_base_mode(): """ Sanity check for the parent class of all modes. """ editor = mock.MagicMock() view = mock.MagicMock() bm = BaseMode(editor, view) assert bm.name == "UNNAMED MODE" assert bm.short_name == "UNDEFINED_MODE" assert bm.description == "DESCRIPTION NOT AVAILABLE." assert bm.icon == "help" assert bm.is_debugger is False assert bm.editor == editor assert bm.view == view assert bm.stop() is None assert bm.actions() == NotImplemented assert bm.workspace_dir() assert bm.api() == NotImplemented assert bm.builtins is None