test_deletecell.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. def cell_is_deletable(nb, index):
  2. JS = 'return Jupyter.notebook.get_cell({}).is_deletable();'.format(index)
  3. return nb.browser.execute_script(JS)
  4. def remove_all_cells(notebook):
  5. for i in range(len(notebook.cells)):
  6. notebook.delete_cell(0)
  7. def test_delete_cells(notebook):
  8. a = 'print("a")'
  9. b = 'print("b")'
  10. c = 'print("c")'
  11. notebook.edit_cell(index=0, content=a)
  12. notebook.append(b, c)
  13. notebook.to_command_mode()
  14. # Validate initial state
  15. assert notebook.get_cells_contents() == [a, b, c]
  16. for cell in range(0, 3):
  17. assert cell_is_deletable(notebook, cell)
  18. notebook.set_cell_metadata(0, 'deletable', 'false')
  19. notebook.set_cell_metadata(1, 'deletable', 0
  20. )
  21. assert not cell_is_deletable(notebook, 0)
  22. assert cell_is_deletable(notebook, 1)
  23. assert cell_is_deletable(notebook, 2)
  24. # Try to delete cell a (should not be deleted)
  25. notebook.delete_cell(0)
  26. assert notebook.get_cells_contents() == [a, b, c]
  27. # Try to delete cell b (should succeed)
  28. notebook.delete_cell(1)
  29. assert notebook.get_cells_contents() == [a, c]
  30. # Try to delete cell c (should succeed)
  31. notebook.delete_cell(1)
  32. assert notebook.get_cells_contents() == [a]
  33. # Change the deletable state of cell a
  34. notebook.set_cell_metadata(0, 'deletable', 'true')
  35. # Try to delete cell a (should succeed)
  36. notebook.delete_cell(0)
  37. assert len(notebook.cells) == 1 # it contains an empty cell
  38. # Make sure copied cells are deletable
  39. notebook.edit_cell(index=0, content=a)
  40. notebook.set_cell_metadata(0, 'deletable', 'false')
  41. assert not cell_is_deletable(notebook, 0)
  42. notebook.to_command_mode()
  43. notebook.current_cell.send_keys('cv')
  44. assert len(notebook.cells) == 2
  45. assert cell_is_deletable(notebook, 1)
  46. notebook.set_cell_metadata(0, 'deletable', 'true') # to perform below test, remove all the cells
  47. remove_all_cells(notebook)
  48. assert len(notebook.cells) == 1 # notebook should create one automatically on empty notebook