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
|
|
|
|
belongs_to :author, __MODULE__.Author
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
attribute :name, :string
|
|
|
|
end
|
2019-12-09 19:44:07 +13:00
|
|
|
|
|
|
|
relationships do
|
|
|
|
has_one :profile, Profile
|
|
|
|
end
|
2019-12-08 10:33:31 +13:00
|
|
|
end
|
|
|
|
|
2019-12-08 19:21:09 +13:00
|
|
|
defmodule PostDefaults do
|
|
|
|
def garbage2(), do: "garbage2"
|
|
|
|
def garbage3(), do: "garbage3"
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
attribute :title, :string
|
|
|
|
attribute :contents, :string
|
2019-12-08 19:21:09 +13:00
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Api do
|
|
|
|
use Ash.Api
|
|
|
|
|
2019-12-09 19:44:07 +13:00
|
|
|
resources [Author, Post, Profile]
|
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"} =
|
|
|
|
Api.create!(Post, %{attributes: %{title: "foo", contents: "bar"}})
|
|
|
|
end
|
2019-12-09 08:02:09 +13:00
|
|
|
|
|
|
|
test "constant default values are set properly" do
|
|
|
|
assert %Post{tag: "garbage"} = Api.create!(Post, %{attributes: %{title: "foo"}})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "constant functions values are set properly" do
|
|
|
|
assert %Post{tag2: "garbage2"} = Api.create!(Post, %{attributes: %{title: "foo"}})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "constant module/function values are set properly" do
|
|
|
|
assert %Post{tag3: "garbage3"} = Api.create!(Post, %{attributes: %{title: "foo"}})
|
|
|
|
end
|
2019-12-08 10:33:31 +13:00
|
|
|
end
|
2019-12-09 19:44:07 +13:00
|
|
|
|
|
|
|
describe "creating with has_one relationships" do
|
|
|
|
test "allows creating with has_one relationship" do
|
|
|
|
profile = Api.create!(Profile, %{attributes: %{bio: "best dude"}})
|
|
|
|
|
|
|
|
Api.create!(Author, %{
|
|
|
|
attributes: %{name: "fred"},
|
|
|
|
relationships: %{profile: profile.id}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it sets the relationship on the destination record accordingly" do
|
|
|
|
profile = Api.create!(Profile, %{attributes: %{bio: "best dude"}})
|
|
|
|
|
|
|
|
author =
|
|
|
|
Api.create!(Author, %{
|
|
|
|
attributes: %{name: "fred"},
|
|
|
|
relationships: %{profile: profile.id}
|
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
profile = Api.create!(Profile, %{attributes: %{bio: "best dude"}})
|
|
|
|
|
|
|
|
author =
|
|
|
|
Api.create!(Author, %{
|
|
|
|
attributes: %{name: "fred"},
|
|
|
|
relationships: %{profile: profile.id}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert author.profile == Map.put(profile, :author_id, author.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "creating with belongs_to relationships" do
|
|
|
|
test "allows creating with belongs_to relationship" do
|
|
|
|
author = Api.create!(Author, %{attributes: %{bio: "best dude"}})
|
|
|
|
|
|
|
|
Api.create!(Post, %{
|
|
|
|
attributes: %{title: "foobar"},
|
|
|
|
relationships: %{
|
|
|
|
author: author.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it sets the relationship on the destination record accordingly" do
|
|
|
|
author = Api.create!(Author, %{attributes: %{bio: "best dude"}})
|
|
|
|
|
|
|
|
post =
|
|
|
|
Api.create!(Post, %{
|
|
|
|
attributes: %{title: "foobar"},
|
|
|
|
relationships: %{
|
|
|
|
author: author.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
assert Api.get!(Post, post.id).author_id == author.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it responds with the relationship field filled in" do
|
|
|
|
author = Api.create!(Author, %{attributes: %{bio: "best dude"}})
|
|
|
|
|
|
|
|
assert Api.create!(Post, %{
|
|
|
|
attributes: %{title: "foobar"},
|
|
|
|
relationships: %{
|
|
|
|
author: author.id
|
|
|
|
}
|
|
|
|
}).author_id == author.id
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it responds with the relationshi filled in" do
|
|
|
|
author = Api.create!(Author, %{attributes: %{bio: "best dude"}})
|
|
|
|
|
|
|
|
assert Api.create!(Post, %{
|
|
|
|
attributes: %{title: "foobar"},
|
|
|
|
relationships: %{
|
|
|
|
author: author.id
|
|
|
|
}
|
|
|
|
}).author == author
|
|
|
|
end
|
2019-12-09 19:44:07 +13:00
|
|
|
end
|
2019-12-08 10:33:31 +13:00
|
|
|
end
|