Skip to content

Commit

Permalink
more tests on apply_operation
Browse files Browse the repository at this point in the history
  • Loading branch information
phcurado committed Jul 24, 2023
1 parent bd07510 commit 8c193af
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions test/parameter/engine_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,70 @@ defmodule Parameter.EngineTest do
|> load(params)
end
end

describe "apply_operation/1" do
import Parameter.Engine

test "load operation on SimpleSchema" do
params = %{"firstName" => "John", "lastName" => "Doe", "age" => "22"}

assert {:ok, %{first_name: "John", last_name: "Doe", age: 22}} ==
SimpleSchema
|> load(params)
|> apply_operation()
end

test "validate on SimpleSchema" do
params = %{"lastName" => "Doe", "age" => "22a"}

assert {:error, %{first_name: "is required", age: "invalid integer type"}} ==
SimpleSchema
|> load(params)
|> apply_operation()
end

test "load operation on NestedSchema" do
params = %{
"addresses" => [
%{"street" => "some street", "number" => 1, "state" => "some state"},
%{"street" => "other street", "number" => 5, "state" => "other state"}
],
"phone" => %{"code" => 55, "number" => "123555"}
}

assert {:ok,
%{
addresses: [
%{state: "some state", number: 1, street: "some street"},
%{state: "other state", number: 5, street: "other street"}
],
phone: %{code: "55", number: "123555"}
}} ==
NestedSchema
|> load(params)
|> apply_operation()
end

test "validate on NestedSchema" do
params = %{
"addresses" => [
%{"street" => "some street", "number" => "1A"},
%{"number" => 5, "state" => "other state"}
],
"phone" => %{"code" => 55}
}

assert {:error,
%{
addresses: [
%{0 => %{number: "invalid integer type"}},
%{1 => %{street: "is required"}}
],
phone: %{number: "is required"}
}} ==
NestedSchema
|> load(params)
|> apply_operation()
end
end
end

0 comments on commit 8c193af

Please sign in to comment.