2020-09-24 13:26:16 +12:00
|
|
|
defmodule Ash.Test.Resource.IdentitiesTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
|
|
|
|
2020-09-24 13:26:16 +12:00
|
|
|
defmacrop defposts(do: body) do
|
2024-03-28 09:06:40 +13:00
|
|
|
module = Module.concat(["rand#{System.unique_integer([:positive])}", Post])
|
|
|
|
|
2020-09-24 13:26:16 +12:00
|
|
|
quote do
|
2024-03-28 09:06:40 +13:00
|
|
|
defmodule unquote(module) do
|
2020-09-24 13:26:16 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain
|
2020-09-24 13:26:16 +12:00
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2020-09-24 13:26:16 +12:00
|
|
|
|
2024-03-28 09:06:40 +13:00
|
|
|
attribute :name, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
attribute :contents, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
unquote(body)
|
|
|
|
end
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
alias unquote(module), as: Post
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "representation" do
|
|
|
|
test "identities are persisted on the resource properly" do
|
|
|
|
defposts do
|
2022-05-29 03:22:43 +12:00
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
|
2022-05-29 03:22:43 +12:00
|
|
|
read :read do
|
|
|
|
primary? true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-22 09:21:58 +13:00
|
|
|
identities do
|
2024-03-28 09:06:40 +13:00
|
|
|
identity :foobar, [:name, :contents], pre_check_with: Domain
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [%Ash.Resource.Identity{name: :foobar, keys: [:name, :contents]}] =
|
2021-02-23 14:29:31 +13:00
|
|
|
Ash.Resource.Info.identities(Post)
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
2020-09-26 00:20:58 +12:00
|
|
|
|
2022-04-07 04:22:34 +12:00
|
|
|
test "eager_check_with requires a primary read action" do
|
2022-08-15 06:06:58 +12:00
|
|
|
assert_raise Spark.Error.DslError,
|
2022-04-07 04:22:34 +12:00
|
|
|
~r/but the resource has no primary read action./,
|
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
identities do
|
2024-03-28 09:06:40 +13:00
|
|
|
identity :foobar, [:name],
|
|
|
|
eager_check_with: Domain,
|
|
|
|
pre_check_with: Domain
|
2022-04-07 04:22:34 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-04-07 03:59:48 +12:00
|
|
|
end
|
|
|
|
|
2020-09-26 00:20:58 +12:00
|
|
|
test "Identity descriptions are allowed" do
|
|
|
|
defposts do
|
2022-05-29 03:22:43 +12:00
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
|
2022-05-29 03:22:43 +12:00
|
|
|
read :read do
|
|
|
|
primary? true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-22 09:21:58 +13:00
|
|
|
identities do
|
2022-05-29 03:22:43 +12:00
|
|
|
identity :foobar, [:name, :contents],
|
|
|
|
description: "require one of name/contents",
|
2024-03-28 09:06:40 +13:00
|
|
|
pre_check_with: Domain
|
2020-09-26 00:20:58 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Ash.Resource.Identity{description: "require one of name/contents"}
|
2021-02-23 14:29:31 +13:00
|
|
|
] = Ash.Resource.Info.identities(Post)
|
2020-09-26 00:20:58 +12:00
|
|
|
end
|
2023-06-20 17:05:41 +12:00
|
|
|
|
|
|
|
test "enforce identity constraints on attributes" do
|
|
|
|
assert_raise Spark.Error.DslError,
|
|
|
|
~r/All identity keys must be attributes. Got: :naem/,
|
|
|
|
fn ->
|
|
|
|
defmodule Site do
|
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain
|
2023-06-20 17:05:41 +12:00
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :url, :string do
|
|
|
|
public?(true)
|
|
|
|
end
|
2023-06-20 17:05:41 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defposts do
|
|
|
|
actions do
|
2024-03-28 09:06:40 +13:00
|
|
|
default_accept :*
|
|
|
|
|
2023-06-20 17:05:41 +12:00
|
|
|
read :read do
|
|
|
|
primary? true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
identities do
|
|
|
|
identity :name_site, [:naem, :site]
|
|
|
|
end
|
2023-07-13 17:38:21 +12:00
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
belongs_to :site, Site do
|
|
|
|
public?(true)
|
|
|
|
end
|
2023-07-13 17:38:21 +12:00
|
|
|
end
|
2023-06-20 17:05:41 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
end
|