forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTestBase.cs
More file actions
796 lines (675 loc) · 33.1 KB
/
TestBase.cs
File metadata and controls
796 lines (675 loc) · 33.1 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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Npgsql.Internal.Postgres;
using Npgsql.Tests.Support;
using NpgsqlTypes;
using NUnit.Framework;
using NUnit.Framework.Constraints;
namespace Npgsql.Tests;
public abstract class TestBase
{
/// <summary>
/// The connection string that will be used when opening the connection to the tests database.
/// May be overridden in fixtures, e.g. to set special connection parameters
/// </summary>
public virtual string ConnectionString => TestUtil.ConnectionString;
static readonly SemaphoreSlim DatabaseCreationLock = new(1);
static readonly object dataSourceLockObject = new();
static ConcurrentDictionary<string, NpgsqlDataSource> DataSources = new(StringComparer.Ordinal);
#region Type testing
public Task<T> AssertType<T>(
T value,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
Func<T, T, bool>? comparer = null,
bool valueTypeEqualsFieldType = true,
bool skipArrayCheck = false)
=> AssertTypeCore(OpenConnectionAsync(), disposeConnection: true, () => value, sqlLiteral, dataTypeName, dataTypeInference,
dbType, comparer, valueTypeEqualsFieldType, skipArrayCheck);
public Task<T> AssertType<T>(
NpgsqlDataSource dataSource,
T value,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
Func<T, T, bool>? comparer = null,
bool valueTypeEqualsFieldType = true,
bool skipArrayCheck = false)
=> AssertTypeCore(dataSource.OpenConnectionAsync(), disposeConnection: true, () => value, sqlLiteral, dataTypeName, dataTypeInference,
dbType, comparer, valueTypeEqualsFieldType, skipArrayCheck);
public Task<T> AssertType<T>(
NpgsqlConnection connection,
T value,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
Func<T, T, bool>? comparer = null,
bool valueTypeEqualsFieldType = true,
bool skipArrayCheck = false)
=> AssertTypeCore(new(connection), disposeConnection: false, () => value, sqlLiteral, dataTypeName, dataTypeInference,
dbType, comparer, valueTypeEqualsFieldType, skipArrayCheck);
public Task<T> AssertType<T>(
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
Func<T, T, bool>? comparer = null,
bool valueTypeEqualsFieldType = true,
bool skipArrayCheck = false)
=> AssertTypeCore(OpenConnectionAsync(), disposeConnection: true, valueFactory, sqlLiteral, dataTypeName, dataTypeInference,
dbType, comparer, valueTypeEqualsFieldType, skipArrayCheck);
public Task<T> AssertType<T>(
NpgsqlDataSource dataSource,
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
Func<T, T, bool>? comparer = null,
bool valueTypeEqualsFieldType = true,
bool skipArrayCheck = false)
=> AssertTypeCore(dataSource.OpenConnectionAsync(), disposeConnection: true, valueFactory, sqlLiteral, dataTypeName, dataTypeInference,
dbType, comparer, valueTypeEqualsFieldType, skipArrayCheck);
public Task<T> AssertType<T>(
NpgsqlConnection connection,
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
Func<T, T, bool>? comparer = null,
bool valueTypeEqualsFieldType = true,
bool skipArrayCheck = false)
=> AssertTypeCore(new(connection), disposeConnection: false, valueFactory, sqlLiteral, dataTypeName, dataTypeInference,
dbType, comparer, valueTypeEqualsFieldType, skipArrayCheck);
static async Task<T> AssertTypeCore<T>(
ValueTask<NpgsqlConnection> connectionTask,
bool disposeConnection,
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
Func<T, T, bool>? comparer = null,
bool valueTypeEqualsFieldType = true,
bool skipArrayCheck = false)
{
var connection = await connectionTask;
await using var _ = disposeConnection ? connection : null;
await AssertTypeWriteCore(new(connection), disposeConnection: false, valueFactory, sqlLiteral,
dataTypeName, dataTypeInference, dbType, skipArrayCheck);
return await AssertTypeReadCore(new(connection), disposeConnection: false, sqlLiteral, dataTypeName, valueFactory(),
valueTypeEqualsFieldType, comparer, skipArrayCheck);
}
public Task AssertTypeWrite<T>(
T value,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
bool skipArrayCheck = false)
=> AssertTypeWriteCore(OpenConnectionAsync(), disposeConnection: true, () => value, sqlLiteral,
dataTypeName, dataTypeInference, dbType, skipArrayCheck);
public Task AssertTypeWrite<T>(
NpgsqlDataSource dataSource,
T value,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
bool skipArrayCheck = false)
=> AssertTypeWriteCore(dataSource.OpenConnectionAsync(), disposeConnection: true, () => value, sqlLiteral,
dataTypeName, dataTypeInference, dbType, skipArrayCheck);
public Task AssertTypeWrite<T>(
NpgsqlConnection connection,
T value,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
bool skipArrayCheck = false)
=> AssertTypeWriteCore(new(connection), disposeConnection: false, () => value, sqlLiteral, dataTypeName, dataTypeInference,
dbType, skipArrayCheck);
public Task AssertTypeWrite<T>(
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
bool skipArrayCheck = false)
=> AssertTypeWriteCore(OpenConnectionAsync(), disposeConnection: true, valueFactory, sqlLiteral,
dataTypeName, dataTypeInference, dbType, skipArrayCheck);
public Task AssertTypeWrite<T>(
NpgsqlDataSource dataSource,
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
bool skipArrayCheck = false)
=> AssertTypeWriteCore(dataSource.OpenConnectionAsync(), disposeConnection: true, valueFactory, sqlLiteral,
dataTypeName, dataTypeInference, dbType, skipArrayCheck);
public Task AssertTypeWrite<T>(
NpgsqlConnection connection,
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference = null,
DbTypes? dbType = null,
bool skipArrayCheck = false)
=> AssertTypeWriteCore(new(connection), disposeConnection: false, valueFactory, sqlLiteral,
dataTypeName, dataTypeInference, dbType, skipArrayCheck);
static async Task AssertTypeWriteCore<T>(
ValueTask<NpgsqlConnection> connectionTask,
bool disposeConnection,
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference? dataTypeInference,
DbTypes? dbType,
bool skipArrayCheck)
{
var connection = await connectionTask;
await using var _ = disposeConnection ? connection : null;
await AssertTypeWriteCore(
connection, valueFactory, sqlLiteral,
dataTypeName, dataTypeInference ?? DataTypeInference.Match,
dbType);
// Check the corresponding array type as well
if (!skipArrayCheck && !dataTypeName.EndsWith("[]", StringComparison.Ordinal))
{
await AssertTypeWriteCore(
connection,
() => new[] { valueFactory(), valueFactory() },
ArrayLiteral(sqlLiteral),
dataTypeName + "[]", dataTypeInference ?? DataTypeInference.Match,
expectedDbTypes: null);
}
}
public enum DataTypeInference
{
/// <summary>
/// Data type is inferred from the CLR value and matches the data type under test.
/// </summary>
Match,
/// <summary>
/// Data type is inferred from the CLR value but differs from the data type under test.
/// </summary>
/// <remarks>
/// Used when we get some inferred data type (e.g. CLR strings are inferred to be 'text') but this does not match the data type (e.g. 'json') under test.
/// </remarks>
Mismatch,
/// <summary>
/// Data type can not be inferred from the CLR value.
/// </summary>
/// <remarks>
/// This is for CLR types that are statically unknown to Npgsql (plugin types: NodaTime/NTS, composite types, enums...),
/// or where we specifically don't want to infer a data type because there's no good option
/// (e.g. uint can be mapped to 'oid/xid/cid', but we don't want any of these as a default/inferred data type)
/// </remarks>
Nothing,
}
public readonly struct DbTypes(DbType dataTypeMappedDbType, DbType valueInferredDbType, DbType dbTypeToSet)
{
public DbType DataTypeMappedDbType { get; } = dataTypeMappedDbType;
public DbType ValueInferredDbType { get; } = valueInferredDbType;
// The DbType to explicitly set on the parameter. Usually same as ValueInferredDbType,
// It differs when testing DbType aliases (e.g. VarNumeric → DbType.Decimal) as we want to test those also work correctly.
public DbType DbTypeToSet { get; } = dbTypeToSet;
public DbTypes(DbType dataTypeMappedDbType, DbType valueInferredDbType)
: this(dataTypeMappedDbType, valueInferredDbType, valueInferredDbType) {}
public static implicit operator DbTypes(DbType dbType) => new(dbType, dbType, dbType);
}
static async Task AssertTypeWriteCore<T>(
NpgsqlConnection connection,
Func<T> valueFactory,
string sqlLiteral,
string dataTypeName,
DataTypeInference dataTypeInference,
DbTypes? expectedDbTypes)
{
var npgsqlDbType = DataTypeName.FromDisplayName(dataTypeName).ToNpgsqlDbType();
// Strip any facet information (length/precision/scale)
var parenIndex = dataTypeName.IndexOf('(');
var dataTypeNameWithoutFacets = parenIndex > -1
? dataTypeName[..parenIndex] + dataTypeName[(dataTypeName.IndexOf(')') + 1)..]
: dataTypeName;
// For composite type with dots in name, Postgresql returns name with quotes - scheme."My.type.name"
// but for npgsql mapping we should use names without quotes - scheme.My.type.name
var dataTypeNameWithoutFacetsAndQuotes = dataTypeNameWithoutFacets.Replace("\"", string.Empty);
// We test the following scenarios (between 2 and 5 in total):
// 1. With value and DataTypeName set
// 2. With value and NpgsqlDbType set (when available)
// 3. With value and DbType explicitly set
// 4. With only the value set
// 5. With only the value set, using generic NpgsqlParameter<T>
// We only actually attempt to write to the database with a set DataTypeName, NpgsqlDbType, or when data type inference is exact.
var errorIdentifierIndex = -1;
var errorIdentifier = new Dictionary<int, string>();
await using var cmd = new NpgsqlCommand { Connection = connection };
NpgsqlParameter p;
// With data type name
p = new NpgsqlParameter { Value = valueFactory(), DataTypeName = dataTypeNameWithoutFacetsAndQuotes };
errorIdentifier[++errorIdentifierIndex] = $"Value and DataTypeName={dataTypeNameWithoutFacetsAndQuotes}";
DataTypeAsserts();
cmd.Parameters.Add(p);
// With NpgsqlDbType
if (npgsqlDbType is not null)
{
p = new NpgsqlParameter { Value = valueFactory(), NpgsqlDbType = npgsqlDbType.Value };
errorIdentifier[++errorIdentifierIndex] = $"Value and NpgsqlDbType={npgsqlDbType}";
DataTypeAsserts();
cmd.Parameters.Add(p);
}
// With DbType, if none was supplied we verify it's DbType.Object.
p = new NpgsqlParameter { Value = valueFactory() };
errorIdentifier[++errorIdentifierIndex] = $"Value and DbType={expectedDbTypes?.DbTypeToSet}";
if (expectedDbTypes?.DbTypeToSet is { } expectedDbType)
p.DbType = expectedDbType;
DbTypeAsserts();
if (dataTypeInference is DataTypeInference.Match)
cmd.Parameters.Add(p);
// With (non-generic) value only
p = new NpgsqlParameter { Value = valueFactory() };
errorIdentifier[++errorIdentifierIndex] = $"Value (type {p.Value!.GetType().Name}, non-generic)";
ValueAsserts();
if (dataTypeInference is DataTypeInference.Match)
cmd.Parameters.Add(p);
// With (generic) value only
p = new NpgsqlParameter<T> { TypedValue = valueFactory() };
errorIdentifier[++errorIdentifierIndex] = $"Value (type {p.Value!.GetType().Name}, generic)";
ValueAsserts();
if (dataTypeInference is DataTypeInference.Match)
cmd.Parameters.Add(p);
cmd.CommandText = "SELECT " + string.Join(", ", Enumerable.Range(1, cmd.Parameters.Count).Select(i =>
"pg_typeof($1)::text, $1::text".Replace("$1", $"${i}")));
await using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess);
await reader.ReadAsync();
for (var i = 0; i < cmd.Parameters.Count * 2; i += 2)
{
var error = errorIdentifier[i / 2];
Assert.That(reader[i], Is.EqualTo(dataTypeNameWithoutFacets), $"Got wrong data type name when writing with {error}");
Assert.That(reader[i+1], Is.EqualTo(sqlLiteral), $"Got wrong SQL literal when writing with {error}");
}
void DataTypeAsserts()
{
var expectedDataTypeName = dataTypeNameWithoutFacetsAndQuotes;
var expectedNpgsqlDbType = npgsqlDbType ?? NpgsqlDbType.Unknown;
var expectedDbType = expectedDbTypes?.DataTypeMappedDbType ?? DbType.Object;
AssertParameterProperties(expectedDataTypeName, expectedNpgsqlDbType, expectedDbType);
}
void DbTypeAsserts()
{
// If DbType was set it overrules any value based data type inference.
// As DbType.Object never has any mapping either we check for null/Unknown when DbType.Object was set.
var (expectedDataTypeName, expectedNpgsqlDbType) =
expectedDbTypes is { DbTypeToSet: DbType.Object }
? (null, NpgsqlDbType.Unknown)
: GetInferredDataType();
var expectedDbType = expectedDbTypes?.DbTypeToSet ?? DbType.Object;
AssertParameterProperties(expectedDataTypeName, expectedNpgsqlDbType, expectedDbType);
}
void ValueAsserts()
{
var (expectedDataTypeName, expectedNpgsqlDbType) = GetInferredDataType();
var expectedDbType = expectedDbTypes?.ValueInferredDbType ?? DbType.Object;
AssertParameterProperties(expectedDataTypeName, expectedNpgsqlDbType, expectedDbType);
}
void AssertParameterProperties(string? expectedDataTypeName, NpgsqlDbType expectedNpgsqlDbType, DbType expectedDbType)
{
Assert.That(p.DataTypeName, Is.EqualTo(expectedDataTypeName),
$"Got wrong DataTypeName when checking with {errorIdentifier[errorIdentifierIndex]}");
Assert.That(p.NpgsqlDbType, Is.EqualTo(expectedNpgsqlDbType),
$"Got wrong NpgsqlDbType when checking with {errorIdentifier[errorIdentifierIndex]}");
Assert.That(p.DbType, Is.EqualTo(expectedDbType),
$"Got wrong DbType when checking with {errorIdentifier[errorIdentifierIndex]}");
}
(string? ExpectedDataTypeName, NpgsqlDbType ExpectedNpgsqlDbType) GetInferredDataType()
=> dataTypeInference switch
{
DataTypeInference.Match =>
(dataTypeNameWithoutFacetsAndQuotes, npgsqlDbType ?? NpgsqlDbType.Unknown),
DataTypeInference.Mismatch =>
// Only respect Mismatch if the type is well known (for now that means it has an NpgsqlDbType).
// Otherwise use the exact values so we'll error with the right details.
p.NpgsqlDbType is not NpgsqlDbType.Unknown
? (p.DataTypeName, p.NpgsqlDbType)
: (dataTypeNameWithoutFacetsAndQuotes, npgsqlDbType ?? NpgsqlDbType.Unknown),
DataTypeInference.Nothing =>
(null, NpgsqlDbType.Unknown),
_ => throw new UnreachableException($"Unknown case {dataTypeInference}")
};
}
public Task<T> AssertTypeRead<T>(string sqlLiteral, string dataTypeName, T value,
bool valueTypeEqualsFieldType = true, Func<T, T, bool>? comparer = null, bool skipArrayCheck = false)
=> AssertTypeReadCore(OpenConnectionAsync(), disposeConnection: true, sqlLiteral, dataTypeName,
value, valueTypeEqualsFieldType, comparer, skipArrayCheck);
public Task<T> AssertTypeRead<T>(NpgsqlDataSource dataSource, string sqlLiteral, string dataTypeName, T value,
bool valueTypeEqualsFieldType = true, Func<T, T, bool>? comparer = null, bool skipArrayCheck = false)
=> AssertTypeReadCore(dataSource.OpenConnectionAsync(), disposeConnection: true, sqlLiteral, dataTypeName,
value, valueTypeEqualsFieldType, comparer, skipArrayCheck);
public Task<T> AssertTypeRead<T>(NpgsqlConnection connection, string sqlLiteral, string dataTypeName, T value,
bool valueTypeEqualsFieldType = true, Func<T, T, bool>? comparer = null, bool skipArrayCheck = false)
=> AssertTypeReadCore(new(connection), disposeConnection: false, sqlLiteral, dataTypeName,
value, valueTypeEqualsFieldType, comparer, skipArrayCheck);
static async Task<T> AssertTypeReadCore<T>(
ValueTask<NpgsqlConnection> connectionTask,
bool disposeConnection,
string sqlLiteral,
string dataTypeName,
T value,
bool valueTypeEqualsFieldType,
Func<T, T, bool>? comparer,
bool skipArrayCheck)
{
var connection = await connectionTask;
await using var _ = disposeConnection ? connection : null;
var result = await AssertTypeReadCore(connection, sqlLiteral, dataTypeName, value, valueTypeEqualsFieldType, comparer);
// Check the corresponding array type as well
if (!skipArrayCheck && !dataTypeName.EndsWith("[]", StringComparison.Ordinal))
{
await AssertTypeReadCore(
connection,
ArrayLiteral(sqlLiteral),
dataTypeName + "[]",
new[] { value, value },
valueTypeEqualsFieldType,
comparer is null ? null : (array1, array2) => array1.SequenceEqual(array2, CreateEqualityComparer(comparer!)));
}
return result;
}
static async Task<T> AssertTypeReadCore<T>(
NpgsqlConnection connection,
string sqlLiteral,
string dataTypeName,
T value,
bool valueTypeEqualsFieldType,
Func<T, T, bool>? comparer)
{
if (sqlLiteral.Contains('\''))
sqlLiteral = sqlLiteral.Replace("'", "''");
await using var cmd = new NpgsqlCommand($"SELECT '{sqlLiteral}'::{dataTypeName}", connection);
await using var reader = await cmd.ExecuteReaderAsync();
await reader.ReadAsync();
var truncatedSqlLiteral = sqlLiteral.Length > 40 ? sqlLiteral[..40] + "..." : sqlLiteral;
var actualDataTypeName = reader.GetDataTypeName(0);
var dotIndex = actualDataTypeName.IndexOf('.');
if (dotIndex > -1 && actualDataTypeName.Substring(0, dotIndex) is "pg_catalog" or "public")
actualDataTypeName = actualDataTypeName.Substring(dotIndex + 1);
// For composite type with dots, postgres works only with quoted name - scheme."My.type.name"
// but npgsql converts it to name without quotes
var dataTypeNameWithoutQuotes = dataTypeName.Replace("\"", string.Empty);
Assert.That(actualDataTypeName, Is.EqualTo(dataTypeNameWithoutQuotes),
$"Got wrong result from GetDataTypeName when reading '{truncatedSqlLiteral}'");
// For arrays, GetFieldType always returns typeof(Array), since PG arrays can have arbitrary dimensionality.
var isArrayTest = actualDataTypeName.EndsWith("[]", StringComparison.Ordinal) && typeof(T).IsArray;
Assert.That(reader.GetFieldType(0),
(valueTypeEqualsFieldType || isArrayTest ? new ConstraintExpression() : Is.Not)
.EqualTo(isArrayTest ? typeof(Array) : typeof(T)),
$"Got wrong result from GetFieldType when reading '{truncatedSqlLiteral}'");
T actual;
if (valueTypeEqualsFieldType)
{
actual = (T)reader.GetValue(0);
Assert.That(actual, comparer is null ? Is.EqualTo(value) : Is.EqualTo(value).Using<T>(CreateEqualityComparer(comparer!)),
$"Got wrong result from GetValue() value when reading '{truncatedSqlLiteral}'");
actual = (T)reader.GetFieldValue<object>(0);
Assert.That(actual, comparer is null ? Is.EqualTo(value) : Is.EqualTo(value).Using<T>(CreateEqualityComparer(comparer)),
$"Got wrong result from GetFieldValue<object>() value when reading '{truncatedSqlLiteral}'");
return actual;
}
actual = reader.GetFieldValue<T>(0);
Assert.That(actual, comparer is null ? Is.EqualTo(value) : Is.EqualTo(value).Using<T>(CreateEqualityComparer(comparer!)),
$"Got wrong result from GetFieldValue<T>() value when reading '{truncatedSqlLiteral}'");
return actual;
}
static EqualityComparer<T> CreateEqualityComparer<T>(Func<T, T, bool> comparer)
=> EqualityComparer<T>.Create((x, y) =>
{
if (x is null && y is null)
return true;
if (x is null || y is null)
return false;
return comparer(x, y);
});
public async Task AssertTypeUnsupported<T>(T value, string sqlLiteral, string dataTypeName, NpgsqlDataSource? dataSource = null, bool skipArrayCheck = false)
{
await AssertTypeUnsupportedRead<T>(sqlLiteral, dataTypeName, dataSource, skipArrayCheck);
await AssertTypeUnsupportedWrite(value, dataTypeName, dataSource, skipArrayCheck);
}
public Task<InvalidCastException> AssertTypeUnsupportedRead<T>(string sqlLiteral, string dataTypeName,
NpgsqlDataSource? dataSource = null, bool skipArrayCheck = false)
=> AssertTypeUnsupportedRead<T, InvalidCastException>(sqlLiteral, dataTypeName, dataSource, skipArrayCheck);
public async Task<TException> AssertTypeUnsupportedRead<T, TException>(string sqlLiteral, string dataTypeName,
NpgsqlDataSource? dataSource = null, bool skipArrayCheck = false)
where TException : Exception
{
var result = await AssertTypeUnsupportedReadCore<T, TException>(sqlLiteral, dataTypeName, dataSource);
// Check the corresponding array type as well
if (!skipArrayCheck && !dataTypeName.EndsWith("[]", StringComparison.Ordinal))
{
await AssertTypeUnsupportedReadCore<T[], TException>(ArrayLiteral(sqlLiteral), dataTypeName + "[]", dataSource);
}
return result;
}
async Task<TException> AssertTypeUnsupportedReadCore<T, TException>(string sqlLiteral, string dataTypeName, NpgsqlDataSource? dataSource = null)
where TException : Exception
{
dataSource ??= DataSource;
await using var conn = await dataSource.OpenConnectionAsync();
await using var tx = await conn.BeginTransactionAsync();
await using var cmd = new NpgsqlCommand($"SELECT '{sqlLiteral}'::{dataTypeName}", conn);
await using var reader = await cmd.ExecuteReaderAsync();
await reader.ReadAsync();
return Assert.Throws<TException>(() =>
{
_ = typeof(T) == typeof(object) ? reader.GetValue(0) : reader.GetFieldValue<T>(0);
})!;
}
public Task<InvalidCastException> AssertTypeUnsupportedWrite<T>(T value, string? dataTypeName = null, NpgsqlDataSource? dataSource = null,
bool skipArrayCheck = false)
=> AssertTypeUnsupportedWrite<T, InvalidCastException>(value, dataTypeName, dataSource, skipArrayCheck);
public async Task<TException> AssertTypeUnsupportedWrite<T, TException>(T value, string? dataTypeName = null,
NpgsqlDataSource? dataSource = null, bool skipArrayCheck = false)
where TException : Exception
{
var result = await AssertTypeUnsupportedWriteCore<T, TException>(value, dataTypeName, dataSource);
// Check the corresponding array type as well
if (!skipArrayCheck && !dataTypeName?.EndsWith("[]", StringComparison.Ordinal) == true)
{
await AssertTypeUnsupportedWriteCore<T[], TException>([value, value], dataTypeName + "[]", dataSource);
}
return result;
}
async Task<TException> AssertTypeUnsupportedWriteCore<T, TException>(T value, string? dataTypeName = null, NpgsqlDataSource? dataSource = null)
where TException : Exception
{
dataSource ??= DataSource;
await using var conn = await dataSource.OpenConnectionAsync();
await using var tx = await conn.BeginTransactionAsync();
await using var cmd = new NpgsqlCommand("SELECT $1", conn)
{
Parameters = { new() { Value = value } }
};
if (dataTypeName is not null)
cmd.Parameters[0].DataTypeName = dataTypeName;
return Assert.ThrowsAsync<TException>(() => cmd.ExecuteReaderAsync())!;
}
// For array quoting rules, see array_out in https://github.com/postgres/postgres/blob/master/src/backend/utils/adt/arrayfuncs.c
static string ArrayLiteral(string elementLiteral)
{
switch (elementLiteral)
{
case "":
elementLiteral = "\"\"";
break;
case "NULL":
elementLiteral = "\"NULL\"";
break;
default:
// Escape quotes and backslashes, quote for special chars
elementLiteral = elementLiteral.Replace("\\", "\\\\").Replace("\"", "\\\"");
if (elementLiteral.Any(c => c is '{' or '}' or ',' or '"' or '\\' || char.IsWhiteSpace(c)))
{
elementLiteral = '"' + elementLiteral + '"';
}
break;
}
return $"{{{elementLiteral},{elementLiteral}}}";
}
#endregion Type testing
#region Utilities for use by tests
protected virtual NpgsqlDataSourceBuilder CreateDataSourceBuilder()
=> new(ConnectionString);
protected virtual NpgsqlDataSource CreateDataSource()
=> CreateDataSource(ConnectionString);
protected NpgsqlDataSource CreateDataSource(string connectionString)
=> NpgsqlDataSource.Create(connectionString);
protected NpgsqlDataSource CreateDataSource(Action<NpgsqlConnectionStringBuilder> connectionStringBuilderAction)
{
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(ConnectionString);
connectionStringBuilderAction(connectionStringBuilder);
return NpgsqlDataSource.Create(connectionStringBuilder);
}
protected NpgsqlDataSource CreateDataSource(Action<NpgsqlDataSourceBuilder> configure)
{
var builder = new NpgsqlDataSourceBuilder(ConnectionString);
configure(builder);
return builder.Build();
}
protected static NpgsqlDataSource GetDataSource(string connectionString)
{
if (!DataSources.TryGetValue(connectionString, out var dataSource))
{
lock (dataSourceLockObject)
{
if (!DataSources.TryGetValue(connectionString, out dataSource))
{
var canonicalConnectionString = new NpgsqlConnectionStringBuilder(connectionString).ToString();
if (!DataSources.TryGetValue(canonicalConnectionString, out dataSource))
{
DataSources[canonicalConnectionString] = dataSource = NpgsqlDataSource.Create(connectionString);
}
DataSources[connectionString] = dataSource;
}
}
}
return dataSource;
}
protected virtual NpgsqlDataSource CreateLoggingDataSource(
out ListLoggerProvider listLoggerProvider,
string? connectionString = null,
bool sensitiveDataLoggingEnabled = true)
{
var builder = new NpgsqlDataSourceBuilder(connectionString ?? ConnectionString);
var provider = listLoggerProvider = new ListLoggerProvider();
builder.UseLoggerFactory(LoggerFactory.Create(loggerFactoryBuilder =>
{
loggerFactoryBuilder.SetMinimumLevel(LogLevel.Trace);
loggerFactoryBuilder.AddProvider(provider);
}));
builder.EnableParameterLogging(sensitiveDataLoggingEnabled);
return builder.Build();
}
protected NpgsqlDataSource DefaultDataSource
=> GetDataSource(ConnectionString);
protected virtual NpgsqlDataSource DataSource => DefaultDataSource;
protected virtual NpgsqlConnection CreateConnection()
=> DataSource.CreateConnection();
protected virtual NpgsqlConnection OpenConnection()
{
var connection = CreateConnection();
try
{
OpenConnection(connection, async: false).GetAwaiter().GetResult();
return connection;
}
catch
{
connection.Dispose();
throw;
}
}
protected virtual async ValueTask<NpgsqlConnection> OpenConnectionAsync()
{
var connection = CreateConnection();
try
{
await OpenConnection(connection, async: true);
return connection;
}
catch
{
await connection.DisposeAsync();
throw;
}
}
static Task OpenConnection(NpgsqlConnection conn, bool async)
{
return OpenConnectionInternal(hasLock: false);
async Task OpenConnectionInternal(bool hasLock)
{
try
{
if (async)
await conn.OpenAsync();
else
conn.Open();
}
catch (PostgresException e)
{
if (e.SqlState == PostgresErrorCodes.InvalidPassword)
throw new Exception("Please create a user npgsql_tests as follows: CREATE USER npgsql_tests PASSWORD 'npgsql_tests' SUPERUSER");
if (e.SqlState == PostgresErrorCodes.InvalidCatalogName)
{
if (!hasLock)
{
DatabaseCreationLock.Wait();
try
{
await OpenConnectionInternal(hasLock: true);
}
finally
{
DatabaseCreationLock.Release();
}
}
// Database does not exist and we have the lock, proceed to creation
var builder = new NpgsqlConnectionStringBuilder(TestUtil.ConnectionString)
{
Pooling = false,
Database = "postgres"
};
using var adminConn = new NpgsqlConnection(builder.ConnectionString);
adminConn.Open();
adminConn.ExecuteNonQuery("CREATE DATABASE " + conn.Database);
adminConn.Close();
Thread.Sleep(1000);
if (async)
await conn.OpenAsync();
else
conn.Open();
return;
}
throw;
}
}
}
// In PG under 9.1 you can't do SELECT pg_sleep(2) in binary because that function returns void and PG doesn't know
// how to transfer that. So cast to text server-side.
protected static NpgsqlCommand CreateSleepCommand(NpgsqlConnection conn, int seconds = 1000)
=> new($"SELECT pg_sleep({seconds}){(conn.PostgreSqlVersion < new Version(9, 1, 0) ? "::TEXT" : "")}", conn);
#endregion
}