|
1 | 1 | defmodule AshPostgres.SortTest do
|
2 | 2 | @moduledoc false
|
3 | 3 | use AshPostgres.RepoCase, async: false
|
4 |
| - alias AshPostgres.Test.{Comment, Post, PostLink} |
| 4 | + alias AshPostgres.Test.{Comment, Post, PostLink, PostView} |
5 | 5 |
|
6 | 6 | require Ash.Query
|
7 | 7 | require Ash.Sort
|
@@ -224,4 +224,46 @@ defmodule AshPostgres.SortTest do
|
224 | 224 | |> Ash.Query.load(linked_posts: posts_query)
|
225 | 225 | |> Ash.read!()
|
226 | 226 | 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 |
227 | 269 | end
|
0 commit comments