Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#78 order by of a has many through not respected #144

Merged
merged 4 commits into from
Apr 25, 2022
Merged
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
2 changes: 1 addition & 1 deletion lib/dataloader/ecto.ex
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ if Code.ensure_loaded?(Ecto) do
records = records |> Enum.map(&Map.put(&1, field, empty))

results =
if query.limit || query.offset do
if query.limit || query.offset || Enum.any?(query.order_bys) do
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting! So is the reason that this is required is because it can have a separate ordering from the overall query? I'm curious about the performance implications of this.

Copy link
Contributor Author

@peaceful-james peaceful-james Apr 25, 2022

Choose a reason for hiding this comment

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

To be completely honest, I don't really know.

Last night was my first time reading this code. I was encountering the bug in my side-project.
I just saw that this if expression was a critical branching point. My has_many...through...order_by expressions were being swallowed by repo.preload and lost somewhere in there. So I decided to try to make them got through the lateral_preload path. When it fixed my problem, and no other tests were failing, I opened the PR.

While I'm being completely honest, I should say that all the tests pass even if I write

raise "This path is not tested"

where the repo.preload call is (the negative case of the if expression).

So that flow is completely untested.

records
|> preload_lateral(field, query, source.repo, repo_opts)
else
Expand Down
49 changes: 49 additions & 0 deletions test/dataloader/ecto_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,19 @@ defmodule Dataloader.EctoTest do
end

defp query(User, args, test_pid) do
{sort_order, args} = Map.pop(args, :sort_order)
{sort_by, args} = Map.pop(args, :sort_by)
send(test_pid, :querying)

User
|> where(^Enum.to_list(args))
|> then(fn user ->
if is_nil(sort_by) or is_nil(sort_order) do
user
else
order_by(user, {^sort_order, ^sort_by})
end
end)
end

defp query(queryable, _args, test_pid) do
Expand Down Expand Up @@ -324,6 +333,46 @@ defmodule Dataloader.EctoTest do
assert length(loaded_posts) == 2
end

test "order_by works", %{loader: loader} do
user1 = %User{username: "Ben Wilson"} |> Repo.insert!()
user2 = %User{username: "Bruce Williams"} |> Repo.insert!()

post1 = %Post{user_id: user1.id} |> Repo.insert!()

[
%Like{user_id: user1.id, post_id: post1.id},
%Like{user_id: user2.id, post_id: post1.id}
]
|> Enum.map(&Repo.insert/1)

for {sort_order, expected_usernames} <- [
{:asc, ["Ben Wilson", "Bruce Williams"]},
{:desc, ["Bruce Williams", "Ben Wilson"]}
] do
loader =
loader
|> Dataloader.load(
Test,
{:liking_users, [sort_order: sort_order, sort_by: :username]},
post1
)
|> Dataloader.run()

loaded_posts =
loader
|> Dataloader.get(
Test,
{:liking_users, [sort_order: sort_order, sort_by: :username]},
post1
)

ordered_usernames = Enum.map(loaded_posts, & &1.username)

assert ordered_usernames == expected_usernames,
"got #{inspect(ordered_usernames)} but was expecting #{inspect(expected_usernames)} for sort_order #{sort_order}"
end
end

test "works with query filtering", %{loader: loader} do
user1 = %User{username: "Ben Wilson"} |> Repo.insert!()
user2 = %User{username: "Bruce Williams"} |> Repo.insert!()
Expand Down