Build typed IPython app execution pipeline
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import contextlib
|
||||
import io
|
||||
import json
|
||||
import subprocess
|
||||
import sys
|
||||
import unittest
|
||||
|
||||
from ipython_demo.app import App
|
||||
from ipython_demo.models import CallResponse
|
||||
|
||||
|
||||
class AppTests(unittest.TestCase):
|
||||
def test_main_emits_one_json_payload(self):
|
||||
completed = subprocess.run(
|
||||
[sys.executable, "-c", "from ipython_demo.app import main; main()"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
payload = json.loads(completed.stdout)
|
||||
self.assertEqual(len(payload), 2)
|
||||
self.assertEqual(payload[0]["events"][0]["kind"], "stdout")
|
||||
self.assertTrue(payload[1]["collapsed"])
|
||||
|
||||
def test_run_code_returns_frontend_facing_call_response(self):
|
||||
app = App()
|
||||
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
call = app.run_code(
|
||||
"print('hello'); 2 + 2",
|
||||
shell_name="new",
|
||||
collapsed=True,
|
||||
)
|
||||
|
||||
self.assertIsInstance(call, CallResponse)
|
||||
self.assertEqual(call.code, "print('hello'); 2 + 2")
|
||||
self.assertTrue(call.collapsed)
|
||||
self.assertEqual(call.result.result, 4)
|
||||
self.assertEqual([event.kind for event in call.events], [
|
||||
"stdout",
|
||||
"execute_result",
|
||||
])
|
||||
|
||||
def test_last_reuses_shell_but_each_call_has_new_id(self):
|
||||
app = App()
|
||||
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
first = app.run_code("x = 41", shell_name="new")
|
||||
second = app.run_code("x + 1", shell_name="last")
|
||||
|
||||
self.assertEqual(first.shell_name, second.shell_name)
|
||||
self.assertNotEqual(first.call_id, second.call_id)
|
||||
self.assertEqual(second.result.result, 42)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user