Search in sources :

Example 16 with MutableDocument

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

the class TestUtil method addedRemoteEvent.

public static RemoteEvent addedRemoteEvent(List<MutableDocument> docs, List<Integer> updatedInTargets, List<Integer> removedFromTargets) {
    Preconditions.checkArgument(!docs.isEmpty(), "Cannot pass empty docs array");
    WatchChangeAggregator aggregator = new WatchChangeAggregator(new WatchChangeAggregator.TargetMetadataProvider() {

        @Override
        public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) {
            return DocumentKey.emptyKeySet();
        }

        @Override
        public TargetData getTargetDataForTarget(int targetId) {
            ResourcePath collectionPath = docs.get(0).getKey().getCollectionPath();
            return targetData(targetId, QueryPurpose.LISTEN, collectionPath.toString());
        }
    });
    SnapshotVersion version = SnapshotVersion.NONE;
    for (MutableDocument doc : docs) {
        DocumentChange change = new DocumentChange(updatedInTargets, removedFromTargets, doc.getKey(), doc);
        aggregator.handleDocumentChange(change);
        version = doc.getVersion().compareTo(version) > 0 ? doc.getVersion() : version;
    }
    return aggregator.createRemoteEvent(version);
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) WatchChangeAggregator(com.google.firebase.firestore.remote.WatchChangeAggregator) ResourcePath(com.google.firebase.firestore.model.ResourcePath) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ImmutableSortedSet(com.google.firebase.database.collection.ImmutableSortedSet) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange)

Example 17 with MutableDocument

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

the class MutationTest method assertVersionTransitions.

private void assertVersionTransitions(Mutation mutation, MutableDocument base, MutationResult mutationResult, MutableDocument expected) {
    MutableDocument clone = base.mutableCopy();
    mutation.applyToRemoteDocument(clone, mutationResult);
    assertEquals(expected, clone);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument)

Example 18 with MutableDocument

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

the class MutationTest method testPatchWithMutationResult.

@Test
public void testPatchWithMutationResult() {
    Map<String, Object> data = map("foo", "bar");
    MutableDocument patchDoc = doc("collection/key", 1, data);
    Mutation patch = patchMutation("collection/key", map("foo", "new-bar"));
    patch.applyToRemoteDocument(patchDoc, mutationResult(4));
    assertEquals(doc("collection/key", 4, map("foo", "new-bar")).setHasCommittedMutations(), patchDoc);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Example 19 with MutableDocument

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

the class MutationTest method testAppliesPatchToNullDocWithMergeToDocuments.

@Test
public void testAppliesPatchToNullDocWithMergeToDocuments() {
    MutableDocument mergeDoc = MutableDocument.newInvalidDocument(key("collection/key"));
    Mutation upsert = mergeMutation("collection/key", map("foo.bar", "new-bar-value"), Arrays.asList(field("foo.bar")));
    upsert.applyToLocalView(mergeDoc, /* previousMask= */
    null, Timestamp.now());
    Map<String, Object> expectedData = map("foo", map("bar", "new-bar-value"));
    assertEquals(doc("collection/key", 0, expectedData).setHasLocalMutations(), mergeDoc);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Example 20 with MutableDocument

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

the class MutationTest method testAppliesServerAckedArrayTransformsToDocuments.

@Test
public void testAppliesServerAckedArrayTransformsToDocuments() {
    Map<String, Object> data = map("array1", Arrays.asList(1, 2), "array2", Arrays.asList("a", "b"));
    MutableDocument transformedDoc = doc("collection/key", 1, data);
    Mutation transform = setMutation("collection/key", map("array1", FieldValue.arrayUnion(2, 3), "array2", FieldValue.arrayRemove("a", "c")));
    // Server just sends null transform results for array operations.
    MutationResult mutationResult = new MutationResult(version(1), Arrays.asList(wrap(null), wrap(null)));
    transform.applyToRemoteDocument(transformedDoc, mutationResult);
    Map<String, Object> expectedData = map("array1", Arrays.asList(1, 2, 3), "array2", Arrays.asList("b"));
    assertEquals(doc("collection/key", 1, expectedData).setHasCommittedMutations(), transformedDoc);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Aggregations

MutableDocument (com.google.firebase.firestore.model.MutableDocument)166 Test (org.junit.Test)125 DocumentKey (com.google.firebase.firestore.model.DocumentKey)43 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)30 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)30 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)30 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)30 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)30 HashMap (java.util.HashMap)22 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)18 ArrayList (java.util.ArrayList)18 TargetData (com.google.firebase.firestore.local.TargetData)15 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)14 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)13 ResourcePath (com.google.firebase.firestore.model.ResourcePath)12 Query (com.google.firebase.firestore.core.Query)10 Map (java.util.Map)10 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)8 Document (com.google.firebase.firestore.model.Document)7 Timestamp (com.google.firebase.Timestamp)6