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(Callable[[:class:`~google.cloud.firestore.document.DocumentSnapshot`], NoneType]): a callback to run when a change occurs Example: .. code-block:: python from google.cloud import firestore_v1 db = firestore_v1.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) -> Watch: """Watch this document. This starts a watch on this document using a background thread. The provided callback is run on the snapshot. Args: callback(Callable[[:class:`~google.cloud.firestore.document.DocumentSnapshot`], NoneType]): a callback to run when a change occurs Example: .. code-block:: python from google.cloud import firestore_v1 db = firestore_v1.Client() collection_ref = db.collection(u'users') def on_snapshot(document_snapshot, changes, read_time): 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_watch_for_document(snapshots): from google.cloud.firestore_v1.watch import Watch def snapshot_callback(*args): # pragma: NO COVER snapshots.append(args) docref = DummyDocumentReference() with mock.patch("google.cloud.firestore_v1.watch.ResumableBidiRpc"): with mock.patch("google.cloud.firestore_v1.watch.BackgroundConsumer"): inst = Watch.for_document( docref, snapshot_callback, document_snapshot_cls=DummyDocumentSnapshot, ) inst._consumer.start.assert_called_once_with() inst._rpc.add_done_callback.assert_called_once_with(inst._on_rpc_done)
def test_for_document(self): from google.cloud.firestore_v1.watch import Watch docref = DummyDocumentReference() snapshot_callback = self._snapshot_callback snapshot_class_instance = DummyDocumentSnapshot document_reference_class_instance = DummyDocumentReference modulename = "google.cloud.firestore_v1.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_v1.watch import Watch docref = DummyDocumentReference() snapshot_callback = self._snapshot_callback snapshot_class_instance = DummyDocumentSnapshot document_reference_class_instance = DummyDocumentReference modulename = "google.cloud.firestore_v1.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])