def test_create_database(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

        # Mock request
        parent = client.instance_path('[PROJECT]', '[INSTANCE]')
        create_statement = 'createStatement552974828'

        # Mock response
        name = 'name3373707'
        expected_response = spanner_database_admin_pb2.Database(name=name)
        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_get_iam_policy(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

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

        # Mock response
        version = 351608024
        etag = b'21'
        expected_response = policy_pb2.Policy(version=version, etag=etag)
        grpc_stub.GetIamPolicy.return_value = expected_response

        response = client.get_iam_policy(resource)
        self.assertEqual(expected_response, response)

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

        expected_request = iam_policy_pb2.GetIamPolicyRequest(
            resource=resource)
        self.assertEqual(expected_request, actual_request)
    def test_test_iam_permissions(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

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

        # Mock response
        expected_response = iam_policy_pb2.TestIamPermissionsResponse()
        grpc_stub.TestIamPermissions.return_value = expected_response

        response = client.test_iam_permissions(resource, permissions)
        self.assertEqual(expected_response, response)

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

        expected_request = iam_policy_pb2.TestIamPermissionsRequest(
            resource=resource, permissions=permissions)
        self.assertEqual(expected_request, actual_request)
    def test_list_databases(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

        # Mock request
        parent = client.instance_path('[PROJECT]', '[INSTANCE]')

        # Mock response
        next_page_token = ''
        databases_element = spanner_database_admin_pb2.Database()
        databases = [databases_element]
        expected_response = spanner_database_admin_pb2.ListDatabasesResponse(
            next_page_token=next_page_token, databases=databases)
        grpc_stub.ListDatabases.return_value = expected_response

        paged_list_response = client.list_databases(parent)
        resources = list(paged_list_response)
        self.assertEqual(1, len(resources))
        self.assertEqual(expected_response.databases[0], resources[0])

        grpc_stub.ListDatabases.assert_called_once()
        args, kwargs = grpc_stub.ListDatabases.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.ListDatabasesRequest(
            parent=parent)
        self.assertEqual(expected_request, actual_request)
    def test_get_database_ddl(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

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

        # Mock response
        expected_response = spanner_database_admin_pb2.GetDatabaseDdlResponse()
        grpc_stub.GetDatabaseDdl.return_value = expected_response

        response = client.get_database_ddl(database)
        self.assertEqual(expected_response, response)

        grpc_stub.GetDatabaseDdl.assert_called_once()
        args, kwargs = grpc_stub.GetDatabaseDdl.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.GetDatabaseDdlRequest(
            database=database)
        self.assertEqual(expected_request, actual_request)
    def test_update_database_ddl(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

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

        # Mock response
        expected_response = empty_pb2.Empty()
        operation = operations_pb2.Operation(
            name='operations/test_update_database_ddl', done=True)
        operation.response.Pack(expected_response)
        grpc_stub.UpdateDatabaseDdl.return_value = operation

        response = client.update_database_ddl(database, statements)
        self.assertEqual(expected_response, response.result())

        grpc_stub.UpdateDatabaseDdl.assert_called_once()
        args, kwargs = grpc_stub.UpdateDatabaseDdl.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.UpdateDatabaseDdlRequest(
            database=database, statements=statements)
        self.assertEqual(expected_request, actual_request)
    def test_get_database_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

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

        # Mock exception response
        grpc_stub.GetDatabase.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.get_database, name)
    def test_list_databases_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

        # Mock request
        parent = client.instance_path('[PROJECT]', '[INSTANCE]')

        # Mock exception response
        grpc_stub.ListDatabases.side_effect = CustomException()

        paged_list_response = client.list_databases(parent)
        self.assertRaises(errors.GaxError, list, paged_list_response)
    def test_test_iam_permissions_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

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

        # Mock exception response
        grpc_stub.TestIamPermissions.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.test_iam_permissions,
                          resource, permissions)
    def test_set_iam_policy_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

        # Mock request
        resource = client.database_path('[PROJECT]', '[INSTANCE]',
                                        '[DATABASE]')
        policy = policy_pb2.Policy()

        # Mock exception response
        grpc_stub.SetIamPolicy.side_effect = CustomException()

        self.assertRaises(errors.GaxError, client.set_iam_policy, resource,
                          policy)
    def test_create_database_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

        # Mock request
        parent = client.instance_path('[PROJECT]', '[INSTANCE]')
        create_statement = 'createStatement552974828'

        # Mock exception response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_create_database_exception', done=True)
        operation.error.CopyFrom(error)
        grpc_stub.CreateDatabase.return_value = operation

        response = client.create_database(parent, create_statement)
        self.assertEqual(error, response.exception())
    def test_update_database_ddl_exception(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = database_admin_client.DatabaseAdminClient()

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

        # Mock exception response
        error = status_pb2.Status()
        operation = operations_pb2.Operation(
            name='operations/test_update_database_ddl_exception', done=True)
        operation.error.CopyFrom(error)
        grpc_stub.UpdateDatabaseDdl.return_value = operation

        response = client.update_database_ddl(database, statements)
        self.assertEqual(error, response.exception())