Skip to content

Commit 4aa8752

Browse files
Fix pytest
1 parent 4e2653c commit 4aa8752

File tree

3 files changed

+64
-53
lines changed

3 files changed

+64
-53
lines changed

pydatalab/src/pydatalab/routes/v0_1/info.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,14 @@ def get_info():
109109
versions, features and so on.
110110
111111
"""
112-
attributes_data = {
113-
"identifier_prefix": CONFIG.IDENTIFIER_PREFIX,
114-
"features": FEATURE_FLAGS.model_dump()
115-
if hasattr(FEATURE_FLAGS, "model_dump")
116-
else FEATURE_FLAGS.__dict__,
117-
}
118-
119-
if CONFIG.DEPLOYMENT_METADATA:
120-
deployment_meta = CONFIG.DEPLOYMENT_METADATA.model_dump(exclude_none=True)
121-
attributes_data.update(deployment_meta)
112+
metadata = _get_deployment_metadata_once().copy()
113+
info = Info(**metadata)
122114

123115
return (
124116
jsonify(
125117
json.loads(
126118
JSONAPIResponse(
127-
data=Data(id="/", type="info", attributes=Attributes(**attributes_data)),
119+
data=Data(id="/", type="info", attributes=info),
128120
meta=Meta(query=request.query_string.decode() if request.query_string else ""),
129121
links=Links(self=request.url),
130122
).model_dump_json()

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

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,6 @@ def get_item_data(
904904
doc = None
905905

906906
if not doc:
907-
LOGGER.error(f"No document found for {match}")
908907
return (
909908
jsonify(
910909
{
@@ -1040,6 +1039,7 @@ def save_item():
10401039
"creators",
10411040
"creator_ids",
10421041
"item_id",
1042+
"relationships",
10431043
):
10441044
if k in updated_data:
10451045
del updated_data[k]
@@ -1084,46 +1084,54 @@ def save_item():
10841084
404,
10851085
)
10861086

1087-
if "collections" in updated_data:
1088-
if updated_data.get("collections", []):
1089-
try:
1090-
updated_data["collections"] = _check_collections(updated_data)
1091-
except ValueError as exc:
1092-
return (
1093-
dict(
1094-
status="error",
1095-
message=f"Cannot update {item_id!r} with missing collections {updated_data['collections']!r}: {exc}",
1096-
item_id=item_id,
1097-
),
1098-
401,
1099-
)
1087+
if updated_data.get("collections", []):
1088+
try:
1089+
updated_data["collections"] = _check_collections(updated_data)
1090+
except ValueError as exc:
1091+
return (
1092+
dict(
1093+
status="error",
1094+
message=f"Cannot update {item_id!r} with missing collections {updated_data['collections']!r}: {exc}",
1095+
item_id=item_id,
1096+
),
1097+
401,
1098+
)
11001099

1101-
existing_item = flask_mongo.db.items.find_one({"item_id": item_id})
1102-
if existing_item:
1103-
existing_relationships = existing_item.get("relationships", [])
1104-
non_collection_relationships = [
1105-
rel for rel in existing_relationships if rel.get("type") != "collections"
1106-
]
1100+
existing_item = flask_mongo.db.items.find_one({"item_id": item_id})
1101+
if existing_item:
1102+
existing_relationships = existing_item.get("relationships", [])
1103+
non_collection_relationships = [
1104+
rel for rel in existing_relationships if rel.get("type") != "collections"
1105+
]
11071106

1108-
collection_relationships = []
1109-
for coll in updated_data.get("collections", []):
1110-
immutable_id = coll.get("immutable_id")
1111-
if immutable_id:
1112-
if isinstance(immutable_id, str):
1113-
from bson import ObjectId
1107+
collection_relationships = []
1108+
for coll in updated_data.get("collections", []):
1109+
immutable_id = coll.get("immutable_id")
1110+
collection_id = coll.get("collection_id")
11141111

1115-
immutable_id = ObjectId(immutable_id)
1112+
if immutable_id:
1113+
if isinstance(immutable_id, str):
1114+
from bson import ObjectId
11161115

1117-
collection_relationships.append(
1118-
{
1119-
"relation": None,
1120-
"immutable_id": immutable_id,
1121-
"type": "collections",
1122-
"description": "Is a member of",
1123-
}
1124-
)
1116+
immutable_id = ObjectId(immutable_id)
1117+
elif collection_id:
1118+
collection_doc = flask_mongo.db.collections.find_one(
1119+
{"collection_id": collection_id}
1120+
)
1121+
if collection_doc:
1122+
immutable_id = collection_doc["_id"]
11251123

1126-
updated_data["relationships"] = non_collection_relationships + collection_relationships
1124+
if immutable_id:
1125+
collection_relationships.append(
1126+
{
1127+
"relation": None,
1128+
"immutable_id": immutable_id,
1129+
"type": "collections",
1130+
"description": "Is a member of",
1131+
}
1132+
)
1133+
1134+
updated_data["relationships"] = non_collection_relationships + collection_relationships
11271135

11281136
item_type = item["type"]
11291137
item.update(updated_data)
@@ -1136,7 +1144,6 @@ def save_item():
11361144
by_alias=True,
11371145
exclude={"collections", "creators"},
11381146
)
1139-
11401147
except ValidationError as exc:
11411148
return (
11421149
jsonify(

pydatalab/tests/server/test_samples.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,13 +341,13 @@ def test_saved_sample_has_new_relationships(client, default_sample_dict, complic
341341
"""
342342

