test: unions test case for primitive subtype (#203)

This commit is contained in:
Roberts Guļāns 2024-06-06 20:12:39 +03:00 committed by GitHub
parent 1eba24b5d5
commit 2a74d91618
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 58 additions and 1 deletions

View file

@ -2,7 +2,7 @@ defmodule AshPhoenix.AutoFormTest do
use ExUnit.Case
alias AshPhoenix.Form.Auto
alias AshPhoenix.Test.{Domain, Post}
alias AshPhoenix.Test.{Domain, Post, SimplePost}
import AshPhoenix.Form, only: [update_opts: 2]
defp form_for(a, _b), do: Phoenix.HTML.FormData.to_form(a, [])
@ -98,6 +98,21 @@ defmodule AshPhoenix.AutoFormTest do
|> AshPhoenix.Form.submit!()
end
test "simple unions" do
SimplePost
|> AshPhoenix.Form.for_create(:create,
domain: Domain,
forms: [
auto?: true
],
params: %{
"text" => "foobar"
}
)
|> AshPhoenix.Form.add_form(:union, params: %{"type" => "custom"})
|> AshPhoenix.Form.submit!()
end
test "deeply nested unions" do
# AshPhoenix.Test.DeepNestedUnionResource
# |> AshPhoenix.Form.for_create(:create,

View file

@ -11,5 +11,6 @@ defmodule AshPhoenix.Test.Domain do
resource(AshPhoenix.Test.PostWithDefault)
resource(AshPhoenix.Test.User)
resource(AshPhoenix.Test.DeepNestedUnionResource)
resource(AshPhoenix.Test.SimplePost)
end
end

View file

@ -0,0 +1,41 @@
defmodule AshPhoenix.Test.SimplePost.SimpleUnion do
use Ash.Type.NewType,
subtype_of: :union,
constraints: [
types: [
predefined: [
type: :atom,
constraints: [one_of: [:update]],
tag: :type,
tag_value: :predefined,
cast_tag?: true
],
custom: [
type: :string,
tag: :type,
tag_value: :custom,
cast_tag?: true
]
]
]
end
defmodule AshPhoenix.Test.SimplePost do
use Ash.Resource,
domain: AshPhoenix.Test.Domain,
data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
attributes do
uuid_primary_key(:id, public?: true)
attribute(:union, AshPhoenix.Test.SimplePost.SimpleUnion, public?: true)
end
actions do
default_accept(:*)
defaults([:create])
end
end