Skip to content

Commit 5c715dd

Browse files
committed
Add a failing test case for granular modifications
Update tests to expect specific events
1 parent dd1fb84 commit 5c715dd

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

tests/test_ynotebook.py

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
33

4-
from pycrdt import Map
4+
5+
from pycrdt import ArrayEvent, Map, MapEvent, TextEvent
6+
from pytest import mark
57

68
from jupyter_ydoc import YNotebook
79

@@ -114,3 +116,64 @@ def record_changes(topic, event):
114116
{"delete": 1},
115117
{"insert": [AnyInstanceOf(Map)]},
116118
]
119+
120+
121+
@mark.parametrize(
122+
"modifications, expected_events",
123+
[
124+
# modifications of single attributes
125+
([["source", "'b'"]], {TextEvent}),
126+
([["outputs", []]], {ArrayEvent}),
127+
([["execution_count", 2]], {MapEvent}),
128+
# multi-attribute modifications
129+
([["source", "10"], ["execution_count", 10]], {TextEvent, MapEvent}),
130+
],
131+
)
132+
def test_modify_single_cell(modifications, expected_events):
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+
after = nb.ycells[0][key]
168+
after_py = after.to_py() if hasattr(after, "to_py") else after
169+
assert after_py == new_value
170+
171+
# there should be only one change
172+
assert len(changes) == 1
173+
cell_events = [e for t, e in changes if t == "cells"]
174+
# and it should be a cell change
175+
assert len(cell_events) == 1
176+
# but it should be a change to cell data, not a change to the cell list
177+
events = cell_events[0]
178+
assert len(events) == len(expected_events)
179+
assert {type(e) for e in events} == expected_events

0 commit comments

Comments
 (0)