|
4 | 4 |
|
5 | 5 | from pydantic import ValidationError |
6 | 6 |
|
7 | | -from aind_data_schema.components.identifiers import Code, Person |
| 7 | +from aind_data_schema.components.identifiers import Code, DataAsset, Person |
8 | 8 |
|
9 | 9 |
|
10 | 10 | class Testexperimenter(unittest.TestCase): |
@@ -54,5 +54,39 @@ def test_git_hash_invalid(self): |
54 | 54 | Code(url="https://github.com/org/repo", commit_hash=git_hash) |
55 | 55 |
|
56 | 56 |
|
| 57 | +class TestDataAsset(unittest.TestCase): |
| 58 | + """Test DataAsset validator""" |
| 59 | + |
| 60 | + def test_name_provided_directly(self): |
| 61 | + """Name is kept as-is when explicitly provided""" |
| 62 | + asset = DataAsset(name="my-dataset") |
| 63 | + self.assertEqual(asset.name, "my-dataset") |
| 64 | + |
| 65 | + def test_name_parsed_from_url_no_subpath(self): |
| 66 | + """Name is inferred from top-level prefix with no nested path""" |
| 67 | + asset = DataAsset(url="s3://aind-open-data/my-dataset") |
| 68 | + self.assertEqual(asset.name, "my-dataset") |
| 69 | + |
| 70 | + def test_name_parsed_from_url_with_subpath(self): |
| 71 | + """Name is inferred from top-level prefix, ignoring nested path""" |
| 72 | + asset = DataAsset(url="s3://aind-open-data/my-dataset/sub/path/file.txt") |
| 73 | + self.assertEqual(asset.name, "my-dataset") |
| 74 | + |
| 75 | + def test_name_not_overridden_when_provided_with_url(self): |
| 76 | + """Explicit name takes precedence over URL-inferred name""" |
| 77 | + asset = DataAsset(name="explicit-name", url="s3://aind-open-data/other-dataset/sub") |
| 78 | + self.assertEqual(asset.name, "explicit-name") |
| 79 | + |
| 80 | + def test_neither_name_nor_url_raises(self): |
| 81 | + """Raises ValidationError when neither name nor url is provided""" |
| 82 | + with self.assertRaises(ValidationError): |
| 83 | + DataAsset() |
| 84 | + |
| 85 | + def test_url_wrong_bucket_leaves_name_none(self): |
| 86 | + """URL from a different bucket does not set name (remains None)""" |
| 87 | + asset = DataAsset(url="s3://other-bucket/my-dataset") |
| 88 | + self.assertIsNone(asset.name) |
| 89 | + |
| 90 | + |
57 | 91 | if __name__ == "__main__": |
58 | 92 | unittest.main() |
0 commit comments