@@ -250,6 +250,7 @@ def test_xrd_block_lifecycle(admin_client, default_sample_dict):
250
250
item_data = response .json ["item_data" ]
251
251
block_data = item_data ["blocks_obj" ][block_id ]
252
252
block_data ["file_id" ] = file_id
253
+ block_data ["wavelength" ] = 2.0
253
254
254
255
response = admin_client .post (
255
256
"/update-block/" , json = {"block_data" : block_data , "save_to_db" : True }
@@ -258,16 +259,30 @@ def test_xrd_block_lifecycle(admin_client, default_sample_dict):
258
259
web_block = response .json ["new_block_data" ]
259
260
assert "bokeh_plot_data" in web_block
260
261
assert "processed_data" in web_block
262
+ assert web_block ["wavelength" ] == 2.0
261
263
assert "peak_data" in web_block ["processed_data" ]
262
264
assert "file_id" in web_block
263
265
assert web_block ["file_id" ] == file_id
264
266
assert web_block .get ("errors" ) is None
265
267
266
268
block = XRDBlock .from_web (web_block )
267
269
db = block .to_db ()
270
+ # 'computed' keys should be dropped when loading from web
268
271
assert "bokeh_plot_data" not in db
269
- assert "processed_data" in db
270
- assert "peak_data" in db ["processed_data" ]
272
+ assert "processed_data" not in db
273
+ assert db ["wavelength" ] == 2.0
274
+
275
+ # But they should still be in the database
276
+ response = admin_client .get (f"/get-item-data/{ sample_id } " )
277
+ assert response .status_code == 200
278
+
279
+ item_data = response .json ["item_data" ]
280
+ assert response .json ["status" ] == "success"
281
+ assert "blocks_obj" in item_data
282
+ block = item_data ["blocks_obj" ][block_id ]
283
+ assert "processed_data" in block
284
+ assert "peak_data" in block ["processed_data" ]
285
+ assert block ["wavelength" ] == 2.0
271
286
272
287
273
288
def test_comment_block_manipulation (admin_client , default_sample_dict , database ):
@@ -313,6 +328,7 @@ def test_comment_block_manipulation(admin_client, default_sample_dict, database)
313
328
# Check that this result was actually stored
314
329
response = admin_client .get (f"/get-item-data/{ sample_id } " )
315
330
assert response .status_code == 200
331
+ item_data = response .json ["item_data" ]
316
332
assert response .json ["status" ] == "success"
317
333
assert (
318
334
response .json ["item_data" ]["blocks_obj" ][block_id ]["freeform_comment" ]
@@ -323,20 +339,46 @@ def test_comment_block_manipulation(admin_client, default_sample_dict, database)
323
339
# Try to add some bad data
324
340
block_data ["bokeh_plot_data" ] = {"bokeh" : "json" }
325
341
block_data ["random_new_key" ] = "test new key"
342
+ block_data ["freeform_comment" ] = "This is a test comment block with extra data."
326
343
response = admin_client .post ("/update-block/" , json = {"block_data" : block_data })
327
344
assert response .status_code == 200
328
345
assert response .json ["status" ] == "success"
329
346
assert response .json ["new_block_data" ]["blocktype" ] == block_type
330
- assert response .json ["new_block_data" ]["freeform_comment" ] == "This is a test comment block."
347
+ assert (
348
+ response .json ["new_block_data" ]["freeform_comment" ]
349
+ == "This is a test comment block with extra data."
350
+ )
331
351
assert response .json ["new_block_data" ]["title" ] == "Test Comment Block"
332
352
assert "bokeh_plot_data" not in response .json ["new_block_data" ]
333
- assert "random_new_key" not in response .json ["new_block_data" ]
353
+
354
+ # Extra random keys will be in the response (in case they are parameters for the block that are not yet handled,
355
+ # but they will not be stored in the database)
356
+ assert "random_new_key" in response .json ["new_block_data" ]
334
357
335
358
raw_item = database .items .find_one ({"item_id" : sample_id })
336
359
assert raw_item
337
- assert "bokeh_plot_data" not in raw_item
338
- assert "random_new_key" not in raw_item
339
- assert "errors" not in raw_item
360
+ raw_block = raw_item ["blocks_obj" ][block_id ]
361
+ assert "bokeh_plot_data" not in raw_block
362
+ # assert "random_new_key" not in raw_block
363
+ assert "errors" not in raw_block
364
+
365
+ # Finally, try to update using the save-item endpoint, and make sure any bad data gets stripped out
366
+ item_data ["blocks_obj" ][block_id ]["bokeh_plot_data" ] = {"bokeh" : "json" }
367
+ item_data ["blocks_obj" ][block_id ]["random_new_key" ] = "test new key again"
368
+ item_data ["blocks_obj" ][block_id ]["freeform_comment" ] = "This is the latest test comment."
369
+
370
+ admin_client .post ("/save-item/" , json = {"item_id" : sample_id , "data" : item_data })
371
+ assert response .status_code == 200
372
+
373
+ response = admin_client .get (f"/get-item-data/{ sample_id } " )
374
+ assert response .status_code == 200
375
+ assert response .json ["status" ] == "success"
376
+ item_data = response .json ["item_data" ]
377
+ block = item_data ["blocks_obj" ][block_id ]
378
+
379
+ assert block ["freeform_comment" ] == "This is the latest test comment."
380
+ assert block .get ("bokeh_plot_data" ) is None
381
+ assert block ["random_new_key" ] == "test new key again"
340
382
341
383
342
384
@pytest .mark .parametrize (
0 commit comments