Skip to content

Commit d58aefe

Browse files
committed
more tests
1 parent 207d77c commit d58aefe

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

firebase-firestore/src/test/java/com/google/firebase/firestore/local/LocalStoreTestCase.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,9 @@ public void tearDown() {
137137
localStorePersistence.shutdown();
138138
}
139139

140-
protected void writeMutation(Mutation mutation) {
140+
protected Mutation writeMutation(Mutation mutation) {
141141
writeMutations(asList(mutation));
142+
return mutation;
142143
}
143144

144145
private void writeMutations(List<Mutation> mutations) {
@@ -188,11 +189,11 @@ private void acknowledgeMutationWithTransformResults(
188189
lastChanges = localStore.acknowledgeBatch(result);
189190
}
190191

191-
private void acknowledgeMutation(long documentVersion) {
192+
protected void acknowledgeMutation(long documentVersion) {
192193
acknowledgeMutationWithTransformResults(documentVersion);
193194
}
194195

195-
private void rejectMutation() {
196+
protected void rejectMutation() {
196197
MutationBatch batch = batches.get(0);
197198
batches.remove(0);
198199
lastChanges = localStore.rejectBatch(batch.getBatchId());

firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteLocalStoreTest.java

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import static com.google.firebase.firestore.testutil.TestUtil.map;
2828
import static com.google.firebase.firestore.testutil.TestUtil.orFilters;
2929
import static com.google.firebase.firestore.testutil.TestUtil.orderBy;
30+
import static com.google.firebase.firestore.testutil.TestUtil.patchMutation;
3031
import static com.google.firebase.firestore.testutil.TestUtil.query;
31-
import static com.google.firebase.firestore.testutil.TestUtil.removedRemoteEvent;
3232
import static com.google.firebase.firestore.testutil.TestUtil.setMutation;
3333
import static com.google.firebase.firestore.testutil.TestUtil.updateRemoteEvent;
3434
import static com.google.firebase.firestore.testutil.TestUtil.version;
@@ -41,12 +41,12 @@
4141
import com.google.firebase.firestore.model.DocumentKey;
4242
import com.google.firebase.firestore.model.FieldIndex;
4343
import com.google.firebase.firestore.model.FieldPath;
44-
import com.google.firebase.firestore.model.MutableDocument;
4544
import com.google.firebase.firestore.model.ObjectValue;
4645
import com.google.firebase.firestore.model.ResourcePath;
4746
import com.google.firebase.firestore.model.ServerTimestamps;
4847
import com.google.firebase.firestore.model.mutation.FieldMask;
4948
import com.google.firebase.firestore.model.mutation.FieldTransform;
49+
import com.google.firebase.firestore.model.mutation.Mutation;
5050
import com.google.firebase.firestore.model.mutation.PatchMutation;
5151
import com.google.firebase.firestore.model.mutation.Precondition;
5252
import com.google.firebase.firestore.model.mutation.ServerTimestampOperation;
@@ -830,67 +830,86 @@ public void testIndexAutoCreationDoesnotWorkWithMultipleInequality() {
830830

831831
@Test
832832
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);
838835

839836
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
841838
assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected);
842839
}
843840

844841
@Test
845842
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);
851845

852846
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
854848
assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected);
855849
}
856850

857851
@Test
858852
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);
863857

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);
865867

866868
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
868870
assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected);
869871
}
870872

871873
@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");
882882
assertWithMessage("precondition check: all rows have null document type")
883883
.that(getDocumentTypeByPathFromRemoteDocumentsTable().values())
884-
.containsExactly(null, null);
884+
.containsExactly(null, null, null);
885885

886-
executeQuery(query);
886+
executeQuery(query("coll"));
887887

888888
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);
891892
assertThat(getDocumentTypeByPathFromRemoteDocumentsTable()).containsExactlyEntriesIn(expected);
892893
}
893894

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+
894913
/**
895914
* Scans the entire "remote documents" sqlite table and returns the value of the "document type"
896915
* column for each row, keyed by the value of the "path" column of the corresponding row. Note

firebase-firestore/src/testUtil/java/com/google/firebase/firestore/testutil/TestUtil.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,11 @@ private static PatchMutation patchMutationHelper(
619619
}
620620

621621
public static DeleteMutation deleteMutation(String path) {
622-
return new DeleteMutation(key(path), Precondition.NONE);
622+
return deleteMutation(key(path));
623+
}
624+
625+
public static DeleteMutation deleteMutation(DocumentKey key) {
626+
return new DeleteMutation(key, Precondition.NONE);
623627
}
624628

625629
public static VerifyMutation verifyMutation(String path, int micros) {

0 commit comments

Comments
 (0)