mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 05:23:03 +12:00
test: add basic tests for resource entities
This commit is contained in:
parent
5a6d6487f2
commit
12891c3f38
4 changed files with 167 additions and 0 deletions
50
test/resource/aggregates_test.exs
Normal file
50
test/resource/aggregates_test.exs
Normal 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
|
40
test/resource/calculations_test.exs
Normal file
40
test/resource/calculations_test.exs
Normal 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
|
37
test/resource/identities_test.exs
Normal file
37
test/resource/identities_test.exs
Normal 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
|
40
test/resource/validations_test.exs
Normal file
40
test/resource/validations_test.exs
Normal 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
|
Loading…
Reference in a new issue