Beispiel #1
0
    def test_for_query_nested(self):
        from google.cloud.firestore_v1.watch import Watch

        snapshot_callback = self._snapshot_callback
        snapshot_class_instance = DummyDocumentSnapshot
        document_reference_class_instance = DummyDocumentReference
        client = DummyFirestore()
        root = DummyCollection(client)
        grandparent = DummyDocument("document", parent=root)
        parent = DummyCollection(client, parent=grandparent)
        modulename = "google.cloud.firestore_v1.watch"
        pb2 = DummyPb2()
        with mock.patch("%s.firestore" % modulename, pb2):
            with mock.patch("%s.Watch.ResumableBidiRpc" % modulename,
                            DummyRpc):
                with mock.patch("%s.Watch.BackgroundConsumer" % modulename,
                                DummyBackgroundConsumer):
                    query = DummyQuery(parent=parent)
                    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: Callable) -> Watch:
        """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(Callable[[:class:`~google.cloud.firestore.query.QuerySnapshot`], NoneType]):
                a callback to run when a change occurs.

        Example:

        .. code-block:: python

            from google.cloud import firestore_v1

            db = firestore_v1.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.

        This starts a watch on this collection using a background thread. The
        provided callback is run on the snapshot of the documents.

        Args:
            callback (Callable[[:class:`~google.cloud.firestore.collection.CollectionSnapshot`], NoneType]):
                a callback to run when a change occurs.

        Example:
            from google.cloud import firestore_v1

            db = firestore_v1.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 (Callable[[:class:`~google.cloud.firestore.collection.CollectionSnapshot`], NoneType]):
                a callback to run when a change occurs.

        Example:
            from google.cloud import firestore_v1

            db = firestore_v1.Client()
            collection_ref = db.collection(u'users')

            def on_snapshot(collection_snapshot, changes, read_time):
                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_nested(self):
        from google.cloud.firestore_v1.watch import Watch

        snapshot_callback = self._snapshot_callback
        snapshot_class_instance = DummyDocumentSnapshot
        document_reference_class_instance = DummyDocumentReference
        client = DummyFirestore()
        root = DummyCollection(client)
        grandparent = DummyDocument("document", parent=root)
        parent = DummyCollection(client, parent=grandparent)
        modulename = "google.cloud.firestore_v1.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(parent=parent)
                    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(Callable[[:class:`~google.cloud.firestore.query.QuerySnapshot`], NoneType]):
                a callback to run when a change occurs.

        Example:

        .. code-block:: python

            from google.cloud import firestore_v1

            db = firestore_v1.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
        )
Beispiel #7
0
def test_watch_for_query_nested(snapshots):
    from google.cloud.firestore_v1.watch import Watch

    def snapshot_callback(*args):  # pragma: NO COVER
        snapshots.append(args)

    client = DummyFirestore()
    root = DummyCollection(client)
    grandparent = DummyDocument("document", parent=root)
    parent = DummyCollection(client, parent=grandparent)
    query = DummyQuery(parent=parent)

    with mock.patch("google.cloud.firestore_v1.watch.ResumableBidiRpc"):
        with mock.patch("google.cloud.firestore_v1.watch.BackgroundConsumer"):
            with mock.patch(
                    "google.cloud.firestore_v1.watch.Target") as target:
                inst = Watch.for_query(
                    query,
                    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)
    query_target = target.QueryTarget.return_value
    parent_path, _ = parent._parent_info()
    target.QueryTarget.assert_called_once_with(
        parent=parent_path,
        structured_query=query._to_protobuf(),
    )
    query_target = target.QueryTarget.return_value
    assert inst._targets["query"] is query_target._pb