Skip to content

Commit 4ab1fa9

Browse files
committed
Finish tests for outputs manager.
1 parent 134cea5 commit 4ab1fa9

File tree

2 files changed

+69
-11
lines changed

2 files changed

+69
-11
lines changed

jupyter_rtc_core/outputs/manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _build_path(self, file_id, cell_id=None, output_index=None):
4242
path = path / f"{output_index}.output"
4343
return path
4444

45-
def get(self, file_id, cell_id, output_index):
45+
def get_output(self, file_id, cell_id, output_index):
4646
"""Get an outputs by file_id, cell_id, and output_index."""
4747
self.log.info(f"OutputsManager.get: {file_id} {cell_id} {output_index}")
4848
path = self._build_path(file_id, cell_id, output_index)

jupyter_rtc_core/tests/test_outputs_manager.py

Lines changed: 68 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,92 @@
22
from pathlib import Path
33
from uuid import uuid4
44

5+
import pytest
6+
57
from ..outputs import OutputsManager
68

7-
text = ''.join([str(i) for i in range(10)])
89

9-
def so(i):
10+
def stream(text: str):
1011
return {
1112
"output_type": "stream",
1213
"name": "stdout",
13-
"text": str(i),
14+
"text": text
1415
}
1516

16-
stream_outputs = list([so(i) for i in range(10)])
17-
17+
def display_data_text(text: str):
18+
return {
19+
"output_type": "display_data",
20+
"data": {
21+
"text/plain": text
22+
}
23+
}
1824

1925
def test_instantiation():
2026
op = OutputsManager()
2127
assert isinstance(op, OutputsManager)
2228

29+
def test_paths():
30+
"""Verify that the paths are working properly."""
31+
op = OutputsManager()
32+
file_id = str(uuid4())
33+
cell_id = str(uuid4())
34+
with TemporaryDirectory() as td:
35+
op.outputs_path = Path(td) / "outputs"
36+
output_index = 0
37+
assert op._build_path(file_id, cell_id, output_index) == \
38+
op.outputs_path / file_id / cell_id / f"{output_index}.output"
39+
2340
def test_stream():
41+
"""Test stream outputs."""
42+
text = "0123456789"
43+
streams = list([stream(c) for c in text])
2444
with TemporaryDirectory() as td:
2545
op = OutputsManager()
2646
op.outputs_path = Path(td) / "outputs"
2747
file_id = str(uuid4())
2848
cell_id = str(uuid4())
29-
output_index = 0
30-
assert op._build_path(file_id, cell_id, output_index) == \
31-
op.outputs_path / file_id / cell_id / f"{output_index}.output"
32-
for stream in stream_outputs:
33-
op.write_stream(file_id, cell_id, stream)
49+
for s in streams:
50+
op.write_stream(file_id, cell_id, s)
3451
assert op.get_stream(file_id, cell_id) == text
3552

53+
def test_display_data():
54+
"""Test display data."""
55+
texts = [
56+
"Hello World!",
57+
"Hola Mundo!",
58+
"Bonjour le monde!"
59+
]
60+
outputs = list([display_data_text(t) for t in texts])
61+
with TemporaryDirectory() as td:
62+
op = OutputsManager()
63+
op.outputs_path = Path(td) / "outputs"
64+
file_id = str(uuid4())
65+
cell_id = str(uuid4())
66+
for (i, output) in enumerate(outputs):
67+
op.write_output(file_id, cell_id, output)
68+
for (i, output) in enumerate(outputs):
69+
assert op.get_output(file_id, cell_id, i) == outputs[i]
70+
71+
def test_clear():
72+
"""Test the clearing of outputs for a file_id."""
73+
output = display_data_text("Hello World!")
74+
with TemporaryDirectory() as td:
75+
op = OutputsManager()
76+
op.outputs_path = Path(td) / "outputs"
77+
file_id = str(uuid4())
78+
cell_id = str(uuid4())
79+
op.write_output(file_id, cell_id, output)
80+
path = op._build_path(file_id, cell_id, output_index=0)
81+
assert path.exists()
82+
op.clear(file_id)
83+
assert not path.exists()
84+
85+
def file_not_found():
86+
"""Test to ensure FileNotFoundError is raised."""
87+
with TemporaryDirectory() as td:
88+
op = OutputsManager()
89+
op.outputs_path = Path(td) / "outputs"
90+
with pytest.raises(FileNotFoundError):
91+
op.get_output('a','b',0)
92+
with pytest.raises(FileNotFoundError):
93+
op.get_stream('a','b')

0 commit comments

Comments
 (0)