-
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.
feat: foreach support and refactor model patching
- Loading branch information
Showing
11 changed files
with
140 additions
and
39 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
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
25 changes: 25 additions & 0 deletions
25
.../data_factory_testing_framework/models/activities/control_activities/for_each_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,25 @@ | ||
from typing import Callable, Generator | ||
|
||
from data_factory_testing_framework.generated.models import ForEachActivity, Activity, ControlActivity | ||
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState | ||
|
||
|
||
class ForEachActivity: | ||
|
||
@staticmethod | ||
def patch_generated_models(models): | ||
models.ForEachActivity.evaluate = ForEachActivity.evaluate | ||
models.ForEachActivity.evaluate_control_activity_iterations = ForEachActivity.evaluate_control_activity_iterations | ||
|
||
def evaluate(self: ForEachActivity, state: PipelineRunState): | ||
self.items.evaluate(state) | ||
|
||
return super(ControlActivity, self).evaluate(state) | ||
|
||
def evaluate_control_activity_iterations(self: ForEachActivity, state: PipelineRunState, evaluate_activities: Callable[[PipelineRunState], Generator[Activity, None, None]]): | ||
for item in self.items.evaluated_items: | ||
scoped_state = state.create_iteration_scope(item) | ||
for activity in evaluate_activities(self.activities, scoped_state): | ||
yield activity | ||
|
||
state.add_scoped_activity_results_from_scoped_state(scoped_state) |
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
20 changes: 20 additions & 0 deletions
20
src/python/data_factory_testing_framework/models/expression.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,20 @@ | ||
from typing import List | ||
|
||
from data_factory_testing_framework.generated.models import Expression | ||
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState | ||
|
||
|
||
class Expression: | ||
|
||
evaluated_items: List[str] = [] | ||
|
||
@staticmethod | ||
def patch_generated_models(models): | ||
models.Expression.evaluate = Expression.evaluate | ||
|
||
def evaluate(self: Expression, state: PipelineRunState): | ||
self.evaluated_items = [ | ||
"item1", | ||
"item2", | ||
"item3" | ||
] |
17 changes: 17 additions & 0 deletions
17
src/python/data_factory_testing_framework/models/patch_models.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,17 @@ | ||
from data_factory_testing_framework.generated import models as _models | ||
|
||
from data_factory_testing_framework.models.activities.base import Activity | ||
from data_factory_testing_framework.models.activities.control_activities.control_activity import ControlActivity | ||
from data_factory_testing_framework.models.activities.control_activities.execute_pipeline_activity import \ | ||
ExecutePipelineActivity | ||
from data_factory_testing_framework.models.activities.control_activities.for_each_activity import ForEachActivity | ||
from data_factory_testing_framework.models.expression import Expression | ||
|
||
|
||
# Patch models with our custom classes | ||
def patch_models(): | ||
Activity.patch_generated_models(_models) | ||
ExecutePipelineActivity.patch_generated_models(_models) | ||
ControlActivity.patch_generated_models(_models) | ||
ForEachActivity.patch_generated_models(_models) | ||
Expression.patch_generated_models(_models) |
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
20 changes: 0 additions & 20 deletions
20
src/python/data_factory_testing_framework/models/repositories/models_repository.py
This file was deleted.
Oops, something went wrong.
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
49 changes: 49 additions & 0 deletions
49
src/python/tests/models/activities/control_activities/test_for_each_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,49 @@ | ||
from data_factory_testing_framework.generated.models import ForEachActivity, Expression, ExpressionType, \ | ||
SetVariableActivity, DataFactoryElement | ||
from data_factory_testing_framework.models.base.pipeline_run_variable import PipelineRunVariable | ||
from data_factory_testing_framework.models.state.pipeline_run_state import PipelineRunState | ||
from data_factory_testing_framework.models.test_framework import TestFramework | ||
|
||
|
||
class TestForEachActivity: | ||
|
||
def test_when_evaluate_child_activities_then_should_return_the_activity_with_item_expression_evaluated(self): | ||
# Arrange | ||
test_framework = TestFramework() | ||
for_each_activity = ForEachActivity(name="ForEachActivity", | ||
items=Expression(type=ExpressionType.EXPRESSION, | ||
value="@split('a,b,c', ',')"), | ||
activities=[ | ||
SetVariableActivity(name="setVariable", variable_name="variable", | ||
value=DataFactoryElement[str]("item()"), | ||
depends_on=[]) | ||
], | ||
depends_on=[]) | ||
state = PipelineRunState() | ||
state.variables.append(PipelineRunVariable("variable", "")) | ||
|
||
# Act | ||
activities = test_framework.evaluate_activity(for_each_activity, state) | ||
|
||
# Assert | ||
set_variable_activity = next(activities) | ||
assert set_variable_activity is not None | ||
assert set_variable_activity.name == "setVariable" | ||
# assert set_variable_activity.Value == "a" | ||
|
||
set_variable_activity = next(activities) | ||
assert set_variable_activity is not None | ||
assert set_variable_activity.name == "setVariable" | ||
# assert set_variable_activity.Value == "b" | ||
|
||
set_variable_activity = next(activities) | ||
assert set_variable_activity is not None | ||
assert set_variable_activity.name == "setVariable" | ||
# assert set_variable_activity.Value == "c" | ||
|
||
# Assert that there are no more activities | ||
try: | ||
next(activities) | ||
assert False # This line should not be reached, an exception should be raised | ||
except StopIteration: | ||
pass |