From ac622715bb9db7ecc34e42c20e9182125fb28ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roberts=20Gu=C4=BC=C4=81ns?= Date: Tue, 4 Jun 2024 06:23:44 +0300 Subject: [PATCH] test_case for nested union types (#199) --- test/auto_form_test.exs | 32 ++++++++++++ test/support/domain.ex | 1 + .../resources/deep_nested_union_resource.ex | 50 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 test/support/resources/deep_nested_union_resource.ex diff --git a/test/auto_form_test.exs b/test/auto_form_test.exs index a9f9e21..76cc524 100644 --- a/test/auto_form_test.exs +++ b/test/auto_form_test.exs @@ -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 diff --git a/test/support/domain.ex b/test/support/domain.ex index 4c4a976..92c4e79 100644 --- a/test/support/domain.ex +++ b/test/support/domain.ex @@ -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 diff --git a/test/support/resources/deep_nested_union_resource.ex b/test/support/resources/deep_nested_union_resource.ex new file mode 100644 index 0000000..becbb23 --- /dev/null +++ b/test/support/resources/deep_nested_union_resource.ex @@ -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