forked from jbogard/Respawn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInformixTests.cs
More file actions
456 lines (404 loc) · 19 KB
/
Copy pathInformixTests.cs
File metadata and controls
456 lines (404 loc) · 19 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//#if INFORMIX
using IBM.Data.DB2.Core;
using Respawn;
using Shouldly;
using System;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using NPoco;
using Respawn.Graph;
using Xunit;
using Xunit.Abstractions;
namespace Respawn.DatabaseTests
{
public class InformixTests : IAsyncLifetime
{
private DB2Connection _connection;
private readonly ITestOutputHelper _output;
private string _databaseName;
public InformixTests(ITestOutputHelper output)
{
_output = output;
}
public Task DisposeAsync()
{
_connection?.Close();
_connection?.Dispose();
_connection = null;
return Task.FromResult(0);
}
public async Task InitializeAsync()
{
const string connString = "Server=127.0.0.1:9089;Database=sysadmin;UID=informix;PWD=in4mix;Persist Security Info=True;Authentication=Server;";
await using (var connection = new DB2Connection(connString))
{
await connection.OpenAsync();
using var database = new Database(connection);
_databaseName = $"dummyifx_{Guid.NewGuid():N}";
await database.ExecuteAsync($"CREATE DATABASE {_databaseName} WITH BUFFERED LOG;");
}
var testDbConnString = $"Server=127.0.0.1:9089;Database={_databaseName};UID=informix;PWD=in4mix;Persist Security Info=True;Authentication=Server;";
_connection = new DB2Connection(testDbConnString);
await _connection.OpenAsync();
}
[SkipOnCI]
public async Task ShouldDeleteData()
{
await using var command = new DB2Command("DROP TABLE IF EXISTS Foo; CREATE TABLE Foo (Value INT);", _connection);
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Foo VALUES (?)";
for (int i = 0; i < 100; i++)
{
command.Parameters.Add(new DB2Parameter("Value", i));
command.ExecuteNonQuery();
command.Parameters.Clear();
}
command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(100);
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
SchemasToInclude = new[] { "informix" }
});
await checkPoint.ResetAsync(_connection);
command.ExecuteScalar().ShouldBe(0);
}
[SkipOnCI]
public async Task ShouldIgnoreTables()
{
await using var command = new DB2Command("DROP TABLE IF EXISTS Foo; CREATE TABLE Foo (Value INT);", _connection);
command.ExecuteNonQuery();
command.CommandText = "DROP TABLE IF EXISTS Bar; CREATE TABLE Bar (Value INT);";
command.ExecuteNonQuery();
for (int i = 0; i < 100; i++)
{
command.Parameters.Add(new DB2Parameter("Value", i));
command.CommandText = "INSERT INTO Foo VALUES (?);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Bar VALUES (?);";
command.ExecuteNonQuery();
command.Parameters.Clear();
}
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions()
{
SchemasToInclude = new[] { "informix" },
TablesToIgnore = new Table[] { "foo" }
});
await checkPoint.ResetAsync(_connection);
command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(100);
command.CommandText = "SELECT COUNT(1) FROM Bar";
command.ExecuteScalar().ShouldBe(0);
}
[SkipOnCI]
public async Task ShouldHandleRelationships()
{
await using var command = new DB2Command("DROP TABLE IF EXISTS Foo; CREATE TABLE Foo (Value INT PRIMARY KEY);", _connection);
command.ExecuteNonQuery();
command.CommandText = @"DROP TABLE IF EXISTS Bar;
CREATE TABLE Bar (
Value INT,
FooValue INT,
FOREIGN KEY (FooValue) REFERENCES Foo(Value)
);";
command.ExecuteNonQuery();
for (int i = 0; i < 100; i++)
{
command.Parameters.Add(new DB2Parameter("Value1", i));
command.Parameters.Add(new DB2Parameter("Value2", i));
command.CommandText = "INSERT INTO Foo VALUES (?);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Bar VALUES (?, ?);";
command.ExecuteNonQuery();
command.Parameters.Clear();
}
command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(100);
command.CommandText = "SELECT COUNT(1) FROM Bar";
command.ExecuteScalar().ShouldBe(100);
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
SchemasToInclude = new[] { "informix" }
});
try
{
await checkPoint.ResetAsync(_connection);
}
catch
{
_output.WriteLine(checkPoint.DeleteSql ?? string.Empty);
throw;
}
command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(0);
command.CommandText = "SELECT COUNT(1) FROM Bar";
command.ExecuteScalar().ShouldBe(0);
}
[SkipOnCI]
public async Task ShouldHandleCircularRelationships()
{
await using var command = new DB2Command(@"DROP TABLE IF EXISTS Parent;
CREATE TABLE Parent (
Id INT PRIMARY KEY,
ChildId INT NULL
);", _connection);
command.ExecuteNonQuery();
command.CommandText = @"DROP TABLE IF EXISTS Child;
CREATE TABLE Child (
Id INT PRIMARY KEY,
ParentId INT NULL
);";
command.ExecuteNonQuery();
command.CommandText = @"ALTER TABLE Parent ADD CONSTRAINT (FOREIGN KEY (ChildId) REFERENCES Child (Id) CONSTRAINT FK_Child)";
command.ExecuteNonQuery();
command.CommandText = @"ALTER TABLE Child ADD CONSTRAINT (FOREIGN KEY (ParentId) REFERENCES Parent (Id) CONSTRAINT FK_Parent)";
command.ExecuteNonQuery();
for (int i = 0; i < 100; i++)
{
command.Parameters.Add(new DB2Parameter("Value1", i));
command.Parameters.Add(new DB2Parameter("Value2", DBNull.Value));
command.CommandText = "INSERT INTO Parent VALUES (?, ?);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Child VALUES (?, ?);";
command.ExecuteNonQuery();
command.Parameters.Clear();
}
command.CommandText = @"UPDATE Parent SET ChildId = 0";
command.ExecuteNonQuery();
command.CommandText = @"UPDATE Child SET ParentId = 1";
command.ExecuteNonQuery();
command.CommandText = "SELECT COUNT(1) FROM Parent";
command.ExecuteScalar().ShouldBe(100);
command.CommandText = "SELECT COUNT(1) FROM Child";
command.ExecuteScalar().ShouldBe(100);
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
SchemasToInclude = new[] { "informix" }
});
try
{
await checkPoint.ResetAsync(_connection);
}
catch
{
_output.WriteLine(checkPoint.DeleteSql ?? string.Empty);
throw;
}
command.CommandText = "SELECT COUNT(1) FROM Parent";
command.ExecuteScalar().ShouldBe(0);
command.CommandText = "SELECT COUNT(1) FROM Child";
command.ExecuteScalar().ShouldBe(0);
}
[SkipOnCI]
public async Task ShouldHandleSelfRelationships()
{
await using var command = new DB2Command(@"DROP TABLE IF EXISTS Foo;
CREATE TABLE Foo (
Id INT PRIMARY KEY,
ParentId INT NULL
);", _connection);
command.ExecuteNonQuery();
command.CommandText = "ALTER TABLE Foo ADD CONSTRAINT (FOREIGN KEY (ParentId) REFERENCES Foo (Id) CONSTRAINT FK_Parent1)";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Foo (Id) VALUES (?)";
command.Parameters.Add(new DB2Parameter("Value", 1));
command.ExecuteNonQuery();
command.Parameters.Clear();
for (int i = 1; i < 100; i++)
{
command.CommandText = "INSERT INTO Foo VALUES (?, ?)";
command.Parameters.Add(new DB2Parameter("Value1", i + 1));
command.Parameters.Add(new DB2Parameter("Value2", i));
command.ExecuteNonQuery();
command.Parameters.Clear();
}
command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(100);
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
SchemasToInclude = new[] { "informix" }
});
try
{
await checkPoint.ResetAsync(_connection);
}
catch
{
_output.WriteLine(checkPoint.DeleteSql ?? string.Empty);
throw;
}
command.CommandText = "SELECT COUNT(1) FROM Foo";
command.ExecuteScalar().ShouldBe(0);
}
[SkipOnCI]
public async Task ShouldHandleComplexCycles()
{
await using var command = new DB2Command("DROP TABLE IF EXISTS A; CREATE TABLE A (Id INT PRIMARY KEY, B_Id INT NULL)", _connection);
command.ExecuteNonQuery();
command.CommandText = "DROP TABLE IF EXISTS B; CREATE TABLE B (Id INT PRIMARY KEY, A_Id INT NULL, C_Id INT NULL, D_Id INT NULL)";
command.ExecuteNonQuery();
command.CommandText = "DROP TABLE IF EXISTS C; CREATE TABLE C (Id INT PRIMARY KEY, D_Id INT NULL)";
command.ExecuteNonQuery();
command.CommandText = "DROP TABLE IF EXISTS D; CREATE TABLE D (Id INT PRIMARY KEY)";
command.ExecuteNonQuery();
command.CommandText = "DROP TABLE IF EXISTS E; CREATE TABLE E (Id INT PRIMARY KEY, A_Id INT NULL)";
command.ExecuteNonQuery();
command.CommandText = "DROP TABLE IF EXISTS F; CREATE TABLE F (Id INT PRIMARY KEY, B_Id INT NULL)";
command.ExecuteNonQuery();
command.CommandText = "ALTER TABLE A ADD CONSTRAINT (FOREIGN KEY (B_Id) REFERENCES B (Id) CONSTRAINT FK_A_B)";
command.ExecuteNonQuery();
command.CommandText = "ALTER TABLE B ADD CONSTRAINT (FOREIGN KEY (A_Id) REFERENCES A (Id) CONSTRAINT FK_B_A)";
command.ExecuteNonQuery();
command.CommandText = "ALTER TABLE B ADD CONSTRAINT (FOREIGN KEY (C_Id) REFERENCES C (Id) CONSTRAINT FK_B_C)";
command.ExecuteNonQuery();
command.CommandText = "ALTER TABLE B ADD CONSTRAINT (FOREIGN KEY (D_Id) REFERENCES D (Id) CONSTRAINT FK_B_D)";
command.ExecuteNonQuery();
command.CommandText = "ALTER TABLE C ADD CONSTRAINT (FOREIGN KEY (D_Id) REFERENCES D (Id) CONSTRAINT FK_C_D)";
command.ExecuteNonQuery();
command.CommandText = "ALTER TABLE E ADD CONSTRAINT (FOREIGN KEY (A_Id) REFERENCES A (Id) CONSTRAINT FK_E_A)";
command.ExecuteNonQuery();
command.CommandText = "ALTER TABLE F ADD CONSTRAINT (FOREIGN KEY (B_Id) REFERENCES B (Id) CONSTRAINT FK_F_B)";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO D (Id) VALUES (1)";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO C (Id, D_Id) VALUES (1, 1)";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO A (Id) VALUES (1)";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO B (Id, C_Id, D_Id) VALUES (1, 1, 1)";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO E (Id, A_Id) VALUES (1, 1)";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO F (Id, B_Id) VALUES (1, 1)";
command.ExecuteNonQuery();
command.CommandText = "UPDATE A SET B_Id = 1";
command.ExecuteNonQuery();
command.CommandText = "UPDATE B SET A_Id = 1";
command.ExecuteNonQuery();
command.CommandText = "SELECT COUNT(1) FROM A";
command.ExecuteScalar().ShouldBe(1);
command.CommandText = "SELECT COUNT(1) FROM B";
command.ExecuteScalar().ShouldBe(1);
command.CommandText = "SELECT COUNT(1) FROM C";
command.ExecuteScalar().ShouldBe(1);
command.CommandText = "SELECT COUNT(1) FROM D";
command.ExecuteScalar().ShouldBe(1);
command.CommandText = "SELECT COUNT(1) FROM E";
command.ExecuteScalar().ShouldBe(1);
command.CommandText = "SELECT COUNT(1) FROM F";
command.ExecuteScalar().ShouldBe(1);
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
SchemasToInclude = new[] { "informix" }
});
try
{
await checkPoint.ResetAsync(_connection);
}
catch
{
_output.WriteLine(checkPoint.DeleteSql ?? string.Empty);
throw;
}
command.CommandText = "SELECT COUNT(1) FROM A";
command.ExecuteScalar().ShouldBe(0);
command.CommandText = "SELECT COUNT(1) FROM B";
command.ExecuteScalar().ShouldBe(0);
command.CommandText = "SELECT COUNT(1) FROM C";
command.ExecuteScalar().ShouldBe(0);
command.CommandText = "SELECT COUNT(1) FROM D";
command.ExecuteScalar().ShouldBe(0);
command.CommandText = "SELECT COUNT(1) FROM E";
command.ExecuteScalar().ShouldBe(0);
command.CommandText = "SELECT COUNT(1) FROM F";
command.ExecuteScalar().ShouldBe(0);
}
[SkipOnCI]
public async Task ShouldExcludeSchemas()
{
const string user_1 = "a";
const string user_2 = "b";
await ManageUser(user_1);
await ManageUser(user_2);
await using var command = new DB2Command($"DROP TABLE IF EXISTS {user_1}.Foo; CREATE TABLE {user_1}.Foo (Value INT)", _connection);
command.ExecuteNonQuery();
command.CommandText = $"DROP TABLE IF EXISTS {user_2}.Bar; CREATE TABLE {user_2}.Bar (Value INT)";
command.ExecuteNonQuery();
for (int i = 0; i < 100; i++)
{
command.Parameters.Add(new DB2Parameter("Value", i));
command.CommandText = $"INSERT INTO {user_1}.Foo VALUES (?)";
command.ExecuteNonQuery();
command.CommandText = $"INSERT INTO {user_2}.Bar VALUES (?)";
command.ExecuteNonQuery();
command.Parameters.Clear();
}
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
SchemasToExclude = new[] { user_1 }
});
try
{
await checkPoint.ResetAsync(_connection);
}
catch
{
_output.WriteLine(checkPoint.DeleteSql ?? string.Empty);
throw;
}
command.CommandText = $"SELECT COUNT(1) FROM {user_1}.Foo";
command.ExecuteScalar().ShouldBe(100);
command.CommandText = $"SELECT COUNT(1) FROM {user_2}.Bar";
command.ExecuteScalar().ShouldBe(0);
}
[SkipOnCI]
public async Task ShouldIncludeSchemas()
{
const string user_1 = "a";
const string user_2 = "b";
await ManageUser(user_1);
await ManageUser(user_2);
await using var command = new DB2Command($"DROP TABLE IF EXISTS {user_1}.Fooo; CREATE TABLE {user_1}.Fooo (Value INT)", _connection);
command.ExecuteNonQuery();
command.CommandText = $"DROP TABLE IF EXISTS {user_2}.Baar; CREATE TABLE {user_2}.Baar (Value INT)";
command.ExecuteNonQuery();
for (int i = 0; i < 100; i++)
{
command.Parameters.Add(new DB2Parameter("Value", i));
command.CommandText = $"INSERT INTO {user_1}.Fooo VALUES (?)";
command.ExecuteNonQuery();
command.CommandText = $"INSERT INTO {user_2}.Baar VALUES (?)";
command.ExecuteNonQuery();
command.Parameters.Clear();
}
var checkPoint = await Respawner.CreateAsync(_connection, new RespawnerOptions
{
SchemasToInclude = new[] { user_2 }
});
try
{
await checkPoint.ResetAsync(_connection);
}
catch
{
_output.WriteLine(checkPoint.DeleteSql ?? string.Empty);
throw;
}
command.CommandText = $"SELECT COUNT(1) FROM {user_1}.Fooo";
command.ExecuteScalar().ShouldBe(100);
command.CommandText = $"SELECT COUNT(1) FROM {user_2}.Baar";
command.ExecuteScalar().ShouldBe(0);
}
private async Task ManageUser(string userName)
{
await using var connection = new DB2Connection($"Server=127.0.0.1:9089;Database={_databaseName};UID=informix;PWD=in4mix;Persist Security Info=True;Authentication=Server;");
await connection.OpenAsync();
//await using var allUsers = new DB2Command("SELECT username FROM sysusers", connection);
var database = new Database(connection);
await database.ExecuteAsync($"DROP USER {userName};");
await database.ExecuteAsync($"CREATE USER {userName} WITH PROPERTIES USER ifxsurr;");
await database.ExecuteAsync($"GRANT DBA TO {userName}");
}
}
}
//#endif