Skip to content

Commit

Permalink
tests for nested associations
Browse files Browse the repository at this point in the history
  • Loading branch information
phcurado committed Jan 6, 2023
1 parent 1cb6f27 commit 3fc0000
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/parameter/types/boolean.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ defmodule Parameter.Types.Boolean do
iex> Parameter.Types.Boolean.load("not boolean")
{:error, "invalid boolean type"}
iex> Parameter.Types.Boolean.load(:not_boolean)
{:error, "invalid boolean type"}
"""
@impl true
def load(value) when is_boolean(value) do
Expand Down
3 changes: 3 additions & 0 deletions lib/parameter/types/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ defmodule Parameter.Types.Date do
iex> Parameter.Types.Date.load("2015-25-23")
{:error, "invalid date type"}
iex> Parameter.Types.Date.load(:not_valid_type)
{:error, "invalid date type"}
"""
@impl true
def load(%Date{} = value) do
Expand Down
84 changes: 83 additions & 1 deletion test/parameter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,39 @@ defmodule ParameterTest do
Parameter.load(UserTestSchema, params)
end

test "load user schema with invalid input type on nested paramaters shoud return an error" do
params = %{
"firstName" => "John",
"lastName" => "Doe",
"age" => 12,
"mainAddress" => "not a map",
"otherAddresses" => "not a list",
"status" => "anotherStatus",
"metadata" => "not a map",
"hexAmount" => 12,
"idInfo" => %{
"number" => "random",
"type" => "identity",
type: 12
}
}

assert {
:error,
%{
hex_amount: "invalid hex",
id_info: %{
number: "invalid integer type",
type: "field is present as atom and string keys"
},
main_address: "invalid inner data type",
metadata: "invalid map type",
other_addresses: "invalid list type",
status: "invalid enum type"
}
} == Parameter.load(UserTestSchema, params)
end

test "load user schema with ignore_nil true" do
params = %{
"firstName" => "John",
Expand Down Expand Up @@ -1096,7 +1129,11 @@ defmodule ParameterTest do
]
}
]
}} == Parameter.load(@attr_schema, params)
}} = result = Parameter.load(@attr_schema, params)

# also loading with struct should not have any effect

assert result == Parameter.load(@attr_schema, params, struct: true)
end

test "load runtime schema with wrong parameters should fail" do
Expand Down Expand Up @@ -1174,6 +1211,37 @@ defmodule ParameterTest do
}
})
end

test "nested params with invalid inner data should fail" do
assert {:error,
%{
nested_array: %{
1 => "invalid array type",
0 => %{0 => "invalid array type", 1 => "invalid array type"}
},
nested_map: %{a: "invalid map type"}
}} ==
Parameter.load(NestedSchemaTest, %{
nested_array: [[%{}, %{}], :string],
nested_map: %{
a: []
}
})
end

test "runtime schema with on_load returning errors should parse correctly" do
schema =
%{
name: [
type: :string,
on_load: fn _val, _input -> {:error, "this input will always load error"} end
]
}
|> Schema.compile!()

assert {:error, %{name: "this input will always load error"}} ==
Parameter.load(schema, %{name: "name"})
end
end

describe "dump/3" do
Expand Down Expand Up @@ -1649,6 +1717,20 @@ defmodule ParameterTest do
}
})
end

test "runtime schema with on_dump returning errors should parse correctly" do
schema =
%{
name: [
type: :string,
on_dump: fn _val, _input -> {:error, "this input will always dump error"} end
]
}
|> Schema.compile!()

assert {:error, %{name: "this input will always dump error"}} ==
Parameter.dump(schema, %{name: "name"})
end
end

describe "validate/3" do
Expand Down

0 comments on commit 3fc0000

Please sign in to comment.