test: add basic tests for resource entities

This commit is contained in:
Zach Daniel 2020-09-23 21:26:16 -04:00
parent 5a6d6487f2
commit 12891c3f38
No known key found for this signature in database
GPG key ID: C377365383138D4B
4 changed files with 167 additions and 0 deletions

View file

@ -0,0 +1,50 @@
defmodule Ash.Test.Resource.AggregatesTest do
@moduledoc false
use ExUnit.Case, async: true
defmodule Comment do
@moduledoc false
use Ash.Resource
attributes do
attribute :id, :uuid, primary_key?: true, default: &Ecto.UUID.generate/0
attribute :post_id, :uuid
end
end
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 "aggregates are persisted on the resource properly" do
defposts do
aggregates do
count :count_of_comments, :comments
end
relationships do
has_many :comments, Comment, destination_field: :post_id
end
end
assert [
%Ash.Resource.Aggregate{
name: :count_of_comments,
relationship_path: [:comments]
}
] = Ash.Resource.aggregates(Post)
end
end
end

View file

@ -0,0 +1,40 @@
defmodule Ash.Test.Resource.CalculationsTest 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
attribute :name, :string
attribute :contents, :string
end
unquote(body)
end
end
end
describe "representation" do
test "calculations are persisted on the resource properly" do
defposts do
calculations do
calculate :name_and_contents, concat([:name, :context])
end
end
assert [
%Ash.Resource.Calculation{
name: :name_and_contents,
calculation:
{Ash.Resource.Calculation.Concat, [keys: [:name, :context], separator: ""]}
}
] = Ash.Resource.calculations(Post)
end
end
end

View file

@ -0,0 +1,37 @@
defmodule Ash.Test.Resource.IdentitiesTest 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
attribute :name, :string
attribute :contents, :string
end
unquote(body)
end
end
end
describe "representation" do
test "identities are persisted on the resource properly" do
defposts do
resource do
identities do
identity :foobar, [:name, :contents]
end
end
end
assert [%Ash.Resource.Identity{name: :foobar, keys: [:name, :contents]}] =
Ash.Resource.identities(Post)
end
end
end

View file

@ -0,0 +1,40 @@
defmodule Ash.Test.Resource.ValidationsTest 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
attribute :name, :string
attribute :contents, :string
end
unquote(body)
end
end
end
describe "representation" do
test "validations are persisted on the resource properly" do
defposts do
validations do
validate present([:name, :contents], at_least: 1)
end
end
assert [
%Ash.Resource.Validation{
validation:
{Ash.Resource.Validation.Present,
[attributes: [:name, :contents], at_least: 1]}
}
] = Ash.Resource.validations(Post)
end
end
end