|
1 | 1 | import pytest
|
| 2 | +from marshmallow import ValidationError |
2 | 3 |
|
3 | 4 | from azure.ai.ml import Input, load_component
|
4 | 5 | from azure.ai.ml.constants._component import ComponentSource
|
5 | 6 | from azure.ai.ml.dsl import pipeline
|
6 | 7 | from azure.ai.ml.dsl._condition import condition
|
7 | 8 | from azure.ai.ml.dsl._parallel_for import parallel_for
|
| 9 | +from azure.ai.ml.entities._job.pipeline._io import InputOutputBase |
8 | 10 | from azure.ai.ml.exceptions import ValidationException
|
| 11 | +from test_utilities.utils import omit_with_wildcard |
9 | 12 |
|
10 | 13 | from .._util import _DSL_TIMEOUT_SECOND
|
11 | 14 |
|
@@ -67,10 +70,139 @@ def condition_pipeline():
|
67 | 70 | # true block and false block has intersection
|
68 | 71 | condition(condition=result.outputs.output, false_block=[node1, node2], true_block=[node1])
|
69 | 72 |
|
70 |
| - with pytest.raises(ValidationException) as e: |
| 73 | + with pytest.raises(ValidationError) as e: |
71 | 74 | pipeline_job = condition_pipeline()
|
72 | 75 | pipeline_job._validate(raise_error=True)
|
73 |
| - assert "'true_block' and 'false_block' of dsl.condition has intersection" in str(e.value) |
| 76 | + assert "True block and false block cannot contain same nodes: {'${{parent.jobs.node1}}'" in str(e.value) |
| 77 | + |
| 78 | + @pipeline(compute="cpu-cluster") |
| 79 | + def condition_pipeline(): |
| 80 | + result = basic_component() |
| 81 | + node1 = hello_world_component_no_paths() |
| 82 | + node2 = hello_world_component_no_paths() |
| 83 | + # true block and false block has intersection |
| 84 | + condition(condition=result.outputs.output, false_block=[node1], true_block=[node2]) |
| 85 | + |
| 86 | + # no error raise |
| 87 | + pipeline_job = condition_pipeline() |
| 88 | + pipeline_job._validate(raise_error=True) |
| 89 | + |
| 90 | + def test_create_dsl_condition_illegal_cases(self): |
| 91 | + basic_component = load_component( |
| 92 | + source="./tests/test_configs/components/component_with_conditional_output/spec.yaml" |
| 93 | + ) |
| 94 | + basic_node = basic_component() |
| 95 | + |
| 96 | + with pytest.raises(ValidationException) as e: |
| 97 | + node = condition(condition=1, true_block=basic_node) |
| 98 | + node._validate(raise_error=True) |
| 99 | + |
| 100 | + assert f"must be an instance of {str}, {bool} or {InputOutputBase}" in str(e.value) |
| 101 | + |
| 102 | + with pytest.raises(ValidationException) as e: |
| 103 | + node = condition(condition=basic_node.outputs.output3, true_block=basic_node) |
| 104 | + node._validate(raise_error=True) |
| 105 | + |
| 106 | + assert "must have 'is_control' field with value 'True'" in str(e.value) |
| 107 | + |
| 108 | + with pytest.raises(ValidationError) as e: |
| 109 | + node = condition(condition="${{parent.jobs.xxx.outputs.output}}") |
| 110 | + node._validate(raise_error=True) |
| 111 | + |
| 112 | + assert "True block and false block cannot be empty at the same time." in str(e.value) |
| 113 | + |
| 114 | + with pytest.raises(ValidationError) as e: |
| 115 | + node = condition( |
| 116 | + condition="${{parent.jobs.xxx.outputs.output}}", true_block=basic_node, false_block=basic_node |
| 117 | + ) |
| 118 | + node._validate(raise_error=True) |
| 119 | + |
| 120 | + assert "True block and false block cannot contain same nodes" in str(e.value) |
| 121 | + |
| 122 | + with pytest.raises(ValidationException) as e: |
| 123 | + node = condition(condition="xxx", true_block=basic_node) |
| 124 | + node._validate(raise_error=True) |
| 125 | + |
| 126 | + assert "condition has invalid binding expression: xxx" in str(e.value) |
| 127 | + |
| 128 | + def test_condition_node(self): |
| 129 | + hello_world_component_no_paths = load_component( |
| 130 | + source="./tests/test_configs/components/helloworld_component_no_paths.yml" |
| 131 | + ) |
| 132 | + node1 = hello_world_component_no_paths() |
| 133 | + node1.name = "node1" |
| 134 | + node2 = hello_world_component_no_paths() |
| 135 | + node2.name = "node2" |
| 136 | + control_node = condition( |
| 137 | + condition="${{parent.jobs.condition_predicate.outputs.output}}", false_block=node1, true_block=node2 |
| 138 | + ) |
| 139 | + assert control_node._to_rest_object() == { |
| 140 | + "_source": "DSL", |
| 141 | + "condition": "${{parent.jobs.condition_predicate.outputs.output}}", |
| 142 | + "false_block": "${{parent.jobs.node1}}", |
| 143 | + "true_block": "${{parent.jobs.node2}}", |
| 144 | + "type": "if_else", |
| 145 | + } |
| 146 | + |
| 147 | + # test boolean type condition |
| 148 | + control_node = condition(condition=True, false_block=node1, true_block=node2) |
| 149 | + assert control_node._to_rest_object() == { |
| 150 | + "_source": "DSL", |
| 151 | + "condition": True, |
| 152 | + "false_block": "${{parent.jobs.node1}}", |
| 153 | + "true_block": "${{parent.jobs.node2}}", |
| 154 | + "type": "if_else", |
| 155 | + } |
| 156 | + |
| 157 | + def test_condition_pipeline(self): |
| 158 | + basic_component = load_component( |
| 159 | + source="./tests/test_configs/components/component_with_conditional_output/spec.yaml" |
| 160 | + ) |
| 161 | + |
| 162 | + hello_world_component_no_paths = load_component( |
| 163 | + source="./tests/test_configs/components/helloworld_component_no_paths.yml" |
| 164 | + ) |
| 165 | + |
| 166 | + @pipeline( |
| 167 | + name="test_mldesigner_component_with_conditional_output", |
| 168 | + compute="cpu-cluster", |
| 169 | + ) |
| 170 | + def condition_pipeline(): |
| 171 | + result = basic_component() |
| 172 | + node1 = hello_world_component_no_paths(component_in_number=1) |
| 173 | + node2 = hello_world_component_no_paths(component_in_number=2) |
| 174 | + condition(condition=result.outputs.output, false_block=node1, true_block=node2) |
| 175 | + |
| 176 | + pipeline_job = condition_pipeline() |
| 177 | + omit_fields = [ |
| 178 | + "name", |
| 179 | + "properties.display_name", |
| 180 | + "properties.jobs.*.componentId", |
| 181 | + "properties.settings", |
| 182 | + ] |
| 183 | + dsl_pipeline_job_dict = omit_with_wildcard(pipeline_job._to_rest_object().as_dict(), *omit_fields) |
| 184 | + assert dsl_pipeline_job_dict["properties"]["jobs"] == { |
| 185 | + "conditionnode": { |
| 186 | + "_source": "DSL", |
| 187 | + "condition": "${{parent.jobs.result.outputs.output}}", |
| 188 | + "false_block": "${{parent.jobs.node1}}", |
| 189 | + "true_block": "${{parent.jobs.node2}}", |
| 190 | + "type": "if_else", |
| 191 | + }, |
| 192 | + "node1": { |
| 193 | + "_source": "YAML.COMPONENT", |
| 194 | + "inputs": {"component_in_number": {"job_input_type": "literal", "value": "1"}}, |
| 195 | + "name": "node1", |
| 196 | + "type": "command", |
| 197 | + }, |
| 198 | + "node2": { |
| 199 | + "_source": "YAML.COMPONENT", |
| 200 | + "inputs": {"component_in_number": {"job_input_type": "literal", "value": "2"}}, |
| 201 | + "name": "node2", |
| 202 | + "type": "command", |
| 203 | + }, |
| 204 | + "result": {"_source": "YAML.COMPONENT", "name": "result", "type": "command"}, |
| 205 | + } |
74 | 206 |
|
75 | 207 |
|
76 | 208 | class TestDoWhilePipelineUT(TestControlFlowPipelineUT):
|
|
0 commit comments