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