ash/test/resource/relationships/has_many_test.exs

99 lines
2.5 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
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
test "it creates a relationship" do
defposts do
relationships do
has_many :foobar, FooBar
end
end
assert [
%Ash.Resource.Relationships.HasMany{
cardinality: :many,
destination: FooBar,
destination_field: :post_id,
name: :foobar,
source_field: :id,
type: :has_many
}
] = Ash.relationships(Post)
end
end
describe "validations" do
test "fails if destination_field is not an atom" do
assert_raise(
Ash.Error.ResourceDslError,
"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(
Ash.Error.ResourceDslError,
"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"
end
end
end
)
end
test "fails if the destination is not an atom" do
assert_raise(
Ash.Error.ResourceDslError,
"relationships -> has_many -> foobar:\n related resource must be a module representing a resource",
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(
Ash.Error.ResourceDslError,
"relationships -> has_many:\n relationship_name must be an atom",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
has_many "foobar", Foobar
end
end
end
)
end
end
end