示例#1
0
    def test_commit(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = firestore_client.FirestoreClient()

        # Mock request
        database = client.database_root_path('[PROJECT]', '[DATABASE]')
        writes = []

        # Mock response
        expected_response = {}
        expected_response = firestore_pb2.CommitResponse(**expected_response)
        grpc_stub.Commit.return_value = expected_response

        response = client.commit(database, writes)
        self.assertEqual(expected_response, response)

        grpc_stub.Commit.assert_called_once()
        args, kwargs = grpc_stub.Commit.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = firestore_pb2.CommitRequest(database=database,
                                                       writes=writes)
        self.assertEqual(expected_request, actual_request)
示例#2
0
    def test_commit(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = firestore_pb2.CommitResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = firestore_client.FirestoreClient(channel=channel)

        # Setup Request
        database = client.database_root_path('[PROJECT]', '[DATABASE]')
        writes = []

        response = client.commit(database, writes)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.CommitRequest(database=database,
                                                       writes=writes)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_commit(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = firestore_pb2.CommitResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = firestore_client.FirestoreClient()

        # Setup Request
        database = "database1789464955"

        response = client.commit(database)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.CommitRequest(database=database)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
示例#4
0
    def commit(self, database, writes, transaction=None, options=None):
        """
        Commits a transaction, while optionally updating documents.

        Example:
            >>> from google.cloud import firestore_v1beta1
            >>>
            >>> client = firestore_v1beta1.FirestoreClient()
            >>>
            >>> database = client.database_root_path('[PROJECT]', '[DATABASE]')
            >>> writes = []
            >>>
            >>> response = client.commit(database, writes)

        Args:
            database (str): The database name. In the format:
                ``projects/{project_id}/databases/{database_id}``.
            writes (list[Union[dict, ~google.cloud.firestore_v1beta1.types.Write]]): The writes to apply.

                Always executed atomically and in order.
                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.firestore_v1beta1.types.Write`
            transaction (bytes): If set, applies all writes in this transaction, and commits it.
            options (~google.gax.CallOptions): Overrides the default
                settings for this call, e.g, timeout, retries etc.

        Returns:
            A :class:`~google.cloud.firestore_v1beta1.types.CommitResponse` instance.

        Raises:
            :exc:`google.gax.errors.GaxError` if the RPC is aborted.
            :exc:`ValueError` if the parameters are invalid.
        """
        request = firestore_pb2.CommitRequest(database=database,
                                              writes=writes,
                                              transaction=transaction)
        return self._commit(request, options)
示例#5
0
    def test_commit(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = firestore_pb2.CommitResponse(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = firestore_client.FirestoreClient()

        # Setup Request
        database = client.database_root_path('[PROJECT]', '[DATABASE]')
        writes = []

        response = client.commit(database, writes)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = firestore_pb2.CommitRequest(database=database,
                                                       writes=writes)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request