def on_snapshot(self, callback): """Watch this document. This starts a watch on this document using a background thread. The provided callback is run on the snapshot. Args: callback(~.firestore.document.DocumentSnapshot):a callback to run when a change occurs Example: from google.cloud import firestore_v1beta1 db = firestore_v1beta1.Client() collection_ref = db.collection(u'users') def on_snapshot(document_snapshot): doc = document_snapshot print(u'{} => {}'.format(doc.id, doc.to_dict())) doc_ref = db.collection(u'users').document( u'alovelace' + unique_resource_id()) # Watch this document doc_watch = doc_ref.on_snapshot(on_snapshot) # Terminate this watch doc_watch.unsubscribe() """ return Watch.for_document(self, callback, DocumentSnapshot, DocumentReference)
def on_snapshot(self, callback): """Monitor the documents in this collection. This starts a watch on this collection using a background thread. The provided callback is run on the snapshot of the documents. Args: callback(~.firestore.collection.CollectionSnapshot): a callback to run when a change occurs. Example: from google.cloud import firestore db = firestore.Client() collection_ref = db.collection(u'users') def on_snapshot(collection_snapshot): for doc in collection_snapshot.documents: print(u'{} => {}'.format(doc.id, doc.to_dict())) # Watch this collection collection_watch = collection_ref.on_snapshot(on_snapshot) # Terminate this watch collection_watch.unsubscribe() """ return Watch.for_query( query_mod.Query(self), callback, document.DocumentSnapshot, document.DocumentReference, )
def on_snapshot(self, callback): """Monitor the documents in this collection. This starts a watch on this collection using a background thread. The provided callback is run on the snapshot of the documents. Args: callback(~.firestore.collection.CollectionSnapshot): a callback to run when a change occurs. Example: from google.cloud import firestore_v1beta1 db = firestore_v1beta1.Client() collection_ref = db.collection(u'users') def on_snapshot(collection_snapshot): for doc in collection_snapshot.documents: print(u'{} => {}'.format(doc.id, doc.to_dict())) # Watch this collection collection_watch = collection_ref.on_snapshot(on_snapshot) # Terminate this watch collection_watch.unsubscribe() """ return Watch.for_query( query_mod.Query(self), callback, document.DocumentSnapshot, document.DocumentReference, )
def test_for_query(self): from google.cloud.firestore_v1beta1.watch import Watch snapshot_callback = self._snapshot_callback snapshot_class_instance = DummyDocumentSnapshot document_reference_class_instance = DummyDocumentReference modulename = 'google.cloud.firestore_v1beta1.watch' pb2 = DummyPb2() with mock.patch( '%s.firestore_pb2' % modulename, pb2, ): with mock.patch( '%s.Watch.ResumableBidiRpc' % modulename, DummyRpc, ): with mock.patch( '%s.Watch.BackgroundConsumer' % modulename, DummyBackgroundConsumer, ): query = DummyQuery() inst = Watch.for_query(query, snapshot_callback, snapshot_class_instance, document_reference_class_instance) self.assertTrue(inst._consumer.started) self.assertTrue(inst._rpc.callbacks, [inst._on_rpc_done]) self.assertEqual(inst._targets['query'], 'dummy query target')
def on_snapshot(self, callback): """Monitor the documents in this collection that match this query. This starts a watch on this query using a background thread. The provided callback is run on the snapshot of the documents. Args: callback(~.firestore.query.QuerySnapshot): a callback to run when a change occurs. Example: from google.cloud import firestore db = firestore.Client() query_ref = db.collection(u'users').where("user", "==", u'Ada') def on_snapshot(docs, changes, read_time): for doc in docs: print(u'{} => {}'.format(doc.id, doc.to_dict())) # Watch this query query_watch = query_ref.on_snapshot(on_snapshot) # Terminate this watch query_watch.unsubscribe() """ return Watch.for_query( self, callback, document.DocumentSnapshot, document.DocumentReference )
def on_snapshot(self, callback): """Monitor the documents in this collection that match this query. This starts a watch on this query using a background thread. The provided callback is run on the snapshot of the documents. Args: callback(~.firestore.query.QuerySnapshot): a callback to run when a change occurs. Example: from google.cloud import firestore_v1beta1 db = firestore_v1beta1.Client() query_ref = db.collection(u'users').where("user", "==", u'Ada') def on_snapshot(docs, changes, read_time): for doc in docs: print(u'{} => {}'.format(doc.id, doc.to_dict())) # Watch this query query_watch = query_ref.on_snapshot(on_snapshot) # Terminate this watch query_watch.unsubscribe() """ return Watch.for_query( self, callback, document.DocumentSnapshot, document.DocumentReference )
def _makeOne( self, document_reference=None, firestore=None, target=None, comparator=None, snapshot_callback=None, snapshot_class=None, reference_class=None, ): # pragma: NO COVER from google.cloud.firestore_v1beta1.watch import Watch if document_reference is None: document_reference = DummyDocumentReference() if firestore is None: firestore = DummyFirestore() if target is None: WATCH_TARGET_ID = 0x5079 # "Py" target = { "documents": { "documents": ["/"] }, "target_id": WATCH_TARGET_ID } if comparator is None: comparator = self._document_watch_comparator if snapshot_callback is None: snapshot_callback = self._snapshot_callback if snapshot_class is None: snapshot_class = DummyDocumentSnapshot if reference_class is None: reference_class = DummyDocumentReference inst = Watch( document_reference, firestore, target, comparator, snapshot_callback, snapshot_class, reference_class, BackgroundConsumer=DummyBackgroundConsumer, ResumableBidiRpc=DummyRpc, ) return inst
def test_for_document(self): from google.cloud.firestore_v1beta1.watch import Watch docref = DummyDocumentReference() snapshot_callback = self._snapshot_callback snapshot_class_instance = DummyDocumentSnapshot document_reference_class_instance = DummyDocumentReference modulename = "google.cloud.firestore_v1beta1.watch" with mock.patch("%s.Watch.ResumableBidiRpc" % modulename, DummyRpc): with mock.patch("%s.Watch.BackgroundConsumer" % modulename, DummyBackgroundConsumer): inst = Watch.for_document( docref, snapshot_callback, snapshot_class_instance, document_reference_class_instance, ) self.assertTrue(inst._consumer.started) self.assertTrue(inst._rpc.callbacks, [inst._on_rpc_done])
def test_for_document(self): from google.cloud.firestore_v1beta1.watch import Watch docref = DummyDocumentReference() snapshot_callback = self._snapshot_callback snapshot_class_instance = DummyDocumentSnapshot document_reference_class_instance = DummyDocumentReference modulename = "google.cloud.firestore_v1beta1.watch" with mock.patch("%s.Watch.ResumableBidiRpc" % modulename, DummyRpc): with mock.patch( "%s.Watch.BackgroundConsumer" % modulename, DummyBackgroundConsumer ): inst = Watch.for_document( docref, snapshot_callback, snapshot_class_instance, document_reference_class_instance, ) self.assertTrue(inst._consumer.started) self.assertTrue(inst._rpc.callbacks, [inst._on_rpc_done])
def test_for_query(self): from google.cloud.firestore_v1beta1.watch import Watch snapshot_callback = self._snapshot_callback snapshot_class_instance = DummyDocumentSnapshot document_reference_class_instance = DummyDocumentReference modulename = "google.cloud.firestore_v1beta1.watch" pb2 = DummyPb2() with mock.patch("%s.firestore_pb2" % modulename, pb2): with mock.patch("%s.Watch.ResumableBidiRpc" % modulename, DummyRpc): with mock.patch( "%s.Watch.BackgroundConsumer" % modulename, DummyBackgroundConsumer ): query = DummyQuery() inst = Watch.for_query( query, snapshot_callback, snapshot_class_instance, document_reference_class_instance, ) self.assertTrue(inst._consumer.started) self.assertTrue(inst._rpc.callbacks, [inst._on_rpc_done]) self.assertEqual(inst._targets["query"], "dummy query target")