def test_interact_uses_telemetry(monkeypatch, tmp_nbs): mock_start_ipython = Mock() monkeypatch.setattr(sys, 'argv', ['interact']) monkeypatch.setattr(interact, 'start_ipython', mock_start_ipython) monkeypatch.setattr(interact.telemetry, 'log_api', mock_start_ipython) interact.main(catch_exception=False) assert mock_start_ipython.call_count == 2
def test_interactive_session_render_fails(monkeypatch, tmp_nbs): monkeypatch.setattr(sys, 'argv', ['python']) mock = Mock(side_effect=['quit']) def fail(self): raise Exception with monkeypatch.context() as m: m.setattr('builtins.input', mock) m.setattr(DAG, 'render', fail) interact.main(catch_exception=False)
def test_interact_command_starts_full_ipython_session(monkeypatch, tmp_nbs): mock_dag = Mock() mock_start_ipython = Mock() monkeypatch.setattr(sys, 'argv', ['interact']) monkeypatch.setattr(interact, 'start_ipython', mock_start_ipython) monkeypatch.setattr(interact, '_custom_command', lambda _: (mock_dag, None)) interact.main(catch_exception=False) mock_start_ipython.assert_called_once_with(argv=[], user_ns={'dag': mock_dag})
def test_interact_command_starts_full_ipython_session(monkeypatch, tmp_nbs): mock_dag = Mock() mock_log = Mock() mock_start_ipython = Mock() monkeypatch.setattr(sys, 'argv', ['interact']) monkeypatch.setattr(interact, 'start_ipython', mock_start_ipython) monkeypatch.setattr(telemetry, 'log_api', mock_log) monkeypatch.setattr(interact.CustomParser, 'load_from_entry_point_arg', lambda _: (mock_dag, None)) interact.main(catch_exception=False) mock_start_ipython.assert_called_once_with(argv=[], user_ns={'dag': mock_dag})
def test_interactive_session_develop(monkeypatch, tmp_nbs): monkeypatch.setattr(sys, 'argv', ['python']) mock = Mock(side_effect=['dag["plot"].develop()', 'quit']) def mock_jupyter_notebook(args, check): nb = jupytext.reads('2 + 2', fmt='py') # args: jupyter {app} {path} {other args,...} nbformat.write(nb, args[2]) with monkeypatch.context() as m: # NOTE: I tried patching # (IPython.terminal.interactiveshell.TerminalInteractiveShell # .prompt_for_code) # but didn't work, patching builtins input works m.setattr('builtins.input', mock) m.setattr(notebook.subprocess, 'run', mock_jupyter_notebook) m.setattr(notebook, '_save', lambda: True) interact.main(catch_exception=False) assert Path('plot.py').read_text().strip() == '2 + 2'