def test_get_database(self):
        # Setup Expected Response
        name_2 = "name2-1052831874"
        expected_response = {"name": name_2}
        expected_response = spanner_database_admin_pb2.Database(
            **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 = spanner_admin_database_v1.DatabaseAdminClient()

        # Setup Request
        name = client.database_path("[PROJECT]", "[INSTANCE]", "[DATABASE]")

        response = client.get_database(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = spanner_database_admin_pb2.GetDatabaseRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Exemplo n.º 2
0
    def get_database(self, name, options=None):
        """
        Gets the state of a Cloud Spanner database.

        Example:
            >>> from google.cloud import spanner_admin_database_v1
            >>>
            >>> client = spanner_admin_database_v1.DatabaseAdminClient()
            >>>
            >>> name = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]')
            >>>
            >>> response = client.get_database(name)

        Args:
            name (str): Required. The name of the requested database. Values are of the form
                ``projects/<project>/instances/<instance>/databases/<database>``.
            options (~google.gax.CallOptions): Overrides the default
                settings for this call, e.g, timeout, retries etc.

        Returns:
            A :class:`~google.cloud.spanner_admin_database_v1.types.Database` instance.

        Raises:
            :exc:`google.gax.errors.GaxError` if the RPC is aborted.
            :exc:`ValueError` if the parameters are invalid.
        """
        request = spanner_database_admin_pb2.GetDatabaseRequest(name=name)
        return self._get_database(request, options)
Exemplo n.º 3
0
    def test_get_database(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = spanner_admin_database_v1.DatabaseAdminClient()

        # Mock request
        name = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]')

        # Mock response
        name_2 = 'name2-1052831874'
        expected_response = {'name': name_2}
        expected_response = spanner_database_admin_pb2.Database(
            **expected_response)
        grpc_stub.GetDatabase.return_value = expected_response

        response = client.get_database(name)
        self.assertEqual(expected_response, response)

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

        expected_request = spanner_database_admin_pb2.GetDatabaseRequest(
            name=name)
        self.assertEqual(expected_request, actual_request)
Exemplo n.º 4
0
    def get_database(
        self,
        name,
        retry=google.api_core.gapic_v1.method.DEFAULT,
        timeout=google.api_core.gapic_v1.method.DEFAULT,
        metadata=None,
    ):
        """
        Gets the state of a Cloud Spanner database.

        Example:
            >>> from google.cloud import spanner_admin_database_v1
            >>>
            >>> client = spanner_admin_database_v1.DatabaseAdminClient()
            >>>
            >>> name = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]')
            >>>
            >>> response = client.get_database(name)

        Args:
            name (str): Required. The name of the requested database. Values are of the form
                ``projects/<project>/instances/<instance>/databases/<database>``.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.spanner_admin_database_v1.types.Database` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if "get_database" not in self._inner_api_calls:
            self._inner_api_calls[
                "get_database"] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.get_database,
                    default_retry=self._method_configs["GetDatabase"].retry,
                    default_timeout=self._method_configs["GetDatabase"].
                    timeout,
                    client_info=self._client_info,
                )

        request = spanner_database_admin_pb2.GetDatabaseRequest(name=name)
        return self._inner_api_calls["get_database"](request,
                                                     retry=retry,
                                                     timeout=timeout,
                                                     metadata=metadata)
    def test_get_database(self):
        # Setup Expected Response
        name_2 = 'name2-1052831874'
        expected_response = {'name': name_2}
        expected_response = spanner_database_admin_pb2.Database(
            **expected_response)

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

        # Setup Request
        name = client.database_path('[PROJECT]', '[INSTANCE]', '[DATABASE]')

        response = client.get_database(name)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = spanner_database_admin_pb2.GetDatabaseRequest(
            name=name)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request