Skip to content

Commit 5f00405

Browse files
authored
fix: use default values for variables (#123)
fix: default values for variables
1 parent e5b5009 commit 5f00405

File tree

5 files changed

+258
-2
lines changed

5 files changed

+258
-2
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
"python.testing.cwd": "${workspaceFolder}",
1111
"python.testing.pytestEnabled": true,
1212
"python.testing.unittestEnabled": false,
13+
"python.defaultInterpreterPath": ".venv/bin/python",
1314
}

src/data_factory_testing_framework/models/_pipeline.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import TYPE_CHECKING, Any, List
1+
from typing import TYPE_CHECKING, Any, List, Union
22

33
from data_factory_testing_framework.exceptions import ActivityNotFoundError
44

@@ -79,10 +79,25 @@ def validate_and_append_default_parameters(self, parameters: List[RunParameter])
7979

8080
return parameters
8181

82+
def _get_default_value_for_variable(self, variable_value: dict[str, Any]) -> Union[str, int, bool, List[Any]]:
83+
"""Get the default value of a variable."""
84+
if variable_value["type"] == "String":
85+
return variable_value.get("defaultValue", "")
86+
elif variable_value["type"] == "Integer":
87+
return variable_value.get("defaultValue", 0)
88+
elif variable_value["type"] == "Boolean":
89+
return variable_value.get("defaultValue", False)
90+
elif variable_value["type"] == "Array":
91+
return variable_value.get("defaultValue", [])
92+
else:
93+
raise NotImplementedError(f"Type {variable_value['type']} not implemented")
94+
8295
def get_run_variables(self) -> List[PipelineRunVariable]:
8396
"""Get the run variables for the pipeline. This can be used to generate the instance variables for a pipeline run."""
8497
run_variables = []
8598
for variable_name, variable_value in self.variables.items():
86-
run_variables.append(PipelineRunVariable(variable_name, variable_value.get("default_value", None)))
99+
run_variables.append(
100+
PipelineRunVariable(variable_name, self._get_default_value_for_variable(variable_value))
101+
)
87102

88103
return run_variables
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"name": "default_variables",
3+
"properties": {
4+
"activities": [
5+
{
6+
"name": "Set outputStringVar",
7+
"type": "SetVariable",
8+
"dependsOn": [],
9+
"policy": {
10+
"secureOutput": false,
11+
"secureInput": false
12+
},
13+
"userProperties": [],
14+
"typeProperties": {
15+
"variableName": "pipelineReturnValue",
16+
"value": [
17+
{
18+
"key": "outputStringVar",
19+
"value": {
20+
"type": "Expression",
21+
"content": "@if(equals(variables('stringVar'), null), 'is null', concat('is not null: ', variables('stringVar')))\n"
22+
}
23+
}
24+
],
25+
"setSystemVariable": true
26+
}
27+
},
28+
{
29+
"name": "Set outputIntVar",
30+
"type": "SetVariable",
31+
"dependsOn": [],
32+
"policy": {
33+
"secureOutput": false,
34+
"secureInput": false
35+
},
36+
"userProperties": [],
37+
"typeProperties": {
38+
"variableName": "pipelineReturnValue",
39+
"value": [
40+
{
41+
"key": "outputIntVar",
42+
"value": {
43+
"type": "Expression",
44+
"content": "@if(equals(variables('intVar'), null), 'is null', concat('is not null: ', variables('intVar')))\n"
45+
}
46+
}
47+
],
48+
"setSystemVariable": true
49+
}
50+
},
51+
{
52+
"name": "Set outputBoolVar",
53+
"type": "SetVariable",
54+
"dependsOn": [],
55+
"policy": {
56+
"secureOutput": false,
57+
"secureInput": false
58+
},
59+
"userProperties": [],
60+
"typeProperties": {
61+
"variableName": "pipelineReturnValue",
62+
"value": [
63+
{
64+
"key": "outputBoolVar",
65+
"value": {
66+
"type": "Expression",
67+
"content": "@if(equals(variables('boolVar'), null), 'is null', concat('is not null: ', variables('boolVar')))\n"
68+
}
69+
}
70+
],
71+
"setSystemVariable": true
72+
}
73+
},
74+
{
75+
"name": "Set outputArrayVar",
76+
"type": "SetVariable",
77+
"dependsOn": [],
78+
"policy": {
79+
"secureOutput": false,
80+
"secureInput": false
81+
},
82+
"userProperties": [],
83+
"typeProperties": {
84+
"variableName": "pipelineReturnValue",
85+
"value": [
86+
{
87+
"key": "outputArrayVar",
88+
"value": {
89+
"type": "Expression",
90+
"content": "@if(equals(variables('arrayVar'), null), 'is null', concat('is not null: ', variables('arrayVar')))\n"
91+
}
92+
}
93+
],
94+
"setSystemVariable": true
95+
}
96+
}
97+
],
98+
"variables": {
99+
"stringVar": {
100+
"type": "String"
101+
},
102+
"intVar": {
103+
"type": "Integer"
104+
},
105+
"boolVar": {
106+
"type": "Boolean"
107+
},
108+
"arrayVar": {
109+
"type": "Array"
110+
}
111+
},
112+
"annotations": []
113+
}
114+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import pytest
2+
from data_factory_testing_framework import TestFramework, TestFrameworkType
3+
4+
5+
def test_string_default_variables(request: pytest.FixtureRequest) -> None:
6+
# Arrange
7+
test_framework = TestFramework(
8+
framework_type=TestFrameworkType.DataFactory, root_folder_path=request.fspath.dirname
9+
)
10+
pipeline = test_framework.get_pipeline_by_name("default_variables")
11+
12+
# Act
13+
activities = test_framework.evaluate_pipeline(
14+
pipeline,
15+
[],
16+
)
17+
18+
# Assert
19+
activity = next(activities)
20+
assert activity.name == "Set outputStringVar"
21+
assert activity.type_properties["value"][0]["key"] == "outputStringVar"
22+
assert activity.type_properties["value"][0]["value"].result == "is not null: "
23+
24+
activity = next(activities)
25+
assert activity.name == "Set outputIntVar"
26+
assert activity.type_properties["value"][0]["key"] == "outputIntVar"
27+
assert activity.type_properties["value"][0]["value"].result == "is not null: 0"
28+
29+
activity = next(activities)
30+
assert activity.name == "Set outputBoolVar"
31+
assert activity.type_properties["value"][0]["key"] == "outputBoolVar"
32+
assert activity.type_properties["value"][0]["value"].result == "is not null: False"
33+
34+
activity = next(activities)
35+
assert activity.name == "Set outputArrayVar"
36+
assert activity.type_properties["value"][0]["key"] == "outputArrayVar"
37+
assert activity.type_properties["value"][0]["value"].result == "is not null: []"
38+
39+
with pytest.raises(StopIteration):
40+
next(activities)

