|
| 1 | +import pytest |
| 2 | +from jupyter_client.utils import ensure_async |
| 3 | +from nbformat import from_dict |
| 4 | +from nbformat.v4 import new_markdown_cell |
| 5 | + |
| 6 | +from jupyter_server.services.contents.filecheckpoints import ( |
| 7 | + AsyncFileCheckpoints, |
| 8 | + AsyncGenericFileCheckpoints, |
| 9 | + FileCheckpoints, |
| 10 | + GenericFileCheckpoints, |
| 11 | +) |
| 12 | +from jupyter_server.services.contents.largefilemanager import ( |
| 13 | + AsyncLargeFileManager, |
| 14 | + LargeFileManager, |
| 15 | +) |
| 16 | + |
| 17 | +param_pairs = [ |
| 18 | + (LargeFileManager, FileCheckpoints), |
| 19 | + (LargeFileManager, GenericFileCheckpoints), |
| 20 | + (AsyncLargeFileManager, AsyncFileCheckpoints), |
| 21 | + (AsyncLargeFileManager, AsyncGenericFileCheckpoints), |
| 22 | +] |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture(params=param_pairs) |
| 26 | +def contents_manager(request, contents): |
| 27 | + """Returns a LargeFileManager instance.""" |
| 28 | + file_manager, checkpoints_class = request.param |
| 29 | + root_dir = str(contents["contents_dir"]) |
| 30 | + return file_manager(root_dir=root_dir, checkpoints_class=checkpoints_class) |
| 31 | + |
| 32 | + |
| 33 | +async def test_checkpoints_follow_file(contents_manager): |
| 34 | + cm: LargeFileManager = contents_manager |
| 35 | + path = "foo/a.ipynb" |
| 36 | + |
| 37 | + # Read initial file. |
| 38 | + model = await ensure_async(cm.get(path)) |
| 39 | + |
| 40 | + # Create a checkpoint of initial state |
| 41 | + cp1 = await ensure_async(cm.create_checkpoint(path)) |
| 42 | + |
| 43 | + # Modify file and save. |
| 44 | + nbcontent = model["content"] |
| 45 | + nb = from_dict(nbcontent) |
| 46 | + hcell = new_markdown_cell("Created by test") |
| 47 | + nb.cells.append(hcell) |
| 48 | + nbmodel = {"content": nb, "type": "notebook"} |
| 49 | + await ensure_async(cm.save(nbmodel, path)) |
| 50 | + |
| 51 | + # List checkpoints |
| 52 | + cps = await ensure_async(cm.list_checkpoints(path)) |
| 53 | + assert cps == [cp1] |
| 54 | + |
| 55 | + model = await ensure_async(cm.get(path)) |
| 56 | + nbcontent = model["content"] |
| 57 | + nb = from_dict(nbcontent) |
| 58 | + assert nb.cells[0].source == "Created by test" |
| 59 | + |
| 60 | + |
| 61 | +async def test_nb_checkpoints(contents_manager): |
| 62 | + cm: LargeFileManager = contents_manager |
| 63 | + path = "foo/a.ipynb" |
| 64 | + model = await ensure_async(cm.get(path)) |
| 65 | + cp1 = await ensure_async(cm.create_checkpoint(path)) |
| 66 | + assert set(cp1) == {"id", "last_modified"} |
| 67 | + |
| 68 | + # Modify it. |
| 69 | + nbcontent = model["content"] |
| 70 | + nb = from_dict(nbcontent) |
| 71 | + hcell = new_markdown_cell("Created by test") |
| 72 | + nb.cells.append(hcell) |
| 73 | + |
| 74 | + # Save it. |
| 75 | + nbmodel = {"content": nb, "type": "notebook"} |
| 76 | + await ensure_async(cm.save(nbmodel, path)) |
| 77 | + |
| 78 | + # List checkpoints |
| 79 | + cps = await ensure_async(cm.list_checkpoints(path)) |
| 80 | + assert cps == [cp1] |
| 81 | + |
| 82 | + nbcontent = await ensure_async(cm.get(path)) |
| 83 | + nb = from_dict(nbcontent["content"]) |
| 84 | + assert nb.cells[0].source == "Created by test" |
| 85 | + |
| 86 | + # Restore Checkpoint cp1 |
| 87 | + await ensure_async(cm.restore_checkpoint(cp1["id"], path)) |
| 88 | + |
| 89 | + nbcontent = await ensure_async(cm.get(path)) |
| 90 | + nb = from_dict(nbcontent["content"]) |
| 91 | + assert nb.cells == [] |
| 92 | + |
| 93 | + # Delete cp1 |
| 94 | + await ensure_async(cm.delete_checkpoint(cp1["id"], path)) |
| 95 | + |
| 96 | + cps = await ensure_async(cm.list_checkpoints(path)) |
| 97 | + assert cps == [] |
| 98 | + |
| 99 | + |
| 100 | +async def test_file_checkpoints(contents_manager): |
| 101 | + cm: LargeFileManager = contents_manager |
| 102 | + path = "foo/a.txt" |
| 103 | + model = await ensure_async(cm.get(path)) |
| 104 | + orig_content = model["content"] |
| 105 | + |
| 106 | + cp1 = await ensure_async(cm.create_checkpoint(path)) |
| 107 | + assert set(cp1) == {"id", "last_modified"} |
| 108 | + |
| 109 | + # Modify and save it. |
| 110 | + model["content"] = new_content = orig_content + "\nsecond line" |
| 111 | + await ensure_async(cm.save(model, path)) |
| 112 | + |
| 113 | + # List checkpoints |
| 114 | + cps = await ensure_async(cm.list_checkpoints(path)) |
| 115 | + assert cps == [cp1] |
| 116 | + |
| 117 | + model = await ensure_async(cm.get(path)) |
| 118 | + assert model["content"] == new_content |
| 119 | + |
| 120 | + # Restore Checkpoint cp1 |
| 121 | + await ensure_async(cm.restore_checkpoint(cp1["id"], path)) |
| 122 | + |
| 123 | + restored_content = await ensure_async(cm.get(path)) |
| 124 | + assert restored_content["content"] == orig_content |
| 125 | + |
| 126 | + # Delete cp1 |
| 127 | + await ensure_async(cm.delete_checkpoint(cp1["id"], path)) |
| 128 | + |
| 129 | + cps = await ensure_async(cm.list_checkpoints(path)) |
| 130 | + assert cps == [] |
0 commit comments