Add JSON-safe serialization
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import contextlib
|
||||
import io
|
||||
import unittest
|
||||
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ipython_demo import App, Shell
|
||||
from ipython_webapp.app import app as web_app
|
||||
|
||||
|
||||
class SerializationTests(unittest.TestCase):
|
||||
def test_call_result_stays_raw_until_to_response(self):
|
||||
shell = Shell("raw-result")
|
||||
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
result, _events = shell.run_cell(
|
||||
"plt.plot([1, 2, 3], [4, 5, 6])",
|
||||
call_id="raw-call",
|
||||
)
|
||||
|
||||
self.assertIn("Line2D", repr(result.result))
|
||||
response = result.to_response()
|
||||
self.assertIsNone(response.result)
|
||||
self.assertIn("Line2D", response.result_repr)
|
||||
|
||||
def test_matplotlib_result_is_safe_for_web_serialization(self):
|
||||
app = App()
|
||||
|
||||
with contextlib.redirect_stdout(io.StringIO()):
|
||||
response = app.run_code(
|
||||
"plt.plot([1, 2, 3], [4, 5, 6])",
|
||||
shell_name="new",
|
||||
)
|
||||
|
||||
encoded = jsonable_encoder(response)
|
||||
|
||||
self.assertIsNone(response.result.result)
|
||||
self.assertIn("Line2D", response.result.result_repr)
|
||||
self.assertIsNone(encoded["result"]["result"])
|
||||
self.assertIn("Line2D", encoded["result"]["result_repr"])
|
||||
|
||||
def test_plot_route_returns_dto_instead_of_recursion_error(self):
|
||||
response = TestClient(web_app).post(
|
||||
"/shells/http-plot/run",
|
||||
content="plt.plot([1, 2, 3], [4, 5, 6])",
|
||||
headers={"content-type": "text/plain"},
|
||||
)
|
||||
|
||||
self.assertEqual(response.status_code, 200)
|
||||
payload = response.json()
|
||||
self.assertIsNone(payload["result"]["result"])
|
||||
self.assertIn("Line2D", payload["result"]["result_repr"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user