Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] authored and ml-evs committed Sep 10, 2024
1 parent ee2369c commit 118a1dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 66 deletions.
24 changes: 8 additions & 16 deletions pydatalab/pydatalab/routes/v0_1/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -282,8 +278,7 @@ def save_collection(collection_id):
@COLLECTIONS.route("/collections/<collection_id>", 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:
Expand Down Expand Up @@ -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))
Expand All @@ -339,17 +333,15 @@ 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

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
Expand Down
82 changes: 32 additions & 50 deletions pydatalab/tests/server/test_samples.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from bson import ObjectId
import copy
import datetime
import json
Expand Down Expand Up @@ -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):
Expand All @@ -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"
Expand All @@ -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']}")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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"
Expand Down Expand Up @@ -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}")
Expand All @@ -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}")
Expand All @@ -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}")
Expand All @@ -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}")
Expand All @@ -636,17 +621,15 @@ 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
default_sample_dict["item_id"] = new_id2
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}")
Expand All @@ -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"
Expand All @@ -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"

0 comments on commit 118a1dc

Please sign in to comment.