2019-12-07 09:54:30 +13:00
|
|
|
defmodule Ash.Test.Resource.AttributesTest do
|
2019-12-05 12:04:07 +13:00
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
|
|
|
defmacrop defposts(do: body) do
|
|
|
|
quote do
|
|
|
|
defmodule Post do
|
2019-12-06 20:00:26 +13:00
|
|
|
use Ash.Resource, name: "posts", type: "post", primary_key: false
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [%Ash.Resource.Attributes.Attribute{name: :foo, type: :string, primary_key?: false}] =
|
|
|
|
Ash.attributes(Post)
|
|
|
|
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(
|
|
|
|
Ash.Error.ResourceDslError,
|
2019-12-06 20:00:26 +13:00
|
|
|
"Attribute name must be an atom, got: 10 at attributes -> attribute",
|
2019-12-05 12:04:07 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
attribute 10, :string
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises if the type is not a known type" do
|
|
|
|
assert_raise(
|
|
|
|
Ash.Error.ResourceDslError,
|
2019-12-06 20:00:26 +13:00
|
|
|
"Attribute type must be a built in type or a type module, got: 10 at attributes -> attribute -> foo",
|
2019-12-05 12:04:07 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
attribute :foo, 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "raises if you pass an invalid value for `primary_key?`" do
|
|
|
|
assert_raise(
|
|
|
|
Ash.Error.ResourceDslError,
|
2019-12-06 20:00:26 +13:00
|
|
|
"option primary_key? at attributes -> attribute must be of type :boolean",
|
2019-12-05 12:04:07 +13:00
|
|
|
fn ->
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
attribute :foo, :string, primary_key?: 10
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|