Skip to content

Commit 939f131

Browse files
authored
Fix a bug with limitToLast and cursors (#9675)
1 parent 2eefff0 commit 939f131

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

Firestore/Example/Tests/Integration/API/FIRQueryTests.mm

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,49 @@ - (void)testListenUnlistenRelistenSequenceOfMirrorQueries {
134134
XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
135135
}
136136

137+
- (void)testLimitToLastQueriesWithCursors {
138+
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
139+
@"a" : @{@"k" : @"a", @"sort" : @0},
140+
@"b" : @{@"k" : @"b", @"sort" : @1},
141+
@"c" : @{@"k" : @"c", @"sort" : @1},
142+
@"d" : @{@"k" : @"d", @"sort" : @2},
143+
}];
144+
145+
FIRQuerySnapshot *snapshot =
146+
[self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
147+
queryEndingBeforeValues:@[ @2 ]]];
148+
XCTAssertEqualObjects(
149+
FIRQuerySnapshotGetData(snapshot), (@[
150+
@{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
151+
]));
152+
153+
snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
154+
queryLimitedToLast:3] queryEndingAtValues:@[ @1 ]]];
155+
XCTAssertEqualObjects(
156+
FIRQuerySnapshotGetData(snapshot), (@[
157+
@{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
158+
]));
159+
160+
snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
161+
queryLimitedToLast:3] queryStartingAtValues:@[ @2 ]]];
162+
XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"d", @"sort" : @2} ]));
163+
snapshot =
164+
[self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
165+
queryStartingAfterValues:@[ @0 ]]];
166+
XCTAssertEqualObjects(
167+
FIRQuerySnapshotGetData(snapshot), (@[
168+
@{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
169+
]));
170+
171+
snapshot =
172+
[self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
173+
queryStartingAfterValues:@[ @-1 ]]];
174+
XCTAssertEqualObjects(
175+
FIRQuerySnapshotGetData(snapshot), (@[
176+
@{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
177+
]));
178+
}
179+
137180
- (void)testKeyOrderIsDescendingForDescendingInequality {
138181
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
139182
@"a" : @{@"foo" : @42},

Firestore/core/src/core/query.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,11 +325,11 @@ const Target& Query::ToTarget() const& {
325325
// We need to swap the cursors to match the now-flipped query ordering.
326326
auto new_start_at = end_at_
327327
? absl::optional<Bound>{Bound::FromValue(
328-
end_at_->position(), !end_at_->inclusive())}
328+
end_at_->position(), end_at_->inclusive())}
329329
: absl::nullopt;
330330
auto new_end_at =
331331
start_at_ ? absl::optional<Bound>{Bound::FromValue(
332-
start_at_->position(), !start_at_->inclusive())}
332+
start_at_->position(), start_at_->inclusive())}
333333
: absl::nullopt;
334334

335335
Target target(path(), collection_group(), filters(), new_order_bys,

0 commit comments

Comments
 (0)