2019-12-07 09:54:30 +13:00
|
|
|
defmodule Ash.Test.Resource.AttributesTest do
|
2020-06-02 17:47:25 +12:00
|
|
|
@moduledoc false
|
2019-12-05 12:04:07 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2020-11-03 09:33:14 +13:00
|
|
|
alias Ash.Resource.Attribute
|
|
|
|
|
2019-12-05 12:04:07 +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-05 12:04:07 +13:00
|
|
|
|
2020-06-30 16:31:07 +12:00
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2020-06-30 16:31:07 +12:00
|
|
|
end
|
|
|
|
|
2019-12-05 12:04:07 +13:00
|
|
|
unquote(body)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-06 20:00:26 +13:00
|
|
|
describe "representation" do
|
|
|
|
test "attributes are persisted on the resource properly" do
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
attribute :foo, :string
|
2020-11-03 09:33:14 +13:00
|
|
|
attribute :bar, :boolean, private?: true
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-03 09:33:14 +13:00
|
|
|
assert [
|
|
|
|
_,
|
|
|
|
%Attribute{name: :foo, type: Ash.Type.String, primary_key?: false},
|
|
|
|
%Attribute{
|
|
|
|
name: :bar,
|
|
|
|
type: Ash.Type.Boolean,
|
|
|
|
primary_key?: false,
|
|
|
|
private?: true
|
|
|
|
}
|
2021-02-23 14:29:31 +13:00
|
|
|
] = Ash.Resource.Info.attributes(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert [_, %Attribute{name: :foo}] = Ash.Resource.Info.public_attributes(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert %Attribute{name: :bar} = Ash.Resource.Info.attribute(Post, :bar)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert nil == Ash.Resource.Info.attribute(Post, :totally_valid_attributes)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert nil == Ash.Resource.Info.public_attribute(Post, :bar)
|
2019-12-06 20:00:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-05 12:04:07 +13:00
|
|
|
describe "validation" do
|
|
|
|
test "raises if the attribute name is not an atom" do
|
|
|
|
assert_raise(
|
2020-07-16 09:06:27 +12:00
|
|
|
Ash.Error.Dsl.DslError,
|
2022-03-16 10:41:25 +13:00
|
|
|
"[Ash.Test.Resource.AttributesTest.Post]\n attributes -> attribute -> 10:\n expected :name to be an atom, got: 10",
|
2019-12-05 12:04:07 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
attribute 10, :string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises if you pass an invalid value for `primary_key?`" do
|
|
|
|
assert_raise(
|
2020-07-16 09:06:27 +12:00
|
|
|
Ash.Error.Dsl.DslError,
|
2022-03-16 10:41:25 +13:00
|
|
|
"[Ash.Test.Resource.AttributesTest.Post]\n attributes -> attribute -> foo:\n expected :primary_key? to be a boolean, got: 10",
|
2019-12-05 12:04:07 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
attribute :foo, :string, primary_key?: 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2020-11-03 09:33:14 +13:00
|
|
|
|
|
|
|
test "raises if you pass an invalid value for `private?`" do
|
|
|
|
assert_raise(
|
|
|
|
Ash.Error.Dsl.DslError,
|
2022-03-16 10:41:25 +13:00
|
|
|
"[Ash.Test.Resource.AttributesTest.Post]\n attributes -> attribute -> foo:\n expected :private? to be a boolean, got: \"an_invalid_value\"",
|
2020-11-03 09:33:14 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
attribute :foo, :string, private?: "an_invalid_value"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
2019-12-05 12:04:07 +13:00
|
|
|
end
|
|
|
|
end
|