|
| 1 | +import pytest |
| 2 | +from pydantic import ValidationError |
| 3 | + |
| 4 | +from mavedb.models.enums.contribution_role import ContributionRole |
| 5 | +from mavedb.view_models.collection import Collection, SavedCollection |
| 6 | +from tests.helpers.constants import TEST_COLLECTION_RESPONSE |
| 7 | +from tests.helpers.util.common import dummy_attributed_object_from_dict |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + "exclude,expected_missing_fields", |
| 12 | + [ |
| 13 | + ("user_associations", ["admins", "editors", "viewers"]), |
| 14 | + ("score_sets", ["scoreSetUrns"]), |
| 15 | + ("experiments", ["experimentUrns"]), |
| 16 | + ], |
| 17 | +) |
| 18 | +def test_cannot_create_saved_experiment_without_all_attributed_properties(exclude, expected_missing_fields): |
| 19 | + collection = TEST_COLLECTION_RESPONSE.copy() |
| 20 | + collection["urn"] = "urn:mavedb:collection-xxx" |
| 21 | + |
| 22 | + # Remove pre-existing synthetic properties |
| 23 | + collection.pop("experimentUrns", None) |
| 24 | + collection.pop("scoreSetUrns", None) |
| 25 | + collection.pop("admins", None) |
| 26 | + collection.pop("editors", None) |
| 27 | + collection.pop("viewers", None) |
| 28 | + |
| 29 | + # Set synthetic properties with dummy attributed objects to mock SQLAlchemy model objects. |
| 30 | + collection["experiments"] = [dummy_attributed_object_from_dict({"urn": "urn:mavedb:experiment-xxx"})] |
| 31 | + collection["score_sets"] = [ |
| 32 | + dummy_attributed_object_from_dict({"urn": "urn:mavedb:score_set-xxx", "superseding_score_set": None}) |
| 33 | + ] |
| 34 | + collection["user_associations"] = [ |
| 35 | + dummy_attributed_object_from_dict( |
| 36 | + { |
| 37 | + "contribution_role": ContributionRole.admin, |
| 38 | + "user": { "id": 1, "username": "test_user", "email": "[email protected]"}, |
| 39 | + } |
| 40 | + ), |
| 41 | + dummy_attributed_object_from_dict( |
| 42 | + { |
| 43 | + "contribution_role": ContributionRole.editor, |
| 44 | + "user": { "id": 1, "username": "test_user", "email": "[email protected]"}, |
| 45 | + } |
| 46 | + ), |
| 47 | + dummy_attributed_object_from_dict( |
| 48 | + { |
| 49 | + "contribution_role": ContributionRole.viewer, |
| 50 | + "user": { "id": 1, "username": "test_user", "email": "[email protected]"}, |
| 51 | + } |
| 52 | + ), |
| 53 | + ] |
| 54 | + |
| 55 | + collection.pop(exclude) |
| 56 | + collection_attributed_object = dummy_attributed_object_from_dict(collection) |
| 57 | + with pytest.raises(ValidationError) as exc_info: |
| 58 | + SavedCollection.model_validate(collection_attributed_object) |
| 59 | + |
| 60 | + # Should fail with missing fields coerced from missing attributed properties |
| 61 | + msg = str(exc_info.value) |
| 62 | + assert "Field required" in msg |
| 63 | + for field in expected_missing_fields: |
| 64 | + assert field in msg |
| 65 | + |
| 66 | + |
| 67 | +def test_saved_collection_can_be_created_with_all_attributed_properties(): |
| 68 | + collection = TEST_COLLECTION_RESPONSE.copy() |
| 69 | + urn = "urn:mavedb:collection-xxx" |
| 70 | + collection["urn"] = urn |
| 71 | + |
| 72 | + # Remove pre-existing synthetic properties |
| 73 | + collection.pop("experimentUrns", None) |
| 74 | + collection.pop("scoreSetUrns", None) |
| 75 | + collection.pop("admins", None) |
| 76 | + collection.pop("editors", None) |
| 77 | + collection.pop("viewers", None) |
| 78 | + |
| 79 | + # Set synthetic properties with dummy attributed objects to mock SQLAlchemy model objects. |
| 80 | + collection["experiments"] = [dummy_attributed_object_from_dict({"urn": "urn:mavedb:experiment-xxx"})] |
| 81 | + collection["score_sets"] = [ |
| 82 | + dummy_attributed_object_from_dict({"urn": "urn:mavedb:score_set-xxx", "superseding_score_set": None}) |
| 83 | + ] |
| 84 | + collection["user_associations"] = [ |
| 85 | + dummy_attributed_object_from_dict( |
| 86 | + { |
| 87 | + "contribution_role": ContributionRole.admin, |
| 88 | + "user": { "id": 1, "username": "test_user", "email": "[email protected]"}, |
| 89 | + } |
| 90 | + ), |
| 91 | + dummy_attributed_object_from_dict( |
| 92 | + { |
| 93 | + "contribution_role": ContributionRole.editor, |
| 94 | + "user": { "id": 1, "username": "test_user", "email": "[email protected]"}, |
| 95 | + } |
| 96 | + ), |
| 97 | + dummy_attributed_object_from_dict( |
| 98 | + { |
| 99 | + "contribution_role": ContributionRole.viewer, |
| 100 | + "user": { "id": 1, "username": "test_user", "email": "[email protected]"}, |
| 101 | + } |
| 102 | + ), |
| 103 | + ] |
| 104 | + |
| 105 | + collection_attributed_object = dummy_attributed_object_from_dict(collection) |
| 106 | + model = SavedCollection.model_validate(collection_attributed_object) |
| 107 | + assert model.name == TEST_COLLECTION_RESPONSE["name"] |
| 108 | + assert model.urn == urn |
| 109 | + assert len(model.admins) == 1 |
| 110 | + assert len(model.editors) == 1 |
| 111 | + assert len(model.viewers) == 1 |
| 112 | + assert len(model.experiment_urns) == 1 |
| 113 | + assert len(model.score_set_urns) == 1 |
| 114 | + |
| 115 | + |
| 116 | +def test_collection_can_be_created_from_non_orm_context(): |
| 117 | + data = dict(TEST_COLLECTION_RESPONSE) |
| 118 | + data["urn"] = "urn:mavedb:collection-xxx" |
| 119 | + model = Collection.model_validate(data) |
| 120 | + assert model.urn == data["urn"] |
0 commit comments