def test_list_backup_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 = backup_pb2.ListBackupOperationsResponse(
            **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_backup_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 = backup_pb2.ListBackupOperationsRequest(
            parent=parent)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Esempio n. 2
0
    def test_list_backup_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 backup_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)

        create_backup_metadata = Any()
        create_backup_metadata.Pack(backup_pb2.CreateBackupMetadata())

        operations_pb = backup_pb2.ListBackupOperationsResponse(
            operations=[
                operations_pb2.Operation(name="op1", metadata=create_backup_metadata)
            ]
        )

        ldo_api = api._inner_api_calls["list_backup_operations"] = mock.Mock(
            return_value=operations_pb
        )

        operations = instance.list_backup_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(
            backup_pb2.ListBackupOperationsRequest(
                parent=self.INSTANCE_NAME, filter="filter", page_size=10
            ),
            metadata=expected_metadata,
            retry=mock.ANY,
            timeout=mock.ANY,
        )