Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase test coverage and fix some discovered issues #12

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Azure.Core.Expressions.DataFactory" Version="1.0.0-beta.4" Aliases="AzureCoreExpressionsDataFactory" />
</ItemGroup>

<ItemGroup>
Expand All @@ -32,8 +33,4 @@
<ProjectReference Include="..\AzureDataFactory.TestingFramework\AzureDataFactory.TestingFramework.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Azure.Core.Expressions.DataFactory;
using AzureDataFactory.TestingFramework.Exceptions;
using AzureDataFactory.TestingFramework.Functions;
using AzureDataFactory.TestingFramework.Models;
using AzureDataFactory.TestingFramework.Models.Activities.Base;
using AzureDataFactory.TestingFramework.Models.Pipelines;

namespace AzureDataFactory.TestingFramework.Tests.Models.Activities.Base;

public class ActivitiesEvaluatorTests
{
private readonly List<PipelineActivity> _activities;
private readonly WebActivity _webActivity;
private readonly SetVariableActivity _setVariableActivity;

public ActivitiesEvaluatorTests()
{
_activities = new List<PipelineActivity>();
_webActivity = new WebActivity("webActivity", WebActivityMethod.Get, "https://www.example.com")
{
DependsOn =
{
new PipelineActivityDependency("setVariableActivity", new[] { DependencyCondition.Succeeded })
}
};
_setVariableActivity = new SetVariableActivity("setVariableActivity")
{
VariableName = "variable1",
Value = "value1"
};
_activities.Add(_webActivity);
_activities.Add(_setVariableActivity);
}

[Fact]
public void EvaluateWithoutIterationActivities_ShouldEvaluateAccordingToDependencies()
{
// Act
var state = new PipelineRunState();
state.Variables.Add(new PipelineRunVariable<string>("variable1", string.Empty));
var evaluatedActivities = ActivitiesEvaluator.Evaluate(_activities, state).ToList();

// Assert
Assert.NotNull(evaluatedActivities);
Assert.Equal(2, evaluatedActivities.Count());
Assert.Equal("setVariableActivity", evaluatedActivities.First().Name);
Assert.Equal("webActivity", evaluatedActivities.Last().Name);
}

[Fact]
public void EvaluateWithCircularDependencies_ShouldThrowActivitiesEvaluatorInvalidDependencyException()
{
// Arrange
_setVariableActivity.DependsOn.Add(new PipelineActivityDependency("webActivity", new[] { DependencyCondition.Succeeded }));

// Assert
Assert.Throws<ActivitiesEvaluatorInvalidDependencyException>(() => ActivitiesEvaluator.Evaluate(_activities, new PipelineRunState()).ToList());
}

[Fact]
public void EvaluateWithForeachActivities_ShouldEvaluateAccordingToDependencies()
{
// Arrange
var state = new PipelineRunState();
state.Variables.Add(new PipelineRunVariable<string>("variable1", string.Empty));
state.Variables.Add(new PipelineRunVariable<string>("iterationItems", "item1,item2,item3"));
var foreachActivity = new ForEachActivity("foreachActivity",
new DataFactoryExpression(DataFactoryExpressionType.Expression, "@split(variables('iterationItems'), ',')"),
_activities);
_webActivity.Uri = new DataFactoryElement<string>("@concat('https://www.example.com/', item())", DataFactoryElementKind.Expression);

// Act
var evaluatedActivities = ActivitiesEvaluator.Evaluate(new List<PipelineActivity> { foreachActivity }, state);

// Assert
using var enumerator = evaluatedActivities.GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.Equal("setVariableActivity", enumerator.Current.Name);
Assert.True(enumerator.MoveNext());
Assert.Equal("webActivity", enumerator.Current.Name);
Assert.Equal("https://www.example.com/item1", ((WebActivity)enumerator.Current).Uri);
Assert.True(enumerator.MoveNext());
Assert.Equal("setVariableActivity", enumerator.Current.Name);
Assert.True(enumerator.MoveNext());
Assert.Equal("webActivity", enumerator.Current.Name);
Assert.Equal("https://www.example.com/item2", ((WebActivity)enumerator.Current).Uri);
Assert.True(enumerator.MoveNext());
Assert.Equal("setVariableActivity", enumerator.Current.Name);
Assert.True(enumerator.MoveNext());
Assert.Equal("webActivity", enumerator.Current.Name);
Assert.Equal("https://www.example.com/item3", ((WebActivity)enumerator.Current).Uri);
Assert.False(enumerator.MoveNext());
}

[Fact]
public void EvaluateWithUntilActivities_ShouldEvaluateAccordingToDependencies()
{
// Arrange
var state = new PipelineRunState();
state.Variables.Add(new PipelineRunVariable<string>("variable1", string.Empty));
var untilActivity = new UntilActivity("untilActivity",
new DataFactoryExpression(DataFactoryExpressionType.Expression, "@equals(variables('variable1'), 'value1')"),
_activities);

// Act
var evaluatedActivities = ActivitiesEvaluator.Evaluate(new List<PipelineActivity> { untilActivity }, state);

// Assert
using var enumerator = evaluatedActivities.GetEnumerator();
Assert.True(enumerator.MoveNext());
Assert.Equal("setVariableActivity", enumerator.Current.Name);
Assert.True(enumerator.MoveNext());
Assert.Equal("webActivity", enumerator.Current.Name);
Assert.False(enumerator.MoveNext());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using AzureDataFactory.TestingFramework.Models;
using AzureDataFactory.TestingFramework.Models.Pipelines;

namespace AzureDataFactory.TestingFramework.Tests.Models.Activities.Base;

public class PipelineActivityTests
{
[Theory]
[InlineData("Succeeded", "Succeeded", true)]
[InlineData("Failed", "Succeeded", false)]
[InlineData("Skipped", "Succeeded", false)]
[InlineData("Completed", "Succeeded", false)]
[InlineData("Failed", "Failed", true)]
[InlineData("Skipped", "Failed", false)]
[InlineData("Completed", "Failed", false)]
[InlineData("Skipped", "Skipped", true)]
[InlineData("Completed", "Skipped", false)]
[InlineData("Completed", "Completed", true)]
public void DependencyConditions_WhenCalled_ReturnsExpected(string requiredCondition, string actualCondition, bool expected)
{
// Arrange
var pipelineActivity = new PipelineActivity("activity")
{
DependsOn = { new PipelineActivityDependency("otherActivity", new[] { new DependencyCondition(requiredCondition) }) }
};
var state = new PipelineRunState();
state.AddActivityResult(new TestActivityResult("otherActivity", new DependencyCondition(actualCondition)));

// Assert
Assert.Equal(expected, pipelineActivity.AreDependencyConditionMet(state));
}

[Fact]
public void EvaluateWhenNoStatusIsSet_ShouldSetStatusToSucceeded()
{
// Arrange
var pipelineActivity = new PipelineActivity("activity");
var state = new PipelineRunState();

// Act
pipelineActivity.Evaluate(state);

// Assert
Assert.Equal(DependencyCondition.Succeeded, pipelineActivity.Status);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Azure.Core.Expressions.DataFactory;
using AzureDataFactory.TestingFramework.Models;
using AzureDataFactory.TestingFramework.Models.Pipelines;

namespace AzureDataFactory.TestingFramework.Tests.Models.Activities.ControlActivities;

public class ForEachActivityTests
{
[Fact]
public void WhenEvaluateChildActivities_ThenShouldReturnTheActivityWithItemExpressionEvaluated()
{
// Arrange
var forEachActivity = new ForEachActivity("ForEachActivity",
new DataFactoryExpression(DataFactoryExpressionType.Expression, "@split('a,b,c', ',')"),
new List<PipelineActivity>()
{
new SetVariableActivity("setVariable")
{
VariableName = "variable",
Value = new DataFactoryElement<string>("item()", DataFactoryElementKind.Expression)
}
});
var state = new PipelineRunState();
state.Variables.Add(new PipelineRunVariable<string>("variable", string.Empty));

// Act
forEachActivity.Evaluate(state);
var childActivities = forEachActivity.EvaluateChildActivities(state);

// Assert
using var enumarator = childActivities.GetEnumerator();
Assert.True(enumarator.MoveNext());
var setVariableActivity = enumarator.Current as SetVariableActivity;
Assert.NotNull(setVariableActivity);
Assert.Equal("a", setVariableActivity.Value);
Assert.True(enumarator.MoveNext());
setVariableActivity = enumarator.Current as SetVariableActivity;
Assert.NotNull(setVariableActivity);
Assert.Equal("b", setVariableActivity.Value);
Assert.True(enumarator.MoveNext());
setVariableActivity = enumarator.Current as SetVariableActivity;
Assert.NotNull(setVariableActivity);
Assert.Equal("c", setVariableActivity.Value);
Assert.False(enumarator.MoveNext());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Azure.Core.Expressions.DataFactory;
using AzureDataFactory.TestingFramework.Models;
using AzureDataFactory.TestingFramework.Models.Pipelines;

namespace AzureDataFactory.TestingFramework.Tests.Models.Activities.ControlActivities;

public class IfConditionActivityTests
{
[Fact]
public void WhenEvaluated_ShouldEvaluateExpression()
{
// Arrange
var activity = new IfConditionActivity("IfConditionActivity",
new DataFactoryExpression(DataFactoryExpressionType.Expression, "@equals(1, 1)"));

// Act
activity.Evaluate(new PipelineRunState());

// Assert
Assert.True(activity.EvaluatedExpression);
}

[Theory]
[InlineData(true, "setVariableActivity1")]
[InlineData(false, "setVariableActivity2")]
public void WhenEvaluated_ShouldEvaluateCorrectChildActivities(bool expressionOutcome, string expectedActivityName)
{
// Arrange
var expression = expressionOutcome ? "@equals(1, 1)" : "@equals(1, 2)";
var activity = new IfConditionActivity("IfConditionActivity",
new DataFactoryExpression(DataFactoryExpressionType.Expression, expression))
{
IfTrueActivities = { new SetVariableActivity("setVariableActivity1") { VariableName = "variable", Value = "dummy" } },
IfFalseActivities = { new SetVariableActivity("setVariableActivity2") { VariableName = "variable", Value = "dummy" } }
};
var state = new PipelineRunState();
state.Variables.Add(new PipelineRunVariable<string>("variable", string.Empty));
activity.Evaluate(state);

// Act
var childActivities = activity.EvaluateChildActivities(state).ToList();

// Assert
Assert.Single(childActivities);
Assert.Equal(expectedActivityName, childActivities.First().Name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using AzureDataFactory.TestingFramework.Models;
using AzureDataFactory.TestingFramework.Models.Pipelines;

namespace AzureDataFactory.TestingFramework.Tests.Models.Activities;

public class SetVariableTests
{
[Fact]
public void WhenStringVariableEvaluated_ThenStateVariableShouldBeSet()
{
// Arrange
var variableName = "TestVariable";
var variable = new PipelineRunVariable<string>(variableName, string.Empty);
var setVariable = new SetVariableActivity("TestSetVariable")
{
VariableName = variableName,
Value = "value1"
};
var state = new PipelineRunState();
state.Variables.Add(variable);

// Act
setVariable.Evaluate(state);

// Assert
Assert.Equal("value1", variable.Value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using AzureDataFactory.TestingFramework.Models;
using Azure.Core.Expressions.DataFactory;
using AzureDataFactory.TestingFramework.Models.Pipelines;

namespace AzureDataFactory.TestingFramework.Tests.Models;

public class DataFactoryEntityTests
{
[Fact]
public void WhenEvaluatingEntity_ShouldEvaluateAllProperties()
{
// Arrange
var entity = new WebActivity("TestActivity", WebActivityMethod.Get, new DataFactoryElement<string>("@concat('https://example.com', '/123')", DataFactoryElementKind.Expression));

// Act
entity.Evaluate(new PipelineRunState());

// Assert
Assert.NotNull(entity);
Assert.Equal("https://example.com/123", entity.Uri);
}

[Fact]
public void WhenNotEvaluatingEntity_ShouldNotEvaluateAllProperties()
{
// Arrange
var entity = new WebActivity("TestActivity", WebActivityMethod.Get, new DataFactoryElement<string>("@concat('https://example.com', '/123')", DataFactoryElementKind.Expression));

// Act

// Assert
Assert.NotNull(entity);
Assert.Throws<ExpressionNotEvaluatedException>(() => entity.Uri.Value);
}
}
Loading
Loading