Skip to content
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
43 changes: 43 additions & 0 deletions Firestore/Example/Tests/Integration/API/FIRQueryTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,49 @@ - (void)testListenUnlistenRelistenSequenceOfMirrorQueries {
XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), expected);
}

- (void)testLimitToLastQueriesWithCursors {
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
@"a" : @{@"k" : @"a", @"sort" : @0},
@"b" : @{@"k" : @"b", @"sort" : @1},
@"c" : @{@"k" : @"c", @"sort" : @1},
@"d" : @{@"k" : @"d", @"sort" : @2},
}];

FIRQuerySnapshot *snapshot =
[self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
queryEndingBeforeValues:@[ @2 ]]];
XCTAssertEqualObjects(
FIRQuerySnapshotGetData(snapshot), (@[
@{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
]));

snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
queryLimitedToLast:3] queryEndingAtValues:@[ @1 ]]];
XCTAssertEqualObjects(
FIRQuerySnapshotGetData(snapshot), (@[
@{@"k" : @"a", @"sort" : @0}, @{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}
]));

snapshot = [self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"]
queryLimitedToLast:3] queryStartingAtValues:@[ @2 ]]];
XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"d", @"sort" : @2} ]));
snapshot =
[self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
queryStartingAfterValues:@[ @0 ]]];
XCTAssertEqualObjects(
FIRQuerySnapshotGetData(snapshot), (@[
@{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
]));

snapshot =
[self readDocumentSetForRef:[[[collRef queryOrderedByField:@"sort"] queryLimitedToLast:3]
queryStartingAfterValues:@[ @-1 ]]];
XCTAssertEqualObjects(
FIRQuerySnapshotGetData(snapshot), (@[
@{@"k" : @"b", @"sort" : @1}, @{@"k" : @"c", @"sort" : @1}, @{@"k" : @"d", @"sort" : @2}
]));
}

- (void)testKeyOrderIsDescendingForDescendingInequality {
FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
@"a" : @{@"foo" : @42},
Expand Down
4 changes: 2 additions & 2 deletions Firestore/core/src/core/query.cc
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,11 @@ const Target& Query::ToTarget() const& {
// We need to swap the cursors to match the now-flipped query ordering.
auto new_start_at = end_at_
? absl::optional<Bound>{Bound::FromValue(
end_at_->position(), !end_at_->inclusive())}
end_at_->position(), end_at_->inclusive())}
: absl::nullopt;
auto new_end_at =
start_at_ ? absl::optional<Bound>{Bound::FromValue(
start_at_->position(), !start_at_->inclusive())}
start_at_->position(), start_at_->inclusive())}
: absl::nullopt;

Target target(path(), collection_group(), filters(), new_order_bys,
Expand Down