Skip to content

Commit ff3402e

Browse files
authored
test: test for default sort referencing parent field in many_to_many relationship with no_attributes? true (#607)
1 parent 22ab05e commit ff3402e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

test/parent_sort_test.ex

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
defmodule AshPostgres.Test.ParentSortTest do
2+
use AshPostgres.RepoCase, async: false
3+
4+
alias AshPostgres.Test.{Organization, Post, User}
5+
6+
require Ash.Query
7+
8+
test "can reference parent field when declaring default sort in has_many no_attributes? relationship" do
9+
organization =
10+
Organization
11+
|> Ash.Changeset.for_create(:create, %{name: "test_org"})
12+
|> Ash.create!()
13+
14+
user =
15+
User
16+
|> Ash.Changeset.for_create(:create, %{organization_id: organization.id, name: "foo bar"})
17+
|> Ash.create!()
18+
19+
Post
20+
|> Ash.Changeset.for_create(:create, %{organization_id: organization.id})
21+
|> Ash.create!()
22+
23+
Post
24+
|> Ash.Changeset.for_create(:create, %{organization_id: organization.id, title: "test_org"})
25+
|> Ash.create!()
26+
27+
assert {:ok, _} =
28+
Post
29+
|> Ash.Query.load(:recommendations)
30+
|> Ash.read(authorize?: false)
31+
end
32+
end

test/support/resources/post.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,12 @@ defmodule AshPostgres.Test.Post do
614614
filter(expr(fragment("? = ?", title, parent(organization.name))))
615615
end
616616

617+
has_many(:recommendations, __MODULE__) do
618+
public?(true)
619+
no_attributes?(true)
620+
sort([calc(fragment("abs(extract epoch from ?))", parent(datetime) - datetime))])
621+
end
622+
617623
belongs_to :parent_post, __MODULE__ do
618624
public?(true)
619625
end

0 commit comments

Comments
 (0)