def test_list_database_operations(self):
        # Setup Expected Response
        next_page_token = ""
        operations_element = {}
        operations = [operations_element]
        expected_response = {
            "next_page_token": next_page_token,
            "operations": operations,
        }
        expected_response = spanner_database_admin_pb2.ListDatabaseOperationsResponse(
            **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
        parent = client.instance_path("[PROJECT]", "[INSTANCE]")

        paged_list_response = client.list_database_operations(parent)
        resources = list(paged_list_response)
        assert len(resources) == 1

        assert expected_response.operations[0] == resources[0]

        assert len(channel.requests) == 1
        expected_request = spanner_database_admin_pb2.ListDatabaseOperationsRequest(
            parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_list_database_operations_w_options(self):
        from google.api_core.operation import Operation
        from google.cloud.spanner_admin_database_v1.gapic import database_admin_client
        from google.cloud.spanner_admin_database_v1.proto import (
            spanner_database_admin_pb2,
        )
        from google.longrunning import operations_pb2
        from google.protobuf.any_pb2 import Any

        api = database_admin_client.DatabaseAdminClient(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(
            spanner_database_admin_pb2.RestoreDatabaseMetadata()
        )

        update_database_metadata = Any()
        update_database_metadata.Pack(
            spanner_database_admin_pb2.UpdateDatabaseDdlMetadata()
        )

        databases_pb = spanner_database_admin_pb2.ListDatabaseOperationsResponse(
            operations=[
                operations_pb2.Operation(
                    name="op1", metadata=restore_database_metadata
                ),
                operations_pb2.Operation(name="op2", metadata=update_database_metadata),
            ]
        )

        ldo_api = api._inner_api_calls["list_database_operations"] = mock.Mock(
            return_value=databases_pb
        )

        operations = instance.list_database_operations(filter_="filter", page_size=10)

        for op in operations:
            self.assertIsInstance(op, Operation)

        expected_metadata = [
            ("google-cloud-resource-prefix", instance.name),
            ("x-goog-request-params", "parent={}".format(instance.name)),
        ]
        ldo_api.assert_called_once_with(
            spanner_database_admin_pb2.ListDatabaseOperationsRequest(
                parent=self.INSTANCE_NAME, filter="filter", page_size=10
            ),
            metadata=expected_metadata,
            retry=mock.ANY,
            timeout=mock.ANY,
        )