Skip to content

Commit cc9fc07

Browse files
committed
chore: refactor dotnet lib
1 parent 2cf0e32 commit cc9fc07

File tree

16 files changed

+1481
-34
lines changed

16 files changed

+1481
-34
lines changed
Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,83 @@
1-
namespace Google.Cloud.SpannerLib.Tests;
1+
using Google.Cloud.Spanner.V1;
2+
using Google.Cloud.SpannerLib.Tests.MockServer;
3+
4+
namespace Google.Cloud.SpannerLib.Tests;
25

36
public class BasicTests
47
{
8+
private SpannerMockServerFixture _fixture;
9+
10+
private string ConnectionString => $"{_fixture.Host}:{_fixture.Port}/projects/p1/instances/i1/databases/d1;UsePlainText=true";
11+
512
[SetUp]
613
public void Setup()
714
{
15+
_fixture = new SpannerMockServerFixture();
16+
_fixture.SpannerMock.AddOrUpdateStatementResult("SELECT 1", StatementResult.CreateSelect1ResultSet());
17+
}
18+
19+
[TearDown]
20+
public void Teardown()
21+
{
22+
_fixture.Dispose();
823
}
924

1025
[Test]
1126
public void TestCreatePool()
1227
{
13-
var pool = Spanner.CreatePool("projects/my-project/instances/my-instance/databases/my-database");
14-
Spanner.ClosePool(pool);
28+
var pool = LibPool.Create(ConnectionString);
29+
pool.Close();
30+
}
31+
32+
[Test]
33+
public void TestCreateConnection()
34+
{
35+
using var pool = LibPool.Create(ConnectionString);
36+
var connection = pool.CreateConnection();
37+
connection.Close();
38+
}
39+
40+
[Test]
41+
public void TestExecuteQuery()
42+
{
43+
using var pool = LibPool.Create(ConnectionString);
44+
using var connection = pool.CreateConnection();
45+
using var rows = connection.Execute(new ExecuteSqlRequest { Sql = "SELECT 1" });
46+
for (var row = rows.Next(); row != null; row = rows.Next())
47+
{
48+
Assert.That(row.Values.Count, Is.EqualTo(1));
49+
Assert.That(row.Values[0].HasStringValue);
50+
Assert.That(row.Values[0].StringValue, Is.EqualTo("1"));
51+
}
52+
}
53+
54+
[Test]
55+
public void TestReadOnlyTransaction()
56+
{
57+
using var pool = LibPool.Create(ConnectionString);
58+
using var connection = pool.CreateConnection();
59+
using var transaction = connection.BeginTransaction(new TransactionOptions
60+
{ ReadOnly = new TransactionOptions.Types.ReadOnly() });
61+
using var rows = transaction.Execute(new ExecuteSqlRequest { Sql = "SELECT 1" });
62+
for (var row = rows.Next(); row != null; row = rows.Next())
63+
{
64+
Assert.That(row.Values.Count, Is.EqualTo(1));
65+
Assert.That(row.Values[0].HasStringValue);
66+
Assert.That(row.Values[0].StringValue, Is.EqualTo("1"));
67+
}
68+
}
69+
70+
[Test]
71+
public void TestReadWriteTransaction()
72+
{
73+
var sql = "update table1 set value='one' where id=1";
74+
_fixture.SpannerMock.AddOrUpdateStatementResult(sql, StatementResult.CreateUpdateCount(1));
75+
using var pool = LibPool.Create(ConnectionString);
76+
using var connection = pool.CreateConnection();
77+
using var transaction = connection.BeginTransaction(new TransactionOptions
78+
{ ReadWrite = new TransactionOptions.Types.ReadWrite() });
79+
using var rows = transaction.Execute(new ExecuteSqlRequest { Sql = sql });
80+
Assert.That(rows.UpdateCount, Is.EqualTo(1));
81+
transaction.Commit();
1582
}
1683
}

spannerlib/dotnet-spannerlib/Google.Cloud.SpannerLib.Tests/Google.Cloud.SpannerLib.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="coverlet.collector" Version="6.0.2"/>
13+
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.71.0" />
1314
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
1415
<PackageReference Include="NUnit" Version="4.2.2"/>
1516
<PackageReference Include="NUnit.Analyzers" Version="4.4.0"/>
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
using Microsoft.AspNetCore.Builder;
16+
using Microsoft.AspNetCore.Hosting;
17+
using Microsoft.Extensions.DependencyInjection;
18+
using Microsoft.Extensions.Hosting;
19+
20+
namespace Google.Cloud.SpannerLib.Tests.MockServer;
21+
22+
/// <summary>
23+
/// Helper class for starting an in-memory mock Spanner server that is used for testing.
24+
/// </summary>
25+
public class MockServerStartup
26+
{
27+
/// <summary>
28+
/// The in-mem Spanner service.
29+
/// </summary>
30+
private MockSpannerService MockSpannerService { get; }
31+
32+
/// <summary>
33+
/// The in-mem Spanner database admin service for executing DDL operations.
34+
/// </summary>
35+
private MockDatabaseAdminService MockDatabaseAdminService { get; }
36+
37+
public MockServerStartup(MockSpannerService mockSpannerService, MockDatabaseAdminService mockDatabaseAdminService)
38+
{
39+
MockSpannerService = mockSpannerService;
40+
MockDatabaseAdminService = mockDatabaseAdminService;
41+
}
42+
43+
/// <summary>
44+
/// Configures the services that will be available on this gRPC server.
45+
/// This method is called by reflection when the tests are started.
46+
/// </summary>
47+
/// <param name="services">The services collection where the services should be added</param>
48+
public void ConfigureServices(IServiceCollection services)
49+
{
50+
services.AddGrpc();
51+
services.AddSingleton(MockSpannerService);
52+
services.AddSingleton(MockDatabaseAdminService);
53+
}
54+
55+
/// <summary>
56+
/// Configures the gRPC server. This method is called by reflection when the tests are started.
57+
/// </summary>
58+
/// <param name="app">The builder for the application that will be hosting the service</param>
59+
/// <param name="env">The webhost environment that is hosting the service</param>
60+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
61+
{
62+
if (env.IsDevelopment())
63+
{
64+
app.UseDeveloperExceptionPage();
65+
}
66+
app.UseRouting();
67+
app.UseEndpoints(endpoints =>
68+
{
69+
endpoints.MapGrpcService<MockSpannerService>();
70+
endpoints.MapGrpcService<MockDatabaseAdminService>();
71+
});
72+
}
73+
}
74+

0 commit comments

Comments
 (0)