Skip to content

Commit 111b4e4

Browse files
authored
test: Increase Query test coverage (#1506)
* test: Increase Query test coverage * Address feedback * revert the variation of data because we cannot guarantee the order of return fields
1 parent 9693c7b commit 111b4e4

File tree

1 file changed

+292
-0
lines changed

1 file changed

+292
-0
lines changed

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITSystemTest.java

+292
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,111 @@ public void defaultQuery() throws Exception {
368368
assertEquals("bar", documents.next().get("foo"));
369369
}
370370

371+
@Test
372+
public void defaultQueryStream() throws Exception {
373+
addDocument("foo", "bar");
374+
addDocument("foo", "bar");
375+
376+
final Semaphore semaphore = new Semaphore(0);
377+
final Iterator<String> iterator = Arrays.asList("bar", "bar").iterator();
378+
randomColl.stream(
379+
new ApiStreamObserver<DocumentSnapshot>() {
380+
@Override
381+
public void onNext(DocumentSnapshot documentSnapshot) {
382+
assertEquals(iterator.next(), documentSnapshot.get("foo"));
383+
}
384+
385+
@Override
386+
public void onError(Throwable throwable) {}
387+
388+
@Override
389+
public void onCompleted() {
390+
semaphore.release();
391+
}
392+
});
393+
394+
semaphore.acquire();
395+
// Verify the number of response
396+
assertFalse(iterator.hasNext());
397+
}
398+
399+
@Test
400+
public void largeQuery() throws Exception {
401+
int count = 100;
402+
while (count-- > 0) {
403+
addDocument("foo", "bar");
404+
}
405+
406+
QuerySnapshot querySnapshot = randomColl.get().get();
407+
assertEquals(100, querySnapshot.size());
408+
}
409+
410+
@Test
411+
public void largeQueryStream() throws Exception {
412+
int count = 100;
413+
while (count-- > 0) {
414+
addDocument("foo", "bar");
415+
}
416+
417+
final Semaphore semaphore = new Semaphore(0);
418+
final int[] docCount = {0};
419+
randomColl.stream(
420+
new ApiStreamObserver<DocumentSnapshot>() {
421+
@Override
422+
public void onNext(DocumentSnapshot documentSnapshot) {
423+
docCount[0]++;
424+
}
425+
426+
@Override
427+
public void onError(Throwable throwable) {
428+
fail();
429+
}
430+
431+
@Override
432+
public void onCompleted() {
433+
semaphore.release();
434+
}
435+
});
436+
437+
semaphore.acquire();
438+
// Verify the number of response
439+
assertEquals(100, docCount[0]);
440+
}
441+
371442
@Test
372443
public void noResults() throws Exception {
373444
QuerySnapshot querySnapshot = randomColl.get().get();
374445
assertTrue(querySnapshot.isEmpty());
375446
assertNotNull(querySnapshot.getReadTime());
376447
}
377448

449+
@Test
450+
public void noResultsStream() throws Exception {
451+
final Semaphore semaphore = new Semaphore(0);
452+
final int[] docCount = {0};
453+
randomColl.stream(
454+
new ApiStreamObserver<DocumentSnapshot>() {
455+
@Override
456+
public void onNext(DocumentSnapshot documentSnapshot) {
457+
docCount[0]++;
458+
}
459+
460+
@Override
461+
public void onError(Throwable throwable) {
462+
fail();
463+
}
464+
465+
@Override
466+
public void onCompleted() {
467+
semaphore.release();
468+
}
469+
});
470+
471+
semaphore.acquire();
472+
// Verify the number of response
473+
assertEquals(0, docCount[0]);
474+
}
475+
378476
@Test
379477
public void queryWithMicrosecondPrecision() throws Exception {
380478
Timestamp microsecondTimestamp = Timestamp.ofTimeSecondsAndNanos(0, 123000);
@@ -412,6 +510,37 @@ public void limitQuery() throws Exception {
412510
assertEquals(asList("doc1", "doc2"), querySnapshotToIds(querySnapshot));
413511
}
414512

513+
@Test
514+
public void limitQueryStream() throws Exception {
515+
setDocument("doc1", Collections.singletonMap("counter", 1));
516+
setDocument("doc2", Collections.singletonMap("counter", 2));
517+
setDocument("doc3", Collections.singletonMap("counter", 3));
518+
519+
final Semaphore semaphore = new Semaphore(0);
520+
final Iterator<String> iterator = Arrays.asList("doc1", "doc2").iterator();
521+
randomColl.orderBy("counter").limit(2).stream(
522+
new ApiStreamObserver<DocumentSnapshot>() {
523+
@Override
524+
public void onNext(DocumentSnapshot documentSnapshot) {
525+
assertEquals(iterator.next(), documentSnapshot.getId());
526+
}
527+
528+
@Override
529+
public void onError(Throwable throwable) {
530+
fail();
531+
}
532+
533+
@Override
534+
public void onCompleted() {
535+
semaphore.release();
536+
}
537+
});
538+
539+
semaphore.acquire();
540+
// Verify the number of response
541+
assertFalse(iterator.hasNext());
542+
}
543+
415544
@Test
416545
public void limitToLastQuery() throws Exception {
417546
setDocument("doc1", Collections.singletonMap("counter", 1));
@@ -422,6 +551,47 @@ public void limitToLastQuery() throws Exception {
422551
assertEquals(asList("doc2", "doc3"), querySnapshotToIds(querySnapshot));
423552
}
424553

554+
@Test
555+
public void largerLimitQuery() throws Exception {
556+
setDocument("doc1", Collections.singletonMap("counter", 1));
557+
setDocument("doc2", Collections.singletonMap("counter", 2));
558+
setDocument("doc3", Collections.singletonMap("counter", 3));
559+
560+
QuerySnapshot querySnapshot = randomColl.orderBy("counter").limit(4).get().get();
561+
assertEquals(asList("doc1", "doc2", "doc3"), querySnapshotToIds(querySnapshot));
562+
}
563+
564+
@Test
565+
public void largerLimitQueryStream() throws Exception {
566+
setDocument("doc1", Collections.singletonMap("counter", 1));
567+
setDocument("doc2", Collections.singletonMap("counter", 2));
568+
setDocument("doc3", Collections.singletonMap("counter", 3));
569+
570+
final Semaphore semaphore = new Semaphore(0);
571+
final Iterator<String> iterator = Arrays.asList("doc1", "doc2", "doc3").iterator();
572+
randomColl.orderBy("counter").limit(4).stream(
573+
new ApiStreamObserver<DocumentSnapshot>() {
574+
@Override
575+
public void onNext(DocumentSnapshot documentSnapshot) {
576+
assertEquals(iterator.next(), documentSnapshot.getId());
577+
}
578+
579+
@Override
580+
public void onError(Throwable throwable) {
581+
fail();
582+
}
583+
584+
@Override
585+
public void onCompleted() {
586+
semaphore.release();
587+
}
588+
});
589+
590+
semaphore.acquire();
591+
// Verify the number of response
592+
assertFalse(iterator.hasNext());
593+
}
594+
425595
@Test
426596
public void offsetQuery() throws Exception {
427597
addDocument("foo", "bar");
@@ -432,6 +602,75 @@ public void offsetQuery() throws Exception {
432602
assertEquals("bar", querySnapshot.getDocuments().get(0).get("foo"));
433603
}
434604

605+
@Test
606+
public void offsetQueryStream() throws Exception {
607+
addDocument("foo", "bar");
608+
addDocument("foo", "bar");
609+
610+
final Semaphore semaphore = new Semaphore(0);
611+
final Iterator<String> iterator = Collections.singletonList("bar").iterator();
612+
randomColl.offset(1).stream(
613+
new ApiStreamObserver<DocumentSnapshot>() {
614+
@Override
615+
public void onNext(DocumentSnapshot documentSnapshot) {
616+
assertEquals(iterator.next(), documentSnapshot.get("foo"));
617+
}
618+
619+
@Override
620+
public void onError(Throwable throwable) {
621+
fail();
622+
}
623+
624+
@Override
625+
public void onCompleted() {
626+
semaphore.release();
627+
}
628+
});
629+
630+
semaphore.acquire();
631+
// Verify the number of response
632+
assertFalse(iterator.hasNext());
633+
}
634+
635+
@Test
636+
public void largerOffsetQuery() throws Exception {
637+
addDocument("foo", "bar");
638+
addDocument("foo", "bar");
639+
640+
QuerySnapshot querySnapshot = randomColl.offset(3).get().get();
641+
assertEquals(0, querySnapshot.size());
642+
}
643+
644+
@Test
645+
public void largerOffsetQueryStream() throws Exception {
646+
addDocument("foo", "bar");
647+
addDocument("foo", "bar");
648+
649+
final Semaphore semaphore = new Semaphore(0);
650+
final int[] docCount = {0};
651+
randomColl.offset(3).stream(
652+
new ApiStreamObserver<DocumentSnapshot>() {
653+
@Override
654+
public void onNext(DocumentSnapshot documentSnapshot) {
655+
docCount[0]++;
656+
}
657+
658+
@Override
659+
public void onError(Throwable throwable) {
660+
fail();
661+
}
662+
663+
@Override
664+
public void onCompleted() {
665+
semaphore.release();
666+
}
667+
});
668+
669+
semaphore.acquire();
670+
// Verify the number of response
671+
assertEquals(0, docCount[0]);
672+
}
673+
435674
@Test
436675
public void orderByQuery() throws Exception {
437676
addDocument("foo", 1);
@@ -448,6 +687,59 @@ public void orderByQuery() throws Exception {
448687
assertEquals(1L, documents.next().get("foo"));
449688
}
450689

690+
@Test
691+
public void orderByQueryStream() throws Exception {
692+
addDocument("foo", 1);
693+
addDocument("foo", 2);
694+
695+
final Semaphore semaphore = new Semaphore(0);
696+
final Iterator<Long> iteratorAscending = Arrays.asList(1L, 2L).iterator();
697+
randomColl.orderBy("foo").stream(
698+
new ApiStreamObserver<DocumentSnapshot>() {
699+
@Override
700+
public void onNext(DocumentSnapshot documentSnapshot) {
701+
assertEquals(iteratorAscending.next(), documentSnapshot.get("foo"));
702+
}
703+
704+
@Override
705+
public void onError(Throwable throwable) {
706+
fail();
707+
}
708+
709+
@Override
710+
public void onCompleted() {
711+
semaphore.release();
712+
}
713+
});
714+
715+
semaphore.acquire();
716+
// Verify the number of response
717+
assertFalse(iteratorAscending.hasNext());
718+
719+
final Iterator<Long> iteratorDescending = Arrays.asList(2L, 1L).iterator();
720+
randomColl.orderBy("foo", Query.Direction.DESCENDING).stream(
721+
new ApiStreamObserver<DocumentSnapshot>() {
722+
@Override
723+
public void onNext(DocumentSnapshot documentSnapshot) {
724+
assertEquals(iteratorDescending.next(), documentSnapshot.get("foo"));
725+
}
726+
727+
@Override
728+
public void onError(Throwable throwable) {
729+
fail();
730+
}
731+
732+
@Override
733+
public void onCompleted() {
734+
semaphore.release();
735+
}
736+
});
737+
738+
semaphore.acquire();
739+
// Verify the number of response
740+
assertFalse(iteratorDescending.hasNext());
741+
}
742+
451743
@Test
452744
public void selectQuery() throws Exception {
453745
addDocument("foo", 1, "bar", 2);

0 commit comments

Comments
 (0)