Search in sources :

Example 61 with MutableDocument

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

the class ViewTest method testRemovesKeysFromMutatedDocumentKeysWhenNewDocDoesNotHaveChanges.

@Test
public void testRemovesKeysFromMutatedDocumentKeysWhenNewDocDoesNotHaveChanges() {
    Query query = messageQuery().limitToFirst(2);
    MutableDocument doc1 = doc("rooms/eros/messages/0", 0, map());
    MutableDocument doc2 = doc("rooms/eros/messages/1", 0, map()).setHasLocalMutations();
    View view = new View(query, DocumentKey.emptyKeySet());
    // Start with a full view.
    View.DocumentChanges changes = view.computeDocChanges(docUpdates(doc1, doc2));
    view.applyChanges(changes);
    assertEquals(keySet(doc2.getKey()), changes.mutatedKeys);
    MutableDocument doc2Prime = doc("rooms/eros/messages/1", 0, map());
    changes = view.computeDocChanges(docUpdates(doc2Prime));
    view.applyChanges(changes);
    assertEquals(keySet(), changes.mutatedKeys);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 62 with MutableDocument

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

the class ViewTest method testReturnsNilIfNoChange.

@Test
public void testReturnsNilIfNoChange() {
    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"));
    // initial state
    applyChanges(view, doc1, doc2);
    ViewSnapshot snapshot = applyChanges(view, doc1, doc2).getSnapshot();
    assertNull(snapshot);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 63 with MutableDocument

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

the class ViewTest method testDoesNotNeedRefillOnReorderWithinLimit.

@Test
public void testDoesNotNeedRefillOnReorderWithinLimit() {
    Query query = messageQuery().orderBy(orderBy("order")).limitToFirst(3);
    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));
    MutableDocument doc4 = doc("rooms/eros/messages/3", 0, map("order", 4));
    MutableDocument doc5 = doc("rooms/eros/messages/4", 0, map("order", 5));
    View view = new View(query, DocumentKey.emptyKeySet());
    // Start with a full view.
    View.DocumentChanges changes = view.computeDocChanges(docUpdates(doc1, doc2, doc3, doc4, doc5));
    assertEquals(3, changes.documentSet.size());
    assertFalse(changes.needsRefill());
    assertEquals(3, changes.changeSet.getChanges().size());
    view.applyChanges(changes);
    // Move one of the docs.
    doc1 = doc("rooms/eros/messages/0", 1, map("order", 3));
    changes = view.computeDocChanges(docUpdates(doc1));
    assertEquals(3, 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 64 with MutableDocument

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

the class ViewTest method testReturnsNeedsRefillOnDeleteInLimitQuery.

@Test
public void testReturnsNeedsRefillOnDeleteInLimitQuery() {
    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 one of the docs.
    changes = view.computeDocChanges(docUpdates(deletedDoc("rooms/eros/messages/0", 0)));
    assertEquals(1, changes.documentSet.size());
    assertTrue(changes.needsRefill());
    assertEquals(1, changes.changeSet.getChanges().size());
    // Refill it with just the one doc remaining.
    changes = view.computeDocChanges(docUpdates(doc2), changes);
    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 65 with MutableDocument

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

the class ViewTest method testDoesNotReportChangesForDocumentBeyondLimit.

@Test
public void testDoesNotReportChangesForDocumentBeyondLimit() {
    Query query = messageQuery().orderBy(orderBy("num")).limitToFirst(2);
    View view = new View(query, DocumentKey.emptyKeySet());
    MutableDocument doc1 = doc("rooms/eros/messages/1", 0, map("num", 1));
    MutableDocument doc2 = doc("rooms/eros/messages/2", 0, map("num", 2));
    MutableDocument doc3 = doc("rooms/eros/messages/3", 0, map("num", 3));
    MutableDocument doc4 = doc("rooms/eros/messages/4", 0, map("num", 4));
    applyChanges(view, doc1, doc2);
    // change doc2 to 5, and add doc3 and doc4.
    // doc2 will be modified + removed = removed
    // doc3 will be added
    // doc4 will be added + removed = nothing
    doc2 = doc("rooms/eros/messages/2", 1, map("num", 5));
    View.DocumentChanges viewDocChanges = view.computeDocChanges(docUpdates(doc2, doc3, doc4));
    assertTrue(viewDocChanges.needsRefill());
    // Verify that all the docs still match.
    viewDocChanges = view.computeDocChanges(docUpdates(doc1, doc2, doc3, doc4), viewDocChanges);
    ViewSnapshot snapshot = view.applyChanges(viewDocChanges, ackTarget(doc1, doc2, doc3, doc4)).getSnapshot();
    assertEquals(query, snapshot.getQuery());
    assertEquals(asList(doc1, doc3), snapshot.getDocuments().toList());
    assertEquals(asList(DocumentViewChange.create(Type.REMOVED, doc2), DocumentViewChange.create(Type.ADDED, doc3)), snapshot.getChanges());
    assertFalse(snapshot.isFromCache());
    assertTrue(snapshot.didSyncStateChange());
}
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