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
|
2020-06-01 17:14:23 +12:00
|
|
|
use Ash.Resource, name: "posts", type: "post"
|
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,
|
2020-06-01 17:14:23 +12:00
|
|
|
"attributes -> attribute:\n Attribute name must 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,
|
2020-06-01 17:14:23 +12:00
|
|
|
"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,
|
2020-06-01 17:14:23 +12:00
|
|
|
"attributes -> attribute:\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
|
2020-05-02 02:22:31 +12:00
|
|
|
|
|
|
|
describe "timestamps" do
|
|
|
|
test "it adds utc_datetime attributes" do
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
default = &DateTime.utc_now/0
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Ash.Resource.Attributes.Attribute{
|
|
|
|
allow_nil?: true,
|
|
|
|
default: ^default,
|
2020-06-01 17:14:23 +12:00
|
|
|
generated?: false,
|
2020-05-02 02:22:31 +12:00
|
|
|
name: :updated_at,
|
|
|
|
primary_key?: false,
|
|
|
|
type: :utc_datetime,
|
|
|
|
update_default: ^default,
|
2020-06-01 17:14:23 +12:00
|
|
|
writable?: false
|
2020-05-02 02:22:31 +12:00
|
|
|
},
|
|
|
|
%Ash.Resource.Attributes.Attribute{
|
|
|
|
allow_nil?: true,
|
|
|
|
default: ^default,
|
2020-06-01 17:14:23 +12:00
|
|
|
generated?: false,
|
2020-05-02 02:22:31 +12:00
|
|
|
name: :inserted_at,
|
|
|
|
primary_key?: false,
|
|
|
|
type: :utc_datetime,
|
|
|
|
update_default: nil,
|
2020-06-01 17:14:23 +12:00
|
|
|
writable?: false
|
2020-05-02 02:22:31 +12:00
|
|
|
}
|
|
|
|
] = Ash.attributes(Post)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it allows overwriting the field names" do
|
|
|
|
defposts do
|
|
|
|
attributes do
|
|
|
|
timestamps(inserted_at_field: :created_at, updated_at_field: :last_visited)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
default = &DateTime.utc_now/0
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Ash.Resource.Attributes.Attribute{
|
|
|
|
allow_nil?: true,
|
|
|
|
default: ^default,
|
|
|
|
name: :last_visited,
|
|
|
|
primary_key?: false,
|
|
|
|
type: :utc_datetime,
|
|
|
|
update_default: ^default,
|
2020-06-01 17:14:23 +12:00
|
|
|
writable?: false
|
2020-05-02 02:22:31 +12:00
|
|
|
},
|
|
|
|
%Ash.Resource.Attributes.Attribute{
|
|
|
|
allow_nil?: true,
|
|
|
|
default: ^default,
|
|
|
|
name: :created_at,
|
|
|
|
primary_key?: false,
|
|
|
|
type: :utc_datetime,
|
|
|
|
update_default: nil,
|
2020-06-01 17:14:23 +12:00
|
|
|
writable?: false
|
2020-05-02 02:22:31 +12:00
|
|
|
}
|
|
|
|
] = Ash.attributes(Post)
|
|
|
|
end
|
|
|
|
end
|
2019-12-05 12:04:07 +13:00
|
|
|
end
|