|
24 | 24 | import static java.util.Collections.emptyList; |
25 | 25 | import static java.util.Collections.singletonList; |
26 | 26 | import static org.junit.Assert.assertArrayEquals; |
| 27 | +import static org.junit.Assert.assertEquals; |
27 | 28 |
|
28 | 29 | import com.google.cloud.firestore.CollectionReference; |
29 | 30 | import com.google.cloud.firestore.DocumentChange; |
30 | 31 | import com.google.cloud.firestore.DocumentReference; |
31 | 32 | import com.google.cloud.firestore.DocumentSnapshot; |
32 | 33 | import com.google.cloud.firestore.EventListener; |
| 34 | +import com.google.cloud.firestore.FieldPath; |
33 | 35 | import com.google.cloud.firestore.FieldValue; |
34 | 36 | import com.google.cloud.firestore.FirestoreException; |
35 | 37 | import com.google.cloud.firestore.ListenerRegistration; |
36 | 38 | import com.google.cloud.firestore.LocalFirestoreHelper; |
37 | 39 | import com.google.cloud.firestore.Query; |
| 40 | +import com.google.cloud.firestore.Query.Direction; |
38 | 41 | import com.google.cloud.firestore.QueryDocumentSnapshot; |
39 | 42 | import com.google.cloud.firestore.QuerySnapshot; |
40 | 43 | import com.google.cloud.firestore.it.ITQueryWatchTest.QuerySnapshotEventListener.ListenerAssertions; |
@@ -644,6 +647,118 @@ public void shutdownNowPreventsAddingNewListener() throws Exception { |
644 | 647 | listenerAssertions.hasError(); |
645 | 648 | } |
646 | 649 |
|
| 650 | + @Test |
| 651 | + public void snapshotListenerSortsQueryByDocumentIdInTheSameOrderAsServer() throws Exception { |
| 652 | + CollectionReference col = randomColl; |
| 653 | + |
| 654 | + firestore |
| 655 | + .batch() |
| 656 | + .set(col.document("A"), Collections.singletonMap("a", 1)) |
| 657 | + .set(col.document("a"), Collections.singletonMap("a", 1)) |
| 658 | + .set(col.document("Aa"), Collections.singletonMap("a", 1)) |
| 659 | + .set(col.document("7"), Collections.singletonMap("a", 1)) |
| 660 | + .set(col.document("12"), Collections.singletonMap("a", 1)) |
| 661 | + .set(col.document("__id7__"), Collections.singletonMap("a", 1)) |
| 662 | + .set(col.document("__id12__"), Collections.singletonMap("a", 1)) |
| 663 | + .set(col.document("__id-2__"), Collections.singletonMap("a", 1)) |
| 664 | + .set(col.document("_id1__"), Collections.singletonMap("a", 1)) |
| 665 | + .set(col.document("__id1_"), Collections.singletonMap("a", 1)) |
| 666 | + .set(col.document("__id"), Collections.singletonMap("a", 1)) |
| 667 | + .commit() |
| 668 | + .get(); |
| 669 | + |
| 670 | + Query query = col.orderBy("__name__", Direction.ASCENDING); |
| 671 | + List<String> expectedOrder = |
| 672 | + Arrays.asList( |
| 673 | + "__id-2__", |
| 674 | + "__id7__", |
| 675 | + "__id12__", |
| 676 | + "12", |
| 677 | + "7", |
| 678 | + "A", |
| 679 | + "Aa", |
| 680 | + "__id", |
| 681 | + "__id1_", |
| 682 | + "_id1__", |
| 683 | + "a"); |
| 684 | + |
| 685 | + QuerySnapshot snapshot = query.get().get(); |
| 686 | + List<String> queryOrder = |
| 687 | + snapshot.getDocuments().stream().map(doc -> doc.getId()).collect(Collectors.toList()); |
| 688 | + assertEquals(expectedOrder, queryOrder); // Assert order from backend |
| 689 | + |
| 690 | + CountDownLatch latch = new CountDownLatch(1); |
| 691 | + List<String> listenerOrder = new ArrayList<>(); |
| 692 | + |
| 693 | + ListenerRegistration registration = |
| 694 | + query.addSnapshotListener( |
| 695 | + (value, error) -> { |
| 696 | + listenerOrder.addAll( |
| 697 | + value.getDocuments().stream() |
| 698 | + .map(doc -> doc.getId()) |
| 699 | + .collect(Collectors.toList())); |
| 700 | + |
| 701 | + latch.countDown(); |
| 702 | + }); |
| 703 | + |
| 704 | + latch.await(); |
| 705 | + registration.remove(); |
| 706 | + |
| 707 | + assertEquals(expectedOrder, listenerOrder); // Assert order in the SDK |
| 708 | + } |
| 709 | + |
| 710 | + @Test |
| 711 | + public void snapshotListenerSortsFilteredQueryByDocumentIdInTheSameOrderAsServer() |
| 712 | + throws Exception { |
| 713 | + CollectionReference col = randomColl; |
| 714 | + |
| 715 | + firestore |
| 716 | + .batch() |
| 717 | + .set(col.document("A"), Collections.singletonMap("a", 1)) |
| 718 | + .set(col.document("a"), Collections.singletonMap("a", 1)) |
| 719 | + .set(col.document("Aa"), Collections.singletonMap("a", 1)) |
| 720 | + .set(col.document("7"), Collections.singletonMap("a", 1)) |
| 721 | + .set(col.document("12"), Collections.singletonMap("a", 1)) |
| 722 | + .set(col.document("__id7__"), Collections.singletonMap("a", 1)) |
| 723 | + .set(col.document("__id12__"), Collections.singletonMap("a", 1)) |
| 724 | + .set(col.document("__id-2__"), Collections.singletonMap("a", 1)) |
| 725 | + .set(col.document("_id1__"), Collections.singletonMap("a", 1)) |
| 726 | + .set(col.document("__id1_"), Collections.singletonMap("a", 1)) |
| 727 | + .set(col.document("__id"), Collections.singletonMap("a", 1)) |
| 728 | + .commit() |
| 729 | + .get(); |
| 730 | + |
| 731 | + Query query = |
| 732 | + col.whereGreaterThan(FieldPath.documentId(), "__id7__") |
| 733 | + .whereLessThanOrEqualTo(FieldPath.documentId(), "A") |
| 734 | + .orderBy("__name__", Direction.ASCENDING); |
| 735 | + List<String> expectedOrder = Arrays.asList("__id12__", "12", "7", "A"); |
| 736 | + |
| 737 | + QuerySnapshot snapshot = query.get().get(); |
| 738 | + List<String> queryOrder = |
| 739 | + snapshot.getDocuments().stream().map(doc -> doc.getId()).collect(Collectors.toList()); |
| 740 | + assertEquals(expectedOrder, queryOrder); // Assert order from backend |
| 741 | + |
| 742 | + CountDownLatch latch = new CountDownLatch(1); |
| 743 | + List<String> listenerOrder = new ArrayList<>(); |
| 744 | + |
| 745 | + ListenerRegistration registration = |
| 746 | + query.addSnapshotListener( |
| 747 | + (value, error) -> { |
| 748 | + listenerOrder.addAll( |
| 749 | + value.getDocuments().stream() |
| 750 | + .map(doc -> doc.getId()) |
| 751 | + .collect(Collectors.toList())); |
| 752 | + |
| 753 | + latch.countDown(); |
| 754 | + }); |
| 755 | + |
| 756 | + latch.await(); |
| 757 | + registration.remove(); |
| 758 | + |
| 759 | + assertEquals(expectedOrder, listenerOrder); // Assert order in the SDK |
| 760 | + } |
| 761 | + |
647 | 762 | /** |
648 | 763 | * A tuple class used by {@code #queryWatch}. This class represents an event delivered to the |
649 | 764 | * registered query listener. |
|
0 commit comments