Skip to content

Commit

Permalink
union test cases (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
revati authored Jun 6, 2024
1 parent 3ae44d7 commit 8f9209e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
76 changes: 60 additions & 16 deletions test/auto_form_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -117,23 +117,67 @@ defmodule AshPhoenix.AutoFormTest do
|> Map.get(:union)
end

test "simple unions with same value" do
assert %Ash.Union{type: :predefined, value: :update2} =
SimplePost
|> AshPhoenix.Form.for_create(:create,
domain: Domain,
forms: [
auto?: true
],
params: %{
"text" => "foobar"
}
)
|> AshPhoenix.Form.add_form(:union,
params: %{"_union_type" => "predefined", "value" => "update"}
)
|> AshPhoenix.Form.submit!(
params: %{"union" => %{"_union_type" => "predefined", "value" => "update2"}}
)
|> Map.get(:union)
end

test "simple unions with cutom value" do

Check failure on line 141 in test/auto_form_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci / mix test

test single unions simple unions with cutom value (AshPhoenix.AutoFormTest)
assert %Ash.Union{type: :custom, value: "update"} =
SimplePost
|> AshPhoenix.Form.for_create(:create,
domain: Domain,
forms: [
auto?: true
],
params: %{
"text" => "foobar"
}
)
|> AshPhoenix.Form.add_form(:union,
params: %{"_union_type" => "predefined"}
)
|> AshPhoenix.Form.submit!(
params: %{"union" => %{"_union_type" => "custom", "value" => "update"}}
)
|> Map.get(:union)
end

test "simple unions with invalid values" do

Check failure on line 162 in test/auto_form_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci / mix test

test single unions simple unions with invalid values (AshPhoenix.AutoFormTest)
assert_raise Ash.Error.Invalid, ~r/atom must be one of "update", got: :create/, fn ->
SimplePost
|> AshPhoenix.Form.for_create(:create,
domain: Domain,
forms: [
auto?: true
],
params: %{
"text" => "foobar"
}
)
|> AshPhoenix.Form.add_form(:union,
params: %{"_union_type" => "predefined", "value" => "create"}
)
|> AshPhoenix.Form.submit!()
end
assert_raise Ash.Error.Invalid,
~r/atom must be one of "update, update2", got: :create/,
fn ->
SimplePost
|> AshPhoenix.Form.for_create(:create,
domain: Domain,
forms: [
auto?: true
],
params: %{
"text" => "foobar"
}
)
|> AshPhoenix.Form.add_form(:union,
params: %{"_union_type" => "predefined", "value" => "create"}
)
|> AshPhoenix.Form.submit!()
end
end

test "deeply nested unions" do
Expand Down
2 changes: 1 addition & 1 deletion test/support/resources/simple_post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule AshPhoenix.Test.SimplePost.SimpleUnion do
],
predefined: [
type: :atom,
constraints: [one_of: [:update]]
constraints: [one_of: [:update, :update2]]
]
]
]
Expand Down

0 comments on commit 8f9209e

Please sign in to comment.