Search in sources :

Example 31 with MutableDocument

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

the class MutationTest method testSetWithMutationResult.

@Test
public void testSetWithMutationResult() {
    Map<String, Object> data = map("foo", "bar");
    MutableDocument setDoc = doc("collection/key", 1, data);
    Mutation set = setMutation("collection/key", map("foo", "new-bar"));
    set.applyToRemoteDocument(setDoc, mutationResult(4));
    assertEquals(doc("collection/key", 4, map("foo", "new-bar")).setHasCommittedMutations(), setDoc);
}
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 32 with MutableDocument

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

the class MutationTest method testAppliesPatchWithMergeToDocuments.

@Test
public void testAppliesPatchWithMergeToDocuments() {
    MutableDocument mergeDoc = deletedDoc("collection/key", 2);
    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", 2, 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 33 with MutableDocument

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

the class RemoteEventTest method testTargetAddedChangeWillResetPreviousState.

@Test
public void testTargetAddedChangeWillResetPreviousState() {
    Map<Integer, TargetData> targetMap = activeQueries(1, 3);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    MutableDocument doc2 = doc("docs/2", 2, map("value", 2));
    WatchChange change1 = new DocumentChange(asList(1, 3), asList(2), doc1.getKey(), doc1);
    WatchChange change2 = new WatchTargetChange(WatchTargetChangeType.Current, asList(1, 2, 3));
    WatchChange change3 = new WatchTargetChange(WatchTargetChangeType.Removed, asList(1));
    WatchChange change4 = new WatchTargetChange(WatchTargetChangeType.Removed, asList(2));
    WatchChange change5 = new WatchTargetChange(WatchTargetChangeType.Added, asList(1));
    WatchChange change6 = new DocumentChange(asList(1), asList(3), doc2.getKey(), doc2);
    Map<Integer, Integer> outstanding = new HashMap<>();
    outstanding.put(1, 2);
    outstanding.put(2, 1);
    RemoteEvent event = createRemoteEvent(3, targetMap, outstanding, keySet(doc2.getKey()), change1, change2, change3, change4, change5, change6);
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(2, event.getDocumentUpdates().size());
    assertEquals(doc1, event.getDocumentUpdates().get(doc1.getKey()));
    assertEquals(doc2, event.getDocumentUpdates().get(doc2.getKey()));
    // target 1 and 3 are affected (1 because of re-add), target 2 is not because of remove.
    assertEquals(2, event.getTargetChanges().size());
    // doc1 was before the remove, so it does not show up in the mapping.
    // Current was before the remove.
    TargetChange mapping1 = targetChange(resumeToken, false, null, asList(doc2), null);
    assertEquals(mapping1, event.getTargetChanges().get(1));
    // Doc1 was before the remove.
    // Current was after the remove
    TargetChange mapping3 = targetChange(resumeToken, true, asList(doc1), null, asList(doc2));
    assertEquals(mapping3, event.getTargetChanges().get(3));
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) HashMap(java.util.HashMap) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 34 with MutableDocument

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

the class RemoteEventTest method testWillHandleTargetAddAndRemovalInSameBatch.

@Test
public void testWillHandleTargetAddAndRemovalInSameBatch() {
    Map<Integer, TargetData> targetMap = activeQueries(1, 2);
    MutableDocument doc1a = doc("docs/1", 1, map("value", 1));
    MutableDocument doc1b = doc("docs/1", 1, map("value", 2));
    WatchChange change1 = new DocumentChange(asList(1), asList(2), doc1a.getKey(), doc1a);
    WatchChange change2 = new DocumentChange(asList(2), asList(1), doc1b.getKey(), doc1b);
    RemoteEvent event = createRemoteEvent(3, targetMap, noOutstandingResponses, keySet(doc1a.getKey()), change1, change2);
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(1, event.getDocumentUpdates().size());
    assertEquals(doc1b, event.getDocumentUpdates().get(doc1b.getKey()));
    assertEquals(2, event.getTargetChanges().size());
    TargetChange mapping1 = targetChange(resumeToken, false, null, null, asList(doc1b));
    assertEquals(mapping1, event.getTargetChanges().get(1));
    TargetChange mapping2 = targetChange(resumeToken, false, null, asList(doc1b), null);
    assertEquals(mapping2, event.getTargetChanges().get(2));
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) Test(org.junit.Test)

Example 35 with MutableDocument

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

the class RemoteEventTest method testSeparatesUpdates.

@Test
public void testSeparatesUpdates() {
    Map<Integer, TargetData> targetMap = activeQueries(1);
    MutableDocument newDoc = doc("docs/new", 1, map("key", "value"));
    DocumentChange newDocChange = new DocumentChange(asList(1), emptyList(), newDoc.getKey(), newDoc);
    MutableDocument existingDoc = doc("docs/existing", 1, map("some", "data"));
    DocumentChange existingDocChange = new DocumentChange(asList(1), emptyList(), existingDoc.getKey(), existingDoc);
    MutableDocument deletedDoc = deletedDoc("docs/deleted", 1);
    DocumentChange deletedDocChange = new DocumentChange(asList(1), emptyList(), deletedDoc.getKey(), deletedDoc);
    MutableDocument missingDoc = deletedDoc("docs/missing  ", 1);
    DocumentChange missingDocChange = new DocumentChange(asList(1), emptyList(), missingDoc.getKey(), missingDoc);
    RemoteEvent event = createRemoteEvent(3, targetMap, noOutstandingResponses, keySet(existingDoc.getKey(), deletedDoc.getKey()), newDocChange, existingDocChange, deletedDocChange, missingDocChange);
    TargetChange mapping = targetChange(resumeToken, false, asList(newDoc), asList(existingDoc), asList(deletedDoc));
    assertEquals(mapping, event.getTargetChanges().get(1));
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) 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