|
1 | 1 | from typing import Callable
|
2 | 2 |
|
3 | 3 | import pytest
|
| 4 | + |
| 5 | +from azure.ai.ml.exceptions import ValidationException |
4 | 6 | from devtools_testutils import AzureRecordedTestCase, is_live
|
5 | 7 | from test_utilities.utils import _PYTEST_TIMEOUT_METHOD
|
6 | 8 |
|
7 | 9 | from azure.ai.ml import MLClient, load_job
|
8 | 10 | from azure.ai.ml._schema.pipeline import pipeline_job
|
9 |
| -from azure.ai.ml._utils.utils import load_yaml |
10 | 11 | from azure.ai.ml.entities._builders import Command, Pipeline
|
11 | 12 | from azure.ai.ml.entities._builders.do_while import DoWhile
|
12 | 13 | from azure.ai.ml.entities._builders.parallel_for import ParallelFor
|
13 | 14 |
|
14 | 15 | from .._util import _PIPELINE_JOB_TIMEOUT_SECOND
|
15 | 16 | from .test_pipeline_job import assert_job_cancel
|
| 17 | +from test_utilities.utils import omit_with_wildcard |
| 18 | + |
| 19 | +omit_fields = [ |
| 20 | + "name", |
| 21 | + "properties.display_name", |
| 22 | + "properties.settings", |
| 23 | + "properties.jobs.*._source", |
| 24 | + "properties.jobs.*.componentId", |
| 25 | +] |
16 | 26 |
|
17 | 27 |
|
18 | 28 | @pytest.fixture()
|
@@ -44,6 +54,86 @@ class TestConditionalNodeInPipeline(AzureRecordedTestCase):
|
44 | 54 | pass
|
45 | 55 |
|
46 | 56 |
|
| 57 | +class TestIfElse(TestConditionalNodeInPipeline): |
| 58 | + def test_happy_path_if_else(self, client: MLClient, randstr: Callable[[], str]) -> None: |
| 59 | + params_override = [{"name": randstr('name')}] |
| 60 | + my_job = load_job( |
| 61 | + "./tests/test_configs/pipeline_jobs/control_flow/if_else/simple_pipeline.yml", |
| 62 | + params_override=params_override, |
| 63 | + ) |
| 64 | + created_pipeline = assert_job_cancel(my_job, client) |
| 65 | + |
| 66 | + pipeline_job_dict = created_pipeline._to_rest_object().as_dict() |
| 67 | + |
| 68 | + pipeline_job_dict = omit_with_wildcard(pipeline_job_dict, *omit_fields) |
| 69 | + assert pipeline_job_dict["properties"]["jobs"] == { |
| 70 | + 'conditionnode': {'condition': '${{parent.jobs.result.outputs.output}}', |
| 71 | + 'false_block': '${{parent.jobs.node1}}', |
| 72 | + 'true_block': '${{parent.jobs.node2}}', |
| 73 | + 'type': 'if_else'}, |
| 74 | + 'node1': {'inputs': {'component_in_number': {'job_input_type': 'literal', |
| 75 | + 'value': '1'}}, |
| 76 | + 'name': 'node1', |
| 77 | + 'type': 'command'}, |
| 78 | + 'node2': {'inputs': {'component_in_number': {'job_input_type': 'literal', |
| 79 | + 'value': '2'}}, |
| 80 | + 'name': 'node2', |
| 81 | + 'type': 'command'}, |
| 82 | + 'result': {'name': 'result', 'type': 'command'} |
| 83 | + } |
| 84 | + |
| 85 | + def test_if_else_one_branch(self, client: MLClient, randstr: Callable[[], str]) -> None: |
| 86 | + params_override = [{"name": randstr('name')}] |
| 87 | + my_job = load_job( |
| 88 | + "./tests/test_configs/pipeline_jobs/control_flow/if_else/one_branch.yml", |
| 89 | + params_override=params_override, |
| 90 | + ) |
| 91 | + created_pipeline = assert_job_cancel(my_job, client) |
| 92 | + |
| 93 | + pipeline_job_dict = created_pipeline._to_rest_object().as_dict() |
| 94 | + |
| 95 | + pipeline_job_dict = omit_with_wildcard(pipeline_job_dict, *omit_fields) |
| 96 | + assert pipeline_job_dict["properties"]["jobs"] == { |
| 97 | + 'conditionnode': {'condition': '${{parent.jobs.result.outputs.output}}', |
| 98 | + 'true_block': '${{parent.jobs.node1}}', |
| 99 | + 'type': 'if_else'}, |
| 100 | + 'node1': {'inputs': {'component_in_number': {'job_input_type': 'literal', |
| 101 | + 'value': '1'}}, |
| 102 | + 'name': 'node1', |
| 103 | + 'type': 'command'}, |
| 104 | + 'result': {'name': 'result', 'type': 'command'} |
| 105 | + } |
| 106 | + |
| 107 | + def test_if_else_literal_condition(self, client: MLClient, randstr: Callable[[], str]) -> None: |
| 108 | + params_override = [{"name": randstr('name')}] |
| 109 | + my_job = load_job( |
| 110 | + "./tests/test_configs/pipeline_jobs/control_flow/if_else/literal_condition.yml", |
| 111 | + params_override=params_override, |
| 112 | + ) |
| 113 | + created_pipeline = assert_job_cancel(my_job, client) |
| 114 | + |
| 115 | + pipeline_job_dict = created_pipeline._to_rest_object().as_dict() |
| 116 | + |
| 117 | + pipeline_job_dict = omit_with_wildcard(pipeline_job_dict, *omit_fields) |
| 118 | + assert pipeline_job_dict["properties"]["jobs"] == { |
| 119 | + 'conditionnode': {'condition': True, |
| 120 | + 'true_block': '${{parent.jobs.node1}}', |
| 121 | + 'type': 'if_else'}, |
| 122 | + 'node1': {'inputs': {'component_in_number': {'job_input_type': 'literal', |
| 123 | + 'value': '1'}}, |
| 124 | + 'name': 'node1', |
| 125 | + 'type': 'command'} |
| 126 | + } |
| 127 | + |
| 128 | + def test_if_else_invalid_case(self, client: MLClient, randstr: Callable[[], str]) -> None: |
| 129 | + my_job = load_job( |
| 130 | + "./tests/test_configs/pipeline_jobs/control_flow/if_else/invalid_binding.yml", |
| 131 | + ) |
| 132 | + with pytest.raises(ValidationException) as e: |
| 133 | + my_job._validate(raise_error=True) |
| 134 | + assert '"path": "jobs.conditionnode.true_block",' in str(e.value) |
| 135 | + assert "'true_block' of dsl.condition has invalid binding expression:" in str(e.value) |
| 136 | + |
47 | 137 | class TestDoWhile(TestConditionalNodeInPipeline):
|
48 | 138 | def test_pipeline_with_do_while_node(self, client: MLClient, randstr: Callable[[], str]) -> None:
|
49 | 139 | params_override = [{"name": randstr('name')}]
|
|
0 commit comments