Skip to content

Commit

Permalink
Merge pull request #1721 from UrbanOS-Public/conditional-greater-than…
Browse files Browse the repository at this point in the history
…-equal

Added greater than or equal to/less than or equal to conditional
  • Loading branch information
AFarrAccenture authored Sep 28, 2023
2 parents e99a6d9 + 5638a66 commit b065d88
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 43 deletions.
2 changes: 1 addition & 1 deletion apps/alchemist/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Alchemist.MixProject do
def project do
[
app: :alchemist,
version: "0.2.54",
version: "0.2.55",
elixir: "~> 1.10",
build_path: "../../_build",
config_path: "../../config/config.exs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule AndiWeb.IngestionLiveView.Transformations.TransformationFieldBuilder d
<%= if show_all_comparison_options? do %>
<div>
<%= label(form, :conditionOperation, "Comparison", class: "label label--required", for: "transformation_condition_#{@id}__comparison") %>
<%= select(form, :conditionOperation, ["", "Is Equal To", "Is Not Equal To", "Is Greater Than", "Is Less Than"], [value: get_in(form.source.changes, [:parameters, :conditionOperation]), id: "transformation_condition_#{@id}__comparison", class: "select transformation-type", required: true]) %>
<%= select(form, :conditionOperation, ["", "Is Equal To", "Is Not Equal To", "Is Greater Than", "Is Less Than","Is Greater Than Or Equal To", "Is Less Than Or Equal To"], [value: get_in(form.source.changes, [:parameters, :conditionOperation]), id: "transformation_condition_#{@id}__comparison", class: "select transformation-type", required: true]) %>
<%= ErrorHelpers.error_tag(form.source, :conditionOperation, bind_to_input: false, id: "#{@id}_transformation_condition_comparison_error") %>
</div>
<% else %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ defmodule AndiWeb.IngestionLiveView.Transformations.TransformationFormTest do
refute has_element?(view, "#transformation_condition_#{transformation.id}__targetDateFormat")
end

test "in the condition form, selecting 'static value' will allow equals, not equals, greater than, or less than comparisons", %{
view: view,
ingestion: ingestion
} do
test "in the condition form, selecting 'static value' will allow equals, not equals, greater than, greater than or equal to, less than or equal to, or less than comparisons",
%{
view: view,
ingestion: ingestion
} do
transformation = Enum.find(ingestion.transformations, fn transformation -> transformation.type == "constant" end)

view
Expand All @@ -260,13 +261,16 @@ defmodule AndiWeb.IngestionLiveView.Transformations.TransformationFormTest do
assert options_html =~ "Is Equal To"
assert options_html =~ "Is Not Equal To"
assert options_html =~ "Is Greater Than"
assert options_html =~ "Is Greater Than Or Equal To"
assert options_html =~ "Is Less Than"
assert options_html =~ "Is Less Than Or Equal To"
end

test "in the condition form, selecting 'target field' will allow equals, not equals, greater than, or less than comparisons", %{
view: view,
ingestion: ingestion
} do
test "in the condition form, selecting 'target field' will allow equals, not equals, greater than, greater than or equal to, less than or equal to, or less than comparisons",
%{
view: view,
ingestion: ingestion
} do
transformation = Enum.find(ingestion.transformations, fn transformation -> transformation.type == "constant" end)

view
Expand All @@ -285,7 +289,9 @@ defmodule AndiWeb.IngestionLiveView.Transformations.TransformationFormTest do
assert options_html =~ "Is Equal To"
assert options_html =~ "Is Not Equal To"
assert options_html =~ "Is Greater Than"
assert options_html =~ "Is Greater Than Or Equal To"
assert options_html =~ "Is Less Than"
assert options_html =~ "Is Less Than Or Equal To"
end

test "in the condition form, selecting 'Null or Empty' will allow equals and not equals comparisons", %{view: view, ingestion: ingestion} do
Expand Down
22 changes: 15 additions & 7 deletions apps/transformers/lib/transformation_conditions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,14 @@ defmodule Transformers.Conditions do
"<" ->
{:ok, left_value < right_value}

_ ->
{:error, "unsupported condition operation"}
">=" ->
{:ok, left_value >= right_value}

"<=" ->
{:ok, left_value <= right_value}

condition_operation ->
{:error, "unsupported condition operation #{condition_operation}"}
end
rescue
error -> {:error, error}
Expand Down Expand Up @@ -194,11 +200,13 @@ defmodule Transformers.Conditions do
end

def map_operation(value) do
case value do
"Is Equal To" -> "="
"Is Not Equal To" -> "!="
"Is Greater Than" -> ">"
"Is Less Than" -> "<"
case String.downcase(value) do
"is equal to" -> "="
"is not equal to" -> "!="
"is greater than" -> ">"
"is less than" -> "<"
"is greater than or equal to" -> ">="
"is less than or equal to" -> "<="
_ -> value
end
end
Expand Down
2 changes: 1 addition & 1 deletion apps/transformers/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule Transformers.MixProject do
def project do
[
app: :transformers,
version: "1.0.38",
version: "1.0.39",
build_path: "../../_build",
config_path: "../../config/config.exs",
deps_path: "../../deps",
Expand Down
142 changes: 117 additions & 25 deletions apps/transformers/test/unit/transformation_conditions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -420,94 +420,184 @@ defmodule Transformers.ConditionsTest do
result = Conditions.check(payload, parameters)
assert result == {:ok, true}
end
end

test "returns false when source field value is less than static target value " do
describe "greater than or equal to" do
test "returns true when source field value is equal to static target value" do
parameters = %{
"targetField" => "testField",
"newValue" => "new value",
"valueType" => "string",
"condition" => "true",
"conditionCompareTo" => "Static Value",
"conditionDataType" => "number",
"sourceConditionField" => "testField",
"conditionOperation" => ">",
"conditionOperation" => ">=",
"targetConditionField" => nil,
"targetConditionValue" => "3"
"targetConditionValue" => "1"
}

payload = %{
"testField" => 2
"testField" => 1
}

result = Conditions.check(payload, parameters)
assert result == {:ok, false}
assert result == {:ok, true}
end

test "returns true when source field value is greater than target field value" do
test "returns true when source field value is greater than or equal to static target value 2" do
parameters = %{
"targetField" => "testField",
"newValue" => "new value",
"valueType" => "string",
"condition" => "true",
"conditionCompareTo" => "Target Field",
"conditionCompareTo" => "Static Value",
"conditionDataType" => "number",
"sourceConditionField" => "testField",
"conditionOperation" => ">",
"targetConditionField" => "compareField",
"targetConditionValue" => nil
"conditionOperation" => ">=",
"targetConditionField" => nil,
"targetConditionValue" => "2"
}

payload = %{
"testField" => 2,
"compareField" => 1
"testField" => 2
}

result = Conditions.check(payload, parameters)
assert result == {:ok, true}
end

test "returns false when source field value is less than target field value" do
test "returns true when source field value is less than static target value" do
parameters = %{
"targetField" => "testField",
"newValue" => "new value",
"valueType" => "string",
"condition" => "true",
"conditionCompareTo" => "Target Field",
"conditionCompareTo" => "Static Value",
"conditionDataType" => "number",
"sourceConditionField" => "testField",
"conditionOperation" => ">",
"targetConditionField" => "compareField",
"targetConditionValue" => nil
"conditionOperation" => "<=",
"targetConditionField" => nil,
"targetConditionValue" => "2"
}

payload = %{
"testField" => 2,
"compareField" => 3
"testField" => 1
}

result = Conditions.check(payload, parameters)
assert result == {:ok, false}
assert result == {:ok, true}
end

test "maps operation string 'Is Greater Than'" do
test "returns true when source field value is less than or 'equal to' static target value 1" do
parameters = %{
"targetField" => "testField",
"newValue" => "new value",
"valueType" => "string",
"condition" => "true",
"conditionCompareTo" => "Static Value",
"conditionDataType" => "number",
"sourceConditionField" => "testField",
"conditionOperation" => "Is Greater Than",
"conditionOperation" => "<=",
"targetConditionField" => nil,
"targetConditionValue" => "1"
}

payload = %{
"testField" => 2
"testField" => 1
}

result = Conditions.check(payload, parameters)
assert result == {:ok, true}
end
end

test "returns false when source field value is less than static target value " do
parameters = %{
"targetField" => "testField",
"newValue" => "new value",
"condition" => "true",
"conditionCompareTo" => "Static Value",
"conditionDataType" => "number",
"sourceConditionField" => "testField",
"conditionOperation" => ">",
"targetConditionField" => nil,
"targetConditionValue" => "3"
}

payload = %{
"testField" => 2
}

result = Conditions.check(payload, parameters)
assert result == {:ok, false}
end

test "returns true when source field value is greater than target field value" do
parameters = %{
"targetField" => "testField",
"newValue" => "new value",
"condition" => "true",
"conditionCompareTo" => "Target Field",
"conditionDataType" => "number",
"sourceConditionField" => "testField",
"conditionOperation" => ">",
"targetConditionField" => "compareField",
"targetConditionValue" => nil
}

payload = %{
"testField" => 2,
"compareField" => 1
}

result = Conditions.check(payload, parameters)
assert result == {:ok, true}
end

test "returns false when source field value is less than target field value" do
parameters = %{
"targetField" => "testField",
"newValue" => "new value",
"condition" => "true",
"conditionCompareTo" => "Target Field",
"conditionDataType" => "number",
"sourceConditionField" => "testField",
"conditionOperation" => ">",
"targetConditionField" => "compareField",
"targetConditionValue" => nil
}

payload = %{
"testField" => 2,
"compareField" => 3
}

result = Conditions.check(payload, parameters)
assert result == {:ok, false}
end

test "maps operation string 'Is Greater Than'" do
parameters = %{
"targetField" => "testField",
"newValue" => "new value",
"condition" => "true",
"conditionCompareTo" => "Static Value",
"conditionDataType" => "number",
"sourceConditionField" => "testField",
"conditionOperation" => "Is Greater Than",
"targetConditionField" => nil,
"targetConditionValue" => "1"
}

payload = %{
"testField" => 2
}

result = Conditions.check(payload, parameters)
assert result == {:ok, true}
end

describe "less than" do
test "returns true when source field value is less than static target value " do
parameters = %{
Expand Down Expand Up @@ -708,8 +798,10 @@ defmodule Transformers.ConditionsTest do
"testField" => 2
}

result = Conditions.check(payload, parameters)
assert result == {:error, "unsupported condition operation"}
condition_operation = Conditions.check(payload, parameters)

assert condition_operation ==
{:error, "unsupported condition operation !"}
end

test "returns error when source field is not present in payload" do
Expand Down

0 comments on commit b065d88

Please sign in to comment.