ash/test/resource/identities_test.exs

106 lines
2.8 KiB
Elixir
Raw Normal View History

defmodule Ash.Test.Resource.IdentitiesTest do
@moduledoc false
use ExUnit.Case, async: true
defmacrop defposts(do: body) do
quote do
defmodule Post do
@moduledoc false
use Ash.Resource
attributes do
uuid_primary_key :id
attribute :name, :string
attribute :contents, :string
end
unquote(body)
end
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
read :read do
primary? true
end
end
feat: freeform expressions feat: validatiosn in actions feat: query arguments feat: add `Ash.Query.for_read/3` feat: return changeset with API errors feat: add case insensitive string `CiString`/`:ci_string` feat: support `context/1` and `arg/1` in filter templates feat: support targeting notifications with the `for` option feat: add `ago/2` query function feat: add basic arithmetic operators (+, *, -, /) feat: `sensitive?` option for attributes feat: `sensitive?` option for arguments feat: `private` arguments, which can’t be set using `for_<action>` feat: add `prevent_change` which will erase changes just before the changeset is committed feat: add `match?` validation that supports a custom error message feat: add `interval` type to support `ago/2` function feat: add `url_encoded_binary` type feat: add `function` type improvement: `changing?` is now a validation improvement: add `Transformer.get_persisted/3` improvement: add `api` field to `Notification` improvement: standardize errors, add `to_error_class` improvement: use `Comp` everywhere Improvement: use action on changeset if set by `for_<action_type>` improvement: `action_failed?` field on change sets improvement: remove ability for data layers to add operators (for now at least) Improvement: Changeset.apply_attributes/2 now returns an error tuple Improvement: add a bunch of new/informative errors improvement: runtime filter now uses left join logic (a naive implementation of it) improvement: support more filter templates in resources Improvement: basic/naive type system for operators/functions Fix: properly expand module aliases for options w/o compile time dependency chore(engine): track changeset changes for the request with `manage_changeset?: true`
2021-01-22 09:21:58 +13:00
identities do
2022-09-21 11:44:04 +12:00
identity :foobar, [:name, :contents], pre_check_with: Api
end
end
assert [%Ash.Resource.Identity{name: :foobar, keys: [:name, :contents]}] =
Ash.Resource.Info.identities(Post)
end
2022-04-07 04:22:34 +12:00
test "eager_check_with requires a primary read action" do
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
2022-09-21 11:44:04 +12:00
identity :foobar, [:name], eager_check_with: Api, pre_check_with: Api
2022-04-07 04:22:34 +12:00
end
end
end
end
test "Identity descriptions are allowed" do
defposts do
2022-05-29 03:22:43 +12:00
actions do
read :read do
primary? true
end
end
feat: freeform expressions feat: validatiosn in actions feat: query arguments feat: add `Ash.Query.for_read/3` feat: return changeset with API errors feat: add case insensitive string `CiString`/`:ci_string` feat: support `context/1` and `arg/1` in filter templates feat: support targeting notifications with the `for` option feat: add `ago/2` query function feat: add basic arithmetic operators (+, *, -, /) feat: `sensitive?` option for attributes feat: `sensitive?` option for arguments feat: `private` arguments, which can’t be set using `for_<action>` feat: add `prevent_change` which will erase changes just before the changeset is committed feat: add `match?` validation that supports a custom error message feat: add `interval` type to support `ago/2` function feat: add `url_encoded_binary` type feat: add `function` type improvement: `changing?` is now a validation improvement: add `Transformer.get_persisted/3` improvement: add `api` field to `Notification` improvement: standardize errors, add `to_error_class` improvement: use `Comp` everywhere Improvement: use action on changeset if set by `for_<action_type>` improvement: `action_failed?` field on change sets improvement: remove ability for data layers to add operators (for now at least) Improvement: Changeset.apply_attributes/2 now returns an error tuple Improvement: add a bunch of new/informative errors improvement: runtime filter now uses left join logic (a naive implementation of it) improvement: support more filter templates in resources Improvement: basic/naive type system for operators/functions Fix: properly expand module aliases for options w/o compile time dependency chore(engine): track changeset changes for the request with `manage_changeset?: true`
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",
2022-09-21 11:44:04 +12:00
pre_check_with: Api
end
end
assert [
%Ash.Resource.Identity{description: "require one of name/contents"}
] = Ash.Resource.Info.identities(Post)
end
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
use Ash.Resource
attributes do
uuid_primary_key :id
attribute :url, :string
end
end
defposts do
actions do
read :read do
primary? true
end
relationships do
belongs_to :site, Site
end
end
identities do
identity :name_site, [:naem, :site]
end
end
end
end
end
end