ash/test/resource/relationships/many_to_many_test.exs

124 lines
3.4 KiB
Elixir
Raw Normal View History

2019-12-07 09:54:30 +13:00
defmodule Ash.Test.Resource.Relationships.ManyToManyTest do
2019-12-06 20:00:26 +13:00
use ExUnit.Case, async: true
defmacrop defposts(do: body) do
quote do
defmodule Post do
use Ash.Resource, name: "posts", type: "post", primary_key: false
unquote(body)
end
end
end
describe "representation" do
test "it creates a relationship" do
defposts do
relationships do
2019-12-10 18:08:59 +13:00
many_to_many :foobars, Foobar, through: SomeResource
2019-12-06 20:00:26 +13:00
end
end
assert [
%Ash.Resource.Relationships.ManyToMany{
cardinality: :many,
destination: Foobar,
destination_field: :id,
destination_field_on_join_table: :foobars_id,
name: :foobars,
source_field: :id,
source_field_on_join_table: :posts_id,
2019-12-10 18:08:59 +13:00
through: SomeResource,
2019-12-06 20:00:26 +13:00
type: :many_to_many
}
] = Ash.relationships(Post)
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(
Ash.Error.ResourceDslError,
"option through at relationships -> many_to_many -> foobars must be atom",
fn ->
defposts do
relationships do
many_to_many :foobars, Foobar, through: "some_table"
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
many_to_many :foobars, Foobar, through: FooBars
end
end
end
test "it fails if you dont pass an atom for `source_field_on_join_table`" do
assert_raise(
Ash.Error.ResourceDslError,
"option source_field_on_join_table at relationships -> many_to_many -> foobars must be atom",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
2019-12-10 18:08:59 +13:00
many_to_many :foobars, Foobar, through: FooBars, source_field_on_join_table: "what"
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(
Ash.Error.ResourceDslError,
"option destination_field_on_join_table at relationships -> many_to_many -> foobars must be atom",
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,
2019-12-06 20:00:26 +13:00
destination_field_on_join_table: "what"
end
end
end
)
end
test "it fails if you dont pass an atom for `source_field`" do
assert_raise(
Ash.Error.ResourceDslError,
"option source_field at relationships -> many_to_many -> foobars must be atom",
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,
2019-12-06 20:00:26 +13:00
source_field: "what"
end
end
end
)
end
test "it fails if you dont pass an atom for `destination_field`" do
assert_raise(
Ash.Error.ResourceDslError,
"option destination_field at relationships -> many_to_many -> foobars must be atom",
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,
2019-12-06 20:00:26 +13:00
destination_field: "what"
end
end
end
)
end
end
end