2019-12-07 09:54:30 +13:00
|
|
|
defmodule Ash.Test.Resource.Relationshihps.HasOneTest do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-06 20:00:26 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2020-11-03 09:33:14 +13:00
|
|
|
alias Ash.Resource.Relationships.HasOne
|
|
|
|
|
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
|
2020-06-14 18:39:11 +12:00
|
|
|
use Ash.Resource
|
2019-12-06 20:00:26 +13:00
|
|
|
|
2020-06-01 17:14:23 +12: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
|
2020-11-03 09:33:14 +13:00
|
|
|
has_one :foo, Foo, destination_field: :post_id
|
|
|
|
has_one :bar, Bar, destination_field: :post_id, private?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
2020-11-03 09:33:14 +13:00
|
|
|
%HasOne{
|
2019-12-06 20:00:26 +13:00
|
|
|
cardinality: :one,
|
2020-11-03 09:33:14 +13:00
|
|
|
destination: Foo,
|
2019-12-06 20:00:26 +13:00
|
|
|
destination_field: :post_id,
|
2020-11-03 09:33:14 +13:00
|
|
|
name: :foo,
|
2019-12-06 20:00:26 +13:00
|
|
|
source_field: :id,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: :has_one,
|
|
|
|
private?: false
|
|
|
|
},
|
|
|
|
%HasOne{
|
|
|
|
cardinality: :one,
|
|
|
|
destination: Bar,
|
|
|
|
destination_field: :post_id,
|
|
|
|
name: :bar,
|
|
|
|
source_field: :id,
|
|
|
|
type: :has_one,
|
|
|
|
private?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
}
|
2020-07-15 17:38:42 +12:00
|
|
|
] = Ash.Resource.relationships(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
|
|
|
assert [%HasOne{name: :foo}] = Ash.Resource.public_relationships(Post)
|
|
|
|
|
|
|
|
assert %HasOne{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.HasOne]\n relationships -> has_one -> 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_one :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.HasOne]\n relationships -> has_one -> foobar:\n expected :source_field to be an atom, got: \"foo\"",
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2020-06-05 15:34:44 +12:00
|
|
|
has_one :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.HasOne]\n relationships -> has_one -> foobar:\n expected :destination to be an atom, got: \"foobar\"",
|
2019-12-06 20:00:26 +13:00
|
|
|
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(
|
2020-07-16 09:06:27 +12:00
|
|
|
Ash.Error.Dsl.DslError,
|
2020-10-06 03:40:22 +13:00
|
|
|
"[Ash.Resource.Dsl.HasOne]\n relationships -> has_one -> foobar:\n expected :name to be an atom, got: \"foobar\"",
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
|
|
|
has_one "foobar", Foobar
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2020-11-03 09:33:14 +13:00
|
|
|
|
|
|
|
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.HasOne]\n relationships -> has_one -> foobar:\n expected :private? to be a boolean, got: \"foo\"",
|
2020-11-03 09:33:14 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
|
|
|
has_one :foobar, FooBar, private?: "foo", destination_field: :post_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|