Search in sources :

Example 36 with MutableDocument

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

the class RemoteEventTest method testWillAccumulateDocumentAddedAndRemovedEvents.

@Test
public void testWillAccumulateDocumentAddedAndRemovedEvents() {
    Map<Integer, TargetData> targetMap = activeQueries(1, 2, 3, 4, 5, 6);
    MutableDocument existingDoc = doc("docs/1", 1, map("value", 1));
    MutableDocument newDoc = doc("docs/2", 2, map("value", 2));
    WatchChange change1 = new DocumentChange(asList(1, 2, 3), asList(4, 5, 6), existingDoc.getKey(), existingDoc);
    WatchChange change2 = new DocumentChange(asList(1, 4), asList(2, 6), newDoc.getKey(), newDoc);
    RemoteEvent event = createRemoteEvent(3, targetMap, noOutstandingResponses, keySet(existingDoc.getKey()), change1, change2);
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(2, event.getDocumentUpdates().size());
    assertEquals(existingDoc, event.getDocumentUpdates().get(existingDoc.getKey()));
    assertEquals(newDoc, event.getDocumentUpdates().get(newDoc.getKey()));
    assertEquals(6, event.getTargetChanges().size());
    TargetChange mapping1 = targetChange(resumeToken, false, asList(newDoc), asList(existingDoc), null);
    assertEquals(mapping1, event.getTargetChanges().get(1));
    TargetChange mapping2 = targetChange(resumeToken, false, null, asList(existingDoc), null);
    assertEquals(mapping2, event.getTargetChanges().get(2));
    TargetChange mapping3 = targetChange(resumeToken, false, null, asList(existingDoc), null);
    assertEquals(mapping3, event.getTargetChanges().get(3));
    TargetChange mapping4 = targetChange(resumeToken, false, asList(newDoc), null, asList(existingDoc));
    assertEquals(mapping4, event.getTargetChanges().get(4));
    TargetChange mapping5 = targetChange(resumeToken, false, null, null, asList(existingDoc));
    assertEquals(mapping5, event.getTargetChanges().get(5));
    TargetChange mapping6 = targetChange(resumeToken, false, null, null, asList(existingDoc));
    assertEquals(mapping6, event.getTargetChanges().get(6));
}
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 37 with MutableDocument

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

the class RemoteEventTest method testWillKeepResetMappingEvenWithUpdates.

