Search in sources :

Example 41 with Mutation

use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.

the class RemoteSerializerTest method testEncodesServerTimestampMutation.

@Test
public void testEncodesServerTimestampMutation() {
    Mutation mutation = setMutation("docs/1", map("a", com.google.firebase.firestore.FieldValue.serverTimestamp(), "bar", com.google.firebase.firestore.FieldValue.serverTimestamp()));
    Write expected = Write.newBuilder().setUpdate(Document.newBuilder().setName("projects/p/databases/d/documents/docs/1")).addUpdateTransforms(FieldTransform.newBuilder().setFieldPath("a").setSetToServerValue(ServerValue.REQUEST_TIME)).addUpdateTransforms(FieldTransform.newBuilder().setFieldPath("bar").setSetToServerValue(ServerValue.REQUEST_TIME)).build();
    assertRoundTripForMutation(mutation, expected);
    mutation = patchMutation("docs/1", map("a", com.google.firebase.firestore.FieldValue.serverTimestamp(), "bar.baz", com.google.firebase.firestore.FieldValue.serverTimestamp()));
    expected = Write.newBuilder().setUpdate(Document.newBuilder().setName("projects/p/databases/d/documents/docs/1")).setUpdateMask(DocumentMask.newBuilder().build()).addUpdateTransforms(FieldTransform.newBuilder().setFieldPath("a").setSetToServerValue(ServerValue.REQUEST_TIME)).addUpdateTransforms(FieldTransform.newBuilder().setFieldPath("bar.baz").setSetToServerValue(ServerValue.REQUEST_TIME)).setCurrentDocument(Precondition.newBuilder().setExists(true)).build();
    assertRoundTripForMutation(mutation, expected);
}
Also used : Write(com.google.firestore.v1.Write) Mutation(com.google.firebase.firestore.model.mutation.Mutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.verifyMutation(com.google.firebase.firestore.testutil.TestUtil.verifyMutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Test(org.junit.Test)

Example 42 with Mutation

use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.

the class SpecTestCase method validateNextWriteSent.

// 
// Methods for mocking out the grpc streams.
// 
/**
 * Validates that a write was sent and matches the expected write.
 */
private void validateNextWriteSent(Mutation expectedWrite) {
    List<Mutation> request = datastore.waitForWriteSend();
    // TODO: Batch writes not supported yet
    assertEquals(1, request.size());
    Mutation actualWrite = request.get(0);
    assertEquals(expectedWrite, actualWrite);
    log("      This write was sent: " + actualWrite);
}
Also used : Mutation(com.google.firebase.firestore.model.mutation.Mutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation)

Example 43 with Mutation

use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.

the class SpecTestCase method doWriteAck.

private void doWriteAck(JSONObject writeAckSpec) throws Exception {
    long version = writeAckSpec.getLong("version");
    boolean keepInQueue = writeAckSpec.optBoolean("keepInQueue", false);
    assertFalse("'keepInQueue=true' is not supported on Android and should only be set in multi-client tests", keepInQueue);
    Pair<Mutation, Task<Void>> write = getCurrentOutstandingWrites().remove(0);
    validateNextWriteSent(write.first);
    MutationResult mutationResult = new MutationResult(version(version), /*transformResults=*/
    Collections.emptyList());
    queue.runSync(() -> datastore.ackWrite(version(version), singletonList(mutationResult)));
}
Also used : Task(com.google.android.gms.tasks.Task) LoadBundleTask(com.google.firebase.firestore.LoadBundleTask) Mutation(com.google.firebase.firestore.model.mutation.Mutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Example 44 with Mutation

use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.

the class MutationQueueTestCase method testAllMutationBatchesAffectingDocumentKey.

@Test
public void testAllMutationBatchesAffectingDocumentKey() {
    List<Mutation> mutations = asList(setMutation("fob/bar", map("a", 1)), setMutation("foo/bar", map("a", 1)), patchMutation("foo/bar", map("b", 1)), setMutation("foo/bar/suffix/key", map("a", 1)), setMutation("foo/baz", map("a", 1)), setMutation("food/bar", map("a", 1)));
    // Store all the mutations.
    List<MutationBatch> batches = new ArrayList<>();
    persistence.runTransaction("New mutation batch", () -> {
        for (Mutation mutation : mutations) {
            batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(mutation)));
        }
    });
    List<MutationBatch> expected = asList(batches.get(1), batches.get(2));
    List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingDocumentKey(key("foo/bar"));
    assertEquals(expected, matches);
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Test(org.junit.Test)

Example 45 with Mutation

use of com.google.firebase.firestore.model.mutation.Mutation in project firebase-android-sdk by firebase.

the class MutationQueueTestCase method testAllMutationBatchesAffectingDocumentLotsOfDocumentKeys.

// PORTING NOTE: this test only applies to Android, because it's the only platform where the
// implementation of getAllMutationBatchesAffectingDocumentKeys might split the input into several
// queries.
@Test
public void testAllMutationBatchesAffectingDocumentLotsOfDocumentKeys() {
    List<Mutation> mutations = new ArrayList<>();
    // Make sure to force SQLite implementation to split the large query into several smaller ones.
    int lotsOfMutations = 2000;
    for (int i = 0; i < lotsOfMutations; i++) {
        mutations.add(setMutation("foo/" + i, map("a", 1)));
    }
    List<MutationBatch> batches = new ArrayList<>();
    persistence.runTransaction("New mutation batch", () -> {
        for (Mutation mutation : mutations) {
            batches.add(mutationQueue.addMutationBatch(Timestamp.now(), Collections.emptyList(), asList(mutation)));
        }
    });
    // To make it easier validating the large resulting set, use a simple criteria to evaluate --
    // query all keys with an even number in them and make sure the corresponding batches make it
    // into the results.
    ImmutableSortedSet<DocumentKey> evenKeys = DocumentKey.emptyKeySet();
    List<MutationBatch> expected = new ArrayList<>();
    for (int i = 2; i < lotsOfMutations; i += 2) {
        evenKeys = evenKeys.insert(key("foo/" + i));
        expected.add(batches.get(i));
    }
    List<MutationBatch> matches = mutationQueue.getAllMutationBatchesAffectingDocumentKeys(evenKeys);
    assertThat(matches).containsExactlyElementsIn(expected).inOrder();
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) Test(org.junit.Test)

Aggregations

Mutation (com.google.firebase.firestore.model.mutation.Mutation)46 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)29 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)27 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)25 Test (org.junit.Test)25 DocumentKey (com.google.firebase.firestore.model.DocumentKey)18 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)13 Write (com.google.firestore.v1.Write)12 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)8 TestUtil.verifyMutation (com.google.firebase.firestore.testutil.TestUtil.verifyMutation)8 ArrayList (java.util.ArrayList)8 SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)7 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)6 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)5 Map (java.util.Map)4 Timestamp (com.google.firebase.Timestamp)3 MutableDocument (com.google.firebase.firestore.model.MutableDocument)3 Overlay (com.google.firebase.firestore.model.mutation.Overlay)3 SQLiteStatement (android.database.sqlite.SQLiteStatement)2