ash/test/actions/create_test.exs

285 lines
7.3 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
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
end
attributes do
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
2019-12-09 19:44:07 +13:00
attribute :bio, :string
2020-06-13 14:39:20 +12:00
attribute :date, :date
2019-12-09 19:44:07 +13:00
end
relationships do
2019-12-10 18:08:59 +13:00
belongs_to :author, Ash.Test.Actions.CreateTest.Author
2019-12-09 19:44:07 +13:00
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
2019-12-10 18:08:59 +13:00
update :default
2019-12-08 10:33:31 +13:00
end
attributes do
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
2019-12-08 10:33:31 +13:00
attribute :name, :string
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,
reverse_relationship: :author,
destination_field: :author
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
create :default
update :default
end
relationships do
2020-01-14 07:16:24 +13:00
belongs_to :source_post, Ash.Test.Actions.CreateTest.Post, primary_key?: true
belongs_to :destination_post, Ash.Test.Actions.CreateTest.Post, primary_key?: 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
2019-12-10 18:08:59 +13:00
update :default
2019-12-08 10:33:31 +13:00
end
attributes do
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
2019-12-08 10:33:31 +13:00
attribute :title, :string
attribute :contents, :string
attribute :tag, :string, default: {:constant, "garbage"}
attribute :tag2, :string, default: &PostDefaults.garbage2/0
attribute :tag3, :string, default: {PostDefaults, :garbage3, []}
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__,
through: PostLink,
source_field_on_join_table: :source_post_id,
destination_field_on_join_table: :destination_post_id,
reverse_relationship: :related_posts
2019-12-08 10:33:31 +13:00
end
end
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)
end
2019-12-08 10:33:31 +13:00
end
describe "simple creates" do
test "allows creating a record with valid attributes" do
assert %Post{title: "foo", contents: "bar"} =
2020-06-13 14:39:20 +12:00
Api.create!(Post,
attributes: %{title: "foo", contents: "bar", date: Date.utc_today()}
)
2019-12-08 10:33:31 +13:00
end
2019-12-09 08:02:09 +13:00
test "constant default values are set properly" do
2019-12-16 13:20:44 +13:00
assert %Post{tag: "garbage"} = Api.create!(Post, attributes: %{title: "foo"})
2019-12-09 08:02:09 +13:00
end
test "constant functions values are set properly" do
2019-12-16 13:20:44 +13:00
assert %Post{tag2: "garbage2"} = Api.create!(Post, attributes: %{title: "foo"})
2019-12-09 08:02:09 +13:00
end
test "constant module/function values are set properly" do
2019-12-16 13:20:44 +13:00
assert %Post{tag3: "garbage3"} = Api.create!(Post, attributes: %{title: "foo"})
2019-12-09 08:02:09 +13:00
end
2019-12-08 10:33:31 +13:00
end
2019-12-09 19:44:07 +13:00
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
post2 = Api.create!(Post, attributes: %{title: "title2"})
post3 = Api.create!(Post, attributes: %{title: "title3"})
Api.create!(Post, relationships: %{related_posts: [post2.id, post3.id]})
end
test "it updates the join table properly" do
post2 = Api.create!(Post, attributes: %{title: "title2"})
post3 = Api.create!(Post, attributes: %{title: "title3"})
Api.create!(Post, relationships: %{related_posts: [post2.id, post3.id]})
assert [_, _] =
PostLink
|> Api.query()
|> Api.read!()
2019-12-24 09:51:01 +13:00
end
test "it responds with the relationship filled in" do
post2 = Api.create!(Post, attributes: %{title: "title2"})
post3 = Api.create!(Post, attributes: %{title: "title3"})
2020-01-01 22:51:56 +13:00
assert Enum.sort(
Api.create!(Post, relationships: %{related_posts: [post2.id, post3.id]}).related_posts
) ==
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
2019-12-16 13:20:44 +13:00
profile = Api.create!(Profile, attributes: %{bio: "best dude"})
2019-12-09 19:44:07 +13:00
2019-12-16 13:20:44 +13:00
Api.create!(Author,
2019-12-09 19:44:07 +13:00
attributes: %{name: "fred"},
relationships: %{profile: profile.id}
2019-12-16 13:20:44 +13:00
)
2019-12-09 19:44:07 +13:00
end
test "it sets the relationship on the destination record accordingly" do
2019-12-16 13:20:44 +13:00
profile = Api.create!(Profile, attributes: %{bio: "best dude"})
2019-12-09 19:44:07 +13:00
author =
2019-12-16 13:20:44 +13:00
Api.create!(Author,
2019-12-09 19:44:07 +13:00
attributes: %{name: "fred"},
relationships: %{profile: profile.id}
2019-12-16 13:20:44 +13:00
)
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
test "it responds with the relationshi filled in" do
2019-12-16 13:20:44 +13:00
profile = Api.create!(Profile, attributes: %{bio: "best dude"})
2019-12-09 20:07:23 +13:00
author =
2019-12-16 13:20:44 +13:00
Api.create!(Author,
2019-12-09 20:07:23 +13:00
attributes: %{name: "fred"},
relationships: %{profile: profile.id}
2019-12-16 13:20:44 +13:00
)
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
2019-12-16 13:20:44 +13:00
post = Api.create!(Post, attributes: %{title: "sup"})
2019-12-10 18:08:59 +13:00
2019-12-16 13:20:44 +13:00
Api.create!(Author,
2019-12-10 18:08:59 +13:00
attributes: %{title: "foobar"},
relationships: %{
posts: [post.id]
}
2019-12-16 13:20:44 +13:00
)
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
2019-12-16 13:20:44 +13:00
author = Api.create!(Author, attributes: %{bio: "best dude"})
2019-12-09 20:07:23 +13:00
2019-12-16 13:20:44 +13:00
Api.create!(Post,
2019-12-09 20:07:23 +13:00
attributes: %{title: "foobar"},
relationships: %{
author: author.id
}
2019-12-16 13:20:44 +13:00
)
2019-12-09 20:07:23 +13:00
end
test "it sets the relationship on the destination record accordingly" do
2019-12-16 13:20:44 +13:00
author = Api.create!(Author, attributes: %{bio: "best dude"})
2019-12-09 20:07:23 +13:00
post =
2019-12-16 13:20:44 +13:00
Api.create!(Post,
2019-12-09 20:07:23 +13:00
attributes: %{title: "foobar"},
relationships: %{
author: author.id
}
2019-12-16 13:20:44 +13:00
)
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
2019-12-16 13:20:44 +13:00
author = Api.create!(Author, attributes: %{bio: "best dude"})
2019-12-09 20:07:23 +13:00
2019-12-16 13:20:44 +13:00
assert Api.create!(Post,
2019-12-09 20:07:23 +13:00
attributes: %{title: "foobar"},
relationships: %{
author: author.id
}
2019-12-16 13:20:44 +13:00
).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
2019-12-16 13:20:44 +13:00
author = Api.create!(Author, attributes: %{bio: "best dude"})
2019-12-09 20:07:23 +13:00
2019-12-16 13:20:44 +13:00
assert Api.create!(Post,
2019-12-09 20:07:23 +13:00
attributes: %{title: "foobar"},
relationships: %{
author: author.id
}
2019-12-16 13:20:44 +13:00
).author == author
2019-12-09 20:07:23 +13:00
end
2019-12-09 19:44:07 +13:00
end
2019-12-08 10:33:31 +13:00
end