Skip to content

Fix a bug with limitToLast and cursors #3668

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

Merged
merged 2 commits into from
Apr 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,50 @@ public void testListenUnlistenRelistenSequenceOfMirrorQueries() {
assertEquals(asList(map("k", "e", "sort", -1L), map("k", "a", "sort", -2L)), data);
}

@Test
public void testLimitToLastQueriesWithCursors() {
CollectionReference collection =
testCollectionWithDocs(
map(
"a", map("k", "a", "sort", 0),
"b", map("k", "b", "sort", 1),
"c", map("k", "c", "sort", 1),
"d", map("k", "d", "sort", 2)));

Query query = collection.limitToLast(3).orderBy("sort").endBefore(2);
QuerySnapshot set = waitFor(query.get());
List<Map<String, Object>> data = querySnapshotToValues(set);
assertEquals(
asList(map("k", "a", "sort", 0L), map("k", "b", "sort", 1L), map("k", "c", "sort", 1L)),
data);

query = collection.limitToLast(3).orderBy("sort").endAt(1);
set = waitFor(query.get());
data = querySnapshotToValues(set);
assertEquals(
asList(map("k", "a", "sort", 0L), map("k", "b", "sort", 1L), map("k", "c", "sort", 1L)),
data);

query = collection.limitToLast(3).orderBy("sort").startAt(2);
set = waitFor(query.get());
data = querySnapshotToValues(set);
assertEquals(asList(map("k", "d", "sort", 2L)), data);

query = collection.limitToLast(3).orderBy("sort").startAfter(0);
set = waitFor(query.get());
data = querySnapshotToValues(set);
assertEquals(
asList(map("k", "b", "sort", 1L), map("k", "c", "sort", 1L), map("k", "d", "sort", 2L)),
data);

query = collection.limitToLast(3).orderBy("sort").startAfter(-1);
set = waitFor(query.get());
data = querySnapshotToValues(set);
assertEquals(
asList(map("k", "b", "sort", 1L), map("k", "c", "sort", 1L), map("k", "d", "sort", 2L)),
data);
}

@Test
public void testKeyOrderIsDescendingForDescendingInequality() {
CollectionReference collection =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,11 @@ public Target toTarget() {
// We need to swap the cursors to match the now-flipped query ordering.
Bound newStartAt =
this.endAt != null
? new Bound(this.endAt.getPosition(), !this.endAt.isInclusive())
? new Bound(this.endAt.getPosition(), this.endAt.isInclusive())
: null;
Bound newEndAt =
this.startAt != null
? new Bound(this.startAt.getPosition(), !this.startAt.isInclusive())
? new Bound(this.startAt.getPosition(), this.startAt.isInclusive())
: null;

this.memoizedTarget =
Expand Down