Skip to content

Commit 5e36f06

Browse files
committed
Moved output index logic to a separate method, added tests
1 parent 3752dec commit 5e36f06

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

jupyter_server_documents/outputs/manager.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,30 @@ def _build_path(self, file_id, cell_id=None, output_index=None):
3434
if output_index is not None:
3535
path = path / f"{output_index}.output"
3636
return path
37+
38+
def _determine_output_index(self, cell_id, display_id=None):
39+
"""
40+
Determines output index for a given cell and optional display ID.
41+
42+
Args:
43+
cell_id (str): The cell identifier
44+
display_id (str, optional): A display identifier. Defaults to None.
45+
46+
Returns:
47+
int: The allocated output index
48+
"""
49+
last_index = self._last_output_index.get(cell_id, -1)
50+
if display_id:
51+
index = self._output_index_by_display_id.get(display_id)
52+
if index is None:
53+
index = last_index + 1
54+
self._last_output_index[cell_id] = index
55+
self._output_index_by_display_id[display_id] = index
56+
else:
57+
index = last_index + 1
58+
self._last_output_index[cell_id] = index
59+
60+
return index
3761

3862
def get_output_index(self, display_id: str):
3963
"""Returns output index for a cell by display_id"""
@@ -121,17 +145,7 @@ def write(self, file_id, cell_id, output, display_id=None):
121145

122146
def write_output(self, file_id, cell_id, output, display_id=None):
123147
self._ensure_path(file_id, cell_id)
124-
last_index = self._last_output_index.get(cell_id, -1)
125-
if display_id:
126-
index = self._output_index_by_display_id.get(display_id)
127-
if index is None:
128-
index = last_index + 1
129-
self._last_output_index[cell_id] = index
130-
self._output_index_by_display_id[display_id] = index
131-
else:
132-
index = last_index + 1
133-
self._last_output_index[cell_id] = index
134-
148+
index = self._determine_output_index(cell_id, display_id)
135149
path = self._build_path(file_id, cell_id, index)
136150
data = json.dumps(output, ensure_ascii=False)
137151
with open(path, "w", encoding="utf-8") as f:

jupyter_server_documents/tests/test_outputs_manager.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,41 @@ def file_not_found():
9191
op.get_output('a','b',0)
9292
with pytest.raises(FileNotFoundError):
9393
op.get_stream('a','b')
94+
95+
96+
@pytest.mark.parametrize(
97+
"_last_output_index, _output_index_by_display_id, cell_id, display_id, expected", [
98+
# Empty dictionaries, no display_id
99+
({}, {}, "cell1", None, 0),
100+
101+
# Empty dictionaries, with display_id
102+
({}, {}, "cell1", "display1", 0),
103+
104+
# Existing last_output_index, no display_id
105+
({"cell1": 5}, {}, "cell1", None, 6),
106+
({"cell2": 3}, {}, "cell1", None, 0),
107+
108+
# Existing output_index_by_display_id
109+
({}, {"display1": 2}, "cell1", "display1", 2),
110+
111+
# Existing last_output_index and display_id
112+
({"cell1": 5}, {"display1": 3}, "cell1", "display1", 3),
113+
({"cell1": 5}, {"display1": 3}, "cell1", "display2", 6),
114+
115+
# Multiple cells with different indices
116+
({"cell1": 2, "cell2": 7}, {}, "cell1", None, 3),
117+
({"cell1": 2, "cell2": 7}, {}, "cell3", None, 0),
118+
])
119+
def test__determine_output_index(
120+
_last_output_index,
121+
_output_index_by_display_id,
122+
cell_id,
123+
display_id,
124+
expected
125+
):
126+
op = OutputsManager()
127+
op._last_output_index = _last_output_index;
128+
op._output_index_by_display_id = _output_index_by_display_id
129+
130+
assert expected == op._determine_output_index(cell_id, display_id)
131+

0 commit comments

Comments
 (0)