Skip to content

Commit d810015

Browse files
committed
Update pagination test to check queries.
1 parent c66b1ff commit d810015

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

firestore/src/main/java/com/example/firestore/snippets/QueryDataSnippets.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -359,27 +359,28 @@ Query createStartAtSnapshotQueryCursor()
359359
/**
360360
* Example of a paginated query.
361361
*/
362-
void paginateCursor() throws InterruptedException, ExecutionException, TimeoutException {
362+
List<Query> paginateCursor() throws InterruptedException, ExecutionException, TimeoutException {
363363
// [START fs_paginate_cursor]
364364
// Construct query for first 25 cities, ordered by population.
365365
CollectionReference cities = db.collection("cities");
366-
ApiFuture<QuerySnapshot> firstPage = cities
366+
Query firstPage = cities
367367
.orderBy("population")
368-
.limit(25)
369-
.get();
368+
.limit(25);
370369

371370
// Wait for the results of the API call, waiting for a maximum of 30 seconds for a result.
372-
List<QueryDocumentSnapshot> docs = firstPage.get(30, TimeUnit.SECONDS).getDocuments();
371+
ApiFuture<QuerySnapshot> future = firstPage.get();
372+
List<QueryDocumentSnapshot> docs = future.get(30, TimeUnit.SECONDS).getDocuments();
373373

374374
// Construct query for the next 25 cities.
375375
QueryDocumentSnapshot lastDoc = docs.get(docs.size() - 1);
376-
ApiFuture<QuerySnapshot> secondPage = cities
376+
Query secondPage = cities
377377
.orderBy("population")
378378
.startAfter(lastDoc)
379-
.limit(25)
380-
.get();
379+
.limit(25);
381380

382-
docs = secondPage.get(30, TimeUnit.SECONDS).getDocuments();
381+
future = secondPage.get();
382+
docs = future.get(30, TimeUnit.SECONDS).getDocuments();
383383
// [END fs_paginate_cursor]
384+
return Arrays.asList(firstPage, secondPage);
384385
}
385386
}

firestore/src/test/java/com/example/firestore/snippets/QueryDataSnippetsIT.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ public void testCreateStartAtSnapshotQueryCursor() throws Exception {
208208
@Test
209209
public void testPaginateCursor() throws Exception {
210210
// Snippet executes it's own query. Failures result in thrown Exceptions
211-
queryDataSnippets.paginateCursor();
211+
List<Query> pages = queryDataSnippets.paginateCursor();
212+
List<String> firstPage = getResults(pages.get(0));
213+
List<String> secondPage = getResults(pages.get(1));
214+
assertEquals(firstPage, Arrays.asList("SF", "LA", "TOK", "BJ"));
215+
assertEquals(firstPage, new ArrayList<String>());
212216
}
213217

214218
private Set<String> getResultsAsSet(Query query) throws Exception {

0 commit comments

Comments
 (0)