2020-09-24 13:26:16 +12:00
|
|
|
defmodule Ash.Test.Resource.AggregatesTest do
|
|
|
|
@moduledoc false
|
|
|
|
use ExUnit.Case, async: true
|
|
|
|
|
2020-11-03 09:33:14 +13:00
|
|
|
alias Ash.Resource.Aggregate
|
|
|
|
|
2020-09-24 13:26:16 +12:00
|
|
|
defmodule Comment do
|
|
|
|
@moduledoc false
|
2021-01-13 09:05:56 +13:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
2020-09-24 13:26:16 +12:00
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2020-09-24 13:26:16 +12:00
|
|
|
attribute :post_id, :uuid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmacrop defposts(do: body) do
|
|
|
|
quote do
|
|
|
|
defmodule Post do
|
|
|
|
@moduledoc false
|
2021-01-13 09:05:56 +13:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
2020-09-24 13:26:16 +12:00
|
|
|
|
|
|
|
attributes do
|
2021-01-13 09:40:55 +13:00
|
|
|
uuid_primary_key :id
|
2020-09-24 13:26:16 +12:00
|
|
|
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
|
2020-11-03 09:33:14 +13:00
|
|
|
count :another_count_but_private, :comments, private?: true
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
has_many :comments, Comment, destination_field: :post_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
2020-11-03 09:33:14 +13:00
|
|
|
%Aggregate{
|
2020-09-24 13:26:16 +12:00
|
|
|
name: :count_of_comments,
|
2020-11-03 09:33:14 +13:00
|
|
|
relationship_path: [:comments],
|
|
|
|
private?: false
|
|
|
|
},
|
|
|
|
%Aggregate{
|
|
|
|
name: :another_count_but_private,
|
|
|
|
relationship_path: [:comments],
|
|
|
|
private?: true
|
2020-09-24 13:26:16 +12:00
|
|
|
}
|
|
|
|
] = Ash.Resource.aggregates(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
|
|
|
assert [%Aggregate{name: :count_of_comments}] = Ash.Resource.public_aggregates(Post)
|
|
|
|
|
|
|
|
assert %Aggregate{name: :another_count_but_private} =
|
|
|
|
Ash.Resource.aggregate(Post, :another_count_but_private)
|
|
|
|
|
|
|
|
assert nil == Ash.Resource.public_aggregate(Post, :another_count_but_private)
|
|
|
|
|
|
|
|
assert nil == Ash.Resource.aggregate(Post, :totally_legit_aggregate)
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
2020-09-26 00:20:58 +12:00
|
|
|
|
|
|
|
test "Aggregate descriptions are allowed" do
|
|
|
|
defposts do
|
|
|
|
aggregates do
|
|
|
|
count :count_of_comments, :comments, description: "require one of name/contents"
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
has_many :comments, Comment, destination_field: :post_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Ash.Resource.Aggregate{description: "require one of name/contents"}
|
|
|
|
] = Ash.Resource.aggregates(Post)
|
|
|
|
end
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
end
|