Skip to content

Commit

Permalink
fix(firestore): correct cursor flipping in limit_to_last (#28137)
Browse files Browse the repository at this point in the history
  • Loading branch information
sarub0b0 authored Jan 6, 2025
1 parent bd86347 commit 13b1dde
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions google-cloud-firestore/lib/google/cloud/firestore/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,12 @@ def limit_to_last num
new_start_at = new_query.end_at.dup
if new_end_at
new_end_at.before = !new_end_at.before
new_query.end_at = new_end_at
end
if new_start_at
new_start_at.before = !new_start_at.before
new_query.start_at = new_start_at
end
new_query.end_at = new_end_at
new_query.start_at = new_start_at
end

new_query.limit = Google::Protobuf::Int32Value.new value: num
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,46 @@
assert_results_enum results_enum
end

it "runs a query with order, start_at, and limit_to_last" do
expected_query = Google::Cloud::Firestore::V1::StructuredQuery.new(
end_at: Google::Cloud::Firestore::V1::Cursor.new(values: [Google::Cloud::Firestore::Convert.raw_to_value("foo")], before: false),
from: [Google::Cloud::Firestore::V1::StructuredQuery::CollectionSelector.new(collection_id: "messages")],
order_by: [
Google::Cloud::Firestore::V1::StructuredQuery::Order.new(
field: Google::Cloud::Firestore::V1::StructuredQuery::FieldReference.new(field_path: "name"),
direction: :DESCENDING),
Google::Cloud::Firestore::V1::StructuredQuery::Order.new(
field: Google::Cloud::Firestore::V1::StructuredQuery::FieldReference.new(field_path: "__name__"),
direction: :ASCENDING)],
limit: Google::Protobuf::Int32Value.new(value: 2)
)
firestore_mock.expect :run_query, query_results_descending_enum, run_query_args(expected_query, parent: collection.parent_path)

results_enum = collection.order(:name).order(firestore.document_id, :desc).start_at(:foo).limit_to_last(2).get

assert_results_enum results_enum
end

it "runs a query with order, end_at, and limit_to_last" do
expected_query = Google::Cloud::Firestore::V1::StructuredQuery.new(
start_at: Google::Cloud::Firestore::V1::Cursor.new(values: [Google::Cloud::Firestore::Convert.raw_to_value("bar")], before: true),
from: [Google::Cloud::Firestore::V1::StructuredQuery::CollectionSelector.new(collection_id: "messages")],
order_by: [
Google::Cloud::Firestore::V1::StructuredQuery::Order.new(
field: Google::Cloud::Firestore::V1::StructuredQuery::FieldReference.new(field_path: "name"),
direction: :DESCENDING),
Google::Cloud::Firestore::V1::StructuredQuery::Order.new(
field: Google::Cloud::Firestore::V1::StructuredQuery::FieldReference.new(field_path: "__name__"),
direction: :ASCENDING)],
limit: Google::Protobuf::Int32Value.new(value: 2)
)
firestore_mock.expect :run_query, query_results_descending_enum, run_query_args(expected_query, parent: collection.parent_path)

results_enum = collection.order(:name).order(firestore.document_id, :desc).end_at(:bar).limit_to_last(2).get

assert_results_enum results_enum
end

it "updates limit but does not reflip cursors when calling limit_to_last more than once" do
expected_query = Google::Cloud::Firestore::V1::StructuredQuery.new(
start_at: Google::Cloud::Firestore::V1::Cursor.new(values: [Google::Cloud::Firestore::Convert.raw_to_value("bar")], before: true),
Expand Down

0 comments on commit 13b1dde

Please sign in to comment.