Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions test/aggregate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,61 @@
assert read_post.count_of_comments == 1
end

test "loading optimizable first aggregate with relationship filter does not cause binding errors" do

Check failure on line 104 in test/aggregate_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (14) / mix test

test loading optimizable first aggregate with relationship filter does not cause binding errors (AshSql.AggregateTest)

Check failure on line 104 in test/aggregate_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (16) / mix test

test loading optimizable first aggregate with relationship filter does not cause binding errors (AshSql.AggregateTest)

Check failure on line 104 in test/aggregate_test.exs

View workflow job for this annotation

GitHub Actions / ash-ci (15) / mix test

test loading optimizable first aggregate with relationship filter does not cause binding errors (AshSql.AggregateTest)
org =
Organization
|> Ash.Changeset.for_create(:create, %{name: "The Org"})
|> Ash.create!()

user =
User
|> Ash.Changeset.for_create(:create, %{})
|> Ash.Changeset.manage_relationship(:organization, org, type: :append_and_remove)
|> Ash.create!()

author =
Author
|> Ash.Changeset.for_create(:create, %{first_name: "John", last_name: "Doe"})
|> Ash.create!()

post =
Post
|> Ash.Changeset.for_create(:create, %{title: "Test Post"})
|> Ash.Changeset.manage_relationship(:organization, org, type: :append_and_remove)
|> Ash.Changeset.manage_relationship(:author, author, type: :append_and_remove)
|> Ash.create!()

comment =
Comment
|> Ash.Changeset.for_create(:create, %{title: "Test Comment"})
|> Ash.Changeset.manage_relationship(:post, post, type: :append_and_remove)
|> Ash.create!()

Rating
|> Ash.Changeset.for_create(:create, %{score: 5, resource_id: comment.id})
|> Ash.Changeset.set_context(%{data_layer: %{table: "comment_ratings"}})
|> Ash.create!()

# 1. Filter through :author adds a binding for the :author relationship
# 2. Policy joins through has_many which adds DISTINCT
# 3. limit(1) also triggers subquery wrapping condition
# 4. Loading :count_of_comment_ratings (non-optimizable, through [:comments, :ratings])
# triggers wrap_in_subquery_for_aggregates
# 5. Loading :author_first_name (optimizable first through belongs_to :author)
# after wrapping, code tries to reuse the :author binding from before wrapping
loaded_post =
Post
|> Ash.Query.filter(id == ^post.id and author.first_name == "John")
|> Ash.Query.limit(1)
|> Ash.Query.load([:author_first_name, :count_of_comment_ratings])
|> Ash.read!(actor: user)
|> hd()

assert loaded_post.id == post.id
assert loaded_post.author_first_name == "John"
assert loaded_post.count_of_comment_ratings == 1
end

test "nested filters on aggregates works" do
org =
Organization
Expand Down
Loading