ash/test/resource/attributes_test.exs
2020-07-15 17:06:27 -04:00

76 lines
1.8 KiB
Elixir

defmodule Ash.Test.Resource.AttributesTest 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
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
end
unquote(body)
end
end
end
describe "representation" do
test "attributes are persisted on the resource properly" do
defposts do
attributes do
attribute :foo, :string
end
end
assert [_, %Ash.Resource.Attribute{name: :foo, type: Ash.Type.String, primary_key?: false}] =
Ash.Resource.attributes(Post)
end
end
describe "validation" do
test "raises if the attribute name is not an atom" do
assert_raise(
Ash.Error.Dsl.DslError,
"attributes -> attribute -> 10:\n expected :name to be an atom, got: 10",
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.Dsl.DslError,
"attributes -> attribute -> foo:\n Attribute type must be a built in type or a type module, got: 10",
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.Dsl.DslError,
"attributes -> attribute -> foo:\n expected :primary_key? to be an boolean, got: 10",
fn ->
defposts do
attributes do
attribute :foo, :string, primary_key?: 10
end
end
end
)
end
end
end