343343
default_sample_dict["item_id"] = "debug"
344+
344345
response = client.post("/new-sample/", json=default_sample_dict)
345346

346347
assert response.json
347348

348-
response = client.get(
349-
f"/get-item-data/{default_sample_dict['item_id']}",
350-
)
349+
response = client.get(f"/get-item-data/{default_sample_dict['item_id']}")
350+
351351
new_refcode = response.json["item_data"]["refcode"]
352352
assert new_refcode.startswith("test:")
353353

@@ -356,7 +356,11 @@ def test_saved_sample_has_new_relationships(client, default_sample_dict, complic
356356
sample_dict = response.json["item_data"]
357357
sample_dict["synthesis_constituents"] = [
358358
{
359-
"item": {"item_id": complicated_sample.item_id, "type": "samples"},
359+
"item": {
360+
"item_id": complicated_sample.item_id,
361+
"type": "samples",
362+
"name": complicated_sample.name,
363+
},
360364
"quantity": 25.2,
361365
"unit": "g",
362366
}
@@ -366,15 +370,22 @@ def test_saved_sample_has_new_relationships(client, default_sample_dict, complic
366370
"/save-item/", json={"item_id": sample_dict["item_id"], "data": sample_dict}
367371
)
368372

369-
# Saving this link *should* add a searchable relationship in the database on both the new and old sample
373+
print(response.status_code)
374+
print(response.data)
375+
370376
response = client.get(
371377
f"/get-item-data/{default_sample_dict['item_id']}",
372378
)
379+
380+
print(response.status_code)
381+
print(response.data)
382+
373383
assert complicated_sample.item_id in response.json["parent_items"]
374384

375385
response = client.get(
376386
f"/get-item-data/{complicated_sample.item_id}",
377387
)
388+
378389
assert sample_dict["item_id"] in response.json["child_items"]
379390

380391

@@ -692,6 +703,7 @@ def test_items_added_to_existing_collection(client, default_collection, default_
692703
assert response.status_code == 200, response.json
693704

694705
response = client.get(f"/get-item-data/{new_id2}")
706+
695707
assert response.status_code == 200, response.json
696708
assert "test_collection_2" in [
697709
d["collection_id"] for d in response.json["item_data"]["collections"]

0 commit comments

Comments
 (0)