|
27 | 27 | import static com.google.firebase.firestore.testutil.TestUtil.map; |
28 | 28 | import static com.google.firebase.firestore.testutil.TestUtil.orFilters; |
29 | 29 | import static com.google.firebase.firestore.testutil.TestUtil.orderBy; |
| 30 | +import static com.google.firebase.firestore.testutil.TestUtil.patchMutation; |
30 | 31 | import static com.google.firebase.firestore.testutil.TestUtil.query; |
31 | | -import static com.google.firebase.firestore.testutil.TestUtil.removedRemoteEvent; |
32 | 32 | import static com.google.firebase.firestore.testutil.TestUtil.setMutation; |
33 | 33 | import static com.google.firebase.firestore.testutil.TestUtil.updateRemoteEvent; |
34 | 34 | import static com.google.firebase.firestore.testutil.TestUtil.version; |
|
41 | 41 | import com.google.firebase.firestore.model.DocumentKey; |
42 | 42 | import com.google.firebase.firestore.model.FieldIndex; |
43 | 43 | import com.google.firebase.firestore.model.FieldPath; |
44 | | -import com.google.firebase.firestore.model.MutableDocument; |
45 | 44 | import com.google.firebase.firestore.model.ObjectValue; |
46 | 45 | import com.google.firebase.firestore.model.ResourcePath; |
47 | 46 | import com.google.firebase.firestore.model.ServerTimestamps; |
48 | 47 | import com.google.firebase.firestore.model.mutation.FieldMask; |
49 | 48 | import com.google.firebase.firestore.model.mutation.FieldTransform; |
| 49 | +import com.google.firebase.firestore.model.mutation.Mutation; |
50 | 50 | import com.google.firebase.firestore.model.mutation.PatchMutation; |
51 | 51 | import com.google.firebase.firestore.model.mutation.Precondition; |
52 | 52 | import com.google.firebase.firestore.model.mutation.ServerTimestampOperation; |
@@ -830,67 +830,86 @@ public void testIndexAutoCreationDoesnotWorkWithMultipleInequality() { |
830 | 830 |
|
831 | 831 | @Test |
832 | 832 | public void testDocumentTypeIsSetToFoundWhenFoundDocumentAdded() { |
833 | | - Query query = query("coll"); |
834 | | - int targetId = allocateQuery(query); |
835 | | - MutableDocument doc = doc("coll/a", 10, map("foo", 42)); |
836 | | - |
837 | | - applyRemoteEvent(addedRemoteEvent(doc, targetId)); |
| 833 | + Mutation mutation = writeMutation(setMutation("coll/a", map("foo", "bar"))); |
| 834 | + acknowledgeMutation(1); |
838 | 835 |
|
839 | 836 | Map<ResourcePath, Integer> expected = new HashMap<>(); |
840 | | - expected.put(doc.getKey().getPath(), 2); // 2 is a "found" document |
| 837 | + expected.put(mutation.getKey().getPath(), 2); // 2 is a "found" document |
841 | 838 | assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected); |
842 | 839 | } |
843 | 840 |
|
844 | 841 | @Test |
845 | 842 | public void testDocumentTypeIsSetToNoDocumentWhenUnknownDocumentDeleted() { |
846 | | - Query query = query("coll"); |
847 | | - int targetId = allocateQuery(query); |
848 | | - MutableDocument doc = doc("coll/a", 10, map("foo", 42)); |
849 | | - |
850 | | - applyRemoteEvent(removedRemoteEvent(doc.getKey(), 10, targetId)); |
| 843 | + Mutation mutation = writeMutation(deleteMutation("coll/a")); |
| 844 | + acknowledgeMutation(1); |
851 | 845 |
|
852 | 846 | Map<ResourcePath, Integer> expected = new HashMap<>(); |
853 | | - expected.put(doc.getKey().getPath(), 1); // 1 is a "no" document |
| 847 | + expected.put(mutation.getKey().getPath(), 1); // 1 is a "no" document |
854 | 848 | assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected); |
855 | 849 | } |
856 | 850 |
|
857 | 851 | @Test |
858 | 852 | public void testDocumentTypeIsUpdatedToNoDocumentWhenFoundDocumentDeleted() { |
859 | | - Query query = query("coll"); |
860 | | - int targetId = allocateQuery(query); |
861 | | - MutableDocument doc = doc("coll/a", 10, map("foo", 42)); |
862 | | - applyRemoteEvent(addedRemoteEvent(doc, targetId)); |
| 853 | + Mutation setMutation = writeMutation(setMutation("coll/a", map("foo", "bar"))); |
| 854 | + acknowledgeMutation(1); |
| 855 | + Mutation deleteMutation = writeMutation(deleteMutation(setMutation.getKey())); |
| 856 | + acknowledgeMutation(1); |
863 | 857 |
|
864 | | - applyRemoteEvent(removedRemoteEvent(doc.getKey(), 11, targetId)); |
| 858 | + Map<ResourcePath, Integer> expected = new HashMap<>(); |
| 859 | + expected.put(deleteMutation.getKey().getPath(), 1); // 1 is a "no" document |
| 860 | + assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected); |
| 861 | + } |
| 862 | + |
| 863 | + @Test |
| 864 | + public void testDocumentTypeIsSetToUnknownWhenApplyingPatchMutationWithNoBaseDocument() { |
| 865 | + Mutation mutation = writeMutation(patchMutation("coll/a", map("foo", "bar"))); |
| 866 | + acknowledgeMutation(1); |
865 | 867 |
|
866 | 868 | Map<ResourcePath, Integer> expected = new HashMap<>(); |
867 | | - expected.put(doc.getKey().getPath(), 1); // 1 is a "no" document |
| 869 | + expected.put(mutation.getKey().getPath(), 3); // 3 is a "unknown" document |
868 | 870 | assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected); |
869 | 871 | } |
870 | 872 |
|
871 | 873 | @Test |
872 | | - public void testDocumentTypeColumnIsBackfilledByQuery() { |
873 | | - Query query = query("coll"); |
874 | | - int targetId = allocateQuery(query); |
875 | | - MutableDocument existingDoc = doc("coll/a", 10, map("foo", 42)); |
876 | | - applyRemoteEvent(addedRemoteEvent(existingDoc, targetId)); |
877 | | - MutableDocument deletedDoc = doc("coll/b", 10, map("foo", 42)); |
878 | | - applyRemoteEvent(addedRemoteEvent(deletedDoc, targetId)); |
879 | | - applyRemoteEvent(removedRemoteEvent(deletedDoc.getKey(), 11, targetId)); |
880 | | - SQLitePersistence persistence = this.sqlitePersistence.get(); |
881 | | - persistence.execute("UPDATE remote_documents SET document_type = NULL"); |
| 874 | + public void testQueryBackfillsRowsWithNullDocumentType() { |
| 875 | + Mutation foundDocMutation = writeMutation(setMutation("coll/a", map("foo", "bar"))); |
| 876 | + acknowledgeMutation(1); |
| 877 | + Mutation noDocMutation = writeMutation(deleteMutation("coll/b")); |
| 878 | + acknowledgeMutation(1); |
| 879 | + Mutation unknownDocMutation = writeMutation(patchMutation("coll/c", map("foo", "bar"))); |
| 880 | + acknowledgeMutation(1); |
| 881 | + sqlitePersistence.get().execute("UPDATE remote_documents SET document_type = NULL"); |
882 | 882 | assertWithMessage("precondition check: all rows have null document type") |
883 | 883 | .that(getDocumentTypeByPathFromRemoteDocumentsTable().values()) |
884 | | - .containsExactly(null, null); |
| 884 | + .containsExactly(null, null, null); |
885 | 885 |
|
886 | | - executeQuery(query); |
| 886 | + executeQuery(query("coll")); |
887 | 887 |
|
888 | 888 | Map<ResourcePath, Integer> expected = new HashMap<>(); |
889 | | - expected.put(existingDoc.getKey().getPath(), 2); // 2 is a "found" document |
890 | | - expected.put(deletedDoc.getKey().getPath(), 1); // 1 is a "no" document |
| 889 | + expected.put(foundDocMutation.getKey().getPath(), 2); |
| 890 | + expected.put(noDocMutation.getKey().getPath(), 1); |
| 891 | + expected.put(unknownDocMutation.getKey().getPath(), 3); |
891 | 892 | assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected); |
892 | 893 | } |
893 | 894 |
|
| 895 | + @Test |
| 896 | + public void testQueryResultsIncludeRowsWithNullDocumentType() { |
| 897 | + writeMutation(setMutation("coll/a", map("foo", "bar"))); |
| 898 | + acknowledgeMutation(1); |
| 899 | + writeMutation(deleteMutation("coll/b")); |
| 900 | + acknowledgeMutation(1); |
| 901 | + writeMutation(patchMutation("coll/c", map("foo", "bar"))); |
| 902 | + acknowledgeMutation(1); |
| 903 | + sqlitePersistence.get().execute("UPDATE remote_documents SET document_type = NULL"); |
| 904 | + assertWithMessage("precondition check: all rows have null document type") |
| 905 | + .that(getDocumentTypeByPathFromRemoteDocumentsTable().values()) |
| 906 | + .containsExactly(null, null, null); |
| 907 | + |
| 908 | + executeQuery(query("coll")); |
| 909 | + |
| 910 | + assertQueryReturned("coll/a"); |
| 911 | + } |
| 912 | + |
894 | 913 | /** |
895 | 914 | * Scans the entire "remote documents" sqlite table and returns the value of the "document type" |
896 | 915 | * column for each row, keyed by the value of the "path" column of the corresponding row. Note |
|
0 commit comments