tests/unit/models/pipelines/test_pipeline_resource.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,92 @@ def test_when_validate_parameters_is_accurate_should_pass() -> None:
4141
assert parameters[2].value == "pipelineParameterValue3"
4242

4343

44+
@pytest.mark.parametrize(
45+
("variable_spec", "expected_value"),
46+
[
47+
(
48+
{
49+
"type": "String",
50+
"defaultValue": "stringDefault",
51+
},
52+
"stringDefault",
53+
),
54+
(
55+
{
56+
"type": "String",
57+
},
58+
"",
59+
),
60+
(
61+
{
62+
"type": "String",
63+
"defaultValue": "",
64+
},
65+
"",
66+
),
67+
(
68+
{
69+
"type": "Boolean",
70+
"defaultValue": True,
71+
},
72+
True,
73+
),
74+
(
75+
{
76+
"type": "Boolean",
77+
},
78+
False,
79+
),
80+
(
81+
{
82+
"type": "Integer",
83+
"defaultValue": 1,
84+
},
85+
1,
86+
),
87+
(
88+
{
89+
"type": "Integer",
90+
},
91+
0,
92+
),
93+
(
94+
{
95+
"type": "Array",
96+
"defaultValue": ["a", "b"],
97+
},
98+
["a", "b"],
99+
),
100+
(
101+
{
102+
"type": "Array",
103+
},
104+
[],
105+
),
106+
],
107+
)
108+
def test_when_get_run_variables_default_values_should_be_used(
109+
variable_spec: dict[str, str], expected_value: object
110+
) -> None:
111+
# Arrange
112+
pipeline = Pipeline(
113+
pipeline_id="some-id",
114+
name="pipeline",
115+
activities=[],
116+
variables={
117+
"variable": variable_spec,
118+
},
119+
)
120+
121+
# Act
122+
variables = pipeline.get_run_variables()
123+
124+
# Assert
125+
assert len(variables) == 1
126+
assert variables[0].name == "variable"
127+
assert variables[0].value == expected_value
128+
129+
44130
def test_when_validate_parameters_is_missing_run_parameter_should_throw_error() -> None:
45131
# Arrange
46132
pipeline = Pipeline(

0 commit comments

Comments
 (0)