Search in sources :

Example 56 with MutableDocument

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

the class ViewTest method testAddsDocumentsBasedOnQuery.

@Test
public void testAddsDocumentsBasedOnQuery() {
    Query query = messageQuery();
    View view = new View(query, DocumentKey.emptyKeySet());
    MutableDocument doc1 = doc("rooms/eros/messages/1", 0, map("text", "msg1"));
    MutableDocument doc2 = doc("rooms/eros/messages/2", 0, map("text", "msg2"));
    MutableDocument doc3 = doc("rooms/other/messages/1", 0, map("text", "msg3"));
    ImmutableSortedMap<DocumentKey, Document> updates = docUpdates(doc1, doc2, doc3);
    View.DocumentChanges docViewChanges = view.computeDocChanges(updates);
    TargetChange targetChange = ackTarget(doc1, doc2, doc3);
    ViewSnapshot snapshot = view.applyChanges(docViewChanges, targetChange).getSnapshot();
    assertEquals(query, snapshot.getQuery());
    assertEquals(asList(doc1, doc2), snapshot.getDocuments().toList());
    assertEquals(asList(DocumentViewChange.create(Type.ADDED, doc1), DocumentViewChange.create(Type.ADDED, doc2)), snapshot.getChanges());
    assertFalse(snapshot.isFromCache());
    assertTrue(snapshot.didSyncStateChange());
    assertFalse(snapshot.hasPendingWrites());
}
Also used : TargetChange(com.google.firebase.firestore.remote.TargetChange) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentKey(com.google.firebase.firestore.model.DocumentKey) Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 57 with MutableDocument

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

the class ViewTest method testSuppressesWriteAcknowledgementIfWatchHasNotCaughtUp.

@Test
public void testSuppressesWriteAcknowledgementIfWatchHasNotCaughtUp() {
    // This test verifies that we don't get three events for a ServerTimestamp mutation. We suppress
    // the event generated by the write acknowledgement and instead wait for Watch to catch up.
    Query query = messageQuery();
    MutableDocument doc1 = doc("rooms/eros/messages/1", 1, map("time", 1)).setHasLocalMutations();
    MutableDocument doc1Committed = doc("rooms/eros/messages/1", 2, map("time", 2)).setHasCommittedMutations();
    MutableDocument doc1Acknowledged = doc("rooms/eros/messages/1", 2, map("time", 2));
    MutableDocument doc2 = doc("rooms/eros/messages/2", 1, map("time", 1)).setHasLocalMutations();
    MutableDocument doc2Modified = doc("rooms/eros/messages/2", 2, map("time", 3)).setHasLocalMutations();
    MutableDocument doc2Acknowledged = doc("rooms/eros/messages/2", 2, map("time", 3));
    View view = new View(query, DocumentKey.emptyKeySet());
    View.DocumentChanges changes = view.computeDocChanges(docUpdates(doc1, doc2));
    ViewChange snap = view.applyChanges(changes);
    assertEquals(asList(DocumentViewChange.create(Type.ADDED, doc1), DocumentViewChange.create(Type.ADDED, doc2)), snap.getSnapshot().getChanges());
    changes = view.computeDocChanges(docUpdates(doc1Committed, doc2Modified));
    snap = view.applyChanges(changes);
    // The 'doc1Committed' update is suppressed
    assertEquals(Collections.singletonList(DocumentViewChange.create(Type.MODIFIED, doc2Modified)), snap.getSnapshot().getChanges());
    changes = view.computeDocChanges(docUpdates(doc1Acknowledged, doc2Acknowledged));
    snap = view.applyChanges(changes);
    assertEquals(asList(DocumentViewChange.create(Type.MODIFIED, doc1Acknowledged), DocumentViewChange.create(Type.METADATA, doc2Acknowledged)), snap.getSnapshot().getChanges());
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 58 with MutableDocument

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

the class ViewTest method testDoesNotNeedRefillForDeletionsWhenNotNearTheLimit.

@Test
public void testDoesNotNeedRefillForDeletionsWhenNotNearTheLimit() {
    Query query = messageQuery().limitToFirst(20);
    MutableDocument doc1 = doc("rooms/eros/messages/0", 0, map());
    MutableDocument doc2 = doc("rooms/eros/messages/1", 0, map());
    View view = new View(query, DocumentKey.emptyKeySet());
    View.DocumentChanges changes = view.computeDocChanges(docUpdates(doc1, doc2));
    assertEquals(2, changes.documentSet.size());
    assertFalse(changes.needsRefill());
    assertEquals(2, changes.changeSet.getChanges().size());
    view.applyChanges(changes);
    // Remove one of the docs.
    changes = view.computeDocChanges(docUpdates(deletedDoc("rooms/eros/messages/1", 0)));
    assertEquals(1, changes.documentSet.size());
    assertFalse(changes.needsRefill());
    assertEquals(1, changes.changeSet.getChanges().size());
    view.applyChanges(changes);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 59 with MutableDocument

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

the class ViewTest method testReturnsNeedsRefillOnReorderInLimitQuery.

@Test
public void testReturnsNeedsRefillOnReorderInLimitQuery() {
    Query query = messageQuery().orderBy(orderBy("order")).limitToFirst(2);
    MutableDocument doc1 = doc("rooms/eros/messages/0", 0, map("order", 1));
    MutableDocument doc2 = doc("rooms/eros/messages/1", 0, map("order", 2));
    MutableDocument doc3 = doc("rooms/eros/messages/2", 0, map("order", 3));
    View view = new View(query, DocumentKey.emptyKeySet());
    // Start with a full view.
    View.DocumentChanges changes = view.computeDocChanges(docUpdates(doc1, doc2, doc3));
    assertEquals(2, changes.documentSet.size());
    assertFalse(changes.needsRefill());
    assertEquals(2, changes.changeSet.getChanges().size());
    view.applyChanges(changes);
    // Move one of the docs.
    doc2 = doc("rooms/eros/messages/1", 1, map("order", 2000));
    changes = view.computeDocChanges(docUpdates(doc2));
    assertEquals(2, changes.documentSet.size());
    assertTrue(changes.needsRefill());
    assertEquals(1, changes.changeSet.getChanges().size());
    // Refill it with all three current docs.
    changes = view.computeDocChanges(docUpdates(doc1, doc2, doc3), changes);
    assertEquals(2, changes.documentSet.size());
    assertFalse(changes.needsRefill());
    assertEquals(2, changes.changeSet.getChanges().size());
    view.applyChanges(changes);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 60 with MutableDocument

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

the class ViewTest method testHandlesApplyingIrrelevantDocs.

@Test
public void testHandlesApplyingIrrelevantDocs() {
    Query query = messageQuery().limitToFirst(2);
    MutableDocument doc1 = doc("rooms/eros/messages/0", 0, map());
    MutableDocument doc2 = doc("rooms/eros/messages/1", 0, map());
    View view = new View(query, DocumentKey.emptyKeySet());
    // Start with a full view.
    View.DocumentChanges changes = view.computeDocChanges(docUpdates(doc1, doc2));
    assertEquals(2, changes.documentSet.size());
    assertFalse(changes.needsRefill());
    assertEquals(2, changes.changeSet.getChanges().size());
    view.applyChanges(changes);
    // Remove a doc that isn't even in the results.
    changes = view.computeDocChanges(docUpdates(deletedDoc("rooms/eros/messages/2", 0)));
    assertEquals(2, changes.documentSet.size());
    assertFalse(changes.needsRefill());
    assertEquals(0, changes.changeSet.getChanges().size());
    view.applyChanges(changes);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) 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