def test_list_backups_defaults(self): from google.cloud.spanner_admin_database_v1 import Backup as BackupPB from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient from google.cloud.spanner_admin_database_v1 import ListBackupsRequest from google.cloud.spanner_admin_database_v1 import ListBackupsResponse api = DatabaseAdminClient(credentials=mock.Mock()) client = _Client(self.PROJECT) client.database_admin_api = api instance = self._make_one(self.INSTANCE_ID, client) backups_pb = ListBackupsResponse(backups=[ BackupPB(name=instance.name + "/backups/op1"), BackupPB(name=instance.name + "/backups/op2"), BackupPB(name=instance.name + "/backups/op3"), ]) lbo_api = api._transport._wrapped_methods[ api._transport.list_backups] = mock.Mock(return_value=backups_pb) backups = instance.list_backups() for backup in backups: self.assertIsInstance(backup, BackupPB) expected_metadata = ( ("google-cloud-resource-prefix", instance.name), ("x-goog-request-params", "parent={}".format(instance.name)), ) lbo_api.assert_called_once_with( ListBackupsRequest(parent=self.INSTANCE_NAME), metadata=expected_metadata, retry=mock.ANY, timeout=mock.ANY, )
def test_list_databases_w_options(self): from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient from google.cloud.spanner_admin_database_v1 import ListDatabasesRequest from google.cloud.spanner_admin_database_v1 import ListDatabasesResponse api = DatabaseAdminClient(credentials=mock.Mock()) client = _Client(self.PROJECT) client.database_admin_api = api instance = self._make_one(self.INSTANCE_ID, client) databases_pb = ListDatabasesResponse(databases=[]) ld_api = api._transport._wrapped_methods[ api._transport.list_databases] = mock.Mock( return_value=databases_pb) page_size = 42 response = instance.list_databases(page_size=page_size) databases = list(response) self.assertEqual(databases, []) expected_metadata = ( ("google-cloud-resource-prefix", instance.name), ("x-goog-request-params", "parent={}".format(instance.name)), ) ld_api.assert_called_once_with( ListDatabasesRequest(parent=self.INSTANCE_NAME, page_size=page_size), metadata=expected_metadata, retry=mock.ANY, timeout=mock.ANY, )
def database_admin_api(self): """Helper for session-related API calls.""" if self._database_admin_api is None: if self._emulator_host is not None: transport = DatabaseAdminGrpcTransport( channel=grpc.insecure_channel(target=self._emulator_host)) self._database_admin_api = DatabaseAdminClient( client_info=self._client_info, client_options=self._client_options, transport=transport, ) else: self._database_admin_api = DatabaseAdminClient( credentials=self.credentials, client_info=self._client_info, client_options=self._client_options, ) return self._database_admin_api
def test_list_database_operations_defaults(self): from google.api_core.operation import Operation from google.cloud.spanner_admin_database_v1 import CreateDatabaseMetadata from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient from google.cloud.spanner_admin_database_v1 import ListDatabaseOperationsRequest from google.cloud.spanner_admin_database_v1 import ( ListDatabaseOperationsResponse, ) from google.cloud.spanner_admin_database_v1 import ( OptimizeRestoredDatabaseMetadata, ) from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any api = DatabaseAdminClient(credentials=mock.Mock()) client = _Client(self.PROJECT) client.database_admin_api = api instance = self._make_one(self.INSTANCE_ID, client) create_database_metadata = Any() create_database_metadata.Pack( CreateDatabaseMetadata.pb(CreateDatabaseMetadata(database="database")) ) optimize_database_metadata = Any() optimize_database_metadata.Pack( OptimizeRestoredDatabaseMetadata.pb( OptimizeRestoredDatabaseMetadata(name="database") ) ) databases_pb = ListDatabaseOperationsResponse( operations=[ operations_pb2.Operation(name="op1", metadata=create_database_metadata), operations_pb2.Operation( name="op2", metadata=optimize_database_metadata ), ] ) ldo_api = api._transport._wrapped_methods[ api._transport.list_database_operations ] = mock.Mock(return_value=databases_pb) ops = instance.list_database_operations() expected_metadata = ( ("google-cloud-resource-prefix", instance.name), ("x-goog-request-params", "parent={}".format(instance.name)), ) ldo_api.assert_called_once_with( ListDatabaseOperationsRequest(parent=self.INSTANCE_NAME), metadata=expected_metadata, retry=mock.ANY, timeout=mock.ANY, ) self.assertTrue(all([type(op) == Operation for op in ops]))
def test_list_database_operations_w_options(self): from google.api_core.operation import Operation from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient from google.cloud.spanner_admin_database_v1 import ListDatabaseOperationsRequest from google.cloud.spanner_admin_database_v1 import ( ListDatabaseOperationsResponse, ) from google.cloud.spanner_admin_database_v1 import RestoreDatabaseMetadata from google.cloud.spanner_admin_database_v1 import RestoreSourceType from google.cloud.spanner_admin_database_v1 import UpdateDatabaseDdlMetadata from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any api = DatabaseAdminClient(credentials=mock.Mock()) client = _Client(self.PROJECT) client.database_admin_api = api instance = self._make_one(self.INSTANCE_ID, client) restore_database_metadata = Any() restore_database_metadata.Pack( RestoreDatabaseMetadata.pb( RestoreDatabaseMetadata(name="database", source_type=RestoreSourceType.BACKUP))) update_database_metadata = Any() update_database_metadata.Pack( UpdateDatabaseDdlMetadata.pb( UpdateDatabaseDdlMetadata(database="database", statements=["statements"]))) databases_pb = ListDatabaseOperationsResponse(operations=[ operations_pb2.Operation(name="op1", metadata=restore_database_metadata), operations_pb2.Operation(name="op2", metadata=update_database_metadata), ]) ldo_api = api._transport._wrapped_methods[ api._transport.list_database_operations] = mock.Mock( return_value=databases_pb) ops = instance.list_database_operations(filter_="filter", page_size=10) expected_metadata = ( ("google-cloud-resource-prefix", instance.name), ("x-goog-request-params", "parent={}".format(instance.name)), ) ldo_api.assert_called_once_with( ListDatabaseOperationsRequest(parent=self.INSTANCE_NAME, filter="filter", page_size=10), metadata=expected_metadata, retry=mock.ANY, timeout=mock.ANY, ) self.assertTrue(all([type(op) == Operation for op in ops]))
def test_list_backup_operations_w_options(self): from google.cloud.spanner_admin_database_v1 import CreateBackupMetadata from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient from google.cloud.spanner_admin_database_v1 import ListBackupOperationsRequest from google.cloud.spanner_admin_database_v1 import ListBackupOperationsResponse from google.longrunning import operations_pb2 from google.protobuf.any_pb2 import Any api = DatabaseAdminClient(credentials=mock.Mock()) client = _Client(self.PROJECT) client.database_admin_api = api instance = self._make_one(self.INSTANCE_ID, client) create_backup_metadata = Any() create_backup_metadata.Pack( CreateBackupMetadata.pb( CreateBackupMetadata(name="backup", database="database") ) ) operations_pb = ListBackupOperationsResponse( operations=[ operations_pb2.Operation(name="op1", metadata=create_backup_metadata) ] ) ldo_api = api._transport._wrapped_methods[ api._transport.list_backup_operations ] = mock.Mock(return_value=operations_pb) instance.list_backup_operations(filter_="filter", page_size=10) expected_metadata = ( ("google-cloud-resource-prefix", instance.name), ("x-goog-request-params", "parent={}".format(instance.name)), ) ldo_api.assert_called_once_with( ListBackupOperationsRequest( parent=self.INSTANCE_NAME, filter="filter", page_size=10 ), metadata=expected_metadata, retry=mock.ANY, timeout=mock.ANY, )
def test_list_databases(self): from google.cloud.spanner_admin_database_v1 import Database as DatabasePB from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient from google.cloud.spanner_admin_database_v1 import ListDatabasesRequest from google.cloud.spanner_admin_database_v1 import ListDatabasesResponse api = DatabaseAdminClient(credentials=mock.Mock()) client = _Client(self.PROJECT) client.database_admin_api = api instance = self._make_one(self.INSTANCE_ID, client) databases_pb = ListDatabasesResponse( databases=[ DatabasePB(name="{}/databases/aa".format(self.INSTANCE_NAME)), DatabasePB(name="{}/databases/bb".format(self.INSTANCE_NAME)), ] ) ld_api = api._transport._wrapped_methods[ api._transport.list_databases ] = mock.Mock(return_value=databases_pb) response = instance.list_databases() databases = list(response) self.assertIsInstance(databases[0], DatabasePB) self.assertTrue(databases[0].name.endswith("/aa")) self.assertTrue(databases[1].name.endswith("/bb")) expected_metadata = ( ("google-cloud-resource-prefix", instance.name), ("x-goog-request-params", "parent={}".format(instance.name)), ) ld_api.assert_called_once_with( ListDatabasesRequest(parent=self.INSTANCE_NAME), metadata=expected_metadata, retry=mock.ANY, timeout=mock.ANY, )