@Test
public void testWillKeepResetMappingEvenWithUpdates() {
    Map<Integer, TargetData> targetMap = activeQueries(1);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    MutableDocument doc2 = doc("docs/2", 2, map("value", 2));
    MutableDocument doc3 = doc("docs/3", 3, map("value", 3));
    WatchChange change1 = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    // Reset stream, ignoring doc1
    WatchChange change2 = new WatchTargetChange(WatchTargetChangeType.Reset, asList(1));
    // Add doc2, doc3
    WatchChange change3 = new DocumentChange(asList(1), emptyList(), doc2.getKey(), doc2);
    WatchChange change4 = new DocumentChange(asList(1), emptyList(), doc3.getKey(), doc3);
    // Remove doc2 again, should not show up in reset mapping.
    WatchChange change5 = new DocumentChange(emptyList(), asList(1), doc2.getKey(), doc2);
    RemoteEvent event = createRemoteEvent(3, targetMap, noOutstandingResponses, keySet(doc1.getKey()), change1, change2, change3, change4, change5);
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(3, event.getDocumentUpdates().size());
    assertEquals(doc1, event.getDocumentUpdates().get(doc1.getKey()));
    assertEquals(doc2, event.getDocumentUpdates().get(doc2.getKey()));
    assertEquals(doc3, event.getDocumentUpdates().get(doc3.getKey()));
    assertEquals(1, event.getTargetChanges().size());
    // Only doc3 is part of the new mapping.
    TargetChange expected = targetChange(resumeToken, false, asList(doc3), null, asList(doc1));
    assertEquals(expected, 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) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 38 with MutableDocument

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

the class RemoteEventTest method testExistenceFilterMismatchRemovesCurrentChanges.

@Test
public void testExistenceFilterMismatchRemovesCurrentChanges() {
    Map<Integer, TargetData> targetMap = activeQueries(1);
    WatchChangeAggregator aggregator = createAggregator(targetMap, noOutstandingResponses, noExistingKeys);
    WatchTargetChange markCurrent = new WatchTargetChange(WatchTargetChangeType.Current, asList(1));
    aggregator.handleTargetChange(markCurrent);
    MutableDocument doc1 = doc("docs/1", 1, map("value", 1));
    DocumentChange addDoc = new DocumentChange(asList(1), emptyList(), doc1.getKey(), doc1);
    aggregator.handleDocumentChange(addDoc);
    // The existence filter mismatch will remove the document from target 1, but not synthesize a
    // document delete.
    WatchChange.ExistenceFilterWatchChange existenceFilter = new WatchChange.ExistenceFilterWatchChange(1, new ExistenceFilter(0));
    aggregator.handleExistenceFilter(existenceFilter);
    RemoteEvent event = aggregator.createRemoteEvent(version(3));
    assertEquals(version(3), event.getSnapshotVersion());
    assertEquals(1, event.getDocumentUpdates().size());
    assertEquals(1, event.getTargetMismatches().size());
    assertEquals(doc1, event.getDocumentUpdates().get(doc1.getKey()));
    assertEquals(1, event.getTargetChanges().size());
    TargetChange mapping1 = targetChange(ByteString.EMPTY, false, null, null, null);
    assertEquals(mapping1, event.getTargetChanges().get(1));
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) TargetData(com.google.firebase.firestore.local.TargetData) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 39 with MutableDocument

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

the class RemoteEventTest method testTracksLimboDocuments.

@Test
public void testTracksLimboDocuments() {
    Map<Integer, TargetData> listens = activeQueries(1);
    listens.putAll(activeLimboQueries("doc/2", 2));
    // Add 3 docs: 1 is limbo and non-limbo, 2 is limbo-only, 3 is non-limbo
    MutableDocument doc1 = doc("docs/1", 1, map("key", "value"));
    MutableDocument doc2 = doc("docs/2", 1, map("key", "value"));
    MutableDocument doc3 = doc("docs/3", 1, map("key", "value"));
    // Target 2 is a limbo target
    DocumentChange docChange1 = new DocumentChange(asList(1, 2), emptyList(), doc1.getKey(), doc1);
    DocumentChange docChange2 = new DocumentChange(asList(2), emptyList(), doc2.getKey(), doc2);
    DocumentChange docChange3 = new DocumentChange(asList(1), emptyList(), doc3.getKey(), doc3);
    WatchTargetChange targetsChange = new WatchTargetChange(WatchTargetChangeType.Current, asList(1, 2));
    RemoteEvent event = createRemoteEvent(3, listens, noOutstandingResponses, noExistingKeys, docChange1, docChange2, docChange3, targetsChange);
    Set<DocumentKey> limboDocuments = event.getResolvedLimboDocuments();
    // Doc1 is in both limbo and non-limbo targets, therefore not tracked as limbo
    assertFalse(limboDocuments.contains(doc1.getKey()));
    // Doc2 is only in the limbo target, so is tracked as a limbo document
    assertTrue(limboDocuments.contains(doc2.getKey()));
    // Doc3 is only in the non-limbo target, therefore not tracked as limbo
    assertFalse(limboDocuments.contains(doc3.getKey()));
}
Also used : TargetData(com.google.firebase.firestore.local.TargetData) MutableDocument(com.google.firebase.firestore.model.MutableDocument) DocumentKey(com.google.firebase.firestore.model.DocumentKey) DocumentChange(com.google.firebase.firestore.remote.WatchChange.DocumentChange) WatchTargetChange(com.google.firebase.firestore.remote.WatchChange.WatchTargetChange) Test(org.junit.Test)

Example 40 with MutableDocument

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

the class LocalSerializerTest method testEncodesDeletedDocument.

@Test
public void testEncodesDeletedDocument() {
    MutableDocument deletedDoc = deletedDoc("some/path", 42);
    com.google.firebase.firestore.proto.MaybeDocument maybeDocProto = com.google.firebase.firestore.proto.MaybeDocument.newBuilder().setNoDocument(com.google.firebase.firestore.proto.NoDocument.newBuilder().setName("projects/p/databases/d/documents/some/path").setReadTime(com.google.protobuf.Timestamp.newBuilder().setSeconds(0).setNanos(42000))).build();
    assertEquals(maybeDocProto, serializer.encodeMaybeDocument(deletedDoc));
    MutableDocument decoded = serializer.decodeMaybeDocument(maybeDocProto);
    assertEquals(deletedDoc, decoded);
}
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