Search in sources :

Example 31 with MutationBatch

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

the class LocalSerializerTest method testSetMutationAndTransFormMutationAreSquashed.

// TODO(b/174608374): Remove these tests once we perform a schema migration.
@Test
public void testSetMutationAndTransFormMutationAreSquashed() {
    WriteBatch batchProto = com.google.firebase.firestore.proto.WriteBatch.newBuilder().setBatchId(42).addAllWrites(asList(setProto, transformProto)).setLocalWriteTime(writeTimeProto).build();
    MutationBatch decoded = serializer.decodeMutationBatch(batchProto);
    assertEquals(1, decoded.getMutations().size());
    assertTrue(decoded.getMutations().get(0) instanceof SetMutation);
    Write encoded = remoteSerializer.encodeMutation(decoded.getMutations().get(0));
    Write expected = new TestWriteBuilder().addSet().addUpdateTransforms().build();
    assertEquals(expected, encoded);
}
Also used : Write(com.google.firestore.v1.Write) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) WriteBatch(com.google.firebase.firestore.proto.WriteBatch) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) Test(org.junit.Test)

Example 32 with MutationBatch

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

the class LocalSerializerTest method testEncodesMutationBatch.

@Test
public void testEncodesMutationBatch() {
    Mutation baseWrite = new PatchMutation(key("foo/bar"), TestUtil.wrapObject(map("a", "b")), FieldMask.fromSet(Collections.singleton(field("a"))), com.google.firebase.firestore.model.mutation.Precondition.NONE);
    Mutation set = setMutation("foo/bar", map("a", "b", "num", 1));
    Mutation patch = new PatchMutation(key("bar/baz"), TestUtil.wrapObject(map("a", "b", "num", 1)), fieldMask("a"), com.google.firebase.firestore.model.mutation.Precondition.exists(true));
    Mutation del = deleteMutation("baz/quux");
    MutationBatch model = new MutationBatch(42, writeTime, Collections.singletonList(baseWrite), asList(set, patch, del));
    Write baseWriteProto = Write.newBuilder().setUpdate(com.google.firestore.v1.Document.newBuilder().setName("projects/p/databases/d/documents/foo/bar").putFields("a", Value.newBuilder().setStringValue("b").build())).setUpdateMask(DocumentMask.newBuilder().addFieldPaths("a")).build();
    com.google.firebase.firestore.proto.WriteBatch batchProto = com.google.firebase.firestore.proto.WriteBatch.newBuilder().setBatchId(42).addBaseWrites(baseWriteProto).addAllWrites(asList(setProto, patchProto, deleteProto)).setLocalWriteTime(writeTimeProto).build();
    assertEquals(batchProto, serializer.encodeMutationBatch(model));
    MutationBatch decoded = serializer.decodeMutationBatch(batchProto);
    assertEquals(model.getBatchId(), decoded.getBatchId());
    assertEquals(model.getLocalWriteTime(), decoded.getLocalWriteTime());
    assertEquals(model.getMutations(), decoded.getMutations());
    assertEquals(model.getBaseMutations(), decoded.getBaseMutations());
    assertEquals(model.getKeys(), decoded.getKeys());
}
Also used : Write(com.google.firestore.v1.Write) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) WriteBatch(com.google.firebase.firestore.proto.WriteBatch) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) Test(org.junit.Test)

Example 33 with MutationBatch

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

the class LocalSerializerTest method testPatchMutationAndTransFormMutationAreSquashed.

// TODO(b/174608374): Remove these tests once we perform a schema migration.
@Test
public void testPatchMutationAndTransFormMutationAreSquashed() {
    WriteBatch batchProto = com.google.firebase.firestore.proto.WriteBatch.newBuilder().setBatchId(42).addAllWrites(asList(patchProto, transformProto)).setLocalWriteTime(writeTimeProto).build();
    MutationBatch decoded = serializer.decodeMutationBatch(batchProto);
    assertEquals(1, decoded.getMutations().size());
    assertTrue(decoded.getMutations().get(0) instanceof PatchMutation);
    Write encoded = remoteSerializer.encodeMutation(decoded.getMutations().get(0));
    Write expected = new TestWriteBuilder().addPatch().addUpdateTransforms().build();
    assertEquals(expected, encoded);
}
Also used : Write(com.google.firestore.v1.Write) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) PatchMutation(com.google.firebase.firestore.model.mutation.PatchMutation) WriteBatch(com.google.firebase.firestore.proto.WriteBatch) Test(org.junit.Test)

Example 34 with MutationBatch

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

the class LocalStoreTestCase method acknowledgeMutationWithTransformResults.

private void acknowledgeMutationWithTransformResults(long documentVersion, Object... transformResult) {
    MutationBatch batch = batches.remove(0);
    SnapshotVersion version = version(documentVersion);
    List<MutationResult> mutationResults = Collections.singletonList(new MutationResult(version, emptyList()));
    if (transformResult.length != 0) {
        mutationResults = Arrays.stream(transformResult).map(r -> new MutationResult(version, Collections.singletonList(TestUtil.wrap(r)))).collect(Collectors.toList());
    }
    MutationBatchResult result = MutationBatchResult.create(batch, version, mutationResults, WriteStream.EMPTY_STREAM_TOKEN);
    lastChanges = localStore.acknowledgeBatch(result);
}
Also used : MutationBatchResult(com.google.firebase.firestore.model.mutation.MutationBatchResult) MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) MutationResult(com.google.firebase.firestore.model.mutation.MutationResult)

Example 35 with MutationBatch

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

the class MutationQueueTestCase method removeFirstBatches.

/**
 * Removes the first n entries from the given <tt>batches</tt> and returns them.
 *
 * @param n The number of batches to remove..
 * @param batches The list to mutate, removing entries from it.
 * @return A new list containing all the entries that were removed from @a batches.
 */
private List<MutationBatch> removeFirstBatches(int n, List<MutationBatch> batches) {
    List<MutationBatch> removed = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        MutationBatch batch = batches.get(0);
        removeMutationBatches(batch);
        batches.remove(0);
        removed.add(batch);
    }
    return removed;
}
Also used : MutationBatch(com.google.firebase.firestore.model.mutation.MutationBatch) ArrayList(java.util.ArrayList)

Aggregations

MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)40 Test (org.junit.Test)15 Mutation (com.google.firebase.firestore.model.mutation.Mutation)13 ArrayList (java.util.ArrayList)12 DocumentKey (com.google.firebase.firestore.model.DocumentKey)11 SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)8 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)7 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)6 Write (com.google.firestore.v1.Write)5 ByteString (com.google.protobuf.ByteString)5 HashSet (java.util.HashSet)5 WriteBatch (com.google.firebase.firestore.proto.WriteBatch)4 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)4 MutableDocument (com.google.firebase.firestore.model.MutableDocument)3 Timestamp (com.google.firebase.Timestamp)2 ImmutableSortedMap (com.google.firebase.database.collection.ImmutableSortedMap)2 Query (com.google.firebase.firestore.core.Query)2 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)2 MutationBatchResult (com.google.firebase.firestore.model.mutation.MutationBatchResult)2 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)2