Skip to content

Commit 7302636

Browse files
authored
test: that shows that parent stacking works in general but not with filter (#544)
1 parent 8579097 commit 7302636

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

test/rel_with_parent_filter_test.exs

+46
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ defmodule AshPostgres.RelWithParentFilterTest do
22
use AshPostgres.RepoCase, async: false
33

44
alias AshPostgres.Test.Author
5+
alias AshPostgres.Test.Post
56

67
require Ash.Query
78

@@ -75,4 +76,49 @@ defmodule AshPostgres.RelWithParentFilterTest do
7576

7677
assert length(authors) == 1
7778
end
79+
80+
test "can stack parents" do
81+
author =
82+
Author
83+
|> Ash.Changeset.for_create(:create, %{
84+
first_name: "abc"
85+
})
86+
|> Ash.create!()
87+
88+
Post
89+
|> Ash.Changeset.for_create(:create, %{title: "the one", author_id: author.id})
90+
|> Ash.create!()
91+
92+
assert %{author_from_exists: author_from_exists} =
93+
Post
94+
|> Ash.Query.for_read(:read)
95+
|> Ash.Query.filter(title == "the one")
96+
|> Ash.Query.load(:author_from_exists)
97+
|> Ash.Query.limit(1)
98+
|> Ash.read_one!()
99+
100+
assert author_from_exists.id == author.id
101+
end
102+
103+
test "can filter with stack parents" do
104+
author =
105+
Author
106+
|> Ash.Changeset.for_create(:create, %{
107+
first_name: "abc"
108+
})
109+
|> Ash.create!()
110+
111+
Post
112+
|> Ash.Changeset.for_create(:create, %{title: "the one", author_id: author.id})
113+
|> Ash.create!()
114+
115+
assert %{author_from_exists: author_from_exists} =
116+
Post
117+
|> Ash.Query.for_read(:read)
118+
|> Ash.Query.filter(author_from_exists.first_name == "abc")
119+
|> Ash.Query.load(:author_from_exists)
120+
|> Ash.read_one!()
121+
122+
assert author_from_exists.id == author.id
123+
end
78124
end

test/support/resources/post.ex

+6
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,12 @@ defmodule AshPostgres.Test.Post do
559559
public?(true)
560560
end
561561

562+
has_one :author_from_exists, AshPostgres.Test.Author do
563+
public?(true)
564+
no_attributes?(true)
565+
filter(expr(exists(posts, id == parent(parent(id)))))
566+
end
567+
562568
has_many :co_author_posts, AshPostgres.Test.CoAuthorPost do
563569
public?(true)
564570

0 commit comments

Comments
 (0)