Skip to content

Commit

Permalink
add test cases for mfa with args and dependent fields too
Browse files Browse the repository at this point in the history
  • Loading branch information
zoedsoupe committed Nov 26, 2024
1 parent 42c9c52 commit 45257af
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/peri_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,56 @@ defmodule PeriTest do
refute {:ok, ^data} = Peri.validate(parent, data)
end
end

test "it should apply the mapper on nested schemas too by MFA with additional args" do
nested = %{foo: :string}
parent = %{bar: {nested, {:transform, {__MODULE__, :id, []}}}}
data = %{bar: %{foo: "10"}}
assert {:ok, ^data} = Peri.validate(parent, data)

# generic map type should pass too
parent = %{bar: {:map, {:transform, {__MODULE__, :id, []}}}}
data = %{bar: %{foo: "hello"}}
assert {:ok, ^data} = Peri.validate(parent, data)

# function are indeed being applied?
assert_raise RuntimeError, fn ->
parent = %{bar: {nested, {:transform, {__MODULE__, :boom, []}}}}
data = %{bar: %{foo: "hello"}}
refute {:ok, ^data} = Peri.validate(parent, data)
end

assert_raise RuntimeError, fn ->
parent = %{bar: {:map, {:transform, {__MODULE__, :boom, []}}}}
data = %{bar: %{foo: "hello"}}
refute {:ok, ^data} = Peri.validate(parent, data)
end
end

test "it should apply the trasnformation on a nested schema type with dependent fields" do
nested = %{foo: :string}
parent = %{bar: {nested, {:transform, fn v, _ -> v end}}}
data = %{bar: %{foo: "hello"}}
assert {:ok, ^data} = Peri.validate(parent, data)

# generic map type should pass too
parent = %{bar: {:map, {:transform, fn v, _ -> v end}}}
data = %{bar: %{foo: "hello"}}
assert {:ok, ^data} = Peri.validate(parent, data)

# function are indeed being applied?
assert_raise RuntimeError, fn ->
parent = %{bar: {nested, {:transform, fn _, _ -> raise("boom") end}}}
data = %{bar: %{foo: "hello"}}
refute {:ok, ^data} = Peri.validate(parent, data)
end

assert_raise RuntimeError, fn ->
parent = %{bar: {:map, {:transform, fn _, _ -> raise("boom") end}}}
data = %{bar: %{foo: "hello"}}
refute {:ok, ^data} = Peri.validate(parent, data)
end
end
end

def id(v), do: v
Expand Down

0 comments on commit 45257af

Please sign in to comment.