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 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])