-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/azure_data_factory_testing_framework/models/activities/append_variable_activity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Any | ||
|
||
from azure_data_factory_testing_framework.models.activities.control_activity import ControlActivity | ||
from azure_data_factory_testing_framework.models.data_factory_element import DataFactoryElement | ||
from azure_data_factory_testing_framework.state import PipelineRunState | ||
|
||
|
||
class AppendVariableActivity(ControlActivity): | ||
def __init__(self, **kwargs: Any) -> None: # noqa: ANN401 | ||
"""This is the class that represents the Append Variable activity in the pipeline. | ||
Args: | ||
**kwargs: AppendVariableActivity properties coming directly from the json representation of the activity. | ||
""" | ||
kwargs["type"] = "AppendVariable" | ||
|
||
super(ControlActivity, self).__init__(**kwargs) | ||
|
||
self.variable_name: str = self.type_properties["variableName"] | ||
self.value: DataFactoryElement = self.type_properties["value"] | ||
|
||
def evaluate(self, state: PipelineRunState) -> "AppendVariableActivity": | ||
super(ControlActivity, self).evaluate(state) | ||
|
||
if isinstance(self.value, DataFactoryElement): | ||
evaluated_value = self.value.evaluate(state) | ||
else: | ||
evaluated_value = self.value | ||
|
||
state.append_variable(self.type_properties["variableName"], evaluated_value) | ||
|
||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"type": "Pipeline", | ||
"displayName": "append-variable-test" | ||
} |
58 changes: 58 additions & 0 deletions
58
tests/functional/append_variable_pipeline/pipeline-content.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"name": "append-variable-test", | ||
"properties": { | ||
"activities": [ | ||
{ | ||
"name": "Append variable1", | ||
"type": "AppendVariable", | ||
"dependsOn": [ | ||
{ | ||
"activity": "Set variable1", | ||
"dependencyConditions": [ | ||
"Succeeded" | ||
] | ||
} | ||
], | ||
"typeProperties": { | ||
"variableName": "values", | ||
"value": { | ||
"value": "@pipeline().parameters.appended_value", | ||
"type": "Expression" | ||
} | ||
} | ||
}, | ||
{ | ||
"name": "Set variable1", | ||
"type": "SetVariable", | ||
"dependsOn": [], | ||
"policy": { | ||
"secureOutput": false, | ||
"secureInput": false | ||
}, | ||
"typeProperties": { | ||
"variableName": "values", | ||
"value": { | ||
"value": "@pipeline().parameters.initial_value", | ||
"type": "Expression" | ||
} | ||
} | ||
} | ||
], | ||
"parameters": { | ||
"initial_value": { | ||
"type": "array", | ||
"defaultValue": [] | ||
}, | ||
"appended_value": { | ||
"type": "int" | ||
} | ||
}, | ||
"variables": { | ||
"values": { | ||
"type": "Array", | ||
"defaultValue": [] | ||
} | ||
}, | ||
"annotations": [] | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
tests/functional/append_variable_pipeline/test_append_variable_activity_pipeline.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import List | ||
|
||
import pytest | ||
from azure_data_factory_testing_framework.models.activities.append_variable_activity import AppendVariableActivity | ||
from azure_data_factory_testing_framework.models.activities.set_variable_activity import SetVariableActivity | ||
from azure_data_factory_testing_framework.state import RunParameter, RunParameterType | ||
from azure_data_factory_testing_framework.test_framework import TestFramework, TestFrameworkType | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"initial_value,appended_value,expected_value", | ||
[ | ||
([1, 2], 3, [1, 2, 3]), | ||
([], 1, [1]), | ||
([4], 5, [4, 5]), | ||
], | ||
) | ||
def test_append_variable_activity( | ||
initial_value: List[int], appended_value: int, expected_value: List[int], request: pytest.FixtureRequest | ||
) -> None: | ||
# Arrange | ||
test_framework = TestFramework( | ||
framework_type=TestFrameworkType.Fabric, | ||
root_folder_path=request.fspath.dirname, | ||
should_evaluate_child_pipelines=True, | ||
) | ||
pipeline = test_framework.repository.get_pipeline_by_name("append-variable-test") | ||
|
||
# Act | ||
activities = test_framework.evaluate_pipeline( | ||
pipeline, | ||
[ | ||
RunParameter(RunParameterType.Pipeline, "initial_value", initial_value), | ||
RunParameter(RunParameterType.Pipeline, "appended_value", appended_value), | ||
], | ||
) | ||
|
||
# Assert | ||
activity: SetVariableActivity = next(activities) | ||
assert activity.type == "SetVariable" | ||
assert activity.value.value == initial_value | ||
|
||
activity: AppendVariableActivity = next(activities) | ||
assert activity.type == "AppendVariable" | ||
assert activity.value.value == appended_value |
66 changes: 66 additions & 0 deletions
66
tests/unit/models/activities/test_append_variable_activity.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from typing import List | ||
|
||
import pytest | ||
from azure_data_factory_testing_framework.exceptions.variable_being_evaluated_does_not_exist_error import ( | ||
VariableBeingEvaluatedDoesNotExistError, | ||
) | ||
from azure_data_factory_testing_framework.models.activities.append_variable_activity import AppendVariableActivity | ||
from azure_data_factory_testing_framework.models.data_factory_element import DataFactoryElement | ||
from azure_data_factory_testing_framework.state import PipelineRunState, PipelineRunVariable | ||
from azure_data_factory_testing_framework.test_framework import TestFramework, TestFrameworkType | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"initial_value,appended_value,expected_value", | ||
[ | ||
([1, 2], 3, [1, 2, 3]), | ||
([], 1, [1]), | ||
([4], 5, [4, 5]), | ||
], | ||
) | ||
def test_when_int_variable_appended_then_state_variable_should_be_set( | ||
initial_value: List[int], appended_value: int, expected_value: List[int] | ||
) -> None: | ||
# Arrange | ||
TestFramework(framework_type=TestFrameworkType.Fabric) | ||
variable_name = "TestVariable" | ||
set_variable_activity = AppendVariableActivity( | ||
name="AppendVariableActivity", | ||
typeProperties={ | ||
"variableName": variable_name, | ||
"value": DataFactoryElement(str(appended_value)), | ||
}, | ||
) | ||
state = PipelineRunState( | ||
variables=[ | ||
PipelineRunVariable(name=variable_name, default_value=initial_value), | ||
], | ||
) | ||
|
||
# Act | ||
set_variable_activity.evaluate(state) | ||
|
||
# Assert | ||
variable = state.get_variable_by_name(variable_name) | ||
assert variable.value == expected_value | ||
|
||
|
||
def test_when_unknown_variable_evaluated_then_should_raise_exception() -> None: | ||
# Arrange | ||
TestFramework(framework_type=TestFrameworkType.Fabric) | ||
variable_name = "TestVariable" | ||
set_variable_activity = AppendVariableActivity( | ||
name="AppendVariableActivity", | ||
typeProperties={ | ||
"variableName": variable_name, | ||
"value": DataFactoryElement("TestValue"), | ||
}, | ||
) | ||
state = PipelineRunState() | ||
|
||
# Act | ||
with pytest.raises(VariableBeingEvaluatedDoesNotExistError) as exception_info: | ||
set_variable_activity.evaluate(state) | ||
|
||
# Assert | ||
assert exception_info.value.args[0] == "Variable being evaluated does not exist: TestVariable" |