2022-03-16 10:41:25 +13:00
|
|
|
defmodule Ash.Test.Resource.Relationships.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
|
2024-03-28 09:06:40 +13:00
|
|
|
module = Module.concat(["rand#{System.unique_integer([:positive])}", Post])
|
|
|
|
|
2019-12-06 20:00:26 +13:00
|
|
|
quote do
|
2024-03-28 09:06:40 +13:00
|
|
|
defmodule unquote(module) do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Ash.Test.Domain, data_layer: Ash.DataLayer.Ets
|
2019-12-06 20:00:26 +13:00
|
|
|
|
2020-06-01 17:14:23 +12:00
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2020-06-01 17:14:23 +12:00
|
|
|
end
|
|
|
|
|
2019-12-06 20:00:26 +13:00
|
|
|
unquote(body)
|
|
|
|
end
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
alias unquote(module), as: Post
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "representation" do
|
|
|
|
test "it creates a relationship" do
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_one :foo, Foo, destination_attribute: :post_id, public?: true
|
|
|
|
has_one :bar, Bar, destination_attribute: :post_id
|
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,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: :post_id,
|
2020-11-03 09:33:14 +13:00
|
|
|
name: :foo,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: :id,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: :has_one,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: true
|
2020-11-03 09:33:14 +13:00
|
|
|
},
|
|
|
|
%HasOne{
|
|
|
|
cardinality: :one,
|
|
|
|
destination: Bar,
|
2022-08-16 06:00:02 +12:00
|
|
|
destination_attribute: :post_id,
|
2020-11-03 09:33:14 +13:00
|
|
|
name: :bar,
|
2022-08-16 06:00:02 +12:00
|
|
|
source_attribute: :id,
|
2020-11-03 09:33:14 +13:00
|
|
|
type: :has_one,
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: false
|
2019-12-06 20:00:26 +13:00
|
|
|
}
|
2021-02-23 14:29:31 +13:00
|
|
|
] = Ash.Resource.Info.relationships(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert [%HasOne{name: :foo}] = Ash.Resource.Info.public_relationships(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert %HasOne{name: :foo} = Ash.Resource.Info.public_relationship(Post, :foo)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert nil == Ash.Resource.Info.relationship(Post, :definitely_legit_relationship)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert nil == Ash.Resource.Info.public_relationship(Post, :bar)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "validations" do
|
2022-08-16 06:00:02 +12:00
|
|
|
test "fails if destination_attribute is not an atom" do
|
2019-12-06 20:00:26 +13:00
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :destination_attribute option: expected atom, got: "foo"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_one :foobar, FooBar, destination_attribute: "foo", public?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-08-16 06:00:02 +12:00
|
|
|
test "fails if source_attribute is not an atom" do
|
2019-12-06 20:00:26 +13:00
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :source_attribute option: expected atom, got: "foo"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_one :foobar, FooBar,
|
|
|
|
source_attribute: "foo",
|
|
|
|
destination_attribute: :post_id,
|
|
|
|
public?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "fails if the destination is not an atom" do
|
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/expected module in :destination option, got: "foobar"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_one :foobar, "foobar" do
|
|
|
|
public?(true)
|
|
|
|
end
|
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(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :name option: expected atom, got: "foobar"/,
|
2019-12-06 20:00:26 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_one "foobar", Foobar do
|
|
|
|
public?(true)
|
|
|
|
end
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
test "fails if public? is not an boolean" do
|
2020-11-03 09:33:14 +13:00
|
|
|
assert_raise(
|
2022-08-15 06:06:58 +12:00
|
|
|
Spark.Error.DslError,
|
2024-03-28 09:06:40 +13:00
|
|
|
~r/invalid value for :public\? option: expected boolean, got: "foo"/,
|
2020-11-03 09:33:14 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_one :foobar, FooBar,
|
|
|
|
public?: "foo",
|
|
|
|
destination_attribute: :post_id
|
2020-11-03 09:33:14 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|