|
1 | 1 | # Copyright (c) Jupyter Development Team. |
2 | 2 | # Distributed under the terms of the Modified BSD License. |
3 | 3 |
|
4 | | -from pycrdt import Map |
| 4 | + |
| 5 | +from pycrdt import Map, MapEvent |
| 6 | +from pytest import mark |
5 | 7 |
|
6 | 8 | from jupyter_ydoc import YNotebook |
7 | 9 |
|
@@ -114,3 +116,63 @@ def record_changes(topic, event): |
114 | 116 | {"delete": 1}, |
115 | 117 | {"insert": [AnyInstanceOf(Map)]}, |
116 | 118 | ] |
| 119 | + |
| 120 | + |
| 121 | +@mark.parametrize( |
| 122 | + "modifications", |
| 123 | + [ |
| 124 | + # modifications of single attributes |
| 125 | + [["source", "'b'"]], |
| 126 | + [["outputs", []]], |
| 127 | + [["execution_count", 2]], |
| 128 | + # multi-attribute modifications |
| 129 | + [["source", "10"], ["execution_count", 10]], |
| 130 | + ], |
| 131 | +) |
| 132 | +def test_modify_single_cell(modifications): |
| 133 | + nb = YNotebook() |
| 134 | + nb.set( |
| 135 | + { |
| 136 | + "cells": [ |
| 137 | + { |
| 138 | + "id": "8800f7d8-6cad-42ef-a339-a9c185ffdd54", |
| 139 | + "cell_type": "code", |
| 140 | + "source": "'a'", |
| 141 | + "metadata": {"tags": ["test-tag"]}, |
| 142 | + "outputs": [{"name": "stdout", "output_type": "stream", "text": ["a\n"]}], |
| 143 | + "execution_count": 1, |
| 144 | + }, |
| 145 | + ] |
| 146 | + } |
| 147 | + ) |
| 148 | + |
| 149 | + # Get the model as Python object |
| 150 | + model = nb.get() |
| 151 | + |
| 152 | + # Make changes |
| 153 | + for modification in modifications: |
| 154 | + key, new_value = modification |
| 155 | + model["cells"][0][key] = new_value |
| 156 | + |
| 157 | + changes = [] |
| 158 | + |
| 159 | + def record_changes(topic, event): |
| 160 | + changes.append((topic, event)) |
| 161 | + |
| 162 | + nb.observe(record_changes) |
| 163 | + nb.set(model) |
| 164 | + |
| 165 | + for modification in modifications: |
| 166 | + key, new_value = modification |
| 167 | + assert nb.ycells[0][key] == new_value |
| 168 | + |
| 169 | + # there should be only one change |
| 170 | + assert len(changes) == 1 |
| 171 | + cell_events = [e for t, e in changes if t == "cells"] |
| 172 | + # and it should be a cell change |
| 173 | + assert len(cell_events) == 1 |
| 174 | + # but it should be a change to cell data, not a change to the cell list |
| 175 | + events = cell_events[0] |
| 176 | + # (it's neseted for some reason) |
| 177 | + assert len(events) == 1 |
| 178 | + assert isinstance(events[0], MapEvent) |
0 commit comments