|
| 1 | +import io |
| 2 | +import re |
| 3 | +from typing import cast |
| 4 | + |
| 5 | +import pytest |
| 6 | +from marshmallow.exceptions import ValidationError |
| 7 | + |
| 8 | +from azure.ai.ml import load_index |
| 9 | +from azure.ai.ml.entities import Index |
| 10 | + |
| 11 | +MOCK_STORAGE_PATH = ( |
| 12 | + "azureml://subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/" |
| 13 | + + "rg/workspaces/ws/datastores/workspaceblobstore/paths/path/to/file" |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.unittest |
| 18 | +class TestIndexSchema: |
| 19 | + def test_deserialize(self) -> None: |
| 20 | + """Validate that load_index can deserialize a YAML file.""" |
| 21 | + yaml_file = io.StringIO( |
| 22 | + f""" |
| 23 | + name: "foo" |
| 24 | + version: "2" |
| 25 | + stage: "Development" |
| 26 | + description: "Hello World" |
| 27 | + tags: {{"tag1": "foo", "tag2": "bar"}} |
| 28 | + properties: {{"prop1": "foo", "prop2": "bar"}} |
| 29 | + path: {MOCK_STORAGE_PATH!r} |
| 30 | + """ |
| 31 | + ) |
| 32 | + |
| 33 | + index = load_index(source=yaml_file) |
| 34 | + |
| 35 | + assert isinstance(index, Index) |
| 36 | + |
| 37 | + assert index.name == "foo" |
| 38 | + assert index.version == "2" |
| 39 | + assert index.stage == "Development" |
| 40 | + assert index.description == "Hello World" |
| 41 | + assert index.tags == {"tag1": "foo", "tag2": "bar"} |
| 42 | + assert index.properties == {"prop1": "foo", "prop2": "bar"} |
| 43 | + assert index.path == MOCK_STORAGE_PATH |
| 44 | + assert index.id == None |
| 45 | + |
| 46 | + def test_missing_name(self) -> None: |
| 47 | + """Validate that load_index fails when no name is provided.""" |
| 48 | + yaml_file = io.StringIO( |
| 49 | + f""" |
| 50 | + path: {MOCK_STORAGE_PATH!r} |
| 51 | + """ |
| 52 | + ) |
| 53 | + |
| 54 | + with pytest.raises( |
| 55 | + ValidationError, match=re.compile(r'"name"[^\n]+\n\s+"Missing data for required field."', re.MULTILINE) |
| 56 | + ) as excinfo: |
| 57 | + index = load_index(source=yaml_file) |
| 58 | + |
| 59 | + def test_missing_path(self) -> None: |
| 60 | + """Validate that load_index fails when no path is provided.""" |
| 61 | + yaml_file = io.StringIO( |
| 62 | + """ |
| 63 | + name: "foo" |
| 64 | + """ |
| 65 | + ) |
| 66 | + |
| 67 | + with pytest.raises( |
| 68 | + ValidationError, match=re.compile(r'"path"[^\n]+\n\s+"Missing data for required field."', re.MULTILINE) |
| 69 | + ) as excinfo: |
| 70 | + index = load_index(source=yaml_file) |
| 71 | + |
| 72 | + def test_minimal(self) -> None: |
| 73 | + """Validate that load_index succeeds when only provided the required fields.""" |
| 74 | + yaml_file = io.StringIO( |
| 75 | + f""" |
| 76 | + name: "foo" |
| 77 | + path: {MOCK_STORAGE_PATH!r} |
| 78 | + """ |
| 79 | + ) |
| 80 | + |
| 81 | + index = load_index(source=yaml_file) |
| 82 | + |
| 83 | + assert index.name == "foo" |
| 84 | + assert index.path == MOCK_STORAGE_PATH |
| 85 | + assert index.stage == "Development" |
| 86 | + assert index.version == None |
| 87 | + assert index.description == None |
| 88 | + assert index.tags == {} |
| 89 | + assert index.properties == {} |
| 90 | + assert index.id == None |
0 commit comments