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 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, )
def list_databases(self, page_size=None): """List databases for the instance. See https://cloud.google.com/spanner/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.ListDatabases :type page_size: int :param page_size: Optional. The maximum number of databases in each page of results from this request. Non-positive values are ignored. Defaults to a sensible value set by the API. :rtype: :class:`~google.api._ore.page_iterator.Iterator` :returns: Iterator of :class:`~google.cloud.spanner_v1.database.Database` resources within the current instance. """ metadata = _metadata_with_prefix(self.name) request = ListDatabasesRequest(parent=self.name, page_size=page_size) page_iter = self._client.database_admin_api.list_databases( request=request, metadata=metadata) return page_iter