ash/test/resource/relationships/has_many_test.exs

134 lines
3.7 KiB
Elixir
Raw Normal View History

2019-12-07 09:54:30 +13:00
defmodule Ash.Test.Resource.Relationshihps.HasManyTest do
2020-06-02 17:47:25 +12:00
@moduledoc false
2019-12-06 20:00:26 +13:00
use ExUnit.Case, async: true
alias Ash.Resource.Relationships.HasMany
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
attributes do
uuid_primary_key :id
end
2019-12-06 20:00:26 +13:00
unquote(body)
end
end
end
describe "representation" do
test "it creates a relationship" do
defposts do
relationships do
has_many :foo, Foo, destination_field: :post_id
has_many :bar, Bar, destination_field: :bazz, private?: true
2019-12-06 20:00:26 +13:00
end
end
assert [
%HasMany{
2019-12-06 20:00:26 +13:00
cardinality: :many,
destination: Foo,
2019-12-06 20:00:26 +13:00
destination_field: :post_id,
name: :foo,
source_field: :id,
type: :has_many,
private?: false
},
%HasMany{
cardinality: :many,
destination: Bar,
destination_field: :bazz,
name: :bar,
2019-12-06 20:00:26 +13:00
source_field: :id,
type: :has_many,
private?: true
2019-12-06 20:00:26 +13:00
}
] = Ash.Resource.relationships(Post)
assert [%HasMany{name: :foo}] = Ash.Resource.public_relationships(Post)
assert %HasMany{name: :foo} = Ash.Resource.public_relationship(Post, :foo)
assert nil == Ash.Resource.relationship(Post, :definitely_legit_relationship)
assert nil == Ash.Resource.public_relationship(Post, :bar)
2019-12-06 20:00:26 +13:00
end
end
describe "validations" do
test "fails if destination_field is not an atom" do
assert_raise(
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError,
2020-10-06 03:40:22 +13:00
"[Ash.Resource.Dsl.HasMany]\n relationships -> has_many -> foobar:\n expected :destination_field to be an atom, got: \"foo\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
has_many :foobar, FooBar, destination_field: "foo"
end
end
end
)
end
test "fails if source_field is not an atom" do
assert_raise(
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError,
2020-10-06 03:40:22 +13:00
"[Ash.Resource.Dsl.HasMany]\n relationships -> has_many -> foobar:\n expected :source_field to be an atom, got: \"foo\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
has_many :foobar, FooBar, source_field: "foo", destination_field: :post_id
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test "fails if the destination is not an atom" do
assert_raise(
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError,
2020-10-06 03:40:22 +13:00
"[Ash.Resource.Dsl.HasMany]\n relationships -> has_many -> foobar:\n expected :destination to be an atom, got: \"foobar\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
has_many :foobar, "foobar"
end
end
end
)
end
test "fails if the relationship name is not an atom" do
assert_raise(
2020-07-16 09:06:27 +12:00
Ash.Error.Dsl.DslError,
2020-10-06 03:40:22 +13:00
"[Ash.Resource.Dsl.HasMany]\n relationships -> has_many -> foobar:\n expected :name to be an atom, got: \"foobar\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
has_many "foobar", Foobar
end
end
end
)
end
test "fails if private? is not an boolean" do
assert_raise(
Ash.Error.Dsl.DslError,
2020-12-01 18:51:24 +13:00
"[Ash.Resource.Dsl.HasMany]\n relationships -> has_many -> foobar:\n expected :private? to be a boolean, got: \"foo\"",
fn ->
defposts do
relationships do
has_many :foobar, FooBar, private?: "foo", destination_field: :post_id
end
end
end
)
end
2019-12-06 20:00:26 +13:00
end
end