ash/test/resource/relationships/many_to_many_test.exs

142 lines
4.3 KiB
Elixir
Raw Normal View History

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
defmacrop defposts(do: body) do
quote do
defmodule Post do
2020-06-02 17:47:25 +12:00
@moduledoc false
use Ash.Resource, name: "posts", type: "post"
2019-12-06 20:00:26 +13:00
attributes do
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
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
many_to_many :related_posts, Post, through: SomeResource
2019-12-06 20:00:26 +13:00
end
end
assert [
2020-04-20 15:15:52 +12:00
%Ash.Resource.Relationships.HasMany{
cardinality: :many,
destination: SomeResource,
destination_field: :posts_id,
name: :related_posts_join_assoc,
2020-04-20 15:15:52 +12:00
source: Ash.Test.Resource.Relationships.ManyToManyTest.Post,
source_field: :id,
type: :has_many,
reverse_relationship: nil
2020-04-20 15:15:52 +12:00
},
2019-12-06 20:00:26 +13:00
%Ash.Resource.Relationships.ManyToMany{
cardinality: :many,
destination: Ash.Test.Resource.Relationships.ManyToManyTest.Post,
2019-12-06 20:00:26 +13:00
destination_field: :id,
destination_field_on_join_table: :related_posts_id,
name: :related_posts,
2020-04-20 15:15:52 +12:00
source: Ash.Test.Resource.Relationships.ManyToManyTest.Post,
2019-12-06 20:00:26 +13:00
source_field: :id,
source_field_on_join_table: :posts_id,
2019-12-10 18:08:59 +13:00
through: SomeResource,
2020-04-20 15:15:52 +12:00
type: :many_to_many,
reverse_relationship: nil
2019-12-06 20:00:26 +13:00
}
] = 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,
"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
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,
"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
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,
"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,
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,
"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,
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,
"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,
2019-12-06 20:00:26 +13:00
destination_field: "what"
end
end
end
)
end
end
end