use of com.google.firebase.firestore.remote.WatchChange.DocumentChange 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()));
}
use of com.google.firebase.firestore.remote.WatchChange.DocumentChange 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));
}
use of com.google.firebase.firestore.remote.WatchChange.DocumentChange 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));
}
use of com.google.firebase.firestore.remote.WatchChange.DocumentChange 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));
}
use of com.google.firebase.firestore.remote.WatchChange.DocumentChange 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);
}
Aggregations