-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft of PostgreSQL samples and tests
- Loading branch information
Showing
33 changed files
with
1,807 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
spanner/api/Spanner.Samples.Tests/AddColumnAsyncPostgreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
spanner/api/Spanner.Samples.Tests/AddStoringIndexAsyncPostgreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
spanner/api/Spanner.Samples.Tests/CastDataTypesAsyncPostgreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
spanner/api/Spanner.Samples.Tests/ConnectToDatabaseAsyncPostgreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
spanner/api/Spanner.Samples.Tests/CreateDatabaseAsyncPostgreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
spanner/api/Spanner.Samples.Tests/CreateInterleavedTablesAsyncPostgreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
spanner/api/Spanner.Samples.Tests/ExecutePartitionedDmlAsyncPostgreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
spanner/api/Spanner.Samples.Tests/GetInformationSchemaAsyncPostgreTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.