ash/test/resource/relationships/belongs_to_test.exs

325 lines
8.6 KiB
Elixir
Raw Normal View History

2019-12-07 09:54:30 +13:00
defmodule Ash.Test.Resource.Relationships.BelongsToTest 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.BelongsTo
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
defmacrop defcomments(do: body) do
quote do
defmodule Comment do
@moduledoc false
use Ash.Resource, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
end
unquote(body)
end
end
end
2019-12-06 20:00:26 +13:00
describe "representation" do
test "it creates an attribute" do
defposts do
relationships do
2020-07-16 09:06:27 +12:00
belongs_to(:foobar, FooBar)
2019-12-06 20:00:26 +13:00
end
end
assert [
_,
%Ash.Resource.Attribute{
2019-12-06 20:00:26 +13:00
name: :foobar_id,
primary_key?: false,
type: Ash.Type.UUID,
private?: true
}
] = Ash.Resource.Info.attributes(Post)
end
test "it creates an attribute that honors private?" do
defposts do
relationships do
belongs_to(:foobar, FooBar, private?: true)
end
end
assert [
_,
%Ash.Resource.Attribute{
name: :foobar_id,
primary_key?: false,
type: Ash.Type.UUID,
private?: true
}
] = Ash.Resource.Info.attributes(Post)
2019-12-06 20:00:26 +13:00
end
test "it creates an attribute that honors attribute_writable?" do
defposts do
relationships do
belongs_to(:foobar, FooBar, attribute_writable?: true)
end
end
assert [
_,
%Ash.Resource.Attribute{
name: :foobar_id,
primary_key?: false,
type: Ash.Type.UUID,
writable?: true
}
] = Ash.Resource.Info.attributes(Post)
end
2019-12-06 20:00:26 +13:00
test "it creates a relationship" do
defposts do
relationships do
belongs_to(:foo, Foo)
belongs_to(:bar, Bar, source_attribute: :bazz, private?: true)
2019-12-06 20:00:26 +13:00
end
end
assert [
%BelongsTo{
2019-12-06 20:00:26 +13:00
cardinality: :one,
define_attribute?: true,
destination: Foo,
destination_attribute: :id,
attribute_type: :uuid,
name: :foo,
2019-12-06 20:00:26 +13:00
primary_key?: false,
source_attribute: :foo_id,
type: :belongs_to,
private?: false
},
%BelongsTo{
cardinality: :one,
define_attribute?: true,
destination: Bar,
destination_attribute: :id,
attribute_type: :uuid,
name: :bar,
primary_key?: false,
source_attribute: :bazz,
type: :belongs_to,
private?: true
2019-12-06 20:00:26 +13:00
}
] = Ash.Resource.Info.relationships(Post)
assert [%BelongsTo{name: :foo}] = Ash.Resource.Info.public_relationships(Post)
assert %BelongsTo{name: :foo} = Ash.Resource.Info.public_relationship(Post, :foo)
assert nil == Ash.Resource.Info.relationship(Post, :definitely_legit_relationship)
assert nil == Ash.Resource.Info.public_relationship(Post, :bar)
2019-12-06 20:00:26 +13:00
end
end
describe "validations" do
test "fails if destination_attribute is not an atom" do
2019-12-06 20:00:26 +13:00
assert_raise(
Spark.Error.DslError,
"[Ash.Test.Resource.Relationships.BelongsToTest.Post]\n relationships -> belongs_to -> foobar:\n invalid value for :destination_attribute option: expected atom, got: \"foo\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
belongs_to(:foobar, FooBar, destination_attribute: "foo")
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test "fails if source_attribute is not an atom" do
2019-12-06 20:00:26 +13:00
assert_raise(
Spark.Error.DslError,
"[Ash.Test.Resource.Relationships.BelongsToTest.Post]\n relationships -> belongs_to -> foobar:\n invalid value for :source_attribute option: expected atom, got: \"foo\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
belongs_to(:foobar, FooBar, source_attribute: "foo")
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test "fails if the destination is not an atom" do
assert_raise(
Spark.Error.DslError,
"[Ash.Test.Resource.Relationships.BelongsToTest.Post]\n relationships -> belongs_to -> foobar:\n invalid value for :destination option: expected atom, got: \"foobar\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
2020-07-16 09:06:27 +12:00
belongs_to(:foobar, "foobar")
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test "fails if the relationship name is not an atom" do
assert_raise(
Spark.Error.DslError,
"[Ash.Test.Resource.Relationships.BelongsToTest.Post]\n relationships -> belongs_to -> foobar:\n invalid value for :name option: expected atom, got: \"foobar\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
2020-07-16 09:06:27 +12:00
belongs_to("foobar", Foobar)
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test "fails if `primary_key?` is not a boolean" do
assert_raise(
Spark.Error.DslError,
"[Ash.Test.Resource.Relationships.BelongsToTest.Post]\n relationships -> belongs_to -> foobar:\n invalid value for :primary_key? option: expected boolean, got: \"blah\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
2020-07-16 09:06:27 +12:00
belongs_to(:foobar, Foobar, primary_key?: "blah")
2019-12-06 20:00:26 +13:00
end
end
end
)
end
test "fails if `private?` is not a boolean" do
assert_raise(
Spark.Error.DslError,
"[Ash.Test.Resource.Relationships.BelongsToTest.Post]\n relationships -> belongs_to -> foobar:\n invalid value for :private? option: expected boolean, got: \"blah\"",
fn ->
defposts do
relationships do
belongs_to(:foobar, Foobar, private?: "blah")
end
end
end
)
end
2019-12-06 20:00:26 +13:00
end
test "fails if `define_attribute?` is not a boolean" do
2019-12-06 20:00:26 +13:00
assert_raise(
Spark.Error.DslError,
"[Ash.Test.Resource.Relationships.BelongsToTest.Post]\n relationships -> belongs_to -> foobar:\n invalid value for :define_attribute? option: expected boolean, got: \"blah\"",
2019-12-06 20:00:26 +13:00
fn ->
defposts do
relationships do
belongs_to(:foobar, Foobar, define_attribute?: "blah")
2019-12-06 20:00:26 +13:00
end
end
end
)
end
2020-07-12 18:25:53 +12:00
test "fails in api initialization if the destination resource doesn't have the correct field" do
2020-07-09 16:20:32 +12:00
assert_raise(
Spark.Error.DslError,
2020-07-09 16:20:32 +12:00
~r/Relationship `post` expects source field `post_id` to be defined/,
fn ->
defposts do
relationships do
belongs_to(:post, __MODULE__, define_attribute?: false)
2020-07-09 16:20:32 +12:00
end
end
2020-07-12 18:25:53 +12:00
defmodule Registry do
@moduledoc false
use Ash.Registry
entries do
entry Post
end
end
2020-07-12 18:25:53 +12:00
defmodule Api do
use Ash.Api
resources do
registry Registry
2020-07-12 18:25:53 +12:00
end
end
2020-07-09 16:20:32 +12:00
end
)
end
test "don't add required error for belongs_to if other errors are present" do
defposts do
actions do
defaults [:create, :read, :update, :destroy]
end
relationships do
has_many(:comments, Comment)
end
end
defcomments do
actions do
defaults [:create]
end
relationships do
belongs_to(:post, Post, allow_nil?: false)
end
end
defmodule Registry do
@moduledoc false
use Ash.Registry
entries do
entry(Post)
entry(Comment)
end
end
defmodule Api do
@moduledoc false
use Ash.Api
resources do
registry Registry
end
end
assert {:error,
%Ash.Error.Invalid{
changeset: %{
errors: [
# there should not be a Ash.Error.Changes.Required error in this list
%Ash.Error.Invalid{path: [:post]}
]
}
}} =
Comment
|> Ash.Changeset.new()
|> Ash.Changeset.manage_relationship(:post, Ecto.UUID.generate(),
type: :append_and_remove
)
|> Api.create()
end
2019-12-06 20:00:26 +13:00
end