2019-12-24 07:17:22 +13:00
|
|
|
defmodule Ash.Test.Actions.UpdateTest do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-02-09 09:17:07 +13:00
|
|
|
require Ash.Flags
|
2019-12-24 07:17:22 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2022-06-22 13:00:47 +12:00
|
|
|
import Ash.Test
|
2021-08-05 16:46:41 +12:00
|
|
|
require Ash.Query
|
2023-08-29 08:05:26 +12:00
|
|
|
require Ash.Expr
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
2021-04-19 11:41:49 +12:00
|
|
|
|
2020-06-22 15:26:47 +12:00
|
|
|
defmodule Authorized do
|
2021-04-19 11:41:49 +12:00
|
|
|
@moduledoc false
|
2020-06-22 15:26:47 +12:00
|
|
|
use Ash.Resource,
|
2024-03-28 09:06:40 +13:00
|
|
|
domain: Domain,
|
2020-06-22 15:26:47 +12:00
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
authorizers: [Ash.Test.Authorizer]
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :name, :string, public?: true
|
2020-06-22 15:26:47 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2020-06-22 15:26:47 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 07:17:22 +13:00
|
|
|
defmodule Profile do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2020-06-14 18:39:11 +12:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2021-03-14 03:25:13 +13:00
|
|
|
|
|
|
|
update :set_private_attribute_to_nil do
|
2024-03-28 09:06:40 +13:00
|
|
|
accept []
|
2021-03-14 03:25:13 +13:00
|
|
|
change set_attribute(:non_nil_private, nil)
|
|
|
|
end
|
2021-06-26 05:51:17 +12:00
|
|
|
|
|
|
|
update :set_private_attribute_from_arg do
|
|
|
|
argument :private, :string
|
2022-10-31 19:25:01 +13:00
|
|
|
change set_attribute(:private, arg(:private))
|
2021-06-26 05:51:17 +12:00
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :bio, :string, allow_nil?: false, public?: true
|
|
|
|
attribute :non_nil_private, :string, allow_nil?: false, default: "non_nil", public?: true
|
|
|
|
attribute :private, :string, default: "non_nil", public?: true
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :author, Ash.Test.Actions.UpdateTest.Author do
|
|
|
|
public?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-30 19:15:16 +12:00
|
|
|
defmodule DuplicateName do
|
2021-04-19 11:41:49 +12:00
|
|
|
@moduledoc false
|
2020-08-30 19:15:16 +12:00
|
|
|
use Ash.Resource.Change
|
|
|
|
|
|
|
|
def change(changeset, _, _) do
|
|
|
|
case Ash.Changeset.fetch_change(changeset, :name) do
|
|
|
|
:error -> changeset
|
|
|
|
{:ok, name} -> Ash.Changeset.change_attribute(changeset, :name, name <> name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-10 08:56:44 +12:00
|
|
|
defmodule ManualUpdateAuthor do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource.Change
|
|
|
|
|
|
|
|
def change(changeset, _, _) do
|
|
|
|
Ash.Changeset.after_action(changeset, fn _changeset, data ->
|
|
|
|
{:ok,
|
|
|
|
data
|
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.change_attribute(:name, "manual")
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()}
|
2021-05-10 08:56:44 +12:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 07:17:22 +13:00
|
|
|
defmodule Author do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2020-06-14 18:39:11 +12:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
2024-04-26 14:18:38 +12:00
|
|
|
defaults [:read, :destroy, create: :*, update: [:name, :score]]
|
2020-08-28 18:58:03 +12:00
|
|
|
|
|
|
|
update :only_allow_name do
|
|
|
|
accept([:name])
|
|
|
|
end
|
2020-08-30 19:15:16 +12:00
|
|
|
|
2023-12-17 04:14:02 +13:00
|
|
|
update :with_validation do
|
|
|
|
accept([:name])
|
|
|
|
|
|
|
|
validate attribute_equals(:name, "fred")
|
2024-01-04 10:48:15 +13:00
|
|
|
validate compare(:score, greater_than_or_equal_to: 0, less_than_or_equal_to: 10)
|
2023-12-17 04:14:02 +13:00
|
|
|
end
|
|
|
|
|
2024-05-17 08:49:08 +12:00
|
|
|
update :with_partially_atomic_validation do
|
|
|
|
accept([:name])
|
|
|
|
|
|
|
|
argument :match?, :boolean do
|
|
|
|
allow_nil? false
|
|
|
|
end
|
|
|
|
|
|
|
|
validate match(:name, ~r/[a-z]+/) do
|
|
|
|
where argument_equals(:match?, true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-30 19:15:16 +12:00
|
|
|
update :duplicate_name do
|
2024-04-26 14:18:38 +12:00
|
|
|
require_atomic? false
|
2020-08-30 19:15:16 +12:00
|
|
|
change {DuplicateName, []}
|
|
|
|
end
|
2021-05-10 08:56:44 +12:00
|
|
|
|
2022-10-31 02:49:49 +13:00
|
|
|
update :manual_update do
|
|
|
|
accept []
|
|
|
|
|
|
|
|
manual fn changeset, _ ->
|
|
|
|
{:ok,
|
|
|
|
changeset.data
|
|
|
|
|> Ash.Changeset.for_update(:update, changeset.attributes)
|
2022-11-23 21:36:55 +13:00
|
|
|
|> Ash.Changeset.force_change_attribute(:name, "manual")
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()}
|
2022-10-31 02:49:49 +13:00
|
|
|
end
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :name, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
attribute :bio, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
attribute :score, :integer do
|
|
|
|
public?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_one :profile, Profile, destination_attribute: :author_id, public?: true
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
has_many :posts, Ash.Test.Actions.UpdateTest.Post,
|
|
|
|
destination_attribute: :author_id,
|
|
|
|
public?: true
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 09:51:01 +13:00
|
|
|
defmodule PostLink do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2020-06-14 18:39:11 +12:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-17 11:17:01 +12:00
|
|
|
attributes do
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :type, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2020-07-17 11:17:01 +12:00
|
|
|
end
|
|
|
|
|
2019-12-24 09:51:01 +13:00
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2021-01-13 09:40:55 +13:00
|
|
|
belongs_to :source_post, Ash.Test.Actions.UpdateTest.Post,
|
|
|
|
primary_key?: true,
|
2024-03-28 09:06:40 +13:00
|
|
|
allow_nil?: false,
|
|
|
|
public?: true
|
2021-01-13 09:40:55 +13:00
|
|
|
|
|
|
|
belongs_to :destination_post, Ash.Test.Actions.UpdateTest.Post,
|
|
|
|
primary_key?: true,
|
2024-03-28 09:06:40 +13:00
|
|
|
allow_nil?: false,
|
|
|
|
public?: true
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Post do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2020-06-14 18:39:11 +12:00
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:read, :destroy, create: :*, update: :*]
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :title, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
attribute :contents, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :author, Author, public?: true
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
many_to_many :related_posts, __MODULE__,
|
|
|
|
through: PostLink,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute_on_join_resource: :source_post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination_attribute_on_join_resource: :destination_post_id,
|
|
|
|
public?: true
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-05 06:55:05 +12:00
|
|
|
defmodule PaginatedPrimaryRead do
|
|
|
|
use Ash.Resource,
|
2024-03-28 09:06:40 +13:00
|
|
|
domain: Domain,
|
2022-04-05 06:55:05 +12:00
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private? true
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
defaults [:destroy, create: :*, update: :*]
|
2022-04-05 06:55:05 +12:00
|
|
|
|
|
|
|
read :read do
|
2022-04-29 10:07:06 +12:00
|
|
|
primary? true
|
2022-04-05 06:55:05 +12:00
|
|
|
pagination offset?: true, required?: true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 07:17:22 +13:00
|
|
|
describe "simple updates" do
|
|
|
|
test "allows updating a record with valid attributes" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foo", contents: "bar"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
assert %Post{title: "bar", contents: "foo"} =
|
2024-03-28 09:06:40 +13:00
|
|
|
post
|
|
|
|
|> Ash.Changeset.for_update(:update, %{title: "bar", contents: "foo"})
|
|
|
|
|> Ash.update!()
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
2020-08-28 18:58:03 +12:00
|
|
|
end
|
|
|
|
|
2021-05-10 08:56:44 +12:00
|
|
|
describe "manual updates" do
|
2023-09-26 02:40:29 +13:00
|
|
|
test "the update occurs properly" do
|
2021-05-10 08:56:44 +12:00
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "auto"})
|
|
|
|
|> Ash.create!()
|
2021-05-10 08:56:44 +12:00
|
|
|
|
2023-12-17 04:14:02 +13:00
|
|
|
assert %Author{name: "manual"} =
|
2024-03-28 09:06:40 +13:00
|
|
|
author
|
|
|
|
|> Ash.Changeset.for_update(:update)
|
|
|
|
|> Ash.update!(action: :manual_update)
|
2021-05-10 08:56:44 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-14 03:25:13 +13:00
|
|
|
describe "allow_nil?" do
|
|
|
|
test "it does not allow updating a value to `nil` when `allow_nil?: false`" do
|
|
|
|
profile =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "foobar"})
|
|
|
|
|> Ash.create!()
|
2021-03-14 03:25:13 +13:00
|
|
|
|
|
|
|
assert_raise Ash.Error.Invalid, ~r/attribute bio is required/, fn ->
|
2024-03-28 09:06:40 +13:00
|
|
|
profile |> Ash.Changeset.for_update(:update, %{bio: ""}) |> Ash.update!()
|
2021-03-14 03:25:13 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not allow updating a private attribute's value to `nil` when `allow_nil?: false`" do
|
|
|
|
profile =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "foobar"})
|
|
|
|
|> Ash.create!()
|
2021-03-14 03:25:13 +13:00
|
|
|
|
|
|
|
assert_raise Ash.Error.Invalid, ~r/attribute non_nil_private is required/, fn ->
|
2023-12-17 04:14:02 +13:00
|
|
|
profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_update(:update, %{bio: "foobar"})
|
|
|
|
|> Ash.update!(action: :set_private_attribute_to_nil)
|
2021-03-14 03:25:13 +13:00
|
|
|
end
|
|
|
|
end
|
2021-06-26 05:51:17 +12:00
|
|
|
|
|
|
|
test "it passes through an argument's value" do
|
|
|
|
profile =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "foobar"})
|
|
|
|
|> Ash.create!()
|
2021-06-26 05:51:17 +12:00
|
|
|
|
|
|
|
profile =
|
|
|
|
profile
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.for_update(:set_private_attribute_from_arg, %{private: "blah"})
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2021-06-26 05:51:17 +12:00
|
|
|
|
|
|
|
assert profile.private == "blah"
|
|
|
|
end
|
2021-03-14 03:25:13 +13:00
|
|
|
end
|
|
|
|
|
2021-03-08 08:09:51 +13:00
|
|
|
describe "select" do
|
|
|
|
test "allows selecting fields on the changeset" do
|
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foo", contents: "bar"})
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
assert %Post{title: "bar", contents: %Ash.NotLoaded{}} =
|
|
|
|
post
|
|
|
|
|> Ash.Changeset.for_update(:update, %{title: "bar", contents: "foo"})
|
|
|
|
|> Ash.Changeset.select(:title)
|
|
|
|
|> Ash.update!()
|
2021-03-08 08:09:51 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-11 02:46:49 +12:00
|
|
|
describe "load" do
|
|
|
|
test "allows loading has_many relationship on the changeset" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Name"})
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
for n <- [2, 1] do
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "Post #{n}"})
|
|
|
|
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|
|
|
|
|> Ash.create!()
|
|
|
|
end
|
|
|
|
|
|
|
|
load_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|
|
|
|
author =
|
|
|
|
author
|
|
|
|
|> Ash.Changeset.for_update(:update, %{name: "Updated Name"})
|
|
|
|
|> Ash.Changeset.load(posts: load_query)
|
|
|
|
|> Ash.update!()
|
|
|
|
|
|
|
|
assert [%Post{title: "Post 1"}, %Post{title: "Post 2"}] = author.posts
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows loading has_many relationship on the action options" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Name"})
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
for n <- [2, 1] do
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "Post #{n}"})
|
|
|
|
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|
|
|
|
|> Ash.create!()
|
|
|
|
end
|
|
|
|
|
|
|
|
load_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|
|
|
|
author = Ash.update!(author, %{name: "Updated Name"}, load: [posts: load_query])
|
|
|
|
|
|
|
|
assert [%Post{title: "Post 1"}, %Post{title: "Post 2"}] = author.posts
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows loading paginated has_many relationship on the changeset" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Name"})
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
for n <- [2, 1] do
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "Post #{n}"})
|
|
|
|
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|
|
|
|
|> Ash.create!()
|
|
|
|
end
|
|
|
|
|
|
|
|
offset_pagination_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|> Ash.Query.page(count: true, limit: 1)
|
|
|
|
|
|
|
|
author =
|
|
|
|
author
|
|
|
|
|> Ash.Changeset.for_update(:update, %{name: "Updated Name 1"})
|
|
|
|
|> Ash.Changeset.load(posts: offset_pagination_query)
|
|
|
|
|> Ash.update!()
|
|
|
|
|
|
|
|
assert %Ash.Page.Offset{
|
|
|
|
results: [%Post{title: "Post 1", __metadata__: %{keyset: keyset}}],
|
|
|
|
limit: 1,
|
|
|
|
offset: 0,
|
|
|
|
count: 2,
|
|
|
|
more?: true
|
|
|
|
} = author.posts
|
|
|
|
|
|
|
|
keyset_pagination_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|> Ash.Query.page(count: true, limit: 1, after: keyset)
|
|
|
|
|
|
|
|
author =
|
|
|
|
author
|
|
|
|
|> Ash.Changeset.for_update(:update, %{name: "Updated Name 2"})
|
|
|
|
|> Ash.Changeset.load(posts: keyset_pagination_query)
|
|
|
|
|> Ash.update!()
|
|
|
|
|
|
|
|
assert %Ash.Page.Keyset{
|
|
|
|
results: [%Post{title: "Post 2"}],
|
|
|
|
limit: 1,
|
|
|
|
count: 2,
|
|
|
|
more?: false,
|
|
|
|
before: nil,
|
|
|
|
after: ^keyset
|
|
|
|
} = author.posts
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows loading paginated has_many relationship on the action options" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "Name"})
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
for n <- [2, 1] do
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "Post #{n}"})
|
|
|
|
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|
|
|
|
|> Ash.create!()
|
|
|
|
end
|
|
|
|
|
|
|
|
offset_pagination_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|> Ash.Query.page(count: true, limit: 1)
|
|
|
|
|
|
|
|
author =
|
|
|
|
Ash.update!(author, %{name: "Updated Name 1"}, load: [posts: offset_pagination_query])
|
|
|
|
|
|
|
|
assert %Ash.Page.Offset{
|
|
|
|
results: [%Post{title: "Post 1", __metadata__: %{keyset: keyset}}],
|
|
|
|
limit: 1,
|
|
|
|
offset: 0,
|
|
|
|
count: 2,
|
|
|
|
more?: true
|
|
|
|
} = author.posts
|
|
|
|
|
|
|
|
keyset_pagination_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|> Ash.Query.page(count: true, limit: 1, after: keyset)
|
|
|
|
|
|
|
|
author =
|
|
|
|
Ash.update!(author, %{name: "Updated Name 1"}, load: [posts: keyset_pagination_query])
|
|
|
|
|
|
|
|
assert %Ash.Page.Keyset{
|
|
|
|
results: [%Post{title: "Post 2"}],
|
|
|
|
limit: 1,
|
|
|
|
count: 2,
|
|
|
|
more?: false,
|
|
|
|
before: nil,
|
|
|
|
after: ^keyset
|
|
|
|
} = author.posts
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows loading many_to_many relationship on the changeset" do
|
|
|
|
related_post1 = Ash.create!(Post, %{title: "Related 1"})
|
|
|
|
related_post2 = Ash.create!(Post, %{title: "Related 2"})
|
|
|
|
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "Title"})
|
|
|
|
|> Ash.Changeset.manage_relationship(:related_posts, [related_post2, related_post1],
|
|
|
|
type: :append_and_remove
|
|
|
|
)
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
load_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|
|
|
|
post =
|
|
|
|
post
|
|
|
|
|> Ash.Changeset.for_update(:update, %{title: "Updated Title"})
|
|
|
|
|> Ash.Changeset.load(related_posts: load_query)
|
|
|
|
|> Ash.update!()
|
|
|
|
|
|
|
|
assert [%Post{title: "Related 1"}, %Post{title: "Related 2"}] = post.related_posts
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows loading many_to_many relationship on the action options" do
|
|
|
|
related_post1 = Ash.create!(Post, %{title: "Related 1"})
|
|
|
|
related_post2 = Ash.create!(Post, %{title: "Related 2"})
|
|
|
|
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "Title"})
|
|
|
|
|> Ash.Changeset.manage_relationship(:related_posts, [related_post2, related_post1],
|
|
|
|
type: :append_and_remove
|
|
|
|
)
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
load_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|
|
|
|
post = Ash.update!(post, %{title: "Updated Title"}, load: [related_posts: load_query])
|
|
|
|
|
|
|
|
assert [%Post{title: "Related 1"}, %Post{title: "Related 2"}] = post.related_posts
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows loading paginated many_to_many relationship on the changeset" do
|
|
|
|
related_post1 = Ash.create!(Post, %{title: "Related 1"})
|
|
|
|
related_post2 = Ash.create!(Post, %{title: "Related 2"})
|
|
|
|
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "Title"})
|
|
|
|
|> Ash.Changeset.manage_relationship(:related_posts, [related_post2, related_post1],
|
|
|
|
type: :append_and_remove
|
|
|
|
)
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
offset_pagination_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|> Ash.Query.page(count: true, limit: 1)
|
|
|
|
|
|
|
|
post =
|
|
|
|
post
|
|
|
|
|> Ash.Changeset.for_update(:update, %{title: "Updated Title 1"})
|
|
|
|
|> Ash.Changeset.load(related_posts: offset_pagination_query)
|
|
|
|
|> Ash.update!()
|
|
|
|
|
|
|
|
assert %Ash.Page.Offset{
|
|
|
|
results: [%Post{title: "Related 1", __metadata__: %{keyset: keyset}}],
|
|
|
|
limit: 1,
|
|
|
|
offset: 0,
|
|
|
|
count: 2,
|
|
|
|
more?: true
|
|
|
|
} = post.related_posts
|
|
|
|
|
|
|
|
keyset_pagination_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|> Ash.Query.page(count: true, limit: 1, after: keyset)
|
|
|
|
|
|
|
|
post =
|
|
|
|
post
|
|
|
|
|> Ash.Changeset.for_update(:update, %{title: "Updated Title 2"})
|
|
|
|
|> Ash.Changeset.load(related_posts: keyset_pagination_query)
|
|
|
|
|> Ash.update!()
|
|
|
|
|
|
|
|
assert %Ash.Page.Keyset{
|
|
|
|
results: [%Post{title: "Related 2"}],
|
|
|
|
limit: 1,
|
|
|
|
count: 2,
|
|
|
|
more?: false,
|
|
|
|
before: nil,
|
|
|
|
after: ^keyset
|
|
|
|
} = post.related_posts
|
|
|
|
end
|
|
|
|
|
|
|
|
test "allows loading paginated many_to_many relationship on the action options" do
|
|
|
|
related_post1 = Ash.create!(Post, %{title: "Related 1"})
|
|
|
|
related_post2 = Ash.create!(Post, %{title: "Related 2"})
|
|
|
|
|
|
|
|
post =
|
|
|
|
Post
|
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "Title"})
|
|
|
|
|> Ash.Changeset.manage_relationship(:related_posts, [related_post2, related_post1],
|
|
|
|
type: :append_and_remove
|
|
|
|
)
|
|
|
|
|> Ash.create!()
|
|
|
|
|
|
|
|
offset_pagination_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|> Ash.Query.page(count: true, limit: 1)
|
|
|
|
|
|
|
|
post =
|
|
|
|
Ash.update!(post, %{title: "Updated Title 1"},
|
|
|
|
load: [related_posts: offset_pagination_query]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert %Ash.Page.Offset{
|
|
|
|
results: [%Post{title: "Related 1", __metadata__: %{keyset: keyset}}],
|
|
|
|
limit: 1,
|
|
|
|
offset: 0,
|
|
|
|
count: 2,
|
|
|
|
more?: true
|
|
|
|
} = post.related_posts
|
|
|
|
|
|
|
|
keyset_pagination_query =
|
|
|
|
Post
|
|
|
|
|> Ash.Query.sort(title: :asc)
|
|
|
|
|> Ash.Query.select([:title])
|
|
|
|
|> Ash.Query.page(count: true, limit: 1, after: keyset)
|
|
|
|
|
|
|
|
post =
|
|
|
|
Ash.update!(post, %{title: "Updated Title 2"},
|
|
|
|
load: [related_posts: keyset_pagination_query]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert %Ash.Page.Keyset{
|
|
|
|
results: [%Post{title: "Related 2"}],
|
|
|
|
limit: 1,
|
|
|
|
count: 2,
|
|
|
|
more?: false,
|
|
|
|
before: nil,
|
|
|
|
after: ^keyset
|
|
|
|
} = post.related_posts
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-28 18:58:03 +12:00
|
|
|
describe "allow" do
|
|
|
|
test "allows attributes in the list" do
|
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "fred"})
|
|
|
|
|> Ash.create!()
|
2020-08-28 18:58:03 +12:00
|
|
|
|
|
|
|
author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_update(:update, %{name: "joe"})
|
|
|
|
|> Ash.update!(action: :only_allow_name)
|
2020-08-28 18:58:03 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
test "does not allow attributes in the list" do
|
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "fred"})
|
|
|
|
|> Ash.create!()
|
2020-08-28 18:58:03 +12:00
|
|
|
|
2024-04-26 14:18:38 +12:00
|
|
|
assert_raise Ash.Error.Invalid, ~r/No such input `bio`/, fn ->
|
2020-08-28 18:58:03 +12:00
|
|
|
author
|
2024-04-26 14:18:38 +12:00
|
|
|
|> Ash.Changeset.for_update(:only_allow_name, %{bio: "bio"})
|
|
|
|
|> Ash.update!()
|
2020-08-28 18:58:03 +12:00
|
|
|
end
|
|
|
|
end
|
2020-08-30 19:15:16 +12:00
|
|
|
end
|
|
|
|
|
2023-08-29 08:05:26 +12:00
|
|
|
describe "atomics" do
|
|
|
|
test "atomics can be added to a changeset" do
|
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "fred"})
|
|
|
|
|> Ash.create!()
|
2023-08-29 08:05:26 +12:00
|
|
|
|
|
|
|
author =
|
|
|
|
author
|
|
|
|
|> Ash.Changeset.for_update(:only_allow_name)
|
2023-08-30 06:39:38 +12:00
|
|
|
|> Ash.Changeset.atomic_update(:name, Ash.Expr.expr(name <> " weasley"))
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2023-08-29 08:05:26 +12:00
|
|
|
|
|
|
|
assert author.name == "fred weasley"
|
|
|
|
end
|
2023-12-17 04:14:02 +13:00
|
|
|
|
|
|
|
test "a changeset can be fully atomic" do
|
|
|
|
changeset =
|
2024-01-24 15:36:17 +13:00
|
|
|
Ash.Changeset.fully_atomic_changeset(Author, :with_validation, %{name: "fred weasly"},
|
|
|
|
eager?: false
|
|
|
|
)
|
2023-12-17 04:14:02 +13:00
|
|
|
|
|
|
|
assert changeset.valid?
|
|
|
|
end
|
2024-05-17 08:49:08 +12:00
|
|
|
|
|
|
|
test "where condition is considered before checking validation atomicity" do
|
|
|
|
changeset =
|
|
|
|
Ash.Changeset.fully_atomic_changeset(
|
|
|
|
Author,
|
|
|
|
:with_partially_atomic_validation,
|
|
|
|
%{match?: false},
|
|
|
|
eager?: false
|
|
|
|
)
|
|
|
|
|
|
|
|
assert changeset.valid?
|
|
|
|
|
|
|
|
# match validation can't be run atomically if the attribute is not changing
|
|
|
|
assert {:not_atomic, _} =
|
|
|
|
Ash.Changeset.fully_atomic_changeset(
|
|
|
|
Author,
|
|
|
|
:with_partially_atomic_validation,
|
|
|
|
%{match?: true},
|
|
|
|
eager?: false
|
|
|
|
)
|
|
|
|
end
|
2023-08-29 08:05:26 +12:00
|
|
|
end
|
|
|
|
|
2020-08-30 19:15:16 +12:00
|
|
|
describe "changeset" do
|
|
|
|
test "changes are run properly" do
|
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "fred"})
|
|
|
|
|> Ash.create!()
|
2020-08-30 19:15:16 +12:00
|
|
|
|
|
|
|
author =
|
|
|
|
author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_update(:duplicate_name, %{name: "joe"})
|
|
|
|
|> Ash.update!()
|
2020-08-30 19:15:16 +12:00
|
|
|
|
|
|
|
assert author.name == "joejoe"
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
2019-12-24 09:51:01 +13:00
|
|
|
describe "updating many to many relationships" do
|
|
|
|
test "allows updating with a many_to_many relationship" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title2"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
post3 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title3"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:related_posts, [post2, post3],
|
|
|
|
type: :append_and_remove
|
|
|
|
)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
|
|
|
|
2021-08-05 16:46:41 +12:00
|
|
|
test "allows directly managing a many_to_many relationship" do
|
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:related_posts, [%{title: "title0"}],
|
|
|
|
type: :direct_control
|
|
|
|
)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2021-08-05 16:46:41 +12:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
other_post = Post |> Ash.Query.filter(title == "title0") |> Ash.read_one!()
|
2021-08-05 16:46:41 +12:00
|
|
|
|
|
|
|
post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(
|
2021-08-05 16:46:41 +12:00
|
|
|
:related_posts,
|
|
|
|
[%{title: "title3", id: other_post.id}, %{title: "title1"}],
|
|
|
|
type: :direct_control
|
|
|
|
)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2021-08-05 16:46:41 +12:00
|
|
|
|
|
|
|
assert ["title", "title1", "title3"] =
|
2024-03-28 09:06:40 +13:00
|
|
|
Post |> Ash.Query.sort(:title) |> Ash.read!() |> Enum.map(& &1.title)
|
2021-08-05 16:46:41 +12:00
|
|
|
end
|
|
|
|
|
2022-08-31 10:45:55 +12:00
|
|
|
test "it updates the join resource properly" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title2"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post3 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title3"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:related_posts, [post2, post3],
|
|
|
|
type: :append_and_remove
|
|
|
|
)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert [_, _] = Ash.read!(PostLink)
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it responds with the relationship filled in" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title2"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post3 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title3"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
new_post =
|
2022-09-20 07:44:06 +12:00
|
|
|
post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:related_posts, [post2, post3],
|
|
|
|
type: :append_and_remove
|
|
|
|
)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2022-06-22 13:00:47 +12:00
|
|
|
assert Enum.sort(strip_metadata(new_post.related_posts)) ==
|
2020-01-15 08:00:38 +13:00
|
|
|
Enum.sort([
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.get!(Post, post2.id),
|
|
|
|
Ash.get!(Post, post3.id)
|
2020-01-15 08:00:38 +13:00
|
|
|
])
|
2022-06-22 13:00:47 +12:00
|
|
|
|> strip_metadata()
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
2020-07-17 11:17:01 +12:00
|
|
|
|
|
|
|
test "it updates any join fields" do
|
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title"})
|
|
|
|
|> Ash.create!()
|
2020-07-17 11:17:01 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title2"})
|
|
|
|
|> Ash.create!()
|
2020-07-17 11:17:01 +12:00
|
|
|
|
|
|
|
post3 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "title3"})
|
|
|
|
|> Ash.create!()
|
2020-07-17 11:17:01 +12:00
|
|
|
|
|
|
|
new_post =
|
|
|
|
post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(
|
2022-09-20 07:44:06 +12:00
|
|
|
:related_posts,
|
|
|
|
[
|
|
|
|
Ash.Resource.set_metadata(post2, %{join_keys: %{type: "a"}}),
|
|
|
|
Ash.Resource.set_metadata(post3, %{join_keys: %{type: "b"}})
|
|
|
|
],
|
|
|
|
type: :append_and_remove
|
|
|
|
)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
|
|
|
|> Ash.load!(:related_posts_join_assoc)
|
2020-07-17 11:17:01 +12:00
|
|
|
|
|
|
|
types = Enum.sort(Enum.map(new_post.related_posts_join_assoc, &Map.get(&1, :type)))
|
|
|
|
|
|
|
|
assert types == ["a", "b"]
|
|
|
|
|
|
|
|
new_post =
|
|
|
|
new_post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(
|
2021-02-23 14:29:31 +13:00
|
|
|
:related_posts,
|
|
|
|
[
|
2022-08-16 08:02:22 +12:00
|
|
|
Ash.Resource.set_metadata(post2, %{join_keys: %{type: "c"}}),
|
|
|
|
Ash.Resource.set_metadata(post3, %{join_keys: %{type: "d"}})
|
2021-02-23 14:29:31 +13:00
|
|
|
],
|
2022-09-20 07:44:06 +12:00
|
|
|
type: :append_and_remove,
|
2021-02-23 14:29:31 +13:00
|
|
|
on_match: :update,
|
|
|
|
on_lookup: :relate
|
|
|
|
)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
|
|
|
|> Ash.load!(:related_posts_join_assoc)
|
2020-07-17 11:17:01 +12:00
|
|
|
|
|
|
|
types = Enum.sort(Enum.map(new_post.related_posts_join_assoc, &Map.get(&1, :type)))
|
|
|
|
|
|
|
|
assert types == ["c", "d"]
|
|
|
|
end
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
|
|
|
|
2019-12-24 07:17:22 +13:00
|
|
|
describe "updating with has_one relationships" do
|
2019-12-24 09:51:01 +13:00
|
|
|
test "allows updating with has_one relationship" do
|
2020-07-12 18:25:53 +12:00
|
|
|
profile =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "best dude"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
profile2 =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "second best dude"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "fred"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:profile, profile, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
author
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:profile, profile2, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it sets the relationship on the destination record accordingly" do
|
2020-07-12 18:25:53 +12:00
|
|
|
profile =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "best dude"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
profile2 =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "second best dude"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "fred"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:profile, profile, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
author
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:profile, profile2, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert Ash.get!(Profile, profile.id).author_id == nil
|
|
|
|
assert Ash.get!(Profile, profile2.id).author_id == author.id
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it responds with the relationship filled in" do
|
2020-07-12 18:25:53 +12:00
|
|
|
profile =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "best dude"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
profile2 =
|
|
|
|
Profile
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{bio: "second best dude"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "fred"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:profile, profile, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
updated_author =
|
|
|
|
author
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:profile, profile2, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2021-05-11 10:03:36 +12:00
|
|
|
assert %{updated_author.profile | __metadata__: nil} == %{
|
|
|
|
profile2
|
|
|
|
| author_id: author.id,
|
|
|
|
__metadata__: nil
|
|
|
|
}
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "updating with a has_many relationship" do
|
|
|
|
test "allows updating with a has_many relationship" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "sup"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "sup2"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "foobar"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:posts, [post], type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
author
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:posts, [post, post2], type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
test "it sets the relationship on the destination records accordingly" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "sup"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "sup2"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "foobar"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:posts, [post], type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
author
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:posts, [post2.id], type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert Ash.get!(Post, post.id).author_id == nil
|
|
|
|
assert Ash.get!(Post, post2.id).author_id == author.id
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it responds with the relationship field filled in" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "sup"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "sup2"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "foobar"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:posts, [post], type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
updated_author =
|
|
|
|
author
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:posts, [post2], type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
post = Ash.get!(Post, post2.id)
|
2021-05-11 10:03:36 +12:00
|
|
|
|
|
|
|
assert Enum.map(updated_author.posts, &%{&1 | __metadata__: nil}) == [
|
|
|
|
%{post | __metadata__: nil}
|
|
|
|
]
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
2019-12-24 09:51:01 +13:00
|
|
|
describe "updating with belongs_to relationships" do
|
|
|
|
test "allows updating with belongs_to relationship" do
|
2020-07-12 18:25:53 +12:00
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "best dude"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
author2 =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "best dude2"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
post =
|
2020-07-12 18:25:53 +12:00
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foobar"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:author, author2, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "sets the relationship on the destination records accordingly" do
|
2020-07-12 18:25:53 +12:00
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "best dude"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
author2 =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "best dude2"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
post =
|
2020-07-12 18:25:53 +12:00
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foobar"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:author, author2, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
author2 = Ash.get!(Author, author2.id, load: :posts)
|
2021-08-03 19:26:01 +12:00
|
|
|
|
|
|
|
assert Enum.map(author2.posts, & &1.id) == [
|
|
|
|
post.id
|
2021-02-23 14:29:31 +13:00
|
|
|
]
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
|
|
|
|
2020-06-14 18:39:11 +12:00
|
|
|
test "it responds with the relationship field filled in" do
|
2020-07-12 18:25:53 +12:00
|
|
|
author =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "best dude"})
|
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
author2 =
|
|
|
|
Author
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "best dude2"})
|
|
|
|
|> Ash.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
post =
|
2020-07-12 18:25:53 +12:00
|
|
|
Post
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{title: "foobar"})
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.create!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
2022-09-20 07:44:06 +12:00
|
|
|
updated_post =
|
|
|
|
post
|
2023-12-17 04:14:02 +13:00
|
|
|
|> Ash.Changeset.new()
|
|
|
|
|> Ash.Changeset.manage_relationship(:author, author2, type: :append_and_remove)
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.update!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
2021-05-21 08:42:26 +12:00
|
|
|
assert updated_post.author.id ==
|
2024-03-28 09:06:40 +13:00
|
|
|
Ash.get!(Author, author2.id).id
|
2019-12-24 09:51:01 +13:00
|
|
|
end
|
|
|
|
end
|
2020-06-22 15:26:47 +12:00
|
|
|
|
|
|
|
describe "unauthorized update" do
|
|
|
|
test "it does not update the record" do
|
2020-07-12 18:25:53 +12:00
|
|
|
record =
|
|
|
|
Authorized
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_create(:create, %{name: "bar"})
|
|
|
|
|> Ash.create!()
|
2020-06-22 15:26:47 +12:00
|
|
|
|
2021-01-29 08:47:59 +13:00
|
|
|
start_supervised({Ash.Test.Authorizer, check: :forbidden, strict_check: :continue})
|
|
|
|
|
2020-06-22 15:26:47 +12:00
|
|
|
assert_raise(Ash.Error.Forbidden, fn ->
|
2020-07-12 18:25:53 +12:00
|
|
|
record
|
2024-03-28 09:06:40 +13:00
|
|
|
|> Ash.Changeset.for_update(:update, %{name: "foo"})
|
|
|
|
|> Ash.update!(authorize?: true)
|
2020-06-22 15:26:47 +12:00
|
|
|
end)
|
|
|
|
|
2024-04-26 14:18:38 +12:00
|
|
|
assert_raise(Ash.Error.Forbidden, fn ->
|
|
|
|
record
|
|
|
|
|> Ash.Changeset.for_update(:update, %{name: "foo"})
|
|
|
|
|> Ash.update!(authorize?: true, atomic_upgrade?: false)
|
|
|
|
end)
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
assert Ash.get!(Authorized, record.id, authorize?: false).name == "bar"
|
2024-04-26 14:18:38 +12:00
|
|
|
|
|
|
|
stop_supervised!(Ash.Test.Authorizer)
|
|
|
|
|
|
|
|
start_supervised({Ash.Test.Authorizer, check: :authorized, strict_check: :continue})
|
|
|
|
|
|
|
|
record
|
|
|
|
|> Ash.Changeset.for_update(:update, %{name: "foo"})
|
|
|
|
|> Ash.update!(authorize?: true)
|
2020-06-22 15:26:47 +12:00
|
|
|
end
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|