def test_create_database(self): # Setup Expected Response name = "name3373707" expected_response = {"name": name} expected_response = spanner_database_admin_pb2.Database( **expected_response) operation = operations_pb2.Operation( name="operations/test_create_database", done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) 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 parent = client.instance_path("[PROJECT]", "[INSTANCE]") create_statement = "createStatement552974828" response = client.create_database(parent, create_statement) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = spanner_database_admin_pb2.CreateDatabaseRequest( parent=parent, create_statement=create_statement) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_create_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 parent = client.instance_path('[PROJECT]', '[INSTANCE]') create_statement = 'createStatement552974828' # Mock response name = 'name3373707' expected_response = {'name': name} expected_response = spanner_database_admin_pb2.Database( **expected_response) operation = operations_pb2.Operation( name='operations/test_create_database', done=True) operation.response.Pack(expected_response) grpc_stub.CreateDatabase.return_value = operation response = client.create_database(parent, create_statement) self.assertEqual(expected_response, response.result()) grpc_stub.CreateDatabase.assert_called_once() args, kwargs = grpc_stub.CreateDatabase.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.CreateDatabaseRequest( parent=parent, create_statement=create_statement) self.assertEqual(expected_request, actual_request)
def test_create_database(self): # Setup Expected Response name = 'name3373707' expected_response = {'name': name} expected_response = spanner_database_admin_pb2.Database( **expected_response) operation = operations_pb2.Operation( name='operations/test_create_database', done=True) operation.response.Pack(expected_response) # Mock the API response channel = ChannelStub(responses=[operation]) client = spanner_admin_database_v1.DatabaseAdminClient(channel=channel) # Setup Request parent = client.instance_path('[PROJECT]', '[INSTANCE]') create_statement = 'createStatement552974828' response = client.create_database(parent, create_statement) result = response.result() assert expected_response == result assert len(channel.requests) == 1 expected_request = spanner_database_admin_pb2.CreateDatabaseRequest( parent=parent, create_statement=create_statement) actual_request = channel.requests[0][1] assert expected_request == actual_request
def create_database(self, parent, create_statement, extra_statements=None, retry=google.api_core.gapic_v1.method.DEFAULT, timeout=google.api_core.gapic_v1.method.DEFAULT, metadata=None): """ Creates a new Cloud Spanner database and starts to prepare it for serving. The returned ``long-running operation`` will have a name of the format ``<database_name>/operations/<operation_id>`` and can be used to track preparation of the database. The ``metadata`` field type is ``CreateDatabaseMetadata``. The ``response`` field type is ``Database``, if successful. Example: >>> from google.cloud import spanner_admin_database_v1 >>> >>> client = spanner_admin_database_v1.DatabaseAdminClient() >>> >>> parent = client.instance_path('[PROJECT]', '[INSTANCE]') >>> >>> # TODO: Initialize `create_statement`: >>> create_statement = '' >>> >>> response = client.create_database(parent, create_statement) >>> >>> def callback(operation_future): ... # Handle result. ... result = operation_future.result() >>> >>> response.add_done_callback(callback) >>> >>> # Handle metadata. >>> metadata = response.metadata() Args: parent (str): Required. The name of the instance that will serve the new database. Values are of the form ``projects/<project>/instances/<instance>``. create_statement (str): Required. A ``CREATE DATABASE`` statement, which specifies the ID of the new database. The database ID must conform to the regular expression ``[a-z][a-z0-9_\-]*[a-z0-9]`` and be between 2 and 30 characters in length. If the database ID is a reserved word or if it contains a hyphen, the database ID must be enclosed in backticks (`````). extra_statements (list[str]): An optional list of DDL statements to run inside the newly created database. Statements can create tables, indexes, etc. These statements execute atomically with the creation of the database: if there is an error in any statement, the database is not created. 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._OperationFuture` 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 'create_database' not in self._inner_api_calls: self._inner_api_calls[ 'create_database'] = google.api_core.gapic_v1.method.wrap_method( self.transport.create_database, default_retry=self._method_configs['CreateDatabase'].retry, default_timeout=self._method_configs['CreateDatabase']. timeout, client_info=self._client_info, ) request = spanner_database_admin_pb2.CreateDatabaseRequest( parent=parent, create_statement=create_statement, extra_statements=extra_statements, ) operation = self._inner_api_calls['create_database'](request, retry=retry, timeout=timeout, metadata=metadata) return google.api_core.operation.from_gapic( operation, self.transport._operations_client, spanner_database_admin_pb2.Database, metadata_type=spanner_database_admin_pb2.CreateDatabaseMetadata, )
def create_database(self, parent, create_statement, extra_statements=None, options=None): """ Creates a new Cloud Spanner database and starts to prepare it for serving. The returned ``long-running operation`` will have a name of the format ``<database_name>/operations/<operation_id>`` and can be used to track preparation of the database. The ``metadata`` field type is ``CreateDatabaseMetadata``. The ``response`` field type is ``Database``, if successful. Example: >>> from google.cloud import spanner_admin_database_v1 >>> >>> client = spanner_admin_database_v1.DatabaseAdminClient() >>> >>> parent = client.instance_path('[PROJECT]', '[INSTANCE]') >>> create_statement = '' >>> >>> response = client.create_database(parent, create_statement) >>> >>> def callback(operation_future): ... # Handle result. ... result = operation_future.result() >>> >>> response.add_done_callback(callback) >>> >>> # Handle metadata. >>> metadata = response.metadata() Args: parent (str): Required. The name of the instance that will serve the new database. Values are of the form ``projects/<project>/instances/<instance>``. create_statement (str): Required. A ``CREATE DATABASE`` statement, which specifies the ID of the new database. The database ID must conform to the regular expression ``[a-z][a-z0-9_\-]*[a-z0-9]`` and be between 2 and 30 characters in length. extra_statements (list[str]): An optional list of DDL statements to run inside the newly created database. Statements can create tables, indexes, etc. These statements execute atomically with the creation of the database: if there is an error in any statement, the database is not created. 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._OperationFuture` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ request = spanner_database_admin_pb2.CreateDatabaseRequest( parent=parent, create_statement=create_statement, extra_statements=extra_statements) return google.gax._OperationFuture( self._create_database(request, options), self.operations_client, spanner_database_admin_pb2.Database, spanner_database_admin_pb2.CreateDatabaseMetadata, options)