Skip to content

Commit c4b9819

Browse files
committed
Addition unit test changes, apply ScoreSetUpdate validation on ScoreSetUpadateAllOptional in router context. Add unit test for ScoreSetUpdateAllOptional view model
1 parent a7f778c commit c4b9819

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/mavedb/routers/score_sets.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,14 @@ async def score_set_update(
149149
item_update: score_set.ScoreSetUpdateAllOptional,
150150
exclude_unset: bool = False,
151151
user_data: UserData,
152+
existing_item: Optional[ScoreSet] = None,
152153
) -> ScoreSetUpdateResult:
153154
logger.info(msg="Updating score set.", extra=logging_context())
154155

155156
should_create_variants = False
156157
item_update_dict: dict[str, Any] = item_update.model_dump(exclude_unset=exclude_unset)
157158

158-
item = db.query(ScoreSet).filter(ScoreSet.urn == urn).one_or_none()
159+
item = existing_item or db.query(ScoreSet).filter(ScoreSet.urn == urn).one_or_none()
159160
if not item or item.id is None:
160161
logger.info(msg="Failed to update score set; The requested score set does not exist.", extra=logging_context())
161162
raise HTTPException(status_code=404, detail=f"score set with URN '{urn}' not found")
@@ -1415,11 +1416,23 @@ async def update_score_set_with_variants(
14151416
}
14161417

14171418
# Create the update object using **kwargs in as_form
1418-
item_update = score_set.ScoreSetUpdateAllOptional.as_form(**form_dict)
1419+
item_update_partial = score_set.ScoreSetUpdateAllOptional.as_form(**form_dict)
14191420
except Exception as e:
14201421
raise HTTPException(status_code=422, detail=str(e))
14211422

1422-
itemUpdateResult = await score_set_update(db=db, urn=urn, item_update=item_update, exclude_unset=True, user_data=user_data)
1423+
# get existing item from db
1424+
existing_item = db.query(ScoreSet).filter(ScoreSet.urn == urn).one_or_none()
1425+
1426+
# merge existing item data with item_update data to validate against ScoreSetUpdate
1427+
if existing_item:
1428+
existing_item_data = score_set.ScoreSet.model_validate(existing_item).model_dump()
1429+
updated_data = {**existing_item_data, **item_update_partial.model_dump(exclude_unset=True)}
1430+
score_set.ScoreSetUpdate.model_validate(updated_data)
1431+
else:
1432+
logger.info(msg="Failed to update score set; The requested score set does not exist.", extra=logging_context())
1433+
raise HTTPException(status_code=404, detail=f"score set with URN '{urn}' not found")
1434+
1435+
itemUpdateResult = await score_set_update(db=db, urn=urn, item_update=item_update_partial, exclude_unset=True, user_data=user_data, existing_item=existing_item)
14231436
updatedItem = itemUpdateResult["item"]
14241437
should_create_variants = itemUpdateResult.get("should_create_variants", False)
14251438

@@ -1471,6 +1484,8 @@ async def update_score_set(
14711484
save_to_logging_context({"requested_resource": urn})
14721485
logger.debug(msg="Began score set update.", extra=logging_context())
14731486

1487+
# this object will contain all required fields because item_update type is ScoreSetUpdate, but
1488+
# is converted to instance of ScoreSetUpdateAllOptional to match expected input of score_set_update function
14741489
score_set_update_item = score_set.ScoreSetUpdateAllOptional.model_validate(item_update.model_dump())
14751490
itemUpdateResult = await score_set_update(db=db, urn=urn, item_update=score_set_update_item, exclude_unset=False, user_data=user_data)
14761491
updatedItem = itemUpdateResult["item"]

tests/routers/test_score_set.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ def test_can_patch_score_set_data_before_publication(
449449
form_value = str(updated_data)
450450
data[attribute] = form_value
451451

452+
# The score ranges attribute requires a publication identifier source
453+
if attribute == "score_ranges":
454+
data["secondary_publication_identifiers"] = json.dumps([{"identifier": TEST_PUBMED_IDENTIFIER, "dbName": "PubMed"}])
455+
452456
response = client.patch(f"/api/v1/score-sets-with-variants/{score_set['urn']}", data=data)
453457
assert response.status_code == 200
454458

tests/view_models/test_score_set.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
from copy import deepcopy
33

44
from mavedb.view_models.publication_identifier import PublicationIdentifier, PublicationIdentifierCreate
5-
from mavedb.view_models.score_set import SavedScoreSet, ScoreSetCreate, ScoreSetModify
5+
from mavedb.view_models.score_set import SavedScoreSet, ScoreSetCreate, ScoreSetModify, ScoreSetUpdateAllOptional
66
from mavedb.view_models.target_gene import SavedTargetGene, TargetGeneCreate
77

88
from tests.helpers.constants import (
9+
EXTRA_LICENSE,
10+
EXTRA_USER,
11+
TEST_CROSSREF_IDENTIFIER,
912
TEST_PUBMED_IDENTIFIER,
1013
TEST_MINIMAL_ACC_SCORESET,
1114
TEST_MINIMAL_SEQ_SCORESET,
@@ -380,6 +383,28 @@ def test_saved_score_set_synthetic_properties():
380383
]
381384
)
382385

386+
@pytest.mark.parametrize(
387+
"attribute,updated_data",
388+
[
389+
("title", "Updated Title"),
390+
("method_text", "Updated Method Text"),
391+
("abstract_text", "Updated Abstract Text"),
392+
("short_description", "Updated Abstract Text"),
393+
("title", "Updated Title"),
394+
("extra_metadata", {"updated": "metadata"}),
395+
("data_usage_policy", "data_usage_policy"),
396+
("contributors", [{"orcid_id": EXTRA_USER["username"]}]),
397+
("primary_publication_identifiers", [{"identifier": TEST_PUBMED_IDENTIFIER}]),
398+
("secondary_publication_identifiers", [{"identifier": TEST_PUBMED_IDENTIFIER}]),
399+
("doi_identifiers", [{"identifier": TEST_CROSSREF_IDENTIFIER}]),
400+
("license_id", EXTRA_LICENSE["id"]),
401+
("target_genes", TEST_MINIMAL_SEQ_SCORESET["targetGenes"]),
402+
("score_ranges", TEST_SCORE_SET_RANGES_ALL_SCHEMAS_PRESENT),
403+
],
404+
)
405+
def test_score_set_update_all_optional(attribute, updated_data):
406+
ScoreSetUpdateAllOptional(**{attribute: updated_data})
407+
383408

384409
# def test_saved_score_set_data_set_columns_are_camelized():
385410
# score_set = TEST_MINIMAL_SEQ_SCORESET_RESPONSE.copy()

0 commit comments

Comments
 (0)