defmodule Ash.Test.Resource.Relationshihps.HasManyTest do @moduledoc false use ExUnit.Case, async: true defmacrop defposts(do: body) do quote do defmodule Post do @moduledoc false use Ash.Resource, name: "posts", type: "post" attributes do attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0 end 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\"", 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\"", 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", 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", fn -> defposts do relationships do has_many "foobar", Foobar end end end ) end end end