Skip to content

Commit 27837d1

Browse files
authored
test: Add a failing test for sorting on relationship (#324)
* add a failing test for sorting on relationship * fix relationship pattern match * fix title sort
1 parent 9531ffa commit 27837d1

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

test/sort_test.exs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule AshPostgres.SortTest do
22
@moduledoc false
33
use AshPostgres.RepoCase, async: false
4-
alias AshPostgres.Test.{Comment, Post, PostLink}
4+
alias AshPostgres.Test.{Comment, Post, PostLink, PostView}
55

66
require Ash.Query
77
require Ash.Sort
@@ -224,4 +224,46 @@ defmodule AshPostgres.SortTest do
224224
|> Ash.Query.load(linked_posts: posts_query)
225225
|> Ash.read!()
226226
end
227+
228+
test "sorting on relationship attributes work" do
229+
post1 =
230+
Post
231+
|> Ash.Changeset.for_create(:create, %{title: "aaa", score: 0})
232+
|> Ash.create!()
233+
234+
view1 =
235+
PostView
236+
|> Ash.Changeset.for_action(:create, %{browser: :firefox, post_id: post1.id})
237+
|> Ash.create!()
238+
239+
post2 =
240+
Post
241+
|> Ash.Changeset.for_create(:create, %{title: "bbb", score: 0})
242+
|> Ash.create!()
243+
244+
view2 =
245+
PostView
246+
|> Ash.Changeset.for_action(:create, %{browser: :chrome, post_id: post2.id})
247+
|> Ash.create!()
248+
249+
assert [
250+
%{title: "aaa", views: [%{browser: :firefox}]},
251+
%{title: "bbb", views: [%{browser: :chrome}]}
252+
] =
253+
Ash.read!(
254+
Post
255+
|> Ash.Query.load(:views)
256+
|> Ash.Query.sort(title: :asc)
257+
)
258+
259+
assert [
260+
%{title: "bbb", views: [%{browser: :chrome}]},
261+
%{title: "aaa", views: [%{browser: :firefox}]}
262+
] =
263+
Ash.read!(
264+
Post
265+
|> Ash.Query.load(:views)
266+
|> Ash.Query.sort({Ash.Sort.expr_sort(views.time, :datetime), :desc}, title: :asc)
267+
)
268+
end
227269
end

0 commit comments

Comments
 (0)