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 Apr 5, 2022
1 parent d6f5e7d commit 1a36f4a
Show file tree
Hide file tree
Showing 34 changed files with 1,868 additions and 2 deletions.
43 changes: 43 additions & 0 deletions spanner/api/Spanner.Samples.Tests/AddColumnAsyncPostgreTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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 AddColumnAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly AddColumnAsyncPostgreSample _sample;

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

[Fact]
public async Task TestAddColumnAsyncPostgre()
{
// Arrange.
// Create a new database to avoid flakiness.
var databaseId = $"my-db-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";
await _spannerFixture.CreatePostgreSqlDatabaseAsync(databaseId);

//Act.
await _sample.AddColumnAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, databaseId);
}
}
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,46 @@
// 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.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 _spannerFixture.ListTableNamesAsync(_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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 ExecutePartitionedDmlAsyncPostgreTest
{
private readonly SpannerFixture _spannerFixture;

private readonly ExecutePartitionedDmlAsyncPostgreSample _sample;

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

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

// Create Database.
var createDatabaseAsyncPostgreSample = new CreateDatabaseAsyncPostgreSample();
await createDatabaseAsyncPostgreSample.CreateDatabaseAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, databaseId);

// Insert data.
var insertDataSample = new InsertUsingDmlWithParametersAsyncPostgreSample();
await insertDataSample.InsertUsingDmlWithParametersAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, databaseId);

// Act.
var result = await _sample.ExecutePartitionedDmlAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, databaseId);
Assert.Equal(2, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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.Linq;
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()
{
// Arrange. Lets create a brand new database, so that tests are not flaky.
var databaseId = $"my-db-{DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()}";
var createDatabaseSample = new CreateDatabaseAsyncPostgreSample();
await createDatabaseSample.CreateDatabaseAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, databaseId);

// Act.
var result = await _sample.GetInformationSchemaAsyncPostgre(_spannerFixture.ProjectId, _spannerFixture.InstanceId, databaseId);

//Assert.
Assert.Collection(result.OrderBy(j => j.Name),
item1 =>
{
Assert.Equal("public", item1.Schema);
Assert.Equal("albums", item1.Name);
Assert.Equal("null", item1.UserDefinedType);
},
item2 =>
{
Assert.Equal("public", item2.Schema);
Assert.Equal("singers", item2.Name);
Assert.Equal("null", item2.UserDefinedType);
});
}
}
Loading

0 comments on commit 1a36f4a

Please sign in to comment.