test_case for nested union types (#199)

This commit is contained in:
Roberts Guļāns 2024-06-04 06:23:44 +03:00 committed by GitHub
parent acc376bf5f
commit ac622715bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 83 additions and 0 deletions

View file

@ -97,6 +97,38 @@ defmodule AshPhoenix.AutoFormTest do
|> AshPhoenix.Form.add_form(:union, params: %{"_union_type" => "bar", "value" => 10})
|> AshPhoenix.Form.submit!()
end
test "my test" do
AshPhoenix.Test.DeepNestedUnionResource
|> AshPhoenix.Form.for_create(:create,
domain: Domain,
forms: [
auto?: true
]
)
|> AshPhoenix.Form.add_form(:items,
params: %{"subject" => %{"_union_type" => "predefined"}}
)
# |> AshPhoenix.Form.add_form(:union_array)
|> AshPhoenix.Form.submit!(
params: %{
"items" => %{
"0" => %{
"_form_type" => "create",
"_persistent_id" => "0",
"_touched" => "_form_type,_persistent_id,_touched,subject",
"subject" => %{
"_form_type" => "create",
"_persistent_id" => "0",
"_touched" => "_form_type,_persistent_id,_touched,_union_type,value",
"_union_type" => "predefined",
"value" => "test"
}
}
}
}
)
end
end
describe "list unions" do

View file

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

View file

@ -0,0 +1,50 @@
defmodule DeepNestedUnionResource.Union do
use Ash.Type.NewType,
subtype_of: :union,
constraints: [
types: [
predefined: [
type: :atom,
constraints: [one_of: [:update]]
],
custom: [
type: :string
]
]
]
end
defmodule DeepNestedUnionResource.Wrapper do
use Ash.Resource,
data_layer: :embedded
attributes do
attribute :subject, DeepNestedUnionResource.Union,
allow_nil?: false,
public?: true
end
end
defmodule AshPhoenix.Test.DeepNestedUnionResource 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(:items, {:array, DeepNestedUnionResource.Wrapper}, public?: true)
end
actions do
defaults [
:read,
:destroy,
create: [:items],
update: [:items]
]
end
end