-
Notifications
You must be signed in to change notification settings - Fork 360
/
BatchDmlTests.cs
315 lines (260 loc) · 13 KB
/
BatchDmlTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2018 Google LLC
//
// 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
//
// https://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.CommonTesting;
using System;
using System.Collections.Generic;
using Xunit;
namespace Google.Cloud.Spanner.Data.IntegrationTests
{
[Collection(nameof(DmlTableFixture))]
public class BatchDmlTests
{
private readonly DmlTableFixture _fixture;
public BatchDmlTests(DmlTableFixture fixture) => _fixture = fixture;
[Fact]
public void ValidBatchDml()
{
string key = _fixture.CreateTestRows();
IEnumerable<long> affectedRows = null;
using (var connection = _fixture.GetConnection())
{
RetryHelpers.ExecuteWithRetry(() =>
{
var command = connection.CreateBatchDmlCommand();
command.Add(
$"INSERT INTO {_fixture.TableName} (Key, OriginalValue, Value, UpdateMe, DeleteMe, CopyMe) SELECT Key, OriginalValue + 10, Value, UpdateMe, DeleteMe, CopyMe FROM {_fixture.TableName} WHERE CopyMe AND KEY=@Key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
command.Add(
$"UPDATE {_fixture.TableName} SET Value = OriginalValue + 1 WHERE UpdateMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
command.Add(
$"DELETE FROM {_fixture.TableName} WHERE DeleteMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
affectedRows = command.ExecuteNonQuery();
});
}
Assert.NotNull(affectedRows);
var expectedAffectedRows = new long[] { 2, 3, 3 };
Assert.Equal(expectedAffectedRows, affectedRows);
var actual = _fixture.FetchValues(key);
var expected = new Dictionary<int, int>
{
{ 0, 0 },
{ 1, 2 }, // Updated
// { 2, 2 }, Deleted
{ 3, 3 },
// { 4, 5 }, Updated then deleted
{ 13, 3 }, // Copied
// { 14, 5 } Copied then updated then deleted
};
Assert.Equal(expected, actual);
}
[Fact]
public void ValidBatchDml_ValidUpdateDml()
{
string key = _fixture.CreateTestRows();
IEnumerable<long> batchAffectedRows = null;
int dmlAffectedRows = 0;
using (var connection = _fixture.GetConnection())
{
using (var transaction = connection.BeginTransaction())
{
RetryHelpers.ExecuteWithRetry(() =>
{
var batchCommand = transaction.CreateBatchDmlCommand();
batchCommand.Add(
$"INSERT INTO {_fixture.TableName} (Key, OriginalValue, Value, UpdateMe, DeleteMe, CopyMe) SELECT Key, OriginalValue + 10, Value, UpdateMe, DeleteMe, CopyMe FROM {_fixture.TableName} WHERE CopyMe AND KEY=@Key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
batchCommand.Add(
$"UPDATE {_fixture.TableName} SET Value = OriginalValue + 1 WHERE UpdateMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
batchCommand.Add(
$"DELETE FROM {_fixture.TableName} WHERE DeleteMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
batchAffectedRows = batchCommand.ExecuteNonQuery();
});
RetryHelpers.ExecuteWithRetry(() =>
{
var dmlCommand = connection.CreateDmlCommand(
$"UPDATE {_fixture.TableName} SET Value = Value + 2 WHERE UpdateMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
dmlCommand.Transaction = transaction;
dmlAffectedRows = dmlCommand.ExecuteNonQuery();
});
transaction.Commit();
}
}
Assert.NotNull(batchAffectedRows);
var expectedBatchAffectedRows = new long[] { 2, 3, 3 };
Assert.Equal(expectedBatchAffectedRows, batchAffectedRows);
int expectedDmlAffectedRows = 1;
Assert.Equal(expectedDmlAffectedRows, dmlAffectedRows);
var actual = _fixture.FetchValues(key);
var expected = new Dictionary<int, int>
{
{ 0, 0 },
{ 1, 4 }, // Updated by batch, updated by dml.
// { 2, 2 }, Deleted
{ 3, 3 },
// { 4, 5 }, Updated then deleted
{ 13, 3 }, // Copied
// { 14, 5 } Copied then updated then deleted
};
Assert.Equal(expected, actual);
}
[Fact]
public void InvalidDmlBatch_FailsClientSide()
{
string key = _fixture.CreateTestRows();
using (var connection = _fixture.GetConnection())
{
var command = connection.CreateBatchDmlCommand();
command.Add(
$"INSERT INTO {_fixture.TableName} (Key, OriginalValue, Value, UpdateMe, DeleteMe, CopyMe) SELECT Key, OriginalValue + 10, Value, UpdateMe, DeleteMe, CopyMe FROM {_fixture.TableName} WHERE CopyMe AND KEY=@Key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
// This is not a valid DML command.
Assert.Throws<InvalidOperationException>(() =>
command.Add(
$"DELETE {_fixture.TableName}",
new SpannerParameterCollection
{
{ "Key", SpannerDbType.String, key },
{ "OriginalValue", SpannerDbType.Int64, 2 }
}));
}
}
[Fact]
public void InvalidConnection_ThrowsSpannerException()
{
string key = _fixture.CreateTestRows();
SpannerException exception = null;
var connectionString = _fixture.ConnectionString;
var connectionBuilder = new SpannerConnectionStringBuilder(connectionString);
connectionBuilder = connectionBuilder.WithDatabase("dummy-database-name");
using (var connection = new SpannerConnection(connectionBuilder))
{
var command = connection.CreateBatchDmlCommand();
command.Add(
$"UPDATE {_fixture.TableName} SET Value = OriginalValue + 1 WHERE UpdateMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
command.Add(
$"DELETE FROM {_fixture.TableName} WHERE DeleteMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
command.Add(
"DELETE FROM inexistentTable WHERE DeleteMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
// Throws SpannerException and not SpannerBatchNonQueryException
exception = Assert.Throws<SpannerException>(command.ExecuteNonQuery);
}
var actual = _fixture.FetchValues(key);
var expected = new Dictionary<int, int>
{
{ 0, 0 },
{ 1, 1 },
{ 2, 2 },
{ 3, 3 },
{ 4, 4 },
};
Assert.Equal(expected, actual);
}
[Fact]
public void InvalidDmlBatch_PartialSuccess_ExplicitTransaction()
{
string key = _fixture.CreateTestRows();
using (var connection = _fixture.GetConnection())
{
using (var transaction = connection.BeginTransaction())
{
var command = transaction.CreateBatchDmlCommand();
command.Add(
$"UPDATE {_fixture.TableName} SET Value = OriginalValue + 1 WHERE UpdateMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
command.Add(
$"DELETE FROM {_fixture.TableName} WHERE DeleteMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
command.Add(
"DELETE FROM inexistentTable WHERE DeleteMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
var exception = Assert.Throws<SpannerBatchNonQueryException>(command.ExecuteNonQuery);
IEnumerable<long> affectedRows = exception.SuccessfulCommandResults;
Assert.NotNull(affectedRows);
var expectedAffectedRows = new long[] { 2, 2 };
Assert.Equal(expectedAffectedRows, affectedRows);
// We haven't yet rollbacked the transaction so the changes from the first two statements
// should be visible
var actualInTransactionScope = _fixture.FetchValues(key, transaction);
var expectedInTransactionScope = new Dictionary<int, int>
{
{ 0, 0 },
{ 1, 2 }, // Updated
// { 2, 2 }, Deleted
{ 3, 3 },
// { 4, 5 }, Updated then deleted
};
Assert.Equal(expectedInTransactionScope, actualInTransactionScope);
transaction.Rollback();
}
}
// After transaction rollback no changes to the data should be seen.
var actual = _fixture.FetchValues(key);
var expected = new Dictionary<int, int>
{
{ 0, 0 },
{ 1, 1 },
{ 2, 2 },
{ 3, 3 },
{ 4, 4 },
};
Assert.Equal(expected, actual);
}
[Fact]
public void InvalidDmlBatch_PartialSuccess_ImplicitTransaction()
{
string key = _fixture.CreateTestRows();
SpannerBatchNonQueryException exception;
using (var connection = _fixture.GetConnection())
{
var command = connection.CreateBatchDmlCommand();
command.Add(
$"UPDATE {_fixture.TableName} SET Value = OriginalValue + 1 WHERE UpdateMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
command.Add(
$"DELETE FROM {_fixture.TableName} WHERE DeleteMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
command.Add(
"DELETE FROM inexistentTable WHERE DeleteMe AND Key=@key",
new SpannerParameterCollection { { "Key", SpannerDbType.String, key } });
// Since this is a batch DML command, for which partial success is not valid,
// the implicit transaction is rollbacked even when some of the commands executed sucesfully.
// Changes made by te command should not be visible to anyone.
exception = Assert.Throws<SpannerBatchNonQueryException>(command.ExecuteNonQuery);
}
// But the exception will contain the number of affected rows by the succesful commands.
IEnumerable<long> affectedRows = exception.SuccessfulCommandResults;
Assert.NotNull(affectedRows);
var expectedAffectedRows = new long[] { 2, 2 };
Assert.Equal(expectedAffectedRows, affectedRows);
var actual = _fixture.FetchValues(key);
var expected = new Dictionary<int, int>
{
{ 0, 0 },
{ 1, 1 },
{ 2, 2 },
{ 3, 3 },
{ 4, 4 },
};
Assert.Equal(expected, actual);
}
}
}