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
|
2024-03-28 09:06:40 +13:00
|
|
|
alias Ash.Test.Domain, as: Domain
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2020-09-24 13:26:16 +12:00
|
|
|
defmodule Comment do
|
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, 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
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :post_id, :uuid do
|
|
|
|
public?(true)
|
|
|
|
end
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
2023-08-18 10:02:05 +12:00
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_many :likes, Like, destination_attribute: :comment_id, public?: true
|
2023-08-18 10:02:05 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Like do
|
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, data_layer: Ash.DataLayer.Ets
|
2023-08-18 10:02:05 +12:00
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
attribute :comment_id, :uuid do
|
|
|
|
public?(true)
|
|
|
|
end
|
2023-08-18 10:02:05 +12:00
|
|
|
end
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
defmacrop defposts(do: body) do
|
2024-03-28 09:06:40 +13:00
|
|
|
module = Module.concat(["rand#{System.unique_integer([:positive])}", Post])
|
|
|
|
|
2020-09-24 13:26:16 +12:00
|
|
|
quote do
|
2024-03-28 09:06:40 +13:00
|
|
|
defmodule unquote(module) do
|
2020-09-24 13:26:16 +12:00
|
|
|
@moduledoc false
|
2024-03-28 09:06:40 +13:00
|
|
|
use Ash.Resource, domain: Domain, 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
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
alias unquote(module), as: Post
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "representation" do
|
|
|
|
test "aggregates are persisted on the resource properly" do
|
|
|
|
defposts do
|
|
|
|
aggregates do
|
2024-03-28 09:06:40 +13:00
|
|
|
count :count_of_comments, :comments, public?: true
|
|
|
|
count :another_count_but_private, :comments
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_many :comments, Comment, destination_attribute: :post_id, public?: true
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
2020-11-03 09:33:14 +13:00
|
|
|
%Aggregate{
|
2020-09-24 13:26:16 +12:00
|
|
|
name: :count_of_comments,
|
2021-04-05 08:00:53 +12:00
|
|
|
kind: :count,
|
2020-11-03 09:33:14 +13:00
|
|
|
relationship_path: [:comments],
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: true
|
2020-11-03 09:33:14 +13:00
|
|
|
},
|
|
|
|
%Aggregate{
|
|
|
|
name: :another_count_but_private,
|
2021-04-05 08:00:53 +12:00
|
|
|
kind: :count,
|
2020-11-03 09:33:14 +13:00
|
|
|
relationship_path: [:comments],
|
2024-03-28 09:06:40 +13:00
|
|
|
public?: false
|
2020-09-24 13:26:16 +12:00
|
|
|
}
|
2021-02-23 14:29:31 +13:00
|
|
|
] = Ash.Resource.Info.aggregates(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-04-05 08:00:53 +12:00
|
|
|
assert [
|
2023-08-18 10:02:05 +12:00
|
|
|
%Aggregate{name: :count_of_comments}
|
2021-04-05 08:00:53 +12:00
|
|
|
] = Ash.Resource.Info.public_aggregates(Post)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
|
|
|
assert %Aggregate{name: :another_count_but_private} =
|
2021-02-23 14:29:31 +13:00
|
|
|
Ash.Resource.Info.aggregate(Post, :another_count_but_private)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert nil == Ash.Resource.Info.public_aggregate(Post, :another_count_but_private)
|
2020-11-03 09:33:14 +13:00
|
|
|
|
2021-02-23 14:29:31 +13:00
|
|
|
assert nil == Ash.Resource.Info.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
|
2024-03-28 09:06:40 +13:00
|
|
|
count :count_of_comments, :comments,
|
|
|
|
description: "require one of name/contents",
|
|
|
|
public?: true
|
2020-09-26 00:20:58 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_many :comments, Comment, destination_attribute: :post_id, public?: true
|
2020-09-26 00:20:58 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert [
|
|
|
|
%Ash.Resource.Aggregate{description: "require one of name/contents"}
|
2021-02-23 14:29:31 +13:00
|
|
|
] = Ash.Resource.Info.aggregates(Post)
|
2020-09-26 00:20:58 +12:00
|
|
|
end
|
2023-08-18 10:02:05 +12:00
|
|
|
|
|
|
|
test "aggregates field should be calculation or attribute on the resource" do
|
|
|
|
assert_raise Spark.Error.DslError, fn ->
|
|
|
|
defposts do
|
|
|
|
aggregates do
|
2024-03-28 09:06:40 +13:00
|
|
|
sum :sum_of_comment_likes, :comments, :likes do
|
|
|
|
public? true
|
|
|
|
end
|
2023-08-18 10:02:05 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2024-03-28 09:06:40 +13:00
|
|
|
has_many :comments, Comment, destination_attribute: :post_id, public?: true
|
2023-08-18 10:02:05 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|
2024-03-28 09:06:40 +13:00
|
|
|
|
|
|
|
test "it can load aggregates on resources which require an explicit domain" do
|
|
|
|
defmodule Leg do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
|
|
|
domain: nil,
|
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
validate_domain_inclusion?: false
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
attribute :side, :string, allow_nil?: false, public?: true
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
belongs_to :pants, Pants do
|
|
|
|
attribute_writable? true
|
|
|
|
public? true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
|
|
|
defaults [:create, :read]
|
|
|
|
default_accept :*
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Pants do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
|
|
|
domain: nil,
|
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
validate_domain_inclusion?: false
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key :id
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
has_many :legs, Leg
|
|
|
|
end
|
|
|
|
|
|
|
|
aggregates do
|
|
|
|
count :leg_count, :legs
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
|
|
|
defaults [:create, :read]
|
|
|
|
default_accept :*
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defmodule Clothing do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Domain, validate_config_inclusion?: false
|
|
|
|
|
|
|
|
resources do
|
|
|
|
allow_unregistered? true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
pants =
|
|
|
|
Pants
|
|
|
|
|> Ash.Changeset.for_create(:create, %{}, domain: Clothing)
|
|
|
|
|> Ash.create!(domain: Clothing)
|
|
|
|
|
|
|
|
~w[left right]
|
|
|
|
|> Enum.map(fn side ->
|
|
|
|
Leg
|
|
|
|
|> Ash.Changeset.for_create(:create, %{pants_id: pants.id, side: side}, domain: Clothing)
|
|
|
|
|> Ash.create!(domain: Clothing)
|
|
|
|
end)
|
|
|
|
|
|
|
|
Ash.load!(pants, :leg_count, domain: Clothing)
|
|
|
|
end
|
2020-09-24 13:26:16 +12:00
|
|
|
end
|