mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
test: reproduce bug casting embedded union arguments (#803)
This commit is contained in:
parent
cfed7c3738
commit
014621cc09
1 changed files with 43 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue