ash/test/resource/relationships/has_one_test.exs
2020-06-01 01:14:23 -04:00

96 lines
2.4 KiB
Elixir

defmodule Ash.Test.Resource.Relationshihps.HasOneTest do
use ExUnit.Case, async: true
defmacrop defposts(do: body) do
quote do
defmodule Post do
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_one :foobar, FooBar
end
end
assert [
%Ash.Resource.Relationships.HasOne{
cardinality: :one,
destination: FooBar,
destination_field: :post_id,
name: :foobar,
source_field: :id,
type: :has_one
}
] = 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_one -> foobar:\n expected :destination_field to be an atom, got: \"foo\"",
fn ->
defposts do
relationships do
has_one :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_one -> foobar:\n expected :source_field to be an atom, got: \"foo\"",
fn ->
defposts do
relationships do
has_one :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_one -> foobar:\n related resource must be a module representing a resource",
fn ->
defposts do
relationships do
has_one :foobar, "foobar"
end
end
end
)
end
test "fails if the relationship name is not an atom" do
assert_raise(
Ash.Error.ResourceDslError,
"relationships -> has_one:\n relationship_name must be an atom",
fn ->
defposts do
relationships do
has_one "foobar", Foobar
end
end
end
)
end
end
end