Skip to content

Commit 2131b84

Browse files
authored
Fix a bug with limitToLast and cursors (#3668)
* Feedback. * One more test
1 parent aa75720 commit 2131b84

File tree

2 files changed

+46
-2
lines changed
  • firebase-firestore/src

2 files changed

+46
-2
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/QueryTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,50 @@ public void testListenUnlistenRelistenSequenceOfMirrorQueries() {
156156
assertEquals(asList(map("k", "e", "sort", -1L), map("k", "a", "sort", -2L)), data);
157157
}
158158

159+
@Test
160+
public void testLimitToLastQueriesWithCursors() {
161+
CollectionReference collection =
162+
testCollectionWithDocs(
163+
map(
164+
"a", map("k", "a", "sort", 0),
165+
"b", map("k", "b", "sort", 1),
166+
"c", map("k", "c", "sort", 1),
167+
"d", map("k", "d", "sort", 2)));
168+
169+
Query query = collection.limitToLast(3).orderBy("sort").endBefore(2);
170+
QuerySnapshot set = waitFor(query.get());
171+
List<Map<String, Object>> data = querySnapshotToValues(set);
172+
assertEquals(
173+
asList(map("k", "a", "sort", 0L), map("k", "b", "sort", 1L), map("k", "c", "sort", 1L)),
174+
data);
175+
176+
query = collection.limitToLast(3).orderBy("sort").endAt(1);
177+
set = waitFor(query.get());
178+
data = querySnapshotToValues(set);
179+
assertEquals(
180+
asList(map("k", "a", "sort", 0L), map("k", "b", "sort", 1L), map("k", "c", "sort", 1L)),
181+
data);
182+
183+
query = collection.limitToLast(3).orderBy("sort").startAt(2);
184+
set = waitFor(query.get());
185+
data = querySnapshotToValues(set);
186+
assertEquals(asList(map("k", "d", "sort", 2L)), data);
187+
188+
query = collection.limitToLast(3).orderBy("sort").startAfter(0);
189+
set = waitFor(query.get());
190+
data = querySnapshotToValues(set);
191+
assertEquals(
192+
asList(map("k", "b", "sort", 1L), map("k", "c", "sort", 1L), map("k", "d", "sort", 2L)),
193+
data);
194+
195+
query = collection.limitToLast(3).orderBy("sort").startAfter(-1);
196+
set = waitFor(query.get());
197+
data = querySnapshotToValues(set);
198+
assertEquals(
199+
asList(map("k", "b", "sort", 1L), map("k", "c", "sort", 1L), map("k", "d", "sort", 2L)),
200+
data);
201+
}
202+
159203
@Test
160204
public void testKeyOrderIsDescendingForDescendingInequality() {
161205
CollectionReference collection =

firebase-firestore/src/main/java/com/google/firebase/firestore/core/Query.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,11 @@ public Target toTarget() {
497497
// We need to swap the cursors to match the now-flipped query ordering.
498498
Bound newStartAt =
499499
this.endAt != null
500-
? new Bound(this.endAt.getPosition(), !this.endAt.isInclusive())
500+
? new Bound(this.endAt.getPosition(), this.endAt.isInclusive())
501501
: null;
502502
Bound newEndAt =
503503
this.startAt != null
504-
? new Bound(this.startAt.getPosition(), !this.startAt.isInclusive())
504+
? new Bound(this.startAt.getPosition(), this.startAt.isInclusive())
505505
: null;
506506

507507
this.memoizedTarget =

0 commit comments

Comments
 (0)