Search in sources :

Example 6 with SetMutation

use of com.google.firebase.firestore.model.mutation.SetMutation 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 7 with SetMutation

use of com.google.firebase.firestore.model.mutation.SetMutation 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)

Example 8 with SetMutation

use of com.google.firebase.firestore.model.mutation.SetMutation 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 9 with SetMutation

use of com.google.firebase.firestore.model.mutation.SetMutation 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 10 with SetMutation

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

the class TestUtil method setMutation.

public static SetMutation setMutation(String path, Map<String, Object> values) {
    UserDataReader dataReader = new UserDataReader(DatabaseId.forProject("project"));
    ParsedSetData parsed = dataReader.parseSetData(values);
    // The order of the transforms doesn't matter, but we sort them so tests can assume a particular
    // order.
    ArrayList<FieldTransform> fieldTransforms = new ArrayList<>(parsed.getFieldTransforms());
    Collections.sort(fieldTransforms, (ft1, ft2) -> ft1.getFieldPath().compareTo(ft2.getFieldPath()));
    return new SetMutation(key(path), parsed.getData(), Precondition.NONE, fieldTransforms);
}
Also used : UserDataReader(com.google.firebase.firestore.UserDataReader) ParsedSetData(com.google.firebase.firestore.core.UserData.ParsedSetData) FieldTransform(com.google.firebase.firestore.model.mutation.FieldTransform) ArrayList(java.util.ArrayList) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation)

Aggregations

SetMutation (com.google.firebase.firestore.model.mutation.SetMutation)10 MutationBatch (com.google.firebase.firestore.model.mutation.MutationBatch)8 Test (org.junit.Test)8 Mutation (com.google.firebase.firestore.model.mutation.Mutation)6 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)6 ArrayList (java.util.ArrayList)6 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)4 DocumentKey (com.google.firebase.firestore.model.DocumentKey)3 PatchMutation (com.google.firebase.firestore.model.mutation.PatchMutation)3 WriteBatch (com.google.firebase.firestore.proto.WriteBatch)3 Write (com.google.firestore.v1.Write)3 FieldTransform (com.google.firebase.firestore.model.mutation.FieldTransform)2 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)2 UserDataReader (com.google.firebase.firestore.UserDataReader)1 Query (com.google.firebase.firestore.core.Query)1 ParsedSetData (com.google.firebase.firestore.core.UserData.ParsedSetData)1 DeleteMutation (com.google.firebase.firestore.model.mutation.DeleteMutation)1 Precondition (com.google.firebase.firestore.model.mutation.Precondition)1 VerifyMutation (com.google.firebase.firestore.model.mutation.VerifyMutation)1 DocumentTransform (com.google.firestore.v1.DocumentTransform)1