示例#1
0
    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,
        )
示例#2
0
    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,
        )
示例#3
0
 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')
示例#4
0
    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 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")