Skip to content

Commit 419313b

Browse files
committed
test: provide tests for Dandiset.sameAs
1 parent 0aa1ac5 commit 419313b

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

dandischema/tests/test_models.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,3 +1001,78 @@ class VendoredFieldModel(BaseModel):
10011001
# Validate the invalid vendored fields against the vendored patterns
10021002
with pytest.raises(ValidationError):
10031003
VendoredFieldModel.model_validate(invalid_vendored_fields)
1004+
1005+
1006+
class TestDandisetSameAs:
1007+
def test_not_specified(self, base_dandiset_metadata: dict[str, Any]) -> None:
1008+
"""
1009+
Test the case that `sameAs` is not specified in instantiating a `Dandiset`
1010+
"""
1011+
dandiset = Dandiset.model_validate(base_dandiset_metadata)
1012+
assert dandiset.sameAs is None
1013+
1014+
def test_empty_list(self, base_dandiset_metadata: dict[str, Any]) -> None:
1015+
"""
1016+
Test the case that `sameAs` in a `Dandiset` is initialized to an empty list
1017+
"""
1018+
base_dandiset_metadata["sameAs"] = []
1019+
dandiset = Dandiset.model_validate(base_dandiset_metadata)
1020+
assert dandiset.sameAs == []
1021+
1022+
@pytest.mark.parametrize(
1023+
"dandi_urls",
1024+
[
1025+
["dandi://DANDI-SANDBOX/123456"],
1026+
["dandi://DANDI-SANDBOX/123456@draft"],
1027+
["dandi://DANDI-SANDBOX/123456@1.22.33"],
1028+
["dandi://DANDI-SANDBOX/123456/path"],
1029+
["dandi://DANDI-SANDBOX/123456@draft/path"],
1030+
["dandi://DANDI-SANDBOX/123456@1.22.33/path"],
1031+
["dandi://EMBER-DANDI/123456"],
1032+
["dandi://DANDI-SANDBOX/123456", "dandi://EMBER-DANDI/123456"],
1033+
["dandi://A/123456", "dandi://B/654321"],
1034+
],
1035+
)
1036+
def test_with_valid_dandi_urls(
1037+
self, dandi_urls: list[str], base_dandiset_metadata: dict[str, Any]
1038+
) -> None:
1039+
"""
1040+
Test the case that `sameAs` is initialized to a list of valid DANDI URLs
1041+
"""
1042+
base_dandiset_metadata["sameAs"] = dandi_urls
1043+
dandiset = Dandiset.model_validate(base_dandiset_metadata)
1044+
assert dandiset.sameAs == dandi_urls
1045+
1046+
@pytest.mark.parametrize(
1047+
"dandi_urls",
1048+
[
1049+
# List of invalid DANDI URLs
1050+
["dandi://DANDI-SANDBOX/123456@abc"],
1051+
["dandi://DANDI-SANDBOX/123456@1.22.33.44"],
1052+
["dandi://DANDI-SANDBOX/123456/"],
1053+
["dandi://DANDI-SANDBOX/123456@draft/"],
1054+
["dandi://DANDI-SANDBOX/123456@1.22.33/"],
1055+
["http://DANDI-SANDBOX/123456"], # Not dandi:// scheme
1056+
["dandi://DANDI- SANDBOX/123456"], # Containing a space
1057+
["dandi://"], # Missing instance name and dandiset id
1058+
["dandi://DANDI-SANDBOX"], # Missing dandiset id
1059+
["dandi://DANDI-SANDBOX/12345"], # Dandiset id too short
1060+
["dandi://-DANDI/123456"], # Invalid instance name
1061+
["dandi://EMBER3DANDI/123456"], # Invalid instance name
1062+
["dandi://DANDI-SANDBOX/123456", "dandi://DANDI- SANDBOX/123456"],
1063+
[42],
1064+
# Value that is not a list
1065+
"DANDI-SANDBOX:123456",
1066+
42,
1067+
],
1068+
)
1069+
def test_with_invalid_dandi_urls(
1070+
self, dandi_urls: Any, base_dandiset_metadata: dict[str, Any]
1071+
) -> None:
1072+
"""
1073+
Test the case that `sameAs` is initialized to an invalid list of DANDI URLs
1074+
or a value that is not a list
1075+
"""
1076+
base_dandiset_metadata["sameAs"] = dandi_urls
1077+
with pytest.raises(ValidationError):
1078+
Dandiset.model_validate(base_dandiset_metadata)

0 commit comments

Comments
 (0)