Skip to content

Commit

Permalink
test: reproduce bug casting embedded union arguments (#803)
Browse files Browse the repository at this point in the history
  • Loading branch information
rgraff authored Dec 14, 2023
1 parent cfed7c3 commit 014621c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions test/type/union_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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

Check failure on line 356 in test/type/union_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci / mix test

test it should cast union arguments appropriately (Ash.Test.Type.UnionTest)
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

0 comments on commit 014621c

Please sign in to comment.