Skip to content

Commit 5783d63

Browse files
committed
Update experiment_set_urn validation to handle None values and add corresponding tests
1 parent ef21155 commit 5783d63

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/mavedb/view_models/experiment.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,11 @@ class ExperimentCreate(ExperimentModify):
8484
experiment_set_urn: Optional[str] = None
8585

8686
@field_validator("experiment_set_urn")
87-
def validate_experiment_urn(cls, v: str) -> str:
88-
if (urn_re.MAVEDB_EXPERIMENT_SET_URN_RE.fullmatch(v) is None) and (
89-
urn_re.MAVEDB_TMP_URN_RE.fullmatch(v) is None
87+
def validate_experiment_urn(cls, v: Optional[str]) -> Optional[str]:
88+
if (
89+
v is not None
90+
and (urn_re.MAVEDB_EXPERIMENT_SET_URN_RE.fullmatch(v) is None)
91+
and (urn_re.MAVEDB_TMP_URN_RE.fullmatch(v) is None)
9092
):
9193
raise ValueError(f"'{v}' is not a valid experiment set URN")
9294
return v
@@ -130,7 +132,9 @@ def publication_identifiers_validator(cls, v: Any, info: ValidationInfo) -> list
130132
# the appropriate field on the model itself. Then, proceed with Pydantic ingestion once fields are created.
131133
@model_validator(mode="before")
132134
def generate_primary_and_secondary_publications(cls, data: Any):
133-
if not hasattr(data, "primary_publication_identifiers") or not hasattr(data, "secondary_publication_identifiers"):
135+
if not hasattr(data, "primary_publication_identifiers") or not hasattr(
136+
data, "secondary_publication_identifiers"
137+
):
134138
try:
135139
publication_identifiers = transform_publication_identifiers_to_primary_and_secondary(
136140
data.publication_identifier_associations

tests/view_models/test_experiment.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,21 @@ def test_cannot_create_saved_experiment_without_all_attributed_properties(exclud
285285

286286
assert "Unable to create SavedExperiment without attribute" in str(exc_info.value)
287287
assert exclude in str(exc_info.value)
288+
289+
290+
def test_can_create_experiment_with_nonetype_experiment_set_urn():
291+
experiment_test = TEST_MINIMAL_EXPERIMENT.copy()
292+
experiment_test["experiment_set_urn"] = None
293+
experiment = ExperimentCreate(**experiment_test)
294+
295+
assert experiment.experiment_set_urn is None
296+
297+
298+
def test_cant_create_experiment_with_invalid_experiment_set_urn():
299+
experiment_test = TEST_MINIMAL_EXPERIMENT.copy()
300+
experiment_test["experiment_set_urn"] = "invalid_urn"
301+
302+
with pytest.raises(ValueError) as exc_info:
303+
ExperimentCreate(**experiment_test)
304+
305+
assert f"'{experiment_test['experiment_set_urn']}' is not a valid experiment set URN" in str(exc_info.value)

0 commit comments

Comments
 (0)