Skip to content

Commit 2765bb1

Browse files
committed
Add a test for outputs being hard-reset
1 parent 87919c6 commit 2765bb1

File tree

1 file changed

+33
-8
lines changed

1 file changed

+33
-8
lines changed

tests/test_ynotebook.py

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

4+
from dataclasses import dataclass
45

56
from pycrdt import ArrayEvent, Map, MapEvent, TextEvent
67
from pytest import mark
@@ -118,17 +119,42 @@ def record_changes(topic, event):
118119
]
119120

120121

122+
@dataclass
123+
class ExpectedEvent:
124+
kind: type
125+
path: str | None = None
126+
127+
def __eq__(self, other):
128+
if not isinstance(other, self.kind):
129+
return False
130+
if self.path is not None and self.path != other.path:
131+
return False
132+
return True
133+
134+
def __repr__(self):
135+
if self.path is not None:
136+
return f"ExpectedEvent({self.kind.__name__}, path={self.path!r})"
137+
return f"ExpectedEvent({self.kind.__name__})"
138+
139+
121140
@mark.parametrize(
122141
"modifications, expected_events",
123142
[
124143
# modifications of single attributes
125-
([["source", "'b'"]], {TextEvent}),
126-
([["outputs", []]], {ArrayEvent}),
127-
([["execution_count", 2]], {MapEvent}),
128-
([["metadata", {"tags": []}]], {MapEvent}),
129-
([["new_key", "test"]], {MapEvent}),
144+
([["source", "'b'"]], [ExpectedEvent(TextEvent)]),
145+
([["outputs", []]], [ExpectedEvent(ArrayEvent, path=[0, "outputs"])]),
146+
(
147+
[["outputs", [{"name": "stdout", "output_type": "stream", "text": "b\n"}]]],
148+
[ExpectedEvent(ArrayEvent, path=[])],
149+
),
150+
([["execution_count", 2]], [ExpectedEvent(MapEvent)]),
151+
([["metadata", {"tags": []}]], [ExpectedEvent(MapEvent)]),
152+
([["new_key", "test"]], [ExpectedEvent(MapEvent)]),
130153
# multi-attribute modifications
131-
([["source", "10"], ["execution_count", 10]], {TextEvent, MapEvent}),
154+
(
155+
[["source", "10"], ["execution_count", 10]],
156+
[ExpectedEvent(MapEvent), ExpectedEvent(TextEvent)],
157+
),
132158
],
133159
)
134160
def test_modify_single_cell(modifications, expected_events):
@@ -177,5 +203,4 @@ def record_changes(topic, event):
177203
assert len(cell_events) == 1
178204
# but it should be a change to cell data, not a change to the cell list
179205
events = cell_events[0]
180-
assert len(events) == len(expected_events)
181-
assert {type(e) for e in events} == expected_events
206+
assert events == expected_events

0 commit comments

Comments
 (0)