Skip to content

Commit 9a2d928

Browse files
committed
Fix unit tests
1 parent bf5719c commit 9a2d928

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

jupyter_server_nbmodel/tests/test_handlers.py

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,27 @@ async def _(kernel_id, ready=None):
2828

2929

3030
@pytest.mark.timeout(TEST_TIMEOUT)
31-
@pytest.mark.parametrize("snippet,output",
31+
@pytest.mark.parametrize(
32+
"snippet,output",
3233
(
33-
("print('hello buddy')", '{"output_type": "stream", "name": "stdout", "text": "hello buddy\\n"}'),
34-
("a = 1", '{}'),
35-
("1 / 0", '{}')
36-
)
34+
(
35+
"print('hello buddy')",
36+
'{"output_type": "stream", "name": "stdout", "text": "hello buddy\\n"}',
37+
),
38+
("a = 1", ""),
39+
),
3740
)
3841
async def test_post_execute(jp_fetch, pending_kernel_is_ready, snippet, output):
39-
# Start the first kernel
4042
r = await jp_fetch(
4143
"api", "kernels", method="POST", body=json.dumps({"name": NATIVE_KERNEL_NAME})
4244
)
43-
kernel1 = json.loads(r.body.decode())
44-
await pending_kernel_is_ready(kernel1["id"])
45+
kernel = json.loads(r.body.decode())
46+
await pending_kernel_is_ready(kernel["id"])
4547

4648
response = await jp_fetch(
4749
"api",
4850
"kernels",
49-
kernel1["id"],
51+
kernel["id"],
5052
"execute",
5153
method="POST",
5254
body=json.dumps({"code": snippet}),
@@ -57,8 +59,47 @@ async def test_post_execute(jp_fetch, pending_kernel_is_ready, snippet, output):
5759
assert payload == {
5860
"status": "ok",
5961
"execution_count": 1,
60-
"outputs": f'[{output}]',
62+
"outputs": f"[{output}]",
63+
}
64+
65+
response2 = await jp_fetch("api", "kernels", kernel["id"], method="DELETE")
66+
assert response2.code == 204
67+
68+
69+
@pytest.mark.timeout(TEST_TIMEOUT)
70+
@pytest.mark.parametrize(
71+
"snippet,output",
72+
(
73+
(
74+
"1 / 0",
75+
'{"output_type": "error", "ename": "ZeroDivisionError", "evalue": "division by zero", "traceback": ["\\u001b[0;31m---------------------------------------------------------------------------\\u001b[0m", "\\u001b[0;31mZeroDivisionError\\u001b[0m Traceback (most recent call last)", "Cell \\u001b[0;32mIn[1], line 1\\u001b[0m\\n\\u001b[0;32m----> 1\\u001b[0m \\u001b[38;5;241;43m1\\u001b[39;49m\\u001b[43m \\u001b[49m\\u001b[38;5;241;43m/\\u001b[39;49m\\u001b[43m \\u001b[49m\\u001b[38;5;241;43m0\\u001b[39;49m\\n", "\\u001b[0;31mZeroDivisionError\\u001b[0m: division by zero"]}', # noqa: E501
76+
),
77+
),
78+
)
79+
async def test_post_erroneous_execute(jp_fetch, pending_kernel_is_ready, snippet, output):
80+
# Start the first kernel
81+
r = await jp_fetch(
82+
"api", "kernels", method="POST", body=json.dumps({"name": NATIVE_KERNEL_NAME})
83+
)
84+
kernel = json.loads(r.body.decode())
85+
await pending_kernel_is_ready(kernel["id"])
86+
87+
response = await jp_fetch(
88+
"api",
89+
"kernels",
90+
kernel["id"],
91+
"execute",
92+
method="POST",
93+
body=json.dumps({"code": snippet}),
94+
)
95+
96+
assert response.code == 200
97+
payload = json.loads(response.body)
98+
assert payload == {
99+
"status": "error",
100+
"execution_count": 1,
101+
"outputs": f"[{output}]",
61102
}
62103

63-
await jp_fetch("api", "kernels", kernel1["id"], method="DELETE")
64-
assert response.code == 204
104+
response2 = await jp_fetch("api", "kernels", kernel["id"], method="DELETE")
105+
assert response2.code == 204

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dependencies = ["jupyter_server>=1.6,<3"]
2424

2525
[project.optional-dependencies]
2626
test = [
27-
"pytest>=8.0",
27+
"pytest~=7.0",
2828
"pytest-jupyter[server]>=0.6",
2929
"pytest-timeout",
3030
]

0 commit comments

Comments
 (0)