2019-12-07 09:54:30 +13:00
|
|
|
defmodule Ash.Test.Resource.Relationships.ManyToManyTest 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.HasMany
|
|
|
|
alias Ash.Resource.Relationships.ManyToMany
|
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, data_layer: Ash.DataLayer.Ets
|
2019-12-06 20:00:26 +13:00
|
|
|
|
2020-06-01 17:14:23 +12:00
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2020-06-01 17:14:23 +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
|
2020-04-20 15:15:52 +12:00
|
|
|
test "it creates a relationship and a join relationship" do
|
2019-12-06 20:00:26 +13:00
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
many_to_many :related_posts, __MODULE__,
|
2020-06-05 15:34:44 +12:00
|
|
|
through: SomeResource,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute_on_join_resource: :post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination_attribute_on_join_resource: :related_post_id,
|
|
|
|
public?: true
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
many_to_many :unrelated_posts, __MODULE__,
|
2020-11-03 09:33:14 +13:00
|
|
|
through: Tabloid,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute_on_join_resource: :post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination_attribute_on_join_resource: :unrelated_post_id
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
2020-11-03 09:33:14 +13:00
|
|
|
%HasMany{
|
|
|
|
cardinality: :many,
|
|
|
|
destination: Tabloid,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: :post_id,
|
2020-11-03 09:33:14 +13:00
|
|
|
name: :unrelated_posts_join_assoc,
|
2024-03-28 09:06:40 +13:00
|
|
|
source: Post,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: :id,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: :has_many,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: false
|
2020-11-03 09:33:14 +13:00
|
|
|
},
|
|
|
|
%HasMany{
|
2020-04-20 15:15:52 +12:00
|
|
|
cardinality: :many,
|
|
|
|
destination: SomeResource,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: :post_id,
|
2020-05-01 18:21:46 +12:00
|
|
|
name: :related_posts_join_assoc,
|
2024-03-28 09:06:40 +13:00
|
|
|
source: Post,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: :id,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: :has_many,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: false
|
2020-04-20 15:15:52 +12:00
|
|
|
},
|
2020-11-03 09:33:14 +13:00
|
|
|
%ManyToMany{
|
2019-12-06 20:00:26 +13:00
|
|
|
cardinality: :many,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination: Post,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: :id,
|
|
|
|
destination_attribute_on_join_resource: :related_post_id,
|
2020-05-01 18:21:46 +12:00
|
|
|
name: :related_posts,
|
2024-03-28 09:06:40 +13:00
|
|
|
source: Post,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: :id,
|
|
|
|
source_attribute_on_join_resource: :post_id,
|
2019-12-10 18:08:59 +13:00
|
|
|
through: SomeResource,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: :many_to_many,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: true
|
2020-11-03 09:33:14 +13:00
|
|
|
},
|
|
|
|
%ManyToMany{
|
|
|
|
cardinality: :many,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination: Post,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: :id,
|
|
|
|
destination_attribute_on_join_resource: :unrelated_post_id,
|
2020-11-03 09:33:14 +13:00
|
|
|
name: :unrelated_posts,
|
2024-03-28 09:06:40 +13:00
|
|
|
source: Post,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: :id,
|
|
|
|
source_attribute_on_join_resource: :post_id,
|
2020-11-03 09:33:14 +13:00
|
|
|
through: Tabloid,
|
|
|
|
type: :many_to_many,
|
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 [%ManyToMany{name: :related_posts}] = Ash.Resource.Info.public_relationships(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
|
|
|
assert %ManyToMany{name: :related_posts} =
|
2021-02-23 14:29:31 +13:00
|
|
|
Ash.Resource.Info.public_relationship(Post, :related_posts)
|
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, :unrelated_posts)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "validation" do
|
2019-12-10 18:08:59 +13:00
|
|
|
test "it fails if you pass a string to `through`" 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 :through option, got: "some_table"/,
|
2019-12-10 18:08:59 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2020-06-05 15:34:44 +12:00
|
|
|
many_to_many :foobars, Foobar,
|
|
|
|
through: "some_table",
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute_on_join_resource: :source_post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination_attribute_on_join_resource: :destination_post_id,
|
|
|
|
public?: true
|
2019-12-10 18:08:59 +13:00
|
|
|
end
|
|
|
|
end
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
2019-12-10 18:08:59 +13:00
|
|
|
)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
test "you can pass a module to `through`" do
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2020-06-05 15:34:44 +12:00
|
|
|
many_to_many :foobars, Foobar,
|
|
|
|
through: FooBars,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute_on_join_resource: :source_post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination_attribute_on_join_resource: :destination_post_id,
|
|
|
|
public?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-18 03:03:21 +13:00
|
|
|
test "it guesses `source_attribute_on_join_resource` if not set" do
|
|
|
|
defposts do
|
|
|
|
relationships do
|
|
|
|
many_to_many :authors, Author do
|
|
|
|
through PostsJoinArticlesAuthors
|
|
|
|
destination_attribute_on_join_resource :manager_id
|
2024-03-28 09:06:40 +13:00
|
|
|
public?(true)
|
2023-10-18 03:03:21 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%HasMany{},
|
|
|
|
%ManyToMany{
|
|
|
|
destination: Author,
|
|
|
|
destination_attribute_on_join_resource: :manager_id,
|
|
|
|
source_attribute_on_join_resource: :post_id,
|
|
|
|
through: PostsJoinArticlesAuthors
|
|
|
|
}
|
|
|
|
] = Ash.Resource.Info.relationships(Post)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it guesses `destination_attribute_on_join_resource` if not set" do
|
|
|
|
defposts do
|
|
|
|
relationships do
|
|
|
|
many_to_many :authors, ArticleAuthor do
|
|
|
|
through PostsJoinArticlesAuthors
|
|
|
|
source_attribute_on_join_resource :article_id
|
2024-03-28 09:06:40 +13:00
|
|
|
public?(true)
|
2023-10-18 03:03:21 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%HasMany{},
|
|
|
|
%ManyToMany{
|
|
|
|
destination: ArticleAuthor,
|
|
|
|
destination_attribute_on_join_resource: :article_author_id,
|
|
|
|
source_attribute_on_join_resource: :article_id,
|
|
|
|
through: PostsJoinArticlesAuthors
|
|
|
|
}
|
|
|
|
] = Ash.Resource.Info.relationships(Post)
|
|
|
|
end
|
|
|
|
|
2022-08-16 06:00:02 +12:00
|
|
|
test "it fails if you dont pass an atom for `source_attribute_on_join_resource`" 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_on_join_resource option: expected atom, got: "what"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2020-06-05 15:34:44 +12:00
|
|
|
many_to_many :foobars, Foobar,
|
|
|
|
through: FooBars,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute_on_join_resource: "what",
|
2024-03-28 09:06:40 +13:00
|
|
|
destination_attribute_on_join_resource: :destination_post_id,
|
|
|
|
public?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-08-16 06:00:02 +12:00
|
|
|
test "it fails if you dont pass an atom for `destination_attribute_on_join_resource`" 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_on_join_resource option: expected atom, got: "what"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
|
|
|
many_to_many :foobars, Foobar,
|
2019-12-10 18:08:59 +13:00
|
|
|
through: FooBar,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute_on_join_resource: "what",
|
2024-03-28 09:06:40 +13:00
|
|
|
source_attribute_on_join_resource: :source_post_id,
|
|
|
|
public?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-08-16 06:00:02 +12:00
|
|
|
test "it fails if you dont pass an atom for `source_attribute`" 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: "what"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
|
|
|
many_to_many :foobars, Foobar,
|
2019-12-10 18:08:59 +13:00
|
|
|
through: FooBar,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: "what",
|
|
|
|
source_attribute_on_join_resource: :source_post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination_attribute_on_join_resource: :destination_post_id,
|
|
|
|
public?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-08-16 06:00:02 +12:00
|
|
|
test "it fails if you dont pass an atom for `destination_attribute`" 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: "what"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
|
|
|
many_to_many :foobars, Foobar,
|
2019-12-10 18:08:59 +13:00
|
|
|
through: FooBars,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: "what",
|
|
|
|
source_attribute_on_join_resource: :source_post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
destination_attribute_on_join_resource: :destination_post_id,
|
|
|
|
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 an 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: "an_invalid_field"/,
|
2020-11-03 09:33:14 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
|
|
|
many_to_many :foobars, Foobar,
|
|
|
|
through: FooBars,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute_on_join_resource: :source_post_id,
|
|
|
|
destination_attribute_on_join_resource: :destination_post_id,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: "an_invalid_field"
|
2020-11-03 09:33:14 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|