forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyTests.cs
More file actions
522 lines (440 loc) · 20.5 KB
/
CopyTests.cs
File metadata and controls
522 lines (440 loc) · 20.5 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#region License
// The PostgreSQL License
//
// Copyright (C) 2015 The Npgsql Development Team
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement is hereby granted, provided that the above copyright notice
// and this paragraph and the following two paragraphs appear in all copies.
//
// IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
// FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
// INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
// DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
// THE POSSIBILITY OF SUCH DAMAGE.
//
// THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
// ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#endregion
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Npgsql;
using NpgsqlTypes;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace Npgsql.Tests
{
public class CopyTests : TestBase
{
#region Raw
[Test, Description("Exports data in binary format (raw mode) and then loads it back in")]
public void RawBinaryRoundtrip()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
//var iterations = Conn.BufferSize / 10 + 100;
//var iterations = Conn.BufferSize / 10 - 100;
var iterations = 500;
// Preload some data into the table
using (var cmd = new NpgsqlCommand("INSERT INTO data (field_text, field_int4) VALUES (@p1, @p2)", Conn))
{
cmd.Parameters.AddWithValue("p1", NpgsqlDbType.Text, "HELLO");
cmd.Parameters.AddWithValue("p2", NpgsqlDbType.Integer, 8);
cmd.Prepare();
for (var i = 0; i < iterations; i++)
{
cmd.ExecuteNonQuery();
}
}
var data = new byte[10000];
int len = 0;
using (var outStream = Conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) TO STDIN BINARY"))
{
StateAssertions(Conn);
while (true) {
var read = outStream.Read(data, len, data.Length - len);
if (read == 0)
break;
len += read;
}
Assert.That(len, Is.GreaterThan(Conn.BufferSize) & Is.LessThan(data.Length));
Console.WriteLine("Exported binary dump, length=" + len);
}
ExecuteNonQuery("TRUNCATE data");
using (var outStream = Conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) FROM STDIN BINARY"))
{
StateAssertions(Conn);
outStream.Write(data, 0, len);
}
Assert.That(ExecuteScalar("SELECT COUNT(*) FROM DATA"), Is.EqualTo(iterations));
}
[Test, Description("Disposes a raw binary stream in the middle of an export")]
public void DisposeInMiddleOfRawBinaryExport()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
ExecuteNonQuery("INSERT INTO data (field_text, field_int4) VALUES ('HELLO', 8)", Conn);
var data = new byte[3];
using (var inStream = Conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) TO STDIN BINARY"))
{
// Read some bytes
var len = inStream.Read(data, 0, data.Length);
Assert.That(len, Is.EqualTo(data.Length));
}
Assert.That(ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
[Test, Description("Disposes a raw binary stream in the middle of an import")]
public void DisposeInMiddleOfRawBinaryImport()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
var inStream = Conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) FROM STDIN BINARY");
inStream.Write(NpgsqlRawCopyStream.BinarySignature, 0, NpgsqlRawCopyStream.BinarySignature.Length);
Assert.That(() => inStream.Dispose(), Throws.Exception
.TypeOf<NpgsqlException>()
.With.Property("Code").EqualTo("22P04")
);
Assert.That(ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
[Test, Description("Cancels a binary write")]
public void CancelRawBinaryImport()
{
using (var conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
var garbage = new byte[] {1, 2, 3, 4};
using (var s = conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) FROM STDIN BINARY"))
{
s.Write(garbage, 0, garbage.Length);
s.Cancel();
}
Assert.That(ExecuteScalar("SELECT COUNT(*) FROM data", conn), Is.EqualTo(0));
}
}
[Test]
public void ImportLargeValueRaw()
{
ExecuteNonQuery("CREATE TEMP TABLE data (blob BYTEA)", Conn);
var data = new byte[Conn.BufferSize + 10];
var dump = new byte[Conn.BufferSize + 200];
var len = 0;
// Insert a blob with a regular insert
using (var cmd = new NpgsqlCommand("INSERT INTO data (blob) VALUES (@p)", Conn))
{
cmd.Parameters.AddWithValue("p", data);
cmd.ExecuteNonQuery();
}
// Raw dump out
using (var outStream = Conn.BeginRawBinaryCopy("COPY data (blob) TO STDIN BINARY"))
{
while (true)
{
var read = outStream.Read(dump, len, dump.Length - len);
if (read == 0)
break;
len += read;
}
Assert.That(len < dump.Length);
}
ExecuteNonQuery("TRUNCATE data");
// And raw dump back in
using (var inStream = Conn.BeginRawBinaryCopy("COPY data (blob) FROM STDIN BINARY"))
{
inStream.Write(dump, 0, len);
}
}
#endregion
#region Binary
[Test, Description("Roundtrips some data")]
public void BinaryRoundtrip()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
var longString = new StringBuilder(Conn.BufferSize + 50).Append('a').ToString();
using (var writer = Conn.BeginBinaryImport("COPY data (field_text, field_int2) FROM STDIN BINARY"))
{
StateAssertions(Conn);
writer.StartRow();
writer.Write("Hello");
writer.Write(8, NpgsqlDbType.Smallint);
writer.WriteRow("Something", (short)9);
writer.StartRow();
writer.Write(longString);
writer.WriteNull();
}
using (var reader = Conn.BeginBinaryExport("COPY data (field_text, field_int2) TO STDIN BINARY"))
{
StateAssertions(Conn);
Assert.That(reader.StartRow(), Is.EqualTo(2));
Assert.That(reader.Read<string>(), Is.EqualTo("Hello"));
Assert.That(reader.Read<int>(NpgsqlDbType.Smallint), Is.EqualTo(8));
Assert.That(reader.StartRow(), Is.EqualTo(2));
Assert.That(reader.IsNull, Is.False);
Assert.That(reader.Read<string>(), Is.EqualTo("Something"));
reader.Skip();
Assert.That(reader.StartRow(), Is.EqualTo(2));
Assert.That(reader.Read<string>(), Is.EqualTo(longString));
Assert.That(reader.IsNull, Is.True);
reader.Skip();
Assert.That(reader.StartRow(), Is.EqualTo(-1));
}
}
[Test]
public void CancelBinaryImport()
{
using (var conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
using (var writer = conn.BeginBinaryImport("COPY data (field_text, field_int4) FROM STDIN BINARY"))
{
writer.StartRow();
writer.Write("Hello");
writer.Write(8);
writer.Cancel();
}
Assert.That(ExecuteScalar(@"SELECT COUNT(*) FROM data", conn), Is.EqualTo(0));
}
}
[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/657")]
public void ImportBytea()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field BYTEA)", Conn);
var data = new byte[] { 1, 5, 8 };
using (var writer = Conn.BeginBinaryImport("COPY data (field) FROM STDIN BINARY"))
{
writer.StartRow();
writer.Write(data, NpgsqlDbType.Bytea);
}
Assert.That(ExecuteScalar("SELECT field FROM data"), Is.EqualTo(data));
}
[Test]
public void ImportStringArray()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field TEXT[])", Conn);
var data = new[] { "foo", "a", "bar" };
using (var writer = Conn.BeginBinaryImport("COPY data (field) FROM STDIN BINARY"))
{
writer.StartRow();
writer.Write(data, NpgsqlDbType.Array | NpgsqlDbType.Text);
}
Assert.That(ExecuteScalar("SELECT field FROM data"), Is.EqualTo(data));
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/816")]
public void ImportStringWithBufferLength()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field TEXT)", Conn);
var data = new string('a', Conn.BufferSize);
using (var writer = Conn.BeginBinaryImport("COPY data (field) FROM STDIN BINARY"))
{
writer.StartRow();
writer.Write(data, NpgsqlDbType.Text);
}
Assert.That(Conn.Connector.Buffer.UsableSize, Is.EqualTo(Conn.Connector.Buffer.Size));
Assert.That(ExecuteScalar("SELECT field FROM data"), Is.EqualTo(data));
}
[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/662")]
public void ImportDirectBuffer()
{
using (var conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (blob BYTEA)", conn);
using (var writer = conn.BeginBinaryImport("COPY data (blob) FROM STDIN BINARY"))
{
// Big value - triggers use of the direct buffer optimization
var data = new byte[conn.BufferSize + 10];
writer.StartRow();
writer.Write(data);
writer.StartRow();
writer.Write(data);
writer.Dispose();
}
}
}
[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/661")]
[Ignore("Unreliable")]
public void UnexpectedExceptionBinaryImport()
{
var conn = new NpgsqlConnection(ConnectionString);
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (blob BYTEA)", conn);
var data = new byte[conn.BufferSize + 10];
var writer = conn.BeginBinaryImport("COPY data (blob) FROM STDIN BINARY");
ExecuteNonQuery(string.Format("SELECT pg_terminate_backend({0})", conn.ProcessID));
Thread.Sleep(50);
Assert.That(() =>
{
writer.StartRow();
writer.Write(data);
writer.Dispose();
}, Throws.Exception.TypeOf<IOException>());
Assert.That(conn.FullState, Is.EqualTo(ConnectionState.Broken));
}
[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/657")]
[Explicit]
public void ImportByteaMassive()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field BYTEA)", Conn);
const int iterations = 10000;
var data = new byte[1024 * 1024];
using (var writer = Conn.BeginBinaryImport("COPY data (field) FROM STDIN BINARY"))
{
for (var i = 0; i < iterations; i++)
{
if (i % 100 == 0)
Console.WriteLine("Iteration " + i);
writer.StartRow();
writer.Write(data, NpgsqlDbType.Bytea);
}
}
Assert.That(ExecuteScalar("SELECT COUNT(*) FROM data"), Is.EqualTo(iterations));
}
#endregion
#region Text
[Test]
public void TextImport()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
const string line = "HELLO\t1\n";
// Short write
var writer = Conn.BeginTextImport("COPY data (field_text, field_int4) FROM STDIN");
StateAssertions(Conn);
writer.Write(line);
writer.Close();
Assert.That(ExecuteScalar(@"SELECT COUNT(*) FROM data WHERE field_int4=1"), Is.EqualTo(1));
Assert.That(() => writer.Write(line), Throws.Exception.TypeOf<ObjectDisposedException>());
ExecuteNonQuery("TRUNCATE data");
// Long (multi-buffer) write
var iterations = NpgsqlBuffer.MinimumBufferSize / line.Length + 100;
writer = Conn.BeginTextImport("COPY data (field_text, field_int4) FROM STDIN");
for (var i = 0; i < iterations; i++)
writer.Write(line);
writer.Close();
Assert.That(ExecuteScalar(@"SELECT COUNT(*) FROM data WHERE field_int4=1"), Is.EqualTo(iterations));
}
[Test]
public void CancelTextImport()
{
using (var conn = new NpgsqlConnection(ConnectionString))
{
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
var writer = (NpgsqlCopyTextWriter)conn.BeginTextImport("COPY data (field_text, field_int4) FROM STDIN");
writer.Write("HELLO\t1\n");
writer.Cancel();
Assert.That(ExecuteScalar(@"SELECT COUNT(*) FROM data", conn), Is.EqualTo(0));
}
}
[Test]
public void TextImportEmpty()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
var writer = Conn.BeginTextImport("COPY data (field_text, field_int4) FROM STDIN");
writer.Close();
Assert.That(ExecuteScalar(@"SELECT COUNT(*) FROM data"), Is.EqualTo(0));
}
[Test]
public void TextExport()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
var chars = new char[30];
// Short read
ExecuteNonQuery("INSERT INTO data (field_text, field_int4) VALUES ('HELLO', 1)");
var reader = Conn.BeginTextExport("COPY data (field_text, field_int4) TO STDIN");
StateAssertions(Conn);
Assert.That(reader.Read(chars, 0, chars.Length), Is.EqualTo(8));
Assert.That(new String(chars, 0, 8), Is.EqualTo("HELLO\t1\n"));
Assert.That(reader.Read(chars, 0, chars.Length), Is.EqualTo(0));
Assert.That(reader.Read(chars, 0, chars.Length), Is.EqualTo(0));
reader.Close();
Assert.That(() => reader.Read(chars, 0, chars.Length), Throws.Exception.TypeOf<ObjectDisposedException>());
ExecuteNonQuery("TRUNCATE data");
}
[Test]
public void DisposeInMiddleOfTextExport()
{
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", Conn);
ExecuteNonQuery("INSERT INTO data (field_text, field_int4) VALUES ('HELLO', 1)");
var reader = Conn.BeginTextExport("COPY data (field_text, field_int4) TO STDIN");
reader.Dispose();
// Make sure the connection is stil OK
Assert.That(ExecuteScalar("SELECT 1"), Is.EqualTo(1));
}
#endregion
#region Other
[Test, Description("Starts a transaction before a COPY, testing that prepended messages are handled well")]
public void PrependedMessages()
{
Conn.BeginTransaction();
TextImport();
}
[Test]
public void UndefinedTable()
{
Assert.That(() => Conn.BeginBinaryImport("COPY undefined_table (field_text, field_int2) FROM STDIN BINARY"),
Throws.Exception.TypeOf<NpgsqlException>().With.Property("Code").EqualTo("42P01"));
}
[Test]
[IssueLink("https://github.com/npgsql/npgsql/issues/621")]
public void CloseDuringCopy()
{
// TODO: Check no broken connections were returned to the pool
using (var conn = new NpgsqlConnection(ConnectionString)) {
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
conn.BeginBinaryImport("COPY data (field_text, field_int4) FROM STDIN BINARY");
}
using (var conn = new NpgsqlConnection(ConnectionString)) {
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
conn.BeginBinaryExport("COPY data (field_text, field_int2) TO STDIN BINARY");
}
using (var conn = new NpgsqlConnection(ConnectionString)) {
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) FROM STDIN BINARY");
}
using (var conn = new NpgsqlConnection(ConnectionString)) {
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
conn.BeginRawBinaryCopy("COPY data (field_text, field_int4) TO STDIN BINARY");
}
using (var conn = new NpgsqlConnection(ConnectionString)) {
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
conn.BeginTextImport("COPY data (field_text, field_int4) FROM STDIN");
}
using (var conn = new NpgsqlConnection(ConnectionString)) {
conn.Open();
ExecuteNonQuery("CREATE TEMP TABLE data (field_text TEXT, field_int2 SMALLINT, field_int4 INTEGER)", conn);
conn.BeginTextExport("COPY data (field_text, field_int4) TO STDIN");
}
}
#endregion
#region Utils
/// <summary>
/// Checks that the connector state is properly managed for COPY operations
/// </summary>
void StateAssertions(NpgsqlConnection conn)
{
Assert.That(conn.Connector.State, Is.EqualTo(ConnectorState.Copy));
Assert.That(conn.State, Is.EqualTo(ConnectionState.Open));
Assert.That(conn.FullState, Is.EqualTo(ConnectionState.Open | ConnectionState.Fetching));
Assert.That(() => ExecuteScalar("SELECT 1", conn), Throws.Exception.TypeOf<InvalidOperationException>());
}
#endregion
public CopyTests(string backendVersion) : base(backendVersion) { }
}
}