2019-12-24 07:17:22 +13:00
|
|
|
defmodule Ash.Test.Actions.UpdateTest do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-24 07:17:22 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2021-04-19 11:41:49 +12:00
|
|
|
import Ash.Changeset
|
2022-06-22 13:00:47 +12:00
|
|
|
import Ash.Test
|
2021-08-05 16:46:41 +12:00
|
|
|
require Ash.Query
|
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,
|
|
|
|
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
|
2020-06-22 15:26:47 +12:00
|
|
|
attribute :name, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2022-04-04 17:48:37 +12:00
|
|
|
defaults [:read, :create, :update, :destroy]
|
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
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
actions do
|
2022-04-29 10:07:06 +12:00
|
|
|
defaults [:create, :read, :update, :destroy]
|
2021-03-14 03:25:13 +13:00
|
|
|
|
|
|
|
update :set_private_attribute_to_nil do
|
|
|
|
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
|
|
|
|
change set_attribute(:private, {:arg, :private})
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2021-03-14 03:25:13 +13:00
|
|
|
attribute :bio, :string, allow_nil?: false
|
|
|
|
attribute :non_nil_private, :string, allow_nil?: false, default: "non_nil"
|
2021-06-26 05:51:17 +12:00
|
|
|
attribute :private, :string, default: "non_nil"
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
belongs_to :author, Ash.Test.Actions.UpdateTest.Author
|
|
|
|
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")
|
|
|
|
|> Ash.Test.Actions.UpdateTest.Api.update!()}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 07:17:22 +13:00
|
|
|
defmodule Author do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
actions do
|
2022-04-29 10:07:06 +12:00
|
|
|
defaults [:create, :read, :update, :destroy]
|
2020-08-28 18:58:03 +12:00
|
|
|
|
|
|
|
update :only_allow_name do
|
|
|
|
accept([:name])
|
|
|
|
end
|
2020-08-30 19:15:16 +12:00
|
|
|
|
|
|
|
update :duplicate_name do
|
|
|
|
change {DuplicateName, []}
|
|
|
|
end
|
2021-05-10 08:56:44 +12:00
|
|
|
|
|
|
|
update :manual_update do
|
|
|
|
accept []
|
|
|
|
manual? true
|
|
|
|
change ManualUpdateAuthor
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2019-12-24 07:17:22 +13:00
|
|
|
attribute :name, :string
|
2020-08-28 18:58:03 +12:00
|
|
|
attribute :bio, :string
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2020-06-05 15:34:44 +12:00
|
|
|
has_one :profile, Profile, destination_field: :author_id
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2020-06-22 16:34:44 +12:00
|
|
|
has_many :posts, Ash.Test.Actions.UpdateTest.Post, destination_field: :author_id
|
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
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-17 11:17:01 +12:00
|
|
|
attributes do
|
|
|
|
attribute :type, :string
|
|
|
|
end
|
|
|
|
|
2019-12-24 09:51:01 +13:00
|
|
|
actions do
|
2022-04-04 17:48:37 +12:00
|
|
|
defaults [:read, :create, :update, :destroy]
|
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,
|
|
|
|
required?: true
|
|
|
|
|
|
|
|
belongs_to :destination_post, Ash.Test.Actions.UpdateTest.Post,
|
|
|
|
primary_key?: true,
|
|
|
|
required?: 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
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
actions do
|
2022-04-04 17:48:37 +12:00
|
|
|
defaults [:read, :create, :update, :destroy]
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2019-12-24 07:17:22 +13:00
|
|
|
attribute :title, :string
|
|
|
|
attribute :contents, :string
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
belongs_to :author, Author
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
many_to_many :related_posts, __MODULE__,
|
|
|
|
through: PostLink,
|
|
|
|
source_field_on_join_table: :source_post_id,
|
2021-10-12 08:14:33 +13:00
|
|
|
destination_field_on_join_table: :destination_post_id
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-05 06:55:05 +12:00
|
|
|
defmodule PaginatedPrimaryRead do
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private? true
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
|
|
|
defaults [:create, :update, :destroy]
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-10-07 19:41:02 +13:00
|
|
|
defmodule Registry do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Registry
|
|
|
|
|
|
|
|
entries do
|
|
|
|
entry(Author)
|
|
|
|
entry(Post)
|
|
|
|
entry(Profile)
|
|
|
|
entry(PostLink)
|
|
|
|
entry(Authorized)
|
2022-04-05 06:55:05 +12:00
|
|
|
entry(PaginatedPrimaryRead)
|
2021-10-07 19:41:02 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-24 07:17:22 +13:00
|
|
|
defmodule Api do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-24 07:17:22 +13:00
|
|
|
use Ash.Api
|
|
|
|
|
2020-06-14 18:39:11 +12:00
|
|
|
resources do
|
2021-10-07 19:41:02 +13:00
|
|
|
registry Registry
|
2020-06-14 18:39:11 +12:00
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "simple updates" do
|
|
|
|
test "allows updating a record with valid attributes" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "foo", contents: "bar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
assert %Post{title: "bar", contents: "foo"} =
|
2020-07-15 17:38:42 +12:00
|
|
|
post |> new(%{title: "bar", contents: "foo"}) |> Api.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
|
|
|
|
test "the update occurs properly" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> new(%{name: "auto"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert %Author{name: "manual"} = author |> new() |> Api.update!(action: :manual_update)
|
|
|
|
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
|
|
|
|
|> new(%{bio: "foobar"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert_raise Ash.Error.Invalid, ~r/attribute bio is required/, fn ->
|
|
|
|
profile |> new(%{bio: ""}) |> Api.update!()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it does not allow updating a private attribute's value to `nil` when `allow_nil?: false`" do
|
|
|
|
profile =
|
|
|
|
Profile
|
|
|
|
|> new(%{bio: "foobar"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert_raise Ash.Error.Invalid, ~r/attribute non_nil_private is required/, fn ->
|
|
|
|
profile |> new(%{bio: "foobar"}) |> Api.update!(action: :set_private_attribute_to_nil)
|
|
|
|
end
|
|
|
|
end
|
2021-06-26 05:51:17 +12:00
|
|
|
|
|
|
|
test "it passes through an argument's value" do
|
|
|
|
profile =
|
|
|
|
Profile
|
|
|
|
|> new(%{bio: "foobar"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
profile =
|
|
|
|
profile
|
|
|
|
|> new(%{bio: "foobar", private: "blah"})
|
|
|
|
|> Api.update!(action: :set_private_attribute_from_arg)
|
|
|
|
|
|
|
|
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
|
|
|
|
|> new(%{title: "foo", contents: "bar"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
assert %Post{title: "bar", contents: nil} =
|
|
|
|
post
|
|
|
|
|> new(%{title: "bar", contents: "foo"})
|
|
|
|
|> Ash.Changeset.select(:title)
|
|
|
|
|> Api.update!()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-28 18:58:03 +12:00
|
|
|
describe "allow" do
|
|
|
|
test "allows attributes in the list" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> new(%{name: "fred"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
author
|
|
|
|
|> new(%{name: "joe"})
|
|
|
|
|> Api.update!(action: :only_allow_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not allow attributes in the list" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> new(%{name: "fred"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
2020-10-21 06:11:21 +13:00
|
|
|
assert_raise Ash.Error.Invalid, ~r/Invalid value provided for bio: cannot be changed/, fn ->
|
2020-08-28 18:58:03 +12:00
|
|
|
author
|
|
|
|
|> new(%{bio: "bio"})
|
|
|
|
|> Api.update!(action: :only_allow_name)
|
|
|
|
end
|
|
|
|
end
|
2020-08-30 19:15:16 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "changeset" do
|
|
|
|
test "changes are run properly" do
|
|
|
|
author =
|
|
|
|
Author
|
|
|
|
|> new(%{name: "fred"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
author =
|
|
|
|
author
|
|
|
|
|> new(%{name: "joe"})
|
|
|
|
|> Api.update!(action: :duplicate_name)
|
|
|
|
|
|
|
|
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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
post3 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title3"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:related_posts, [post2, post3])
|
|
|
|
|> Api.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
|
|
|
|
|> new(%{title: "title"})
|
|
|
|
|> manage_relationship(:related_posts, [%{title: "title0"}], type: :direct_control)
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
other_post = Post |> Ash.Query.filter(title == "title0") |> Api.read_one!()
|
|
|
|
|
|
|
|
post
|
|
|
|
|> new()
|
|
|
|
|> manage_relationship(
|
|
|
|
:related_posts,
|
|
|
|
[%{title: "title3", id: other_post.id}, %{title: "title1"}],
|
|
|
|
type: :direct_control
|
|
|
|
)
|
|
|
|
|> Api.update!()
|
|
|
|
|
|
|
|
assert ["title", "title1", "title3"] =
|
|
|
|
Post |> Ash.Query.sort(:title) |> Api.read!() |> Enum.map(& &1.title)
|
|
|
|
end
|
|
|
|
|
2019-12-24 09:51:01 +13:00
|
|
|
test "it updates the join table properly" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post3 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title3"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:related_posts, [post2, post3])
|
|
|
|
|> Api.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-05-14 03:54:44 +12:00
|
|
|
assert [_, _] = Api.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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post3 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "title3"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
new_post =
|
2020-07-15 17:38:42 +12:00
|
|
|
post |> new() |> replace_relationship(:related_posts, [post2, post3]) |> Api.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([
|
2019-12-24 09:51:01 +13:00
|
|
|
Api.get!(Post, post2.id),
|
|
|
|
Api.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
|
|
|
|
|> new(%{title: "title"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
|
|
|
|> new(%{title: "title2"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post3 =
|
|
|
|
Post
|
|
|
|
|> new(%{title: "title3"})
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
new_post =
|
|
|
|
post
|
|
|
|
|> new()
|
2021-02-23 14:29:31 +13:00
|
|
|
|> replace_relationship(:related_posts, [
|
|
|
|
Ash.Resource.Info.set_metadata(post2, %{join_keys: %{type: "a"}}),
|
|
|
|
Ash.Resource.Info.set_metadata(post3, %{join_keys: %{type: "b"}})
|
|
|
|
])
|
2020-07-17 11:17:01 +12:00
|
|
|
|> Api.update!()
|
2021-02-23 14:29:31 +13:00
|
|
|
|> Api.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
|
|
|
|
|> new()
|
2021-02-23 14:29:31 +13:00
|
|
|
|> replace_relationship(
|
|
|
|
:related_posts,
|
|
|
|
[
|
|
|
|
Ash.Resource.Info.set_metadata(post2, %{join_keys: %{type: "c"}}),
|
|
|
|
Ash.Resource.Info.set_metadata(post3, %{join_keys: %{type: "d"}})
|
|
|
|
],
|
|
|
|
on_match: :update,
|
|
|
|
on_lookup: :relate
|
|
|
|
)
|
2021-06-04 17:37:11 +12:00
|
|
|
|> Api.update!()
|
2021-02-23 14:29:31 +13:00
|
|
|
|> Api.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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{bio: "best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
profile2 =
|
|
|
|
Profile
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{bio: "second best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "fred"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:profile, profile)
|
|
|
|
|> Api.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:profile, profile2)
|
|
|
|
|> Api.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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{bio: "best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
profile2 =
|
|
|
|
Profile
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{bio: "second best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "fred"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:profile, profile)
|
|
|
|
|> Api.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:profile, profile2)
|
|
|
|
|> Api.update!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
assert Api.get!(Profile, profile.id).author_id == nil
|
|
|
|
assert Api.get!(Profile, profile2.id).author_id == author.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it responds with the relationship filled in" do
|
2020-07-12 18:25:53 +12:00
|
|
|
profile =
|
|
|
|
Profile
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{bio: "best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
profile2 =
|
|
|
|
Profile
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{bio: "second best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "fred"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:profile, profile)
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
updated_author =
|
|
|
|
author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:profile, profile2)
|
|
|
|
|> Api.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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "sup"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "sup2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "foobar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:posts, [post])
|
|
|
|
|> Api.create!()
|
2019-12-24 07:17:22 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:posts, [post, post2])
|
|
|
|
|> Api.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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "sup"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "sup2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "foobar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:posts, [post])
|
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:posts, [post2.id])
|
|
|
|
|> Api.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
assert Api.get!(Post, post.id).author_id == nil
|
|
|
|
assert Api.get!(Post, post2.id).author_id == author.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it responds with the relationship field filled in" do
|
2020-07-12 18:25:53 +12:00
|
|
|
post =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "sup"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
post2 =
|
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "sup2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
author =
|
2020-07-12 18:25:53 +12:00
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "foobar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:posts, [post])
|
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
updated_author =
|
|
|
|
author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:posts, [post2])
|
|
|
|
|> Api.update!()
|
|
|
|
|
2021-05-11 10:03:36 +12:00
|
|
|
post = Api.get!(Post, post2.id)
|
|
|
|
|
|
|
|
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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
author2 =
|
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "best dude2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
post =
|
2020-07-12 18:25:53 +12:00
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "foobar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:author, author)
|
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:author, author2)
|
|
|
|
|> Api.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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
author2 =
|
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "best dude2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
post =
|
2020-07-12 18:25:53 +12:00
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "foobar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:author, author)
|
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2020-07-12 18:25:53 +12:00
|
|
|
post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new()
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:author, author2)
|
|
|
|
|> Api.update!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
2021-08-03 19:26:01 +12:00
|
|
|
author2 = Api.get!(Author, author2.id, load: :posts)
|
|
|
|
|
|
|
|
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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "best dude"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
|
|
|
|
|
|
|
author2 =
|
|
|
|
Author
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "best dude2"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.create!()
|
2019-12-24 09:51:01 +13:00
|
|
|
|
|
|
|
post =
|
2020-07-12 18:25:53 +12:00
|
|
|
Post
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{title: "foobar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> replace_relationship(:author, author)
|
|
|
|
|> Api.create!()
|
|
|
|
|
2020-07-15 17:38:42 +12:00
|
|
|
updated_post = post |> new() |> replace_relationship(:author, author2) |> Api.update!()
|
2020-07-12 18:25:53 +12:00
|
|
|
|
2021-05-21 08:42:26 +12:00
|
|
|
assert updated_post.author.id ==
|
|
|
|
Api.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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "bar"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.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
|
2020-07-15 17:38:42 +12:00
|
|
|
|> new(%{name: "foo"})
|
2020-07-12 18:25:53 +12:00
|
|
|
|> Api.update!(authorize?: true)
|
2020-06-22 15:26:47 +12:00
|
|
|
end)
|
|
|
|
|
|
|
|
assert Api.get!(Authorized, record.id).name == "bar"
|
|
|
|
end
|
|
|
|
end
|
2019-12-24 07:17:22 +13:00
|
|
|
end
|