Skip to content

Commit 20f3391

Browse files
Fix insert into collection when creating an item by copying (#1356)
1 parent 915ca97 commit 20f3391

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed

pydatalab/src/pydatalab/routes/v0_1/items.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,8 +413,13 @@ def _create_sample(
413413
if constituent["item"].get("item_id") is None
414414
or constituent["item"].get("item_id") not in existing_consituent_ids
415415
]
416+
417+
original_collections = sample_dict.get("collections", [])
416418
sample_dict = copied_doc
417419

420+
if original_collections:
421+
sample_dict["collections"] = original_collections
422+
418423
elif copied_doc["type"] == "cells":
419424
for component in (
420425
"positive_electrode",
@@ -432,8 +437,12 @@ def _create_sample(
432437
or constituent["item"].get("item_id") not in existing_consituent_ids
433438
]
434439

440+
original_collections = sample_dict.get("collections", [])
435441
sample_dict = copied_doc
436442

443+
if original_collections:
444+
sample_dict["collections"] = original_collections
445+
437446
try:
438447
# If passed collection data, dereference it and check if the collection exists
439448
sample_dict["collections"] = _check_collections(sample_dict)

pydatalab/tests/server/test_samples.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,3 +933,119 @@ def test_remove_items_from_collection_partial_success(
933933
rel for rel in item.get("relationships", []) if rel.get("type") == "collections"
934934
]
935935
assert len(collection_relationships) == 0
936+
937+
938+
@pytest.mark.dependency(depends=["test_create_collections"])
939+
def test_copy_sample_and_add_to_collection(client, default_sample_dict, default_collection):
940+
original_sample = default_sample_dict.copy()
941+
original_sample["item_id"] = "original_for_copy_test"
942+
original_sample["name"] = "Original sample"
943+
944+
response = client.post("/new-sample/", json=original_sample)
945+
assert response.status_code == 201
946+
assert response.json["status"] == "success"
947+
948+
collection_dict = default_collection.dict().copy()
949+
collection_dict["collection_id"] = "test_copy_collection"
950+
response = client.put("/collections", json={"data": collection_dict})
951+
assert response.status_code == 201
952+
assert response.json["status"] == "success"
953+
954+
copy_request = {
955+
"item_id": "copied_in_collection",
956+
"type": default_sample_dict["type"],
957+
"collections": [{"collection_id": "test_copy_collection"}],
958+
"copy_from_item_id": "original_for_copy_test",
959+
}
960+
response = client.post("/new-sample/", json=copy_request)
961+
assert response.status_code == 201
962+
assert response.json["status"] == "success"
963+
964+
response = client.get("/get-item-data/copied_in_collection")
965+
assert response.status_code == 200
966+
item_data = response.json["item_data"]
967+
assert item_data["item_id"] == "copied_in_collection"
968+
969+
response = client.get("/collections/test_copy_collection")
970+
assert response.status_code == 200
971+
child_items = response.json["child_items"]
972+
assert any(item["item_id"] == "copied_in_collection" for item in child_items)
973+
assert not any(item["item_id"] == "original_for_copy_test" for item in child_items)
974+
975+
976+
@pytest.mark.dependency(depends=["test_copy_sample_and_add_to_collection"])
977+
def test_copy_sample_from_collection_to_different_collection(
978+
client, default_sample_dict, default_collection
979+
):
980+
collection1_dict = default_collection.dict().copy()
981+
collection1_dict["collection_id"] = "collection_1"
982+
response = client.put("/collections", json={"data": collection1_dict})
983+
assert response.status_code == 201
984+
985+
collection2_dict = default_collection.dict().copy()
986+
collection2_dict["collection_id"] = "collection_2"
987+
response = client.put("/collections", json={"data": collection2_dict})
988+
assert response.status_code == 201
989+
990+
original_sample = default_sample_dict.copy()
991+
original_sample["item_id"] = "sample_in_collection1"
992+
original_sample["collections"] = [{"collection_id": "collection_1"}]
993+
994+
response = client.post("/new-sample/", json=original_sample)
995+
assert response.status_code == 201
996+
assert response.json["status"] == "success"
997+
998+
response = client.get("/collections/collection_1")
999+
assert response.status_code == 200
1000+
assert any(item["item_id"] == "sample_in_collection1" for item in response.json["child_items"])
1001+
1002+
copy_request = {
1003+
"item_id": "sample_in_collection2",
1004+
"type": default_sample_dict["type"],
1005+
"collections": [{"collection_id": "collection_2"}],
1006+
"copy_from_item_id": "sample_in_collection1",
1007+
}
1008+
response = client.post("/new-sample/", json=copy_request)
1009+
assert response.status_code == 201
1010+
assert response.json["status"] == "success"
1011+
1012+
response = client.get("/collections/collection_2")
1013+
assert response.status_code == 200
1014+
assert any(item["item_id"] == "sample_in_collection2" for item in response.json["child_items"])
1015+
1016+
response = client.get("/collections/collection_1")
1017+
assert response.status_code == 200
1018+
child_items = response.json["child_items"]
1019+
assert not any(item["item_id"] == "sample_in_collection2" for item in child_items)
1020+
assert any(item["item_id"] == "sample_in_collection1" for item in child_items)
1021+
1022+
1023+
@pytest.mark.dependency(depends=["test_copy_sample_from_collection_to_different_collection"])
1024+
def test_copy_sample_without_copying_collections(client, default_sample_dict, default_collection):
1025+
collection_dict = default_collection.dict().copy()
1026+
collection_dict["collection_id"] = "test_no_auto_copy_collection"
1027+
response = client.put("/collections", json={"data": collection_dict})
1028+
assert response.status_code == 201
1029+
1030+
original_sample = default_sample_dict.copy()
1031+
original_sample["item_id"] = "original_in_collection"
1032+
original_sample["collections"] = [{"collection_id": "test_no_auto_copy_collection"}]
1033+
1034+
response = client.post("/new-sample/", json=original_sample)
1035+
assert response.status_code == 201
1036+
assert response.json["status"] == "success"
1037+
1038+
copy_request = {
1039+
"item_id": "copy_without_collection",
1040+
"type": default_sample_dict["type"],
1041+
"copy_from_item_id": "original_in_collection",
1042+
}
1043+
response = client.post("/new-sample/", json=copy_request)
1044+
assert response.status_code == 201
1045+
assert response.json["status"] == "success"
1046+
1047+
response = client.get("/collections/test_no_auto_copy_collection")
1048+
assert response.status_code == 200
1049+
child_items = response.json["child_items"]
1050+
assert not any(item["item_id"] == "copy_without_collection" for item in child_items)
1051+
assert any(item["item_id"] == "original_in_collection" for item in child_items)

webapp/src/server_fetch_utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,21 @@ export function createNewItem(
123123
}).then(function (response_json) {
124124
if (SAMPLE_TABLE_TYPES.includes(response_json.sample_list_entry.type)) {
125125
store.commit("prependToSampleList", response_json.sample_list_entry);
126+
if (startingCollection && startingCollection.length > 0) {
127+
getSampleList();
128+
}
126129
}
127130
if (INVENTORY_TABLE_TYPES.includes(response_json.sample_list_entry.type)) {
128131
store.commit("prependToStartingMaterialList", response_json.sample_list_entry);
132+
if (startingCollection && startingCollection.length > 0) {
133+
getStartingMaterialList();
134+
}
129135
}
130136
if (EQUIPMENT_TABLE_TYPES.includes(response_json.sample_list_entry.type)) {
131137
store.commit("prependToEquipmentList", response_json.sample_list_entry);
138+
if (startingCollection && startingCollection.length > 0) {
139+
getEquipmentList();
140+
}
132141
}
133142
return "success";
134143
});

0 commit comments

Comments
 (0)