|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +import IPython.display |
| 4 | +import ipywidgets as widgets |
| 5 | + |
| 6 | +from solara.server import app, kernel |
| 7 | + |
| 8 | + |
| 9 | +def test_interactive_shell(no_app_context): |
| 10 | + ws1 = Mock() |
| 11 | + ws2 = Mock() |
| 12 | + kernel1 = kernel.Kernel() |
| 13 | + kernel2 = kernel.Kernel() |
| 14 | + kernel1.session.websockets.add(ws1) |
| 15 | + kernel2.session.websockets.add(ws2) |
| 16 | + context1 = app.AppContext(id="1", kernel=kernel1) |
| 17 | + context2 = app.AppContext(id="2", kernel=kernel2) |
| 18 | + |
| 19 | + with context1: |
| 20 | + output1 = widgets.Output() |
| 21 | + with output1: |
| 22 | + IPython.display.display("test1") |
| 23 | + assert output1.outputs[0]["data"]["text/plain"] == "'test1'" |
| 24 | + assert ws1.send.call_count == 3 # create 2 widgets (layout and output) and update data |
| 25 | + assert ws2.send.call_count == 0 |
| 26 | + with context2: |
| 27 | + output2 = widgets.Output() |
| 28 | + with output2: |
| 29 | + IPython.display.display("test2") |
| 30 | + assert output2.outputs[0]["data"]["text/plain"] == "'test2'" |
| 31 | + assert ws1.send.call_count == 3 |
| 32 | + assert ws2.send.call_count == 3 |
| 33 | + |
| 34 | + context1.close() |
| 35 | + context2.close() |
| 36 | + |
| 37 | + |
| 38 | +def test_clear_output(): |
| 39 | + output1 = widgets.Output() |
| 40 | + with output1: |
| 41 | + IPython.display.display("test1") |
| 42 | + IPython.display.clear_output() |
| 43 | + assert len(output1.outputs) == 0 |
0 commit comments