Skip to content

Commit f99d6fd

Browse files
committed
test: Add a test for an expression calculation using an aggregation in a sort
This replicates an issue seen. We have an expression calculation using a aggregation from a transitive relationship. If that calculation is used in a sort, the text of the expression is passed as the aggregate name, instead of a varaible name such as "aggregate0" etc.
1 parent 2c4e6fd commit f99d6fd

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

test/calculation_test.exs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,4 +1076,62 @@ defmodule AshPostgres.CalculationTest do
10761076
highest_score = hd(Enum.sort(scores, :desc))
10771077
assert post_with_highest_score.score == highest_score
10781078
end
1079+
1080+
test "an expression calculation can use an aggregate" do
1081+
post1 =
1082+
Post
1083+
|> Ash.Changeset.for_create(:create, %{title: "my post"})
1084+
|> Ash.create!()
1085+
1086+
_post2 =
1087+
Post
1088+
|> Ash.Changeset.for_create(:create, %{title: "another post"})
1089+
|> Ash.create!()
1090+
1091+
author =
1092+
Author
1093+
|> Ash.Changeset.for_create(:create, %{
1094+
first_name: "ashley"
1095+
})
1096+
|> Ash.create!()
1097+
1098+
Comment
1099+
|> Ash.Changeset.for_create(:create, %{title: "first comment"})
1100+
|> Ash.Changeset.manage_relationship(:post, post1, type: :append_and_remove)
1101+
|> Ash.create!()
1102+
1103+
first_comment_by_author =
1104+
Comment
1105+
|> Ash.Changeset.for_create(:create, %{title: "first comment by Ashley"})
1106+
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
1107+
|> Ash.Changeset.manage_relationship(:post, post1, type: :append_and_remove)
1108+
|> Ash.create!()
1109+
1110+
Comment
1111+
|> Ash.Changeset.for_create(:create, %{title: "another comment"})
1112+
|> Ash.Changeset.manage_relationship(:post, post1, type: :append_and_remove)
1113+
|> Ash.create!()
1114+
1115+
second_comment_by_author =
1116+
Comment
1117+
|> Ash.Changeset.for_create(:create, %{title: "first comment by Ashley"})
1118+
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
1119+
|> Ash.Changeset.manage_relationship(:post, post1, type: :append_and_remove)
1120+
|> Ash.create!()
1121+
1122+
post1 =
1123+
Post
1124+
|> Ash.Query.for_read(:read_by_comment_author, %{
1125+
author_id: author.id
1126+
})
1127+
|> Ash.Query.sort_input([
1128+
{"datetime_of_first_comment_by_author", {%{author_id: author.id}, :desc}}
1129+
])
1130+
|> Ash.read_one!()
1131+
1132+
assert DateTime.compare(
1133+
post1.datetime_of_first_comment_by_author,
1134+
first_comment_by_author.created_at
1135+
)
1136+
end
10791137
end

test/support/resources/post.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,20 @@ defmodule AshPostgres.Test.Post do
488488
pagination(keyset?: true, default_limit: 25)
489489
filter(expr(count_nils(latest_comment.linked_comment_post_ids) == 0))
490490
end
491+
492+
read :read_by_comment_author do
493+
argument(:author_id, :uuid, allow_nil?: false)
494+
495+
filter(expr(comments.author_id == ^arg(:author_id)))
496+
497+
prepare(
498+
build(
499+
load: [
500+
datetime_of_first_comment_by_author: %{author_id: arg(:author_id)}
501+
]
502+
)
503+
)
504+
end
491505
end
492506

493507
identities do
@@ -1030,6 +1044,20 @@ defmodule AshPostgres.Test.Post do
10301044
calculate(:latest_comment_title, :string, expr(latest_comment.title), allow_nil?: true)
10311045

10321046
calculate(:concated_comment_titles, :string, expr(fragment("concat(?)", comment_titles)))
1047+
1048+
calculate :datetime_of_first_comment_by_author,
1049+
:utc_datetime_usec,
1050+
expr(
1051+
first(comments,
1052+
field: :created_at,
1053+
query: [
1054+
filter: author_id == ^arg(:author_id)
1055+
]
1056+
)
1057+
) do
1058+
public?(true)
1059+
argument(:author_id, :uuid, allow_nil?: false)
1060+
end
10331061
end
10341062

10351063
aggregates do

0 commit comments

Comments
 (0)