Пример #1
0
    public async Task RunWithTemporaryDatabaseAsync(string instanceId, string databaseId, Func <string, Task> testFunction, params string[] extraStatements)
    {
        var operation = await DatabaseAdminClient.CreateDatabaseAsync(new CreateDatabaseRequest
        {
            ParentAsInstanceName = InstanceName.FromProjectInstance(ProjectId, instanceId),
            CreateStatement      = $"CREATE DATABASE `{databaseId}`",
            ExtraStatements      = { extraStatements },
        });

        var completedResponse = await operation.PollUntilCompletedAsync();

        if (completedResponse.IsFaulted)
        {
            throw completedResponse.Exception;
        }

        try
        {
            await testFunction(databaseId);
        }
        finally
        {
            // Cleanup the test database.
            await DatabaseAdminClient.DropDatabaseAsync(DatabaseName.FormatProjectInstanceDatabase(ProjectId, instanceId, databaseId));
        }
    }
Пример #2
0
    public async Task TestUpdateDatabaseWithDefaultLeaderAsync()
    {
        await _spannerFixture.RunWithTemporaryDatabaseAsync(_spannerFixture.InstanceIdWithMultiRegion, async databaseId =>
        {
            var defaultLeader = "us-central1";
            var sample        = new UpdateDatabaseWithDefaultLeaderAsyncSample();
            await sample.UpdateDatabaseWithDefaultLeaderAsync(_spannerFixture.ProjectId,
                                                              _spannerFixture.InstanceIdWithMultiRegion, databaseId, defaultLeader);

            var databaseAdminClient = await DatabaseAdminClient.CreateAsync();
            var database            = await databaseAdminClient.GetDatabaseAsync(
                DatabaseName.FormatProjectInstanceDatabase(_spannerFixture.ProjectId,
                                                           _spannerFixture.InstanceIdWithMultiRegion, databaseId));
            Assert.Equal(defaultLeader, database.DefaultLeader);
        });
    }