|
31 | 31 | import com.google.cloud.firestore.DocumentReference; |
32 | 32 | import com.google.cloud.firestore.DocumentSnapshot; |
33 | 33 | import com.google.cloud.firestore.EventListener; |
| 34 | +import com.google.cloud.firestore.FieldPath; |
34 | 35 | import com.google.cloud.firestore.FieldValue; |
35 | 36 | import com.google.cloud.firestore.FirestoreException; |
36 | 37 | import com.google.cloud.firestore.ListenerRegistration; |
@@ -647,7 +648,7 @@ public void shutdownNowPreventsAddingNewListener() throws Exception { |
647 | 648 | } |
648 | 649 |
|
649 | 650 | @Test |
650 | | - public void snapshotListenerSortsDocumentsByDocumentIdInTheSameOrderAsServer() throws Exception { |
| 651 | + public void snapshotListenerSortsQueryByDocumentIdInTheSameOrderAsServer() throws Exception { |
651 | 652 | CollectionReference col = randomColl; |
652 | 653 |
|
653 | 654 | firestore |
@@ -690,6 +691,54 @@ public void snapshotListenerSortsDocumentsByDocumentIdInTheSameOrderAsServer() t |
690 | 691 | assertEquals(expectedOrder, listenerOrder); // Assert order in the SDK |
691 | 692 | } |
692 | 693 |
|
| 694 | + @Test |
| 695 | + public void snapshotListenerSortsFilteredQueryByDocumentIdInTheSameOrderAsServer() |
| 696 | + throws Exception { |
| 697 | + CollectionReference col = randomColl; |
| 698 | + |
| 699 | + firestore |
| 700 | + .batch() |
| 701 | + .set(col.document("A"), Collections.singletonMap("a", 1)) |
| 702 | + .set(col.document("a"), Collections.singletonMap("a", 1)) |
| 703 | + .set(col.document("Aa"), Collections.singletonMap("a", 1)) |
| 704 | + .set(col.document("7"), Collections.singletonMap("a", 1)) |
| 705 | + .set(col.document("12"), Collections.singletonMap("a", 1)) |
| 706 | + .set(col.document("__id7__"), Collections.singletonMap("a", 1)) |
| 707 | + .set(col.document("__id12__"), Collections.singletonMap("a", 1)) |
| 708 | + .commit() |
| 709 | + .get(); |
| 710 | + |
| 711 | + Query query = |
| 712 | + col.whereGreaterThan(FieldPath.documentId(), "__id7__") |
| 713 | + .whereLessThanOrEqualTo(FieldPath.documentId(), "A") |
| 714 | + .orderBy("__name__", Direction.ASCENDING); |
| 715 | + List<String> expectedOrder = Arrays.asList("__id12__", "12", "7", "A"); |
| 716 | + |
| 717 | + QuerySnapshot snapshot = query.get().get(); |
| 718 | + List<String> queryOrder = |
| 719 | + snapshot.getDocuments().stream().map(doc -> doc.getId()).collect(Collectors.toList()); |
| 720 | + assertEquals(expectedOrder, queryOrder); // Assert order from backend |
| 721 | + |
| 722 | + CountDownLatch latch = new CountDownLatch(1); |
| 723 | + List<String> listenerOrder = new ArrayList<>(); |
| 724 | + |
| 725 | + ListenerRegistration registration = |
| 726 | + query.addSnapshotListener( |
| 727 | + (value, error) -> { |
| 728 | + listenerOrder.addAll( |
| 729 | + value.getDocuments().stream() |
| 730 | + .map(doc -> doc.getId()) |
| 731 | + .collect(Collectors.toList())); |
| 732 | + |
| 733 | + latch.countDown(); |
| 734 | + }); |
| 735 | + |
| 736 | + latch.await(); |
| 737 | + registration.remove(); |
| 738 | + |
| 739 | + assertEquals(expectedOrder, listenerOrder); // Assert order in the SDK |
| 740 | + } |
| 741 | + |
693 | 742 | /** |
694 | 743 | * A tuple class used by {@code #queryWatch}. This class represents an event delivered to the |
695 | 744 | * registered query listener. |
|
0 commit comments