test: reproduce bug casting embedded union arguments (#803)

This commit is contained in:
Robert Graff 2023-12-14 06:04:56 -08:00 committed by GitHub
parent cfed7c3738
commit 014621cc09
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -28,6 +28,25 @@ defmodule Ash.Test.Type.UnionTest do
end
end
defmodule FooBarUnion do
use Ash.Type.NewType,
subtype_of: :union,
constraints: [
types: [
foo: [
type: Foo,
tag: :type,
tag_value: :foo
],
bar: [
type: Bar,
tag: :type,
tag_value: :bar
]
]
]
end
defmodule Example do
use Ash.Resource, data_layer: Ash.DataLayer.Ets
@ -37,6 +56,21 @@ defmodule Ash.Test.Type.UnionTest do
actions do
defaults [:create, :read, :update, :destroy]
update :add_thing do
argument :new_thing, FooBarUnion, allow_nil?: false
change fn changeset, _ ->
new_thing = Ash.Changeset.get_argument(changeset, :new_thing)
things = Ash.Changeset.get_attribute(changeset, :things)
Ash.Changeset.change_attribute(
changeset,
:things,
things ++ [new_thing]
)
end
end
end
attributes do
@ -318,4 +352,13 @@ defmodule Ash.Test.Type.UnionTest do
assert Ash.Type.dump_to_native(Ash.Type.Union, union, constraints) ==
{:ok, %{type: :foo, bar: 1}}
end
test "it should cast union arguments appropriately" do
assert {:ok, _} =
Example
|> Ash.Changeset.for_create(:create, %{things: []})
|> Ash.Test.AnyApi.create!()
|> Ash.Changeset.for_update(:add_thing, %{new_thing: %{type: :foo, foo: "foo"}})
|> Ash.Test.AnyApi.update()
end
end