ash/test/actions/create_test.exs

579 lines
13 KiB
Elixir
Raw Normal View History

2019-12-08 10:33:31 +13:00
defmodule Ash.Test.Actions.CreateTest do
2020-06-02 17:47:25 +12:00
@moduledoc false
2019-12-08 10:33:31 +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-09 19:44:07 +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-09 19:44:07 +13:00
actions do
read(:default)
create(:default)
update(:default)
2019-12-09 19:44:07 +13:00
end
attributes do
uuid_primary_key :id
attribute(:bio, :string)
attribute(:date, :date)
2019-12-09 19:44:07 +13:00
end
relationships do
belongs_to(:author, Ash.Test.Actions.CreateTest.Author)
2019-12-09 19:44:07 +13:00
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-08 10:33:31 +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-08 10:33:31 +13:00
actions do
read :default
create :default, primary?: true
create :only_allow_name do
accept([:name])
end
2020-08-30 19:15:16 +12:00
create :duplicate_name do
change {DuplicateName, []}
end
update :default
2019-12-08 10:33:31 +13:00
end
attributes do
uuid_primary_key :id
attribute(:name, :string)
attribute(:bio, :string)
2019-12-08 10:33:31 +13:00
end
2019-12-09 19:44:07 +13:00
relationships do
has_one(:profile, Profile, destination_field: :author_id)
2019-12-10 18:08:59 +13:00
has_many(:posts, Ash.Test.Actions.CreateTest.Post, destination_field: :author_id)
2019-12-09 19:44:07 +13:00
end
2019-12-08 10:33:31 +13:00
end
defmodule PostDefaults do
2020-06-02 17:47:25 +12:00
@moduledoc false
def garbage2, do: "garbage2"
def garbage3, do: "garbage3"
end
2020-01-01 22:51:56 +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
actions do
read(:default)
2019-12-24 09:51:01 +13:00
create(:default)
update(:default)
2019-12-24 09:51:01 +13:00
end
relationships do
belongs_to(:source_post, Ash.Test.Actions.CreateTest.Post,
primary_key?: true,
required?: true
)
belongs_to(:destination_post, Ash.Test.Actions.CreateTest.Post,
primary_key?: true,
required?: true
)
2019-12-24 09:51:01 +13:00
end
end
2019-12-08 10:33:31 +13:00
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-08 10:33:31 +13:00
actions do
read(:default)
create(:default)
update(:default)
2019-12-08 10:33:31 +13:00
end
attributes do
uuid_primary_key :id
attribute(:title, :string, allow_nil?: false)
attribute(:contents, :string)
attribute(:tag, :string, default: "garbage")
attribute(:tag2, :string, default: &PostDefaults.garbage2/0)
attribute(:tag3, :string, default: {PostDefaults, :garbage3, []})
attribute(:list_attribute, {:array, :integer})
attribute(:date, :date)
attribute(:private_content, :string, private?: true, allow_nil?: false)
2021-01-07 16:42:39 +13:00
attribute(:binary, :binary)
attribute(:list_attribute_with_constraints, {:array, :integer},
2020-07-01 15:57:24 +12:00
constraints: [
min_length: 2,
max_length: 10,
items: [min: -10, max: 10]
]
)
2019-12-08 10:33:31 +13:00
end
relationships do
belongs_to(:author, Author)
2019-12-24 09:51:01 +13:00
many_to_many(:related_posts, __MODULE__,
2019-12-24 09:51:01 +13:00
through: PostLink,
source_field_on_join_table: :source_post_id,
2020-06-22 16:34:44 +12:00
destination_field_on_join_table: :destination_post_id
)
2019-12-08 10:33:31 +13:00
end
end
2020-12-27 12:30:51 +13:00
defmodule GeneratedPkey do
use Ash.Resource,
data_layer: Ash.DataLayer.Ets
ets do
private?(true)
end
actions do
read(:default)
create(:default)
update(:default)
end
attributes do
2021-01-08 16:35:10 +13:00
uuid_primary_key :id
2020-12-27 12:30:51 +13:00
end
end
2019-12-08 10:33:31 +13:00
defmodule Api do
2020-06-02 17:47:25 +12:00
@moduledoc false
2019-12-08 10:33:31 +13:00
use Ash.Api
resources do
resource(Author)
resource(Post)
resource(Profile)
resource(PostLink)
resource(Authorized)
2020-12-27 12:30:51 +13:00
resource(GeneratedPkey)
end
2019-12-08 10:33:31 +13:00
end
2020-07-12 18:25:53 +12:00
import Ash.Changeset
2019-12-08 10:33:31 +13:00
describe "simple creates" do
test "allows creating a record with valid attributes" do
assert %Post{title: "foo", contents: "bar"} =
2020-07-12 18:25:53 +12:00
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attributes(%{
title: "foo",
contents: "bar",
2021-01-07 16:42:39 +13:00
date: Date.utc_today(),
binary: <<0, 1, 2, 3, 4, 5>>
2020-07-12 18:25:53 +12:00
})
|> Api.create!()
2019-12-08 10:33:31 +13:00
end
2019-12-09 08:02:09 +13:00
test "return missing required attribute" do
{:error, err} =
Post
|> new()
|> change_attributes(%{
contents: "bar",
date: Date.utc_today()
})
|> Api.create()
assert %Ash.Error.Invalid{
class: :invalid,
errors: [
%Ash.Error.Changes.Required{
class: :invalid,
field: :title
}
]
} = err
end
2020-12-27 12:30:51 +13:00
test "generated fields are not required" do
assert %GeneratedPkey{} =
GeneratedPkey
|> new()
|> Api.create!()
end
2019-12-09 08:02:09 +13:00
test "constant default values are set properly" do
2020-07-12 18:25:53 +12:00
assert %Post{tag: "garbage"} =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "foo")
|> Api.create!()
2019-12-09 08:02:09 +13:00
end
test "constant functions values are set properly" do
2020-07-12 18:25:53 +12:00
assert %Post{tag2: "garbage2"} =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "foo")
|> Api.create!()
2019-12-09 08:02:09 +13:00
end
test "constant module/function values are set properly" do
2020-07-12 18:25:53 +12:00
assert %Post{tag3: "garbage3"} =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "foo")
|> Api.create!()
2019-12-09 08:02:09 +13:00
end
2021-01-07 16:42:39 +13:00
test "binary values are set properly" do
assert %Post{binary: <<0, 1, 2>>} =
Post
|> new()
|> change_attribute(:title, "foo")
|> change_attribute(:binary, <<0, 1, 2>>)
|> Api.create!()
end
2019-12-08 10:33:31 +13:00
end
2019-12-09 19:44:07 +13:00
describe "accept" do
test "allows using attributes in the list" do
Author
|> new()
|> change_attribute(:name, "fred")
|> Api.create!(action: :only_allow_name)
end
test "it prevents using attributes not in the list" do
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()
|> change_attribute(:bio, "foo")
|> Api.create!(action: :only_allow_name)
end
end
end
2020-08-30 19:15:16 +12:00
describe "changeset" do
test "changes are run properly" do
author =
Author
|> new(%{name: "fred"})
|> Api.create!(action: :duplicate_name)
assert author.name == "fredfred"
end
end
2019-12-24 09:51:01 +13:00
describe "creating many to many relationships" do
test "allows creating with a many_to_many relationship" do
2020-07-12 18:25:53 +12:00
post2 =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "title2")
|> Api.create!()
2019-12-24 09:51:01 +13:00
2020-07-12 18:25:53 +12:00
post3 =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "title3")
|> Api.create!()
Post
|> new(%{title: "cannot_be_missing"})
2020-07-12 18:25:53 +12:00
|> replace_relationship(:related_posts, [post2, post3])
|> Api.create!()
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
post2 =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "title2")
|> Api.create!()
post3 =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "title3")
|> Api.create!()
Post
|> new(%{title: "title4"})
2020-07-12 18:25:53 +12:00
|> replace_relationship(:related_posts, [post2, post3])
|> Api.create!()
2019-12-24 09:51:01 +13:00
assert [_, _] =
PostLink
|> Ash.Query.new()
|> Api.read!()
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
post2 =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "title2")
|> Api.create!()
post3 =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "title3")
|> Api.create!()
2019-12-24 09:51:01 +13:00
2020-07-12 18:25:53 +12:00
post =
Post
|> new(%{title: "cannot_be_missing"})
2020-07-12 18:25:53 +12:00
|> replace_relationship(:related_posts, [post2, post3])
|> Api.create!()
assert Enum.sort(post.related_posts) ==
2020-01-01 22:51:56 +13:00
Enum.sort([
2019-12-24 09:51:01 +13:00
Api.get!(Post, post2.id),
Api.get!(Post, post3.id)
2020-01-01 22:51:56 +13:00
])
2019-12-24 09:51:01 +13:00
end
end
2019-12-09 19:44:07 +13:00
describe "creating with has_one relationships" do
test "allows creating with has_one relationship" do
2020-07-12 18:25:53 +12:00
profile =
Profile
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:bio, "best dude")
|> Api.create!()
2019-12-09 19:44:07 +13:00
2020-07-12 18:25:53 +12:00
Author
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:name, "fred")
|> replace_relationship(:profile, profile)
2019-12-09 19:44:07 +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()
2020-07-12 18:25:53 +12:00
|> change_attribute(:bio, "best dude")
|> Api.create!()
2019-12-09 19:44:07 +13:00
author =
2020-07-12 18:25:53 +12:00
Author
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:name, "fred")
|> replace_relationship(:profile, profile)
|> Api.create!()
2019-12-09 19:44:07 +13:00
assert Api.get!(Profile, profile.id).author_id == author.id
end
2019-12-09 20:07:23 +13:00
2020-10-15 17:54:02 +13:00
test "it responds with the relationship filled in" do
2020-07-12 18:25:53 +12:00
profile =
Profile
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:bio, "best dude")
|> Api.create!()
2019-12-09 20:07:23 +13:00
author =
2020-07-12 18:25:53 +12:00
Author
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:name, "fred")
|> replace_relationship(:profile, profile)
|> Api.create!()
2019-12-09 20:07:23 +13:00
2020-01-14 07:16:24 +13:00
assert author.profile.author_id == author.id
2019-12-09 20:07:23 +13:00
end
end
2019-12-10 18:08:59 +13:00
describe "creating with a has_many relationship" do
test "allows creating with a has_many relationship" do
2020-07-12 18:25:53 +12:00
post =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "sup")
|> Api.create!()
2019-12-10 18:08:59 +13:00
2020-07-12 18:25:53 +12:00
Author
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:name, "foobar")
|> replace_relationship(:posts, [post])
|> Api.create!()
2019-12-10 18:08:59 +13:00
end
end
2019-12-09 20:07:23 +13:00
describe "creating with belongs_to relationships" do
test "allows creating with belongs_to relationship" do
2020-07-12 18:25:53 +12:00
author =
Author
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:bio, "best dude")
|> Api.create!()
2019-12-09 20:07:23 +13:00
2020-07-12 18:25:53 +12:00
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "foobar")
|> replace_relationship(:author, author)
|> Api.create!()
2019-12-09 20:07:23 +13:00
end
test "it sets the relationship on the destination record accordingly" do
2020-07-12 18:25:53 +12:00
author =
Author
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:bio, "best dude")
|> Api.create!()
2019-12-09 20:07:23 +13:00
post =
2020-07-12 18:25:53 +12:00
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "foobar")
|> replace_relationship(:author, author)
|> Api.create!()
2019-12-09 20:07:23 +13:00
assert Api.get!(Post, post.id).author_id == author.id
end
test "it responds with the relationship field filled in" do
2020-07-12 18:25:53 +12:00
author =
Author
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:bio, "best dude")
|> Api.create!()
2019-12-09 20:07:23 +13:00
2020-07-12 18:25:53 +12:00
post =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "foobar")
|> replace_relationship(:author, author)
|> Api.create!()
assert post.author_id == author.id
2019-12-09 20:07:23 +13:00
end
2019-12-10 18:08:59 +13:00
test "it responds with the relationship filled in" do
2020-07-12 18:25:53 +12:00
author =
Author
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:bio, "best dude")
|> Api.create!()
2019-12-09 20:07:23 +13:00
2020-07-12 18:25:53 +12:00
post =
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:title, "foobar")
|> replace_relationship(:author, author)
|> Api.create!()
assert post.author == author
2019-12-09 20:07:23 +13:00
end
2019-12-09 19:44:07 +13:00
end
2020-07-01 15:57:24 +12:00
describe "list type" do
test "it can store a list" do
2020-07-12 18:25:53 +12:00
assert Post
|> new(%{title: "cannot_be_missing"})
2020-07-12 18:25:53 +12:00
|> change_attribute(:list_attribute, [1, 2, 3, 4])
|> Api.create!()
2020-07-01 15:57:24 +12:00
end
end
describe "list type constraints" do
test "it honors min_length" do
2021-01-13 09:05:56 +13:00
assert_raise Ash.Error.Invalid, ~r/must have 2 or more items/, fn ->
2020-07-12 18:25:53 +12:00
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:list_attribute_with_constraints, [])
|> Api.create!()
2020-07-01 15:57:24 +12:00
end
end
test "it honors max_length" do
2021-01-13 09:05:56 +13:00
assert_raise Ash.Error.Invalid, ~r/must have 10 or fewer items/, fn ->
2020-07-12 18:25:53 +12:00
list = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:list_attribute_with_constraints, list)
|> Api.create!()
2020-07-01 15:57:24 +12:00
end
end
test "it honors item constraints" do
2021-01-13 09:05:56 +13:00
assert_raise Ash.Error.Invalid, ~r/must be less than or equal to 10/, fn ->
2020-07-12 18:25:53 +12:00
list = [28, 2, 4]
Post
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:list_attribute_with_constraints, list)
|> Api.create!()
2020-07-01 15:57:24 +12:00
end
end
end
describe "unauthorized create" do
test "it does not create the record" do
assert_raise(Ash.Error.Forbidden, fn ->
2020-07-12 18:25:53 +12:00
Authorized
|> new()
2020-07-12 18:25:53 +12:00
|> change_attribute(:name, "foo")
|> Api.create!(authorize?: true)
end)
assert [] = Api.read!(Authorized)
end
end
2019-12-08 10:33:31 +13:00
end