|
2 | 2 | from pathlib import Path |
3 | 3 | from uuid import uuid4 |
4 | 4 |
|
| 5 | +import pytest |
| 6 | + |
5 | 7 | from ..outputs import OutputsManager |
6 | 8 |
|
7 | | -text = ''.join([str(i) for i in range(10)]) |
8 | 9 |
|
9 | | -def so(i): |
| 10 | +def stream(text: str): |
10 | 11 | return { |
11 | 12 | "output_type": "stream", |
12 | 13 | "name": "stdout", |
13 | | - "text": str(i), |
| 14 | + "text": text |
14 | 15 | } |
15 | 16 |
|
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 | +} |
18 | 24 |
|
19 | 25 | def test_instantiation(): |
20 | 26 | op = OutputsManager() |
21 | 27 | assert isinstance(op, OutputsManager) |
22 | 28 |
|
| 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 | + |
23 | 40 | def test_stream(): |
| 41 | + """Test stream outputs.""" |
| 42 | + text = "0123456789" |
| 43 | + streams = list([stream(c) for c in text]) |
24 | 44 | with TemporaryDirectory() as td: |
25 | 45 | op = OutputsManager() |
26 | 46 | op.outputs_path = Path(td) / "outputs" |
27 | 47 | file_id = str(uuid4()) |
28 | 48 | 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) |
34 | 51 | assert op.get_stream(file_id, cell_id) == text |
35 | 52 |
|
| 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