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

Fixes bug that generates queries with cursor conditions out of order #73

Merged
merged 1 commit into from
Aug 18, 2020
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
5 changes: 4 additions & 1 deletion lib/paginator/ecto/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ defmodule Paginator.Ecto.Query do
end

defp filter_values(query, fields, values, cursor_direction) when is_map(values) do
sorts = Enum.reject(values, fn val -> match?({_column, nil}, val) end)
sorts =
fields
|> Enum.map(fn {column, _order} -> {column, Map.get(values, column)} end)
|> Enum.reject(fn val -> match?({_column, nil}, val) end)

dynamic_sorts =
sorts
Expand Down
22 changes: 16 additions & 6 deletions test/paginator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -305,19 +305,29 @@ defmodule PaginatorTest do
customers: {c1, _c2, _c3},
payments: {_p1, _p2, _p3, _p4, p5, p6, p7, p8, _p9, _p10, _p11, _p12}
} do
%Page{entries: entries, metadata: metadata} =
customer_payments_by_charged_at_and_amount(c1)
|> Repo.paginate(cursor_fields: [:charged_at, :amount, :id], limit: 2)

assert to_ids(entries) == to_ids([p5, p6])

%Page{entries: entries, metadata: _metadata} =
customer_payments_by_amount(c1)
|> Repo.paginate(cursor_fields: [:amount, :charged_at, :id], limit: 50)
customer_payments_by_charged_at_and_amount(c1)
|> Repo.paginate(
cursor_fields: [:charged_at, :amount, :id],
limit: 2,
after: metadata.after
)

assert to_ids(entries) == to_ids([p6, p5, p7, p8])
assert to_ids(entries) == to_ids([p7, p8])
end

test "before cursor with multiple cursor_fields and pre-existing where filter in query", %{
customers: {c1, _c2, _c3},
payments: {_p1, _p2, _p3, _p4, _p5, p6, _p7, _p8, _p9, _p10, _p11, _p12}
} do
assert %Page{entries: [], metadata: _metadata} =
customer_payments_by_amount(c1)
customer_payments_by_charged_at_and_amount(c1)
|> Repo.paginate(
cursor_fields: [:amount, :charged_at, :id],
before:
Expand Down Expand Up @@ -1019,11 +1029,11 @@ defmodule PaginatorTest do
)
end

defp customer_payments_by_amount(customer, direction \\ :asc) do
defp customer_payments_by_charged_at_and_amount(customer, direction \\ :asc) do
from(
p in Payment,
where: p.customer_id == ^customer.id,
order_by: [{^direction, p.amount}, {^direction, p.charged_at}, {^direction, p.id}]
order_by: [{^direction, p.charged_at}, {^direction, p.amount}, {^direction, p.id}]
)
end

Expand Down