def test_changing_cell_type_sends_replace(): new_contents = evolve_cell_type(SIMPLE_CONTENTS, 0, "markdown") opcodes = opcode_merge_cell_contents(SIMPLE_CONTENTS, new_contents) assert new_contents != SIMPLE_CONTENTS assert len(opcodes) == 1 assert opcodes[0].op_code == OpCodes.REPLACE
def test_change_one_of_multiple_values(): new_contents = evolve_cell_source(THREE_CELL_CONTENTS, 0, ["x = 4; x"]) opcodes = opcode_merge_cell_contents(THREE_CELL_CONTENTS, new_contents) assert len(opcodes) == 2 assert opcodes[0].op_code == OpCodes.REPLACE assert opcodes[1].op_code == OpCodes.EQUAL
def test_insert_a_new_cell(): inserted_cell = JupyterCell(cell_type="code", index=2, source=["print('hello world')"], output=None) new_contents = _insert_notebook_cell(THREE_CELL_CONTENTS, inserted_cell) opcodes = opcode_merge_cell_contents(THREE_CELL_CONTENTS, new_contents) assert [x.op_code for x in opcodes] == [OpCodes.EQUAL, OpCodes.INSERT, OpCodes.EQUAL] assert opcodes[1].current == (2, 2) assert opcodes[1].updated == (2, 3)
def test_remove_one_cell_beginning(): # Notice that there isn't [0], and the output is removed cells_with_one_removed = [ attr.evolve(THREE_CELL_CONTENTS.cells[1], output=None, index=0), attr.evolve(THREE_CELL_CONTENTS.cells[2], output=None, index=1), ] new_contents = NotebookContents(cells=cells_with_one_removed) opcodes = opcode_merge_cell_contents(THREE_CELL_CONTENTS, new_contents) assert len(opcodes) == 2 assert [x.op_code for x in opcodes] == [OpCodes.DELETE, OpCodes.EQUAL] assert opcodes[0].current == (0, 1) assert opcodes[1].current == (1, 3)
def test_insert_a_new_cell_and_update_another(): modified_source = ["y = 3; y"] inserted_cell = JupyterCell(cell_type="code", index=2, source=["print('hello world')"], output=None) new_contents = evolve_cell_source(_insert_notebook_cell(THREE_CELL_CONTENTS, inserted_cell), 1, modified_source) opcodes = opcode_merge_cell_contents(THREE_CELL_CONTENTS, new_contents) assert [x.op_code for x in opcodes] == [ OpCodes.EQUAL, OpCodes.REPLACE, OpCodes.EQUAL, ] assert opcodes[1].current == (1, 2) assert opcodes[1].updated == (1, 3)
def test_remove_one_cell(): # Notice that there isn't [2], and the output is removed cells_with_one_removed = [ attr.evolve(THREE_CELL_CONTENTS.cells[0], output=None), attr.evolve(THREE_CELL_CONTENTS.cells[1], output=None), ] new_contents = NotebookContents(cells=cells_with_one_removed) opcodes = opcode_merge_cell_contents(THREE_CELL_CONTENTS, new_contents) assert len(opcodes) == 2 assert [x.op_code for x in opcodes] == [OpCodes.EQUAL, OpCodes.DELETE] assert opcodes[0].current_start_idx == 0 assert opcodes[0].current_final_idx == 2 assert opcodes[0].updated_start_idx == 0 assert opcodes[0].updated_final_idx == 2
def test_reorder_multiple_values(): first_cell = attr.evolve(THREE_CELL_CONTENTS.cells[1], index=0, output=None) second_cell = attr.evolve(THREE_CELL_CONTENTS.cells[0], index=1, output=None) new_contents = NotebookContents(cells=[first_cell, second_cell, THREE_CELL_CONTENTS.cells[2]]) opcodes = opcode_merge_cell_contents(THREE_CELL_CONTENTS, new_contents) assert len(opcodes) == 5 assert [x.op_code for x in opcodes] == [ OpCodes.INSERT, OpCodes.EQUAL, OpCodes.DELETE, OpCodes.EQUAL, OpCodes.COPY_OUTPUT, ] assert opcodes[-1].current_final_idx == 1 assert opcodes[-1].updated_final_idx == 0
def test_remove_one_cell_and_update_another(): # Updated source for index=1 modified_source = ["y = 3; y"] cells_with_one_removed = [ attr.evolve(THREE_CELL_CONTENTS.cells[0], output=None, index=0), attr.evolve(THREE_CELL_CONTENTS.cells[1], source=modified_source, output=None, index=1), ] new_contents = NotebookContents(cells=cells_with_one_removed) opcodes = opcode_merge_cell_contents(THREE_CELL_CONTENTS, new_contents) assert [x.op_code for x in opcodes] == [OpCodes.EQUAL, OpCodes.REPLACE] assert opcodes[0].current == (0, 1) assert opcodes[0].updated == (0, 1) # Note: The replace takes the two cells and then replaces down to only one assert opcodes[1].current == (1, 3) assert opcodes[1].updated == (1, 2)
def merge_notebooks(comm: Comm, result: Dict[str, Any]) -> None: javascript_cells = result["javascript_cells"] current_notebook = NotebookContents(cells=[ JupyterCell( index=i, cell_type=x["cell_type"], source=x["source"], output=get_output_text(x), # metadata=x["metadata"], ) for i, x in enumerate(javascript_cells) ]) new_notebook = NotebookContents( cells=[JupyterCell(**x) for x in result["new_notebook"]]) opcodes = opcode_merge_cell_contents(current_notebook, new_notebook) J_LOGGER.info("Performing Opcodes...") J_LOGGER.info(opcodes) net_shift = 0 for op_action in opcodes: net_shift = perform_op_code(comm, op_action, current_notebook, new_notebook, net_shift)
def test_change_one_value(): new_contents = evolve_cell_source(SIMPLE_CONTENTS, 0, ["x = 2; x"]) opcodes = opcode_merge_cell_contents(SIMPLE_CONTENTS, new_contents) assert len(opcodes) == 1 assert opcodes[0].op_code == OpCodes.REPLACE
def test_passing_same_contents_means_same_output(): opcodes = opcode_merge_cell_contents(SIMPLE_CONTENTS, SIMPLE_CONTENTS) assert len(opcodes) == 1 assert opcodes[0].op_code == OpCodes.EQUAL