ash/test/actions/update_test.exs

584 lines
12 KiB
Elixir
Raw Normal View History

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
defmodule Authorized do
use Ash.Resource,
data_layer: Ash.DataLayer.Ets,
authorizers: [Ash.Test.Authorizer]
ets do
private?(true)
end
attributes do
uuid_primary_key :id
attribute :name, :string
end
actions do
read :default
create :default
update :default
end
end
2019-12-24 07:17:22 +13:00
defmodule Profile do
2020-06-02 17:47:25 +12:00
@moduledoc false
use Ash.Resource, data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
2019-12-24 07:17:22 +13:00
actions do
read :default
create :default
update :default
end
attributes do
uuid_primary_key :id
2019-12-24 07:17:22 +13:00
attribute :bio, :string
end
relationships do
belongs_to :author, Ash.Test.Actions.UpdateTest.Author
end
end
2020-08-30 19:15:16 +12:00
defmodule DuplicateName do
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
2019-12-24 07:17:22 +13:00
defmodule Author do
2020-06-02 17:47:25 +12:00
@moduledoc false
use Ash.Resource, data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
2019-12-24 07:17:22 +13:00
actions do
read :default
create :default
update :default, primary?: true
update :only_allow_name do
accept([:name])
end
2020-08-30 19:15:16 +12:00
update :duplicate_name do
change {DuplicateName, []}
end
2019-12-24 07:17:22 +13:00
end
attributes do
uuid_primary_key :id
2019-12-24 07:17:22 +13:00
attribute :name, :string
attribute :bio, :string
2019-12-24 07:17:22 +13:00
end
relationships do
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
use Ash.Resource, data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
2019-12-24 09:51:01 +13:00
attributes do
attribute :type, :string
end
2019-12-24 09:51:01 +13:00
actions do
read :default
create :default
update :default
end
relationships do
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
use Ash.Resource, data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
2019-12-24 07:17:22 +13:00
actions do
read :default
create :default
update :default
end
attributes do
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,
destination_field_on_join_table: :destination_post_id,
join_attributes: [:type]
2019-12-24 07:17:22 +13:00
end
end
defmodule Api do
2020-06-02 17:47:25 +12:00
@moduledoc false
2019-12-24 07:17:22 +13:00
use Ash.Api
resources do
resource(Author)
resource(Post)
resource(Profile)
resource(PostLink)
resource(Authorized)
end
2019-12-24 07:17:22 +13:00
end
2020-07-12 18:25:53 +12:00
import Ash.Changeset
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
|> 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"} =
post |> new(%{title: "bar", contents: "foo"}) |> Api.update!()
2019-12-24 07:17:22 +13:00
end
end
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 ->
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
|> new(%{title: "title"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> 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
|> new(%{title: "title3"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post
|> 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
test "it updates the join table properly" do
2020-07-12 18:25:53 +12:00
post =
Post
|> new(%{title: "title"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> new(%{title: "title2"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post3 =
Post
|> 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
|> 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
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
|> new(%{title: "title"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> new(%{title: "title2"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post3 =
Post
|> new(%{title: "title3"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
new_post =
post |> new() |> replace_relationship(:related_posts, [post2, post3]) |> Api.update!()
2019-12-24 09:51:01 +13:00
2020-07-12 18:25:53 +12:00
assert Enum.sort(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
])
2019-12-24 09:51:01 +13:00
end
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()
|> replace_relationship(:related_posts, [{post2, %{type: "a"}}, {post3, %{type: "b"}}])
|> Api.update!()
types = Enum.sort(Enum.map(new_post.related_posts_join_assoc, &Map.get(&1, :type)))
assert types == ["a", "b"]
new_post =
new_post
|> new()
|> replace_relationship(:related_posts, [{post2, %{type: "c"}}, {post3, %{type: "d"}}])
|> Api.update!()
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
|> new(%{bio: "best dude"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
profile2 =
Profile
|> 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
|> 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
|> 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
|> new(%{bio: "best dude"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
profile2 =
Profile
|> 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
|> 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
|> 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
|> new(%{bio: "best dude"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
profile2 =
Profile
|> 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
|> new(%{name: "fred"})
2020-07-12 18:25:53 +12:00
|> replace_relationship(:profile, profile)
|> Api.create!()
updated_author =
author
|> new()
2020-07-12 18:25:53 +12:00
|> replace_relationship(:profile, profile2)
|> Api.update!()
2019-12-24 07:17:22 +13:00
assert updated_author.profile == %{profile2 | author_id: author.id}
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
|> new(%{title: "sup"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> 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
|> 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
|> 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
|> new(%{title: "sup"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> 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
|> 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
|> 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
|> new(%{title: "sup"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
post2 =
Post
|> 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
|> new(%{name: "foobar"})
2020-07-12 18:25:53 +12:00
|> replace_relationship(:posts, [post])
|> Api.create!()
updated_author =
author
|> new()
2020-07-12 18:25:53 +12:00
|> replace_relationship(:posts, [post2])
|> Api.update!()
assert updated_author.posts == [Api.get!(Post, post2.id)]
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
|> new(%{name: "best dude"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
author2 =
Author
|> 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
|> 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
|> 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
|> new(%{name: "best dude"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
author2 =
Author
|> 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
|> 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
|> new()
2020-07-12 18:25:53 +12:00
|> replace_relationship(:author, author2)
|> Api.update!()
2019-12-24 09:51:01 +13:00
2020-08-09 05:55:36 +12:00
assert Api.get!(Author, author2.id, load: [:posts]).posts == [Api.get!(Post, post.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
author =
Author
|> new(%{name: "best dude"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
author2 =
Author
|> 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
|> new(%{title: "foobar"})
2020-07-12 18:25:53 +12:00
|> replace_relationship(:author, author)
|> Api.create!()
updated_post = post |> new() |> replace_relationship(:author, author2) |> Api.update!()
2020-07-12 18:25:53 +12:00
assert updated_post.author ==
2019-12-24 09:51:01 +13:00
Api.get!(Author, author2.id)
end
end
describe "unauthorized update" do
test "it does not update the record" do
2020-07-12 18:25:53 +12:00
record =
Authorized
|> new(%{name: "bar"})
2020-07-12 18:25:53 +12:00
|> Api.create!()
start_supervised({Ash.Test.Authorizer, check: :forbidden, strict_check: :continue})
assert_raise(Ash.Error.Forbidden, fn ->
2020-07-12 18:25:53 +12:00
record
|> new(%{name: "foo"})
2020-07-12 18:25:53 +12:00
|> Api.update!(authorize?: true)
end)
assert Api.get!(Authorized, record.id).name == "bar"
end
end
2019-12-24 07:17:22 +13:00
end