diff --git a/test/rel_with_parent_filter_test.exs b/test/rel_with_parent_filter_test.exs index 20a4082e..d8fa5161 100644 --- a/test/rel_with_parent_filter_test.exs +++ b/test/rel_with_parent_filter_test.exs @@ -2,6 +2,7 @@ defmodule AshPostgres.RelWithParentFilterTest do use AshPostgres.RepoCase, async: false alias AshPostgres.Test.Author + alias AshPostgres.Test.Post require Ash.Query @@ -75,4 +76,49 @@ defmodule AshPostgres.RelWithParentFilterTest do assert length(authors) == 1 end + + test "can stack parents" do + author = + Author + |> Ash.Changeset.for_create(:create, %{ + first_name: "abc" + }) + |> Ash.create!() + + Post + |> Ash.Changeset.for_create(:create, %{title: "the one", author_id: author.id}) + |> Ash.create!() + + assert %{author_from_exists: author_from_exists} = + Post + |> Ash.Query.for_read(:read) + |> Ash.Query.filter(title == "the one") + |> Ash.Query.load(:author_from_exists) + |> Ash.Query.limit(1) + |> Ash.read_one!() + + assert author_from_exists.id == author.id + end + + test "can filter with stack parents" do + author = + Author + |> Ash.Changeset.for_create(:create, %{ + first_name: "abc" + }) + |> Ash.create!() + + Post + |> Ash.Changeset.for_create(:create, %{title: "the one", author_id: author.id}) + |> Ash.create!() + + assert %{author_from_exists: author_from_exists} = + Post + |> Ash.Query.for_read(:read) + |> Ash.Query.filter(author_from_exists.first_name == "abc") + |> Ash.Query.load(:author_from_exists) + |> Ash.read_one!() + + assert author_from_exists.id == author.id + end end diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index e459cfa1..b6f4e3a5 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -559,6 +559,12 @@ defmodule AshPostgres.Test.Post do public?(true) end + has_one :author_from_exists, AshPostgres.Test.Author do + public?(true) + no_attributes?(true) + filter(expr(exists(posts, id == parent(parent(id))))) + end + has_many :co_author_posts, AshPostgres.Test.CoAuthorPost do public?(true)