Skip to content

Commit

Permalink
First draft of PostgreSQL samples and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh-V committed May 5, 2022
1 parent 946bfc4 commit 25b5654
Show file tree
Hide file tree
Showing 34 changed files with 1,808 additions and 1 deletion.
37 changes: 37 additions & 0 deletions spanner/api/Spanner.Samples.Tests/AddColumnAsyncPostgreTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading.Tasks;
using Xunit;

[Collection(nameof(SpannerFixture))]
public class AddColumnAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly AddColumnAsyncPostgreSample _sample;

public AddColumnAsyncPostgreTest(SpannerFixture spannerFixture)
{
_spannerFixture = spannerFixture;
_sample = new AddColumnAsyncPostgreSample();
}

[Fact]
public async Task TestAddColumnAsyncPostgre()
{
//Act.
await _sample.AddColumnAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2022 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading.Tasks;
using Xunit;

[Collection(nameof(SpannerFixture))]
public class AddStoringIndexAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly AddStoringIndexAsyncPostgreSample _sample;

public AddStoringIndexAsyncPostgreTest(SpannerFixture spannerFixture)
{
_spannerFixture = spannerFixture;
_sample = new AddStoringIndexAsyncPostgreSample();
}

[Fact]
public async Task TestAddStoringIndexAsyncPostgre()
{
//Act.
await _sample.AddStoringIndexAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);
}
}
47 changes: 47 additions & 0 deletions spanner/api/Spanner.Samples.Tests/CastDataTypesAsyncPostgreTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Threading.Tasks;
using Xunit;

[Collection(nameof(SpannerFixture))]
public class CastDataTypesAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly CastDataTypesAsyncPostgreSample _sample;

public CastDataTypesAsyncPostgreTest(SpannerFixture spannerFixture)
{
_spannerFixture = spannerFixture;
_sample = new CastDataTypesAsyncPostgreSample();
}

[Fact]
public async Task TestCastDataTypesAsyncPostgre()
{
// Act.
var result = await _sample.CastDataTypesAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);

//Assert.
Assert.Equal("1", result.String);
Assert.Equal(2, result.Integer);
Assert.Equal(3, result.Decimal);
Assert.Equal("34", BitConverter.ToString(result.Bytes));
Assert.Equal(5.00, result.Float);
Assert.True(result.Bool);
Assert.Equal("2021-11-03 09:35:01Z", result.Timestamp.ToString("u"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2022 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading.Tasks;
using Xunit;

[Collection(nameof(SpannerFixture))]
public class ConnectToDatabaseAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly ConnectToDatabaseAsyncPostgreSample _sample;

public ConnectToDatabaseAsyncPostgreTest(SpannerFixture spannerFixture)
{
_spannerFixture = spannerFixture;
_sample = new ConnectToDatabaseAsyncPostgreSample();
}

[Fact]
public async Task TestConnectToDatabaseAsyncPostgre()
{
// Act.
var result = await _sample.ConnectToDatabaseAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);

//Assert.
Assert.Equal($"Hello from Spanner PostgreSQL!", result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2022 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Cloud.Spanner.Admin.Database.V1;
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

[Collection(nameof(SpannerFixture))]
public class CreateDatabaseAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly CreateDatabaseAsyncPostgreSample _sample;

public CreateDatabaseAsyncPostgreTest(SpannerFixture spannerFixture)
{
_spannerFixture = spannerFixture;
_sample = new CreateDatabaseAsyncPostgreSample();
}

[Fact]
public async Task TestCreateDatabaseAsyncPostgre()
{
// Arrange.
var databaseId = $"my-db-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";

// Act.
await _sample.CreateDatabaseAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, databaseId);

// Assert.
var databases = _spannerFixture.GetDatabases();
Assert.Contains(databases, d => d.DatabaseName.DatabaseId == databaseId && d.DatabaseDialect == DatabaseDialect.Postgresql);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2022 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Cloud.Spanner.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

[Collection(nameof(SpannerFixture))]
public class CreateInterleavedTablesAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly CreateInterleavedTablesAsyncPostgreSample _sample;

public CreateInterleavedTablesAsyncPostgreTest(SpannerFixture spannerFixture)
{
_spannerFixture = spannerFixture;
_sample = new CreateInterleavedTablesAsyncPostgreSample();
}

[Fact]
public async Task TestCreateInterleavedTablesAsyncPostgre()
{
// Act.
await _sample.CreateInterleavedTablesAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);

//Assert.
var tables = await ListTableNamesAsync(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);
Assert.Collection(tables.OrderBy(j => j),
item1 => Assert.Equal("albums", item1),
item2 => Assert.Equal("authors", item2),
item3 => Assert.Equal("books", item3),
item4 => Assert.Equal("singers", item4));
}

private async Task<List<string>> ListTableNamesAsync(string projectId, string instanceId, string databaseId)
{
string connectionString = $"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";
using var connection = new SpannerConnection(connectionString);
await connection.OpenAsync();

var command = connection.CreateSelectCommand("SELECT table_name FROM INFORMATION_SCHEMA.tables WHERE table_schema='public' OR table_schema=''");

var tableNames = new List<string>();
using var reader = await command.ExecuteReaderAsync();
while (await reader.ReadAsync())
{
tableNames.Add(reader.GetFieldValue<string>("table_name"));
}

return tableNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2022 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Google.Cloud.Spanner.Data;
using System.Threading.Tasks;
using Xunit;

[Collection(nameof(SpannerFixture))]
public class ExecutePartitionedDmlAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly ExecutePartitionedDmlAsyncPostgreSample _sample;

public ExecutePartitionedDmlAsyncPostgreTest(SpannerFixture spannerFixture)
{
_spannerFixture = spannerFixture;
_sample = new ExecutePartitionedDmlAsyncPostgreSample();
}

[Fact]
public async Task TestExecutePartitionedDmlAsyncPostgre()
{
// Arrange.
// Insert data that cannot be inserted by any other tests to avoid errors.
await InsertDataAsync(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);

// Act.
var result = await _sample.ExecutePartitionedDmlAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);
Assert.Equal(2, result);
}

private async Task InsertDataAsync(string projectId, string instanceId, string databaseId)
{
string connectionString = $"Data Source=projects/{projectId}/instances/{instanceId}/databases/{databaseId}";

using var connection = new SpannerConnection(connectionString);
await connection.OpenAsync();

SpannerBatchCommand batchCommand = connection.CreateBatchDmlCommand();
batchCommand.Add("INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (12, 'Elvis', 'Presley')");
batchCommand.Add("INSERT INTO Singers (SingerId, FirstName, LastName) VALUES (13, 'John', 'Lennon')");

await batchCommand.ExecuteNonQueryAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2022 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Threading.Tasks;
using Xunit;

[Collection(nameof(SpannerFixture))]
public class GetInformationSchemaAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly GetInformationSchemaAsyncPostgreSample _sample;

public GetInformationSchemaAsyncPostgreTest(SpannerFixture spannerFixture)
{
_spannerFixture = spannerFixture;
_sample = new GetInformationSchemaAsyncPostgreSample();
}

[Fact]
public async Task TestGetInformationSchemaAsyncPostgre()
{
// Act.
var result = await _sample.GetInformationSchemaAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, _spannerFixture.PostgreSqlDatabaseId);

// Assert.
// These two tables will always exist.
Assert.Contains(result, r => r.Name == "albums" && r.Schema == "public" && r.UserDefinedType == "undefined");
Assert.Contains(result, r => r.Name == "singers" && r.Schema == "public" && r.UserDefinedType == "undefined");
}
}
Loading

0 comments on commit 25b5654

Please sign in to comment.