Skip to content

Commit e3f39fd

Browse files
committed
Add test for input case
1 parent 2018ead commit e3f39fd

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

jupyter_server_nbmodel/handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ async def post(self, kernel_id: str) -> None:
375375
client.stop_channels()
376376
del client
377377

378+
self.set_status(HTTPStatus.CREATED)
379+
378380

379381
class RequestHandler(ExtensionHandlerMixin, APIHandler):
380382
"""Handler for /api/kernels/<kernel_id>/requests/<request_id>"""

jupyter_server_nbmodel/tests/test_handlers.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ async def _wait_request(fetch, endpoint: str):
1919

2020
while (datetime.datetime.now() - start_time).total_seconds() < 0.9 * TEST_TIMEOUT:
2121
await asyncio.sleep(SLEEP)
22-
response = await fetch(endpoint)
23-
response.rethrow()
22+
response = await fetch(endpoint, raise_error=False)
23+
if response.code >= 400:
24+
response.rethrow()
2425
if response.code != 202:
2526
return response
2627

@@ -142,6 +143,54 @@ async def test_post_erroneous_execute(jp_fetch, pending_kernel_is_ready, snippet
142143
assert response2.code == 204
143144

144145

146+
@pytest.mark.timeout(TEST_TIMEOUT)
147+
async def test_post_input_execute(jp_fetch, pending_kernel_is_ready):
148+
# Start the first kernel
149+
r = await jp_fetch(
150+
"api", "kernels", method="POST", body=json.dumps({"name": NATIVE_KERNEL_NAME})
151+
)
152+
kernel = json.loads(r.body.decode())
153+
await pending_kernel_is_ready(kernel["id"])
154+
155+
response = await jp_fetch(
156+
"api",
157+
"kernels",
158+
kernel["id"],
159+
"execute",
160+
method="POST",
161+
body=json.dumps({"code": "input('Age:')"}),
162+
)
163+
assert response.code == 202
164+
location = response.headers["Location"]
165+
166+
response2 = await _wait_request(jp_fetch, location)
167+
168+
assert response2.code == 300
169+
payload = json.loads(response2.body)
170+
assert "parent_header" in payload
171+
assert payload["input_request"] == {"prompt": "Age:", "password": False}
172+
173+
response3 = await jp_fetch(
174+
"api", "kernels", kernel["id"], "input", method="POST", body=json.dumps({"input": "42"})
175+
)
176+
assert response3.code == 201
177+
178+
response4 = await _wait_request(
179+
jp_fetch,
180+
location
181+
)
182+
assert response4.code == 200
183+
payload2 = json.loads(response4.body)
184+
assert payload2 == {
185+
"status": "ok",
186+
"execution_count": 1,
187+
"outputs": '[{"output_type": "execute_result", "metadata": {}, "data": {"text/plain": "\'42\'"}, "execution_count": 1}]',
188+
}
189+
190+
r2 = await jp_fetch("api", "kernels", kernel["id"], method="DELETE")
191+
assert r2.code == 204
192+
193+
145194
# FIXME
146195
# @pytest.mark.timeout(TEST_TIMEOUT)
147196
# async def test_cancel_execute(jp_fetch, pending_kernel_is_ready):

0 commit comments

Comments
 (0)