def test_help_with_cell_content(): msg = "Cell body for %%help magic must be empty; got 'HAHAH' instead" magic.help("", cell="HAHAH") assert_equals(ipython_display.send_error.call_count, 1) assert_equals(ipython_display.html.call_count, 0) _assert_magic_failure_event_emitted_once('help', BadUserDataException(msg))
def test_configure_cant_parse_object_as_json(): magic.info = MagicMock() magic._override_session_settings = MagicMock( side_effect=BadUserDataException('help')) magic.configure('', "I CAN'T PARSE THIS AS JSON") _assert_magic_successful_event_emitted_once('configure') assert_equals(ipython_display.send_error.call_count, 1)
def test_logs_with_cell_content(): logs = "logs" line = "" msg = "Cell body for %%logs magic must be empty; got 'BOOP' instead" magic.logs(line, cell="BOOP") assert_equals(ipython_display.send_error.call_count, 1) _assert_magic_failure_event_emitted_once('logs', BadUserDataException(msg))
def parse_argstring_or_throw(magic_func, argstring, parse_argstring=parse_argstring): """An alternative to the parse_argstring method from IPython.core.magic_arguments. Catches IPython.core.error.UsageError and propagates it as a livyclientlib.exceptions.BadUserDataException.""" try: return parse_argstring(magic_func, argstring) except UsageError as e: from remotespark.livyclientlib.exceptions import BadUserDataException raise BadUserDataException(str(e))
def test_configure_expected_exception(): magic.info = MagicMock() magic._override_session_settings = MagicMock( side_effect=BadUserDataException('help')) magic.configure('', '{"extra": "yes"}') _assert_magic_failure_event_emitted_once( 'configure', magic._override_session_settings.side_effect) ipython_display.send_error.assert_called_once_with(constants.EXPECTED_ERROR_MSG\ .format(magic._override_session_settings.side_effect))
def test_cleanup_with_cell_content(): line = "-f" cell = "HEHEHE" msg = "Cell body for %%cleanup magic must be empty; got 'HEHEHE' instead" magic.session_started = True spark_controller.cleanup_endpoint = MagicMock() spark_controller.delete_session_by_name = MagicMock() magic.cleanup(line, cell) assert_equals(ipython_display.send_error.call_count, 1) _assert_magic_failure_event_emitted_once('cleanup', BadUserDataException(msg))
def test_delete_with_cell_content(): # This happens when session has not been created session_id = 0 line = "-f -s {}".format(session_id) cell = "~~~" msg = "Cell body for %%delete magic must be empty; got '~~~' instead" spark_controller.delete_session_by_id = MagicMock() spark_controller.get_session_id_for_client = MagicMock(return_value=None) magic.delete(line, cell) _assert_magic_failure_event_emitted_once('delete', BadUserDataException(msg)) assert_equals(ipython_display.send_error.call_count, 1)
def test_delete_session_expected_exception(): line = "" magic.session_started = True spark_controller.delete_session_by_name.side_effect = BadUserDataException( 'hey') magic._do_not_call_delete_session(line) assert not magic.session_started spark_controller.delete_session_by_name.assert_called_once_with( magic.session_name) ipython_display.send_error.assert_called_once_with( constants.EXPECTED_ERROR_MSG.format( spark_controller.delete_session_by_name.side_effect))
def test_info_with_cell_content(): magic.print_endpoint_info = print_info_mock = MagicMock() line = "" session_info = ["1", "2"] spark_controller.get_all_sessions_endpoint_info = MagicMock( return_value=session_info) error_msg = "Cell body for %%info magic must be empty; got 'howdy' instead" magic.info(line, cell='howdy') print_info_mock.assert_not_called() assert_equals(ipython_display.send_error.call_count, 1) spark_controller.get_session_id_for_client.assert_not_called() _assert_magic_failure_event_emitted_once('info', BadUserDataException(error_msg))
def _assure_cell_body_is_empty(magic_name, cell): if cell.strip(): raise BadUserDataException( "Cell body for %%{} magic must be empty; got '{}' instead". format(magic_name, cell.strip()))