Skip to content

Commit 96f1833

Browse files
committed
Add tests for distinct sort
1 parent f1b956a commit 96f1833

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

test/calculation_test.exs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,4 +1045,35 @@ defmodule AshPostgres.CalculationTest do
10451045
|> Ash.Query.sort("posts_with_matching_title.relevance_score")
10461046
|> Ash.read!()
10471047
end
1048+
1049+
test "calculation relationships can be used to sort" do
1050+
post =
1051+
Post
1052+
|> Ash.Changeset.for_create(:create, %{title: "test post"})
1053+
|> Ash.create!()
1054+
1055+
_comment1 =
1056+
Comment
1057+
|> Ash.Changeset.for_create(:create, %{title: "first comment", likes: 5})
1058+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
1059+
|> Ash.create!()
1060+
1061+
comment2 =
1062+
Comment
1063+
|> Ash.Changeset.for_create(:create, %{title: "second comment", likes: 8})
1064+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
1065+
|> Ash.create!()
1066+
1067+
_comment3 =
1068+
Comment
1069+
|> Ash.Changeset.for_create(:create, %{title: "third comment"})
1070+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
1071+
|> Ash.create!()
1072+
1073+
most_popular_comment = Ash.load!(post, :most_popular_comment).most_popular_comment
1074+
most_popular_comment = Ash.load!(most_popular_comment, :double_likes)
1075+
1076+
assert most_popular_comment.id == comment2.id
1077+
assert most_popular_comment.double_likes == 16
1078+
end
10481079
end

test/sort_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,34 @@ defmodule AshPostgres.SortTest do
267267
|> Ash.Query.sort({Ash.Sort.expr_sort(views.time, :datetime), :desc}, title: :asc)
268268
)
269269
end
270+
271+
test "has_one relationships can sort by attribute" do
272+
post =
273+
Post
274+
|> Ash.Changeset.for_create(:create, %{title: "test post"})
275+
|> Ash.create!()
276+
277+
_comment1 =
278+
Comment
279+
|> Ash.Changeset.for_create(:create, %{title: "first comment", likes: 5})
280+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
281+
|> Ash.create!()
282+
283+
comment2 =
284+
Comment
285+
|> Ash.Changeset.for_create(:create, %{title: "second comment", likes: 15})
286+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
287+
|> Ash.create!()
288+
289+
_comment3 =
290+
Comment
291+
|> Ash.Changeset.for_create(:create, %{title: "third comment", likes: 10})
292+
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
293+
|> Ash.create!()
294+
295+
most_liked_comment = Ash.load!(post, :most_liked_comment).most_liked_comment
296+
297+
assert most_liked_comment.id == comment2.id
298+
assert most_liked_comment.likes == 15
299+
end
270300
end

0 commit comments

Comments
 (0)