2019-12-07 09:54:30 +13:00
|
|
|
defmodule Ash.Test.Resource.Relationships.BelongsToTest do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-06 20:00:26 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2020-11-03 09:33:14 +13:00
|
|
|
alias Ash.Resource.Relationships.BelongsTo
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2019-12-06 20:00:26 +13:00
|
|
|
defmacrop defposts(do: body) do
|
2024-03-28 09:06:40 +13:00
|
|
|
module = Module.concat(["rand#{System.unique_integer([:positive])}", Post])
|
|
|
|
|
2019-12-06 20:00:26 +13:00
|
|
|
quote do
|
2024-03-28 09:06:40 +13:00
|
|
|
defmodule unquote(module) do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain
|
2019-12-06 20:00:26 +13:00
|
|
|
|
2020-06-30 16:31:07 +12:00
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2020-06-30 16:31:07 +12:00
|
|
|
end
|
|
|
|
|
2019-12-06 20:00:26 +13:00
|
|
|
unquote(body)
|
|
|
|
end
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
alias unquote(module), as: Post
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "representation" do
|
|
|
|
test "it creates an attribute" do
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :foobar, FooBar
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
2021-07-27 12:55:44 +12:00
|
|
|
_,
|
2020-06-14 18:39:11 +12:00
|
|
|
%Ash.Resource.Attribute{
|
2019-12-06 20:00:26 +13:00
|
|
|
name: :foobar_id,
|
|
|
|
primary_key?: false,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: Ash.Type.UUID,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: false
|
2021-07-27 12:55:44 +12:00
|
|
|
}
|
2021-02-23 14:29:31 +13:00
|
|
|
] = Ash.Resource.Info.attributes(Post)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
|
2022-07-07 00:29:46 +12:00
|
|
|
test "it creates an attribute that honors attribute_writable?" do
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :foobar, FooBar, attribute_writable?: true, public?: true
|
2022-07-07 00:29:46 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
|
|
|
_,
|
|
|
|
%Ash.Resource.Attribute{
|
|
|
|
name: :foobar_id,
|
|
|
|
primary_key?: false,
|
|
|
|
type: Ash.Type.UUID,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: true,
|
2022-07-07 00:29:46 +12:00
|
|
|
writable?: true
|
|
|
|
}
|
|
|
|
] = Ash.Resource.Info.attributes(Post)
|
|
|
|
end
|
|
|
|
|
2019-12-06 20:00:26 +13:00
|
|
|
test "it creates a relationship" do
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to(:foo, Foo, public?: true)
|
|
|
|
belongs_to(:bar, Bar, source_attribute: :bazz)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
2020-11-03 09:33:14 +13:00
|
|
|
%BelongsTo{
|
2019-12-06 20:00:26 +13:00
|
|
|
cardinality: :one,
|
2022-08-16 06:00:02 +12:00
|
|
|
define_attribute?: true,
|
2020-11-03 09:33:14 +13:00
|
|
|
destination: Foo,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: :id,
|
|
|
|
attribute_type: :uuid,
|
2020-11-03 09:33:14 +13:00
|
|
|
name: :foo,
|
2019-12-06 20:00:26 +13:00
|
|
|
primary_key?: false,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: :foo_id,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: :belongs_to,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: true
|
2020-11-03 09:33:14 +13:00
|
|
|
},
|
|
|
|
%BelongsTo{
|
|
|
|
cardinality: :one,
|
2022-08-16 06:00:02 +12:00
|
|
|
define_attribute?: true,
|
2020-11-03 09:33:14 +13:00
|
|
|
destination: Bar,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: :id,
|
|
|
|
attribute_type: :uuid,
|
2020-11-03 09:33:14 +13:00
|
|
|
name: :bar,
|
|
|
|
primary_key?: false,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: :bazz,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: :belongs_to,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: false
|
2019-12-06 20:00:26 +13:00
|
|
|
}
|
2021-02-23 14:29:31 +13:00
|
|
|
] = Ash.Resource.Info.relationships(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert [%BelongsTo{name: :foo}] = Ash.Resource.Info.public_relationships(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert %BelongsTo{name: :foo} = Ash.Resource.Info.public_relationship(Post, :foo)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert nil == Ash.Resource.Info.relationship(Post, :definitely_legit_relationship)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert nil == Ash.Resource.Info.public_relationship(Post, :bar)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "validations" do
|
2022-08-16 06:00:02 +12:00
|
|
|
test "fails if destination_attribute is not an atom" do
|
2019-12-06 20:00:26 +13:00
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :destination_attribute option: expected atom, got: \"foo\"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to(:foobar, FooBar, destination_attribute: "foo", public?: true)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-08-16 06:00:02 +12:00
|
|
|
test "fails if source_attribute is not an atom" do
|
2019-12-06 20:00:26 +13:00
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :source_attribute option: expected atom, got: \"foo\"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to(:foobar, FooBar, source_attribute: "foo", public?: true)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fails if the destination is not an atom" do
|
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/expected module in :destination option, got: \"foobar\"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to(:foobar, "foobar", public?: true)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fails if the relationship name is not an atom" do
|
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :name option: expected atom, got: \"foobar\"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to("foobar", Foobar, public?: true)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fails if `primary_key?` is not a boolean" do
|
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :primary_key\? option: expected boolean, got: \"blah\"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to(:foobar, Foobar, primary_key?: "blah", public?: true)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
test "fails if `public?` is not a boolean" do
|
2020-11-03 09:33:14 +13:00
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :public\? option: expected boolean, got: "blah"/,
|
2020-11-03 09:33:14 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to(:foobar, Foobar, public?: "blah")
|
2020-11-03 09:33:14 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
|
2022-08-16 06:00:02 +12:00
|
|
|
test "fails if `define_attribute?` is not a boolean" do
|
2019-12-06 20:00:26 +13:00
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :define_attribute\? option: expected boolean, got: \"blah\"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to(:foobar, Foobar, define_attribute?: "blah", public?: true)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
test "fails in domain initialization if the destination resource doesn't have the correct field" do
|
2020-07-09 16:20:32 +12:00
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2023-09-21 08:51:38 +12:00
|
|
|
~r/Relationship `post` expects source attribute `post_id` to be defined/,
|
2020-07-09 16:20:32 +12:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to(:post, __MODULE__, define_attribute?: false, public?: true)
|
2021-10-07 19:41:02 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
defmodule Domain do
|
|
|
|
use Ash.Domain
|
2020-07-12 18:25:53 +12:00
|
|
|
|
|
|
|
resources do
|
2024-03-28 09:06:40 +13:00
|
|
|
resource Post
|
2020-07-12 18:25:53 +12:00
|
|
|
end
|
|
|
|
end
|
2020-07-09 16:20:32 +12:00
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|