ash/test/actions/create_test.exs

246 lines
6.7 KiB
Elixir
Raw Normal View History

2019-12-08 10:33:31 +13:00
defmodule Ash.Test.Actions.CreateTest do
use ExUnit.Case, async: true
2019-12-09 19:44:07 +13:00
defmodule Profile do
use Ash.Resource, name: "authors", type: "author"
use Ash.DataLayer.Ets, private?: true
actions do
read :default
create :default
update :default
end
attributes do
attribute :bio, :string
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
use Ash.Resource, name: "authors", type: "author"
use Ash.DataLayer.Ets, private?: true
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 :name, :string
end
2019-12-09 19:44:07 +13:00
relationships do
has_one :profile, Profile
2019-12-10 18:08:59 +13:00
2019-12-24 09:51:01 +13:00
has_many :posts, Ash.Test.Actions.CreateTest.Post, reverse_relationship: :author
2019-12-09 19:44:07 +13:00
end
2019-12-08 10:33:31 +13:00
end
defmodule PostDefaults do
def garbage2(), do: "garbage2"
def garbage3(), do: "garbage3"
end
2019-12-24 09:51:01 +13:00
defmodule PostLink do
use Ash.Resource, name: "post_links", type: "post_link", primary_key: false
use Ash.DataLayer.Ets, private?: true
actions do
read :default
create :default
update :default
end
relationships do
belongs_to :source_post, Ash.Test.Filter.FilterTest.Post, primary_key?: true
belongs_to :destination_post, Ash.Test.Filter.FilterTest.Post, primary_key?: true
end
end
2019-12-08 10:33:31 +13:00
defmodule Post do
use Ash.Resource, name: "posts", type: "post"
use Ash.DataLayer.Ets, private?: true
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 :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
use Ash.Api
2019-12-24 09:51:01 +13:00
resources [Author, Post, Profile, PostLink]
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"} =
2019-12-16 13:20:44 +13:00
Api.create!(Post, attributes: %{title: "foo", contents: "bar"})
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 %{results: [_, _]} = Api.read!(PostLink, filter: [])
end
test "it responds with the relationship filled in" do
post2 = Api.create!(Post, attributes: %{title: "title2"})
post3 = Api.create!(Post, attributes: %{title: "title3"})
assert Api.create!(Post, relationships: %{related_posts: [post2.id, post3.id]}).related_posts ==
[
Api.get!(Post, post2.id),
Api.get!(Post, post3.id)
]
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
assert author.profile == Map.put(profile, :author_id, author.id)
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