Skip to content
Open
Show file tree
Hide file tree
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
37 changes: 37 additions & 0 deletions test/filter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,6 +1151,43 @@
|> length == 1
end

test "using exists with has_many with limit" do

Check failure on line 1154 in test/filter_test.exs

View workflow job for this annotation

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

test using exists with has_many with limit (AshPostgres.FilterTest)

Check failure on line 1154 in test/filter_test.exs

View workflow job for this annotation

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

test using exists with has_many with limit (AshPostgres.FilterTest)

Check failure on line 1154 in test/filter_test.exs

View workflow job for this annotation

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

test using exists with has_many with limit (AshPostgres.FilterTest)
comment =
Comment
|> Ash.Changeset.for_create(:create, %{title: "test"})
|> Ash.create!()

for score <- 1..5 do
Ash.Changeset.for_create(AshPostgres.Test.Rating, :create, %{
score: score,
resource_id: comment.id
})
|> Ash.create!(context: %{data_layer: %{table: "comment_ratings"}})
end

# The top_ratings relationship has limit: 2 and sort: [score: :desc]
# So it should only include ratings with scores 5 and 4
assert Comment
|> Ash.Query.filter(exists(top_ratings, score == 5))
|> Ash.read!()
|> length() == 1

assert Comment
|> Ash.Query.filter(exists(top_ratings, score == 4))
|> Ash.read!()
|> length() == 1

assert Comment
|> Ash.Query.filter(exists(top_ratings, score == 3))
|> Ash.read!()
|> length() == 0

assert Comment
|> Ash.Query.filter(exists(top_ratings, score == 1))
|> Ash.read!()
|> length() == 0
end

test "using `(is_nil(relationship) and other_relation_filter)` will trigger left join" do
organization =
Organization
Expand Down
7 changes: 7 additions & 0 deletions test/support/resources/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ defmodule AshPostgres.Test.Comment do
relationship_context: %{data_layer: %{table: "comment_ratings"}}
)

has_many :top_ratings, AshPostgres.Test.Rating do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the solution in ash_sql, would it be possible to add and test a relationship here that also has a parent/1 expression in its filter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's another issue I haven't quite managed to figure out, parents and exists seem to be a bit weird, filters in the wrong places

destination_attribute(:resource_id)
relationship_context(%{data_layer: %{table: "comment_ratings"}})
sort(score: :desc)
limit(2)
end

has_many(:popular_ratings, AshPostgres.Test.Rating,
public?: true,
destination_attribute: :resource_id,
Expand Down
Loading