diff --git a/pydatalab/pydatalab/routes/v0_1/collections.py b/pydatalab/pydatalab/routes/v0_1/collections.py index ce00161d7..d9e217166 100644 --- a/pydatalab/pydatalab/routes/v0_1/collections.py +++ b/pydatalab/pydatalab/routes/v0_1/collections.py @@ -109,8 +109,7 @@ def create_collection(): ) if copy_from_id: - raise NotImplementedError( - "Copying collections is not yet implemented.") + raise NotImplementedError("Copying collections is not yet implemented.") if CONFIG.TESTING: data["creator_ids"] = [24 * "0"] @@ -135,8 +134,7 @@ def create_collection(): 409, # 409: Conflict ) - data["last_modified"] = data.get( - "last_modified", datetime.datetime.now().isoformat()) + data["last_modified"] = data.get("last_modified", datetime.datetime.now().isoformat()) try: data_model = Collection(**data) @@ -179,8 +177,7 @@ def create_collection(): "item_id": {"$in": list(item_ids)}, **get_default_permissions(user_only=True), }, - {"$push": {"relationships": { - "type": "collections", "immutable_id": immutable_id}}}, + {"$push": {"relationships": {"type": "collections", "immutable_id": immutable_id}}}, ) data_model.num_items = results.modified_count @@ -234,8 +231,7 @@ def save_collection(collection_id): updated_data["last_modified"] = datetime.datetime.now().isoformat() collection = flask_mongo.db.collections.find_one( - {"collection_id": collection_id, ** - get_default_permissions(user_only=True)} + {"collection_id": collection_id, **get_default_permissions(user_only=True)} ) if not collection: @@ -282,8 +278,7 @@ def save_collection(collection_id): @COLLECTIONS.route("/collections/", methods=["DELETE"]) def delete_collection(collection_id: str): result = flask_mongo.db.collections.delete_one( - {"collection_id": collection_id, ** - get_default_permissions(user_only=True)} + {"collection_id": collection_id, **get_default_permissions(user_only=True)} ) if result.deleted_count != 1: @@ -311,8 +306,7 @@ def search_collections(): query = request.args.get("query", type=str) nresults = request.args.get("nresults", default=100, type=int) - match_obj = {"$text": {"$search": query}, ** - get_default_permissions(user_only=True)} + match_obj = {"$text": {"$search": query}, **get_default_permissions(user_only=True)} cursor = [ json.loads(Collection(**doc).json(exclude_unset=True)) @@ -339,8 +333,7 @@ def add_items_to_collection(collection_id): data = request.get_json() refcodes = data.get("data", {}).get("refcodes", []) - collection = flask_mongo.db.collections.find_one( - {"collection_id": collection_id}) + collection = flask_mongo.db.collections.find_one({"collection_id": collection_id}) if not collection: return jsonify({"error": "Collection not found"}), 404 @@ -348,8 +341,7 @@ def add_items_to_collection(collection_id): if not refcodes: return jsonify({"error": "No item provided"}), 400 - item_count = flask_mongo.db.items.count_documents( - {"refcode": {"$in": refcodes}}) + item_count = flask_mongo.db.items.count_documents({"refcode": {"$in": refcodes}}) if item_count == 0: return jsonify({"error": "No matching items found"}), 404 diff --git a/pydatalab/tests/server/test_samples.py b/pydatalab/tests/server/test_samples.py index 3c9d8c9ae..d300b4c80 100644 --- a/pydatalab/tests/server/test_samples.py +++ b/pydatalab/tests/server/test_samples.py @@ -1,4 +1,3 @@ -from bson import ObjectId import copy import datetime import json @@ -80,8 +79,7 @@ def test_new_sample_with_automatically_generated_id(client, user_id): response = client.get(f"/get-item-data/{created_item_id}") assert response.status_code == 200 assert response.json["status"] == "success" - assert response.json["item_data"]["refcode"].split(":")[ - 1] == created_item_id + assert response.json["item_data"]["refcode"].split(":")[1] == created_item_id for key in new_sample_data.keys(): if isinstance(v := new_sample_data[key], datetime.datetime): @@ -97,8 +95,7 @@ def test_save_good_sample(client, default_sample_dict): updated_sample.update({"description": "This is a newer test sample."}) response = client.post( "/save-item/", - json={ - "item_id": default_sample_dict["item_id"], "data": updated_sample}, + json={"item_id": default_sample_dict["item_id"], "data": updated_sample}, ) assert response.status_code == 200, response.json assert response.json["status"] == "success" @@ -117,8 +114,7 @@ def test_save_bad_sample(client, default_sample_dict): updated_sample.update({"unknown_key": "This should not be allowed in."}) response = client.post( "/save-item/", - json={ - "item_id": default_sample_dict["item_id"], "data": updated_sample}, + json={"item_id": default_sample_dict["item_id"], "data": updated_sample}, ) assert response.status_code == 200 response = client.get(f"/get-item-data/{default_sample_dict['item_id']}") @@ -150,8 +146,7 @@ def test_create_indices(real_mongo_client): create_default_indices(real_mongo_client) indexes = list(real_mongo_client.get_database().items.list_indexes()) - expected_index_names = ( - "_id_", "items full-text search", "item type", "unique item ID") + expected_index_names = ("_id_", "items full-text search", "item type", "unique item ID") names = [index["name"] for index in indexes] assert all(name in names for name in expected_index_names) @@ -349,8 +344,7 @@ def test_copy_from_sample(client, complicated_sample): # Now try to directly make a copy that has the same data with a new ID copy_doc = {"item_id": "copy_of_complicated_sample"} - copy_request = {"new_sample_data": copy_doc, - "copy_from_item_id": complicated_sample.item_id} + copy_request = {"new_sample_data": copy_doc, "copy_from_item_id": complicated_sample.item_id} response = client.post("/new-sample/", json=copy_request) assert response.status_code == 201, response.json @@ -412,8 +406,7 @@ def test_create_multiple_samples(client, complicated_sample): @pytest.mark.dependency(depends=["test_create_multiple_samples"]) def test_create_cell(client, default_cell): - response = client.post( - "/new-sample/", json=json.loads(default_cell.json())) + response = client.post("/new-sample/", json=json.loads(default_cell.json())) assert response.status_code == 201, response.json assert response.json["status"] == "success" @@ -423,12 +416,10 @@ def test_create_cell(client, default_cell): "item_id": test_id, "type": "cells", "electrolyte": [ - {"item": {"name": "salt", "chemform": "NaCl"}, - "quantity": 100, "unit": "ml"} + {"item": {"name": "salt", "chemform": "NaCl"}, "quantity": 100, "unit": "ml"} ], } - copy_request = {"new_sample_data": copy_doc, - "copy_from_item_id": default_cell.item_id} + copy_request = {"new_sample_data": copy_doc, "copy_from_item_id": default_cell.item_id} response = client.post("/new-sample/", json=copy_request) # Check that the copy retains the old components and the new assert response.status_code == 201, response.json @@ -470,8 +461,7 @@ def test_cell_from_scratch(client): ) response = client.post( "/new-sample/", - json={"new_sample_data": cell, - "copy_from_item_id": "test_cell_from_scratch"}, + json={"new_sample_data": cell, "copy_from_item_id": "test_cell_from_scratch"}, ) assert response.status_code == 201 @@ -489,8 +479,7 @@ def test_create_collections(client, default_collection): assert response.status_code == 200 # Create an empty collection - response = client.put( - "/collections", json={"data": json.loads(default_collection.json())}) + response = client.put("/collections", json={"data": json.loads(default_collection.json())}) assert response.status_code == 201, response.json assert response.json["status"] == "success" assert response.json["data"]["collection_id"] == "test_collection" @@ -565,8 +554,7 @@ def test_items_added_to_existing_collection(client, default_collection, default_ # Create a new item that is inside the default collection by passing collection_id new_id = "testing_collection_insert_by_id" default_sample_dict["item_id"] = new_id - default_sample_dict["collections"] = [ - {"collection_id": default_collection.collection_id}] + default_sample_dict["collections"] = [{"collection_id": default_collection.collection_id}] response = client.post("/new-sample/", json=default_sample_dict) assert response.status_code == 201, response.json response = client.get(f"/collections/{default_collection.collection_id}") @@ -586,8 +574,7 @@ def test_items_added_to_existing_collection(client, default_collection, default_ # Create a new item that is inside the default collection by passing immutable id new_id2 = "testing_collection_insert_by_immutable" default_sample_dict["item_id"] = new_id2 - default_sample_dict["collections"] = [ - {"immutable_id": collection_immutable_id}] + default_sample_dict["collections"] = [{"immutable_id": collection_immutable_id}] response = client.post("/new-sample/", json=default_sample_dict) assert response.status_code == 201, response.json response = client.get(f"/collections/{default_collection.collection_id}") @@ -600,8 +587,7 @@ def test_items_added_to_existing_collection(client, default_collection, default_ {"immutable_id": collection_immutable_id}, {"collection_id": "test_collection_2"}, ] - response = client.post( - "/save-item/", json={"data": default_sample_dict, "item_id": new_id2}) + response = client.post("/save-item/", json={"data": default_sample_dict, "item_id": new_id2}) assert response.status_code == 200, response.json response = client.get(f"/get-item-data/{new_id2}") @@ -618,8 +604,7 @@ def test_items_added_to_existing_collection(client, default_collection, default_ default_sample_dict["collections"] = [ {"collection_id": "test_collection_2"}, ] - response = client.post( - "/save-item/", json={"data": default_sample_dict, "item_id": new_id2}) + response = client.post("/save-item/", json={"data": default_sample_dict, "item_id": new_id2}) assert response.status_code == 200, response.json response = client.get(f"/get-item-data/{new_id2}") @@ -636,8 +621,7 @@ def test_items_added_to_existing_collection(client, default_collection, default_ default_sample_dict["collections"] = [ {"collection_id": "test_collection_3"}, ] - response = client.post( - "/save-item/", json={"data": default_sample_dict, "item_id": new_id2}) + response = client.post("/save-item/", json={"data": default_sample_dict, "item_id": new_id2}) assert response.status_code == 401, response.json # Check that sending same collection multiple times doesn't lead to duplicates @@ -645,8 +629,7 @@ def test_items_added_to_existing_collection(client, default_collection, default_ default_sample_dict["collections"] = [ {"collection_id": "test_collection_2"}, ] - response = client.post( - "/save-item/", json={"data": default_sample_dict, "item_id": new_id2}) + response = client.post("/save-item/", json={"data": default_sample_dict, "item_id": new_id2}) assert response.status_code == 200, response.json response = client.get(f"/get-item-data/{new_id2}") @@ -656,31 +639,28 @@ def test_items_added_to_existing_collection(client, default_collection, default_ ] assert len(response.json["item_data"]["collections"]) == 1 assert ( - len([d for d in response.json["item_data"] - ["relationships"] if d["type"] == "collections"]) + len([d for d in response.json["item_data"]["relationships"] if d["type"] == "collections"]) == 1 ) ##! + @pytest.mark.dependency() def test_add_items_to_collection_not_found(client): collection_id = "invalid_collection_id" - response = client.post(f"/collections/{collection_id}", json={ - "data": {"refcodes": []} - }) + response = client.post(f"/collections/{collection_id}", json={"data": {"refcodes": []}}) assert response.status_code == 404 assert response.json["error"] == "Collection not found" @pytest.mark.dependency(depends=["test_add_items_to_collection_not_found"]) def test_add_items_to_collection_no_items(client, default_collection): - - response = client.post(f"/collections/{default_collection.collection_id}", json={ - "data": {"refcodes": []} - }) + response = client.post( + f"/collections/{default_collection.collection_id}", json={"data": {"refcodes": []}} + ) assert response.status_code == 400 assert response.json["error"] == "No item provided" @@ -690,18 +670,20 @@ def test_add_items_to_collection_no_items(client, default_collection): def test_add_items_to_collection_no_matching_items(client, default_collection): refcodes = ["item123", "item456"] - response = client.post(f"/collections/{default_collection.collection_id}", json={ - "data": {"refcodes": refcodes} - }) + response = client.post( + f"/collections/{default_collection.collection_id}", json={"data": {"refcodes": refcodes}} + ) assert response.status_code == 404 assert response.json["error"] == "No matching items found" @pytest.mark.dependency(depends=["test_add_items_to_collection_no_matching_items"]) -def test_add_items_to_collection_success(client, default_collection, default_sample, complicated_sample): - - response = client.post(f"/collections/{default_collection.collection_id}", json={ - "data": {"refcodes": [default_sample.refcode, complicated_sample.refcode]} - }) +def test_add_items_to_collection_success( + client, default_collection, default_sample, complicated_sample +): + response = client.post( + f"/collections/{default_collection.collection_id}", + json={"data": {"refcodes": [default_sample.refcode, complicated_sample.refcode]}}, + ) assert response.status_code == 200 assert response.json["status"] == "success"