2022-05-29 13:21:14 +12:00
|
|
|
defmodule Ash.Test.Resource.Changes.RelateActorTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
|
|
|
|
2022-05-29 13:21:14 +12:00
|
|
|
defmodule Author do
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:06:40 +13:00
|
|
|
domain: Domain,
|
2022-05-29 13:21:14 +12:00
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, create: :*]
|
2023-04-03 13:50:32 +12:00
|
|
|
|
|
|
|
create :create_with_account do
|
|
|
|
argument :account, :map
|
|
|
|
change manage_relationship(:account, type: :append_and_remove)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :account, Ash.Test.Resource.Changes.RelateActorTest.Account do
|
|
|
|
public?(true)
|
|
|
|
end
|
2023-04-03 13:50:32 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Account do
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:06:40 +13:00
|
|
|
domain: Domain,
|
2023-04-03 13:50:32 +12:00
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, create: :*]
|
2022-05-29 13:21:14 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Post do
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:06:40 +13:00
|
|
|
domain: Domain,
|
2022-05-29 13:21:14 +12:00
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :text, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2022-05-29 13:21:14 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2023-04-03 13:50:32 +12:00
|
|
|
belongs_to :author, Ash.Test.Resource.Changes.RelateActorTest.Author do
|
2024-03-28 09:06:40 +13:00
|
|
|
public?(true)
|
2023-04-03 13:50:32 +12:00
|
|
|
allow_nil? true
|
|
|
|
end
|
|
|
|
|
|
|
|
belongs_to :account, Ash.Test.Resource.Changes.RelateActorTest.Account do
|
2024-03-28 09:06:40 +13:00
|
|
|
public?(true)
|
2022-09-14 05:52:11 +12:00
|
|
|
allow_nil? true
|
2022-05-29 13:21:14 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
|
2022-05-29 13:21:14 +12:00
|
|
|
create :create_with_actor do
|
|
|
|
change relate_actor(:author)
|
|
|
|
end
|
|
|
|
|
2023-04-03 13:50:32 +12:00
|
|
|
create :create_with_actor_field do
|
|
|
|
change relate_actor(:account, field: :account)
|
|
|
|
end
|
|
|
|
|
2022-05-29 13:21:14 +12:00
|
|
|
create :create_possibly_without_actor do
|
|
|
|
change relate_actor(:author, allow_nil?: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "relate_actor change with defaults work" do
|
|
|
|
actor =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2022-05-29 13:21:14 +12:00
|
|
|
|
|
|
|
params = [text: "foo"]
|
|
|
|
|
|
|
|
post_with =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create_with_actor, params, actor: actor)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2022-05-29 13:21:14 +12:00
|
|
|
|
|
|
|
assert post_with.author_id == actor.id
|
|
|
|
|
|
|
|
{:error, changeset} =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create_with_actor, params, actor: nil)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create()
|
2022-05-29 13:21:14 +12:00
|
|
|
|
|
|
|
assert changeset.errors |> Enum.count() == 1
|
|
|
|
end
|
|
|
|
|
2023-04-03 13:50:32 +12:00
|
|
|
test "relate_actor change with field" do
|
|
|
|
account =
|
|
|
|
Account
|
|
|
|
|> Ash.Changeset.for_create(:create, %{})
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2023-04-03 13:50:32 +12:00
|
|
|
|
|
|
|
actor =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create_with_account, %{account: account})
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2023-04-03 13:50:32 +12:00
|
|
|
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create_with_actor_field, %{text: "foo"}, actor: actor)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2023-04-03 13:50:32 +12:00
|
|
|
|
|
|
|
assert post.account_id == account.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "relate_actor change with field when field is nil" do
|
|
|
|
actor =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
|
|
|
|> Ash.load!(:account)
|
2023-04-03 13:50:32 +12:00
|
|
|
|
2024-01-28 03:53:03 +13:00
|
|
|
{:error, changeset} =
|
2023-04-03 13:50:32 +12:00
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create_with_actor_field, %{text: "foo"}, actor: actor)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create()
|
2023-04-03 13:50:32 +12:00
|
|
|
|
2024-01-28 03:53:03 +13:00
|
|
|
assert changeset.errors |> Enum.count() == 1
|
2023-04-03 13:50:32 +12:00
|
|
|
end
|
|
|
|
|
2022-05-29 13:21:14 +12:00
|
|
|
test "relate_actor change with `allow_nil?: true` allows both nil and an actor" do
|
|
|
|
actor =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2022-05-29 13:21:14 +12:00
|
|
|
|
|
|
|
params = [text: "foo"]
|
|
|
|
|
|
|
|
post_with =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create_possibly_without_actor, params, actor: actor)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2022-05-29 13:21:14 +12:00
|
|
|
|
|
|
|
assert post_with.author_id == actor.id
|
|
|
|
|
|
|
|
post_without =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create_possibly_without_actor, params, actor: nil)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2022-05-29 13:21:14 +12:00
|
|
|
|
|
|
|
assert is_nil(post_without.author_id)
|
|
|
|
end
|
|
|
|
end
|