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 __MODULE__
alias Ash.Resource.Relationships.HasMany
alias Ash.Resource.Relationships.ManyToMany
2019-12-06 20:00:26 +13:00
defmacrop defposts ( do : body ) do
quote do
defmodule Post do
2020-06-02 17:47:25 +12:00
@moduledoc false
2021-01-13 09:05:56 +13:00
use Ash.Resource , 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
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
2020-06-05 15:34:44 +12:00
many_to_many :related_posts , Post ,
through : SomeResource ,
source_field_on_join_table : :post_id ,
destination_field_on_join_table : :related_post_id
2020-11-03 09:33:14 +13:00
many_to_many :unrelated_posts , Post ,
through : Tabloid ,
source_field_on_join_table : :post_id ,
destination_field_on_join_table : :unrelated_post_id ,
private? : true
2019-12-06 20:00:26 +13:00
end
end
assert [
2020-11-03 09:33:14 +13:00
% HasMany {
cardinality : :many ,
destination : Tabloid ,
destination_field : :post_id ,
name : :unrelated_posts_join_assoc ,
source : ManyToManyTest.Post ,
source_field : :id ,
type : :has_many ,
private? : true
} ,
% HasMany {
2020-04-20 15:15:52 +12:00
cardinality : :many ,
destination : SomeResource ,
2020-06-05 15:34:44 +12:00
destination_field : :post_id ,
2020-05-01 18:21:46 +12:00
name : :related_posts_join_assoc ,
2020-11-03 09:33:14 +13:00
source : ManyToManyTest.Post ,
2020-04-20 15:15:52 +12:00
source_field : :id ,
2020-11-03 09:33:14 +13:00
type : :has_many ,
private? : true
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 ,
2020-11-03 09:33:14 +13:00
destination : ManyToManyTest.Post ,
2019-12-06 20:00:26 +13:00
destination_field : :id ,
2020-06-05 15:34:44 +12:00
destination_field_on_join_table : :related_post_id ,
2020-05-01 18:21:46 +12:00
name : :related_posts ,
2020-11-03 09:33:14 +13:00
source : ManyToManyTest.Post ,
2019-12-06 20:00:26 +13:00
source_field : :id ,
2020-06-05 15:34:44 +12:00
source_field_on_join_table : :post_id ,
2019-12-10 18:08:59 +13:00
through : SomeResource ,
2020-11-03 09:33:14 +13:00
type : :many_to_many ,
private? : false
} ,
% ManyToMany {
cardinality : :many ,
destination : ManyToManyTest.Post ,
destination_field : :id ,
destination_field_on_join_table : :unrelated_post_id ,
name : :unrelated_posts ,
source : ManyToManyTest.Post ,
source_field : :id ,
source_field_on_join_table : :post_id ,
through : Tabloid ,
type : :many_to_many ,
private? : true
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 (
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.ManyToManyTest.Post] \n relationships -> many_to_many -> foobars: \n expected :through to be an atom, 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 " ,
source_field_on_join_table : :source_post_id ,
destination_field_on_join_table : :destination_post_id
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 ,
source_field_on_join_table : :source_post_id ,
destination_field_on_join_table : :destination_post_id
2019-12-06 20:00:26 +13:00
end
end
end
test " it fails if you dont pass an atom for `source_field_on_join_table` " do
assert_raise (
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.ManyToManyTest.Post] \n relationships -> many_to_many -> foobars: \n expected :source_field_on_join_table to be an 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 ,
source_field_on_join_table : " what " ,
destination_field_on_join_table : :destination_post_id
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test " it fails if you dont pass an atom for `destination_field_on_join_table` " do
assert_raise (
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.ManyToManyTest.Post] \n relationships -> many_to_many -> foobars: \n expected :destination_field_on_join_table to be an 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 ,
2020-06-05 15:34:44 +12:00
destination_field_on_join_table : " what " ,
source_field_on_join_table : :source_post_id
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test " it fails if you dont pass an atom for `source_field` " do
assert_raise (
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.ManyToManyTest.Post] \n relationships -> many_to_many -> foobars: \n expected :source_field to be an 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 ,
2020-06-05 15:34:44 +12:00
source_field : " what " ,
source_field_on_join_table : :source_post_id ,
destination_field_on_join_table : :destination_post_id
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test " it fails if you dont pass an atom for `destination_field` " do
assert_raise (
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.ManyToManyTest.Post] \n relationships -> many_to_many -> foobars: \n expected :destination_field to be an 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 ,
2020-06-05 15:34:44 +12:00
destination_field : " what " ,
source_field_on_join_table : :source_post_id ,
destination_field_on_join_table : :destination_post_id
2019-12-06 20:00:26 +13:00
end
end
end
)
end
2020-11-03 09:33:14 +13:00
test " fails if private? is not an boolean " do
assert_raise (
Ash.Error.Dsl.DslError ,
2022-03-16 10:41:25 +13:00
" [Ash.Test.Resource.Relationships.ManyToManyTest.Post] \n relationships -> many_to_many -> foobars: \n expected :private? to be a 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 ,
source_field_on_join_table : :source_post_id ,
destination_field_on_join_table : :destination_post_id ,
private? : " an_invalid_field "
end
end
end
)
end
2019-12-06 20:00:26 +13:00
end
end