|
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; |
2 | 5 |
|
3 | 6 | public class BasicTests |
4 | 7 | { |
| 8 | + private SpannerMockServerFixture _fixture; |
| 9 | + |
| 10 | + private string ConnectionString => $"{_fixture.Host}:{_fixture.Port}/projects/p1/instances/i1/databases/d1;UsePlainText=true"; |
| 11 | + |
5 | 12 | [SetUp] |
6 | 13 | public void Setup() |
7 | 14 | { |
| 15 | + _fixture = new SpannerMockServerFixture(); |
| 16 | + _fixture.SpannerMock.AddOrUpdateStatementResult("SELECT 1", StatementResult.CreateSelect1ResultSet()); |
| 17 | + } |
| 18 | + |
| 19 | + [TearDown] |
| 20 | + public void Teardown() |
| 21 | + { |
| 22 | + _fixture.Dispose(); |
8 | 23 | } |
9 | 24 |
|
10 | 25 | [Test] |
11 | 26 | public void TestCreatePool() |
12 | 27 | { |
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(); |
15 | 82 | } |
16 | 83 | } |
0 commit comments