mirror of
https://github.com/ash-project/ash.git
synced 2024-09-20 13:33:20 +12:00
51 lines
1.1 KiB
Elixir
51 lines
1.1 KiB
Elixir
|
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
|