@@ -74,6 +74,23 @@ async def _(kernel_id, ready=None):
74
74
return _
75
75
76
76
77
+ @pytest .fixture ()
78
+ def rtc_test_notebook (jp_serverapp , rtc_create_notebook ):
79
+ async def _ (notebook , path = "test.ipynb" ):
80
+ nb_content = nbformat .writes (notebook , version = 4 )
81
+ returned_path , _ = await rtc_create_notebook (path , nb_content , store = True )
82
+ assert path == returned_path
83
+ document_id = get_document_id (jp_serverapp , "test.ipynb" )
84
+ return document_id
85
+ return _
86
+
87
+
88
+ def get_document_id (jp_serverapp , notebook_name ):
89
+ fim = jp_serverapp .web_app .settings ["file_id_manager" ]
90
+ document_id = f'json:notebook:{ fim .get_id (notebook_name )} '
91
+ return document_id
92
+
93
+
77
94
@pytest .mark .timeout (TEST_TIMEOUT )
78
95
@pytest .mark .parametrize (
79
96
"snippet,output" ,
@@ -82,29 +99,89 @@ async def _(kernel_id, ready=None):
82
99
"print('hello buddy')" ,
83
100
'{"output_type": "stream", "name": "stdout", "text": "hello buddy\\ n"}' ,
84
101
),
102
+ ),
103
+ )
104
+ async def test_post_execute_no_ycell (jp_fetch , pending_kernel_is_ready , snippet , output ):
105
+ r = await jp_fetch (
106
+ "api" , "kernels" , method = "POST" , body = json .dumps ({"name" : NATIVE_KERNEL_NAME })
107
+ )
108
+ kernel = json .loads (r .body .decode ())
109
+ await pending_kernel_is_ready (kernel ["id" ])
110
+
111
+ response = await wait_for_request (
112
+ jp_fetch ,
113
+ "api" ,
114
+ "kernels" ,
115
+ kernel ["id" ],
116
+ "execute" ,
117
+ method = "POST" ,
118
+ body = json .dumps ({"code" : snippet }),
119
+ )
120
+
121
+ assert response .code == 200
122
+ payload = json .loads (response .body )
123
+ assert payload == {
124
+ "status" : "ok" ,
125
+ "execution_count" : 1 ,
126
+ "outputs" : f"[{ output } ]" ,
127
+ }
128
+
129
+ response2 = await jp_fetch ("api" , "kernels" , kernel ["id" ], method = "DELETE" )
130
+ assert response2 .code == 204
131
+
132
+ await asyncio .sleep (1 )
133
+
134
+
135
+ @pytest .mark .timeout (2 * TEST_TIMEOUT )
136
+ @pytest .mark .parametrize (
137
+ "snippet,output" ,
138
+ (
139
+ (
140
+ "print('hello buddy')" ,
141
+ '{"output_type": "stream", "name": "stdout", "text": ["hello buddy"]}' ,
142
+ ),
85
143
("a = 1" , "" ),
86
144
(
87
145
"""from IPython.display import HTML
88
146
HTML('<p><b>Jupyter</b> rocks.</p>')""" ,
89
147
'{"output_type": "execute_result", "metadata": {}, "data": {"text/plain": "<IPython.core.display.HTML object>", "text/html": "<p><b>Jupyter</b> rocks.</p>"}, "execution_count": 1}' , # noqa: E501
90
148
),
149
+ (
150
+ "display('a'); print('b')" ,
151
+ (
152
+ '{"output_type": "display_data", "metadata": {}, "data": {"text/plain": "\' a\' "}}'
153
+ ', {"output_type": "stream", "name": "stdout", "text": ["b"]}'
154
+ )
155
+ )
91
156
),
92
157
)
93
- async def test_post_execute (jp_fetch , pending_kernel_is_ready , snippet , output ):
158
+ async def test_post_execute_wiht_ycell (jp_fetch , pending_kernel_is_ready , snippet , output , rtc_test_notebook ):
94
159
r = await jp_fetch (
95
160
"api" , "kernels" , method = "POST" , body = json .dumps ({"name" : NATIVE_KERNEL_NAME })
96
161
)
97
162
kernel = json .loads (r .body .decode ())
98
163
await pending_kernel_is_ready (kernel ["id" ])
99
164
165
+ nb = nbformat .v4 .new_notebook (
166
+ cells = [nbformat .v4 .new_code_cell (source = snippet )]
167
+ )
168
+ document_id = await rtc_test_notebook (nb )
169
+ cell_id = nb ["cells" ][0 ]["id" ]
170
+
100
171
response = await wait_for_request (
101
172
jp_fetch ,
102
173
"api" ,
103
174
"kernels" ,
104
175
kernel ["id" ],
105
176
"execute" ,
106
177
method = "POST" ,
107
- body = json .dumps ({"code" : snippet }),
178
+ body = json .dumps ({
179
+ "code" : snippet ,
180
+ "metadata" : {
181
+ "cell_id" : cell_id ,
182
+ "document_id" : document_id
183
+ }
184
+ }),
108
185
)
109
186
110
187
assert response .code == 200
@@ -223,17 +300,15 @@ async def fake_execute(client, ydoc, snippet, metadata, stdin_hook):
223
300
224
301
225
302
@pytest .mark .timeout (TEST_TIMEOUT )
226
- async def test_execution_timing_metadata (jp_root_dir , jp_fetch , pending_kernel_is_ready , rtc_create_notebook , jp_serverapp ):
303
+ async def test_execution_timing_metadata (jp_root_dir , jp_fetch , pending_kernel_is_ready , rtc_test_notebook , jp_serverapp ):
227
304
snippet = "a = 1"
228
305
nb = nbformat .v4 .new_notebook (
229
306
cells = [nbformat .v4 .new_code_cell (source = snippet , execution_count = 1 )]
230
307
)
231
- nb_content = nbformat . writes ( nb , version = 4 )
232
- path , _ = await rtc_create_notebook ( "test.ipynb" , nb_content , store = True )
308
+ path = "test.ipynb"
309
+ document_id = await rtc_test_notebook ( nb , path = path )
233
310
collaboration = jp_serverapp .web_app .settings ["jupyter_server_ydoc" ]
234
- fim = jp_serverapp .web_app .settings ["file_id_manager" ]
235
- document_id = f'json:notebook:{ fim .get_id ("test.ipynb" )} '
236
- cell_id = nb ["cells" ][0 ].get ("id" )
311
+ cell_id = nb ["cells" ][0 ]["id" ]
237
312
238
313
r = await jp_fetch (
239
314
"api" , "kernels" , method = "POST" , body = json .dumps ({"name" : NATIVE_KERNEL_NAME })
0 commit comments