ash/test/resource/attributes_test.exs

77 lines
1.8 KiB
Elixir
Raw Normal View History

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
defmacrop defposts(do: body) do
quote do
defmodule Post do
2020-06-02 17:47:25 +12:00
@moduledoc false
use Ash.Resource
2019-12-05 12:04:07 +13:00
attributes do
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
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
end
end
assert [_, %Ash.Resource.Attribute{name: :foo, type: Ash.Type.String, primary_key?: false}] =
2019-12-06 20:00:26 +13:00
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,
"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 the type is not a known type" do
assert_raise(
Ash.Error.ResourceDslError,
"attributes -> attribute -> foo:\n Attribute type must be a built in type or a type module, got: 10",
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,
"attributes -> attribute -> foo:\n expected :primary_key? to be an 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
end
end