-
Notifications
You must be signed in to change notification settings - Fork 874
Expand file tree
/
Copy pathNpgsqlParameterTests.cs
More file actions
850 lines (721 loc) · 34 KB
/
NpgsqlParameterTests.cs
File metadata and controls
850 lines (721 loc) · 34 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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
using NpgsqlTypes;
using NUnit.Framework;
using System;
using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
namespace Npgsql.Tests;
public class NpgsqlParameterTest : TestBase
{
[Test, Description("Makes sure that when NpgsqlDbType or Value/NpgsqlValue are set, DbType and NpgsqlDbType are set accordingly")]
public void Implicit_setting_of_DbType()
{
var p = new NpgsqlParameter("p", DbType.Int32);
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Integer));
// As long as NpgsqlDbType/DbType aren't set explicitly, infer them from Value
p = new NpgsqlParameter("p", 8);
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Integer));
Assert.That(p.DbType, Is.EqualTo(DbType.Int32));
p.Value = 3.0;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Double));
Assert.That(p.DbType, Is.EqualTo(DbType.Double));
p.NpgsqlDbType = NpgsqlDbType.Bytea;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea));
Assert.That(p.DbType, Is.EqualTo(DbType.Binary));
p.Value = "dont_change";
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea));
Assert.That(p.DbType, Is.EqualTo(DbType.Binary));
p = new NpgsqlParameter("p", new int[0]);
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Array | NpgsqlDbType.Integer));
Assert.That(p.DbType, Is.EqualTo(DbType.Object));
}
[Test]
public void DataTypeName()
{
using var conn = OpenConnection();
using var cmd = new NpgsqlCommand("SELECT @p", conn);
var p1 = new NpgsqlParameter { ParameterName = "p", Value = 8, DataTypeName = "integer" };
cmd.Parameters.Add(p1);
Assert.That(cmd.ExecuteScalar(), Is.EqualTo(8));
// Purposefully try to send int as string, which should fail. This makes sure
// the above doesn't work simply because of type inference from the CLR type.
p1.DataTypeName = "text";
Assert.That(() => cmd.ExecuteScalar(), Throws.Exception.TypeOf<InvalidCastException>());
cmd.Parameters.Clear();
var p2 = new NpgsqlParameter<int> { ParameterName = "p", TypedValue = 8, DataTypeName = "integer" };
cmd.Parameters.Add(p2);
Assert.That(cmd.ExecuteScalar(), Is.EqualTo(8));
// Purposefully try to send int as string, which should fail. This makes sure
// the above doesn't work simply because of type inference from the CLR type.
p2.DataTypeName = "text";
Assert.That(() => cmd.ExecuteScalar(), Throws.Exception.TypeOf<InvalidCastException>());
}
[Test]
public void Positional_parameter_is_positional()
{
var p = new NpgsqlParameter(NpgsqlParameter.PositionalName, 1);
Assert.That(p.IsPositional, Is.True);
var p2 = new NpgsqlParameter(null, 1);
Assert.That(p2.IsPositional, Is.True);
}
[Test]
public void Infer_data_type_name_from_NpgsqlDbType()
{
var p = new NpgsqlParameter("par_field1", NpgsqlDbType.Varchar, 50);
Assert.That(p.DataTypeName, Is.EqualTo("character varying"));
}
[Test]
public void Infer_data_type_name_from_DbType()
{
var p = new NpgsqlParameter("par_field1", DbType.String , 50);
Assert.That(p.DataTypeName, Is.EqualTo("text"));
}
[Test]
public void Infer_data_type_name_from_NpgsqlDbType_for_array()
{
var p = new NpgsqlParameter("int_array", NpgsqlDbType.Array | NpgsqlDbType.Integer);
Assert.That(p.DataTypeName, Is.EqualTo("integer[]"));
}
[Test]
public void Infer_data_type_name_from_NpgsqlDbType_for_built_in_range()
{
var p = new NpgsqlParameter("numeric_range", NpgsqlDbType.Range | NpgsqlDbType.Numeric);
Assert.That(p.DataTypeName, Is.EqualTo("numrange"));
}
[Test]
public void Cannot_infer_data_type_name_from_NpgsqlDbType_for_unknown_range()
{
var p = new NpgsqlParameter("text_range", NpgsqlDbType.Range | NpgsqlDbType.Text);
Assert.That(p.DataTypeName, Is.EqualTo(null));
}
[Test]
public void Infer_data_type_name_from_ClrType()
{
var p = new NpgsqlParameter("p1", Array.Empty<byte>());
Assert.That(p.DataTypeName, Is.EqualTo("bytea"));
}
[Test]
public void Setting_DbType_sets_NpgsqlDbType()
{
var p = new NpgsqlParameter();
p.DbType = DbType.Binary;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea));
}
[Test]
public void Setting_NpgsqlDbType_sets_DbType()
{
var p = new NpgsqlParameter();
p.NpgsqlDbType = NpgsqlDbType.Bytea;
Assert.That(p.DbType, Is.EqualTo(DbType.Binary));
}
[Test]
public void Setting_value_does_not_change_DbType()
{
var p = new NpgsqlParameter { DbType = DbType.Binary, NpgsqlDbType = NpgsqlDbType.Bytea };
p.Value = 8;
Assert.That(p.DbType, Is.EqualTo(DbType.Binary));
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea));
}
// Older tests
#region Constructors
[Test]
public void Constructor1()
{
var p = new NpgsqlParameter();
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "DbType");
Assert.That(p.Direction, Is.EqualTo(ParameterDirection.Input), "Direction");
Assert.That(p.IsNullable, Is.False, "IsNullable");
Assert.That(p.ParameterName, Is.Empty, "ParameterName");
Assert.That(p.Precision, Is.EqualTo(0), "Precision");
Assert.That(p.Scale, Is.EqualTo(0), "Scale");
Assert.That(p.Size, Is.EqualTo(0), "Size");
Assert.That(p.SourceColumn, Is.Empty, "SourceColumn");
Assert.That(p.SourceVersion, Is.EqualTo(DataRowVersion.Current), "SourceVersion");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "NpgsqlDbType");
Assert.That(p.Value, Is.Null, "Value");
}
[Test]
public void Constructor2_Value_DateTime()
{
var value = new DateTime(2004, 8, 24);
var p = new NpgsqlParameter("address", value);
Assert.That(p.DbType, Is.EqualTo(DbType.DateTime2), "B:DbType");
Assert.That(p.Direction, Is.EqualTo(ParameterDirection.Input), "B:Direction");
Assert.That(p.IsNullable, Is.False, "B:IsNullable");
Assert.That(p.ParameterName, Is.EqualTo("address"), "B:ParameterName");
Assert.That(p.Precision, Is.EqualTo(0), "B:Precision");
Assert.That(p.Scale, Is.EqualTo(0), "B:Scale");
//Assert.AreEqual (0, p.Size, "B:Size");
Assert.That(p.SourceColumn, Is.Empty, "B:SourceColumn");
Assert.That(p.SourceVersion, Is.EqualTo(DataRowVersion.Current), "B:SourceVersion");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Timestamp), "B:NpgsqlDbType");
Assert.That(p.Value, Is.EqualTo(value), "B:Value");
}
[Test]
public void Constructor2_Value_DBNull()
{
var p = new NpgsqlParameter("address", DBNull.Value);
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "B:DbType");
Assert.That(p.Direction, Is.EqualTo(ParameterDirection.Input), "B:Direction");
Assert.That(p.IsNullable, Is.False, "B:IsNullable");
Assert.That(p.ParameterName, Is.EqualTo("address"), "B:ParameterName");
Assert.That(p.Precision, Is.EqualTo(0), "B:Precision");
Assert.That(p.Scale, Is.EqualTo(0), "B:Scale");
Assert.That(p.Size, Is.EqualTo(0), "B:Size");
Assert.That(p.SourceColumn, Is.Empty, "B:SourceColumn");
Assert.That(p.SourceVersion, Is.EqualTo(DataRowVersion.Current), "B:SourceVersion");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "B:NpgsqlDbType");
Assert.That(p.Value, Is.EqualTo(DBNull.Value), "B:Value");
}
[Test]
public void Constructor2_Value_null()
{
var p = new NpgsqlParameter("address", null);
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "A:DbType");
Assert.That(p.Direction, Is.EqualTo(ParameterDirection.Input), "A:Direction");
Assert.That(p.IsNullable, Is.False, "A:IsNullable");
Assert.That(p.ParameterName, Is.EqualTo("address"), "A:ParameterName");
Assert.That(p.Precision, Is.EqualTo(0), "A:Precision");
Assert.That(p.Scale, Is.EqualTo(0), "A:Scale");
Assert.That(p.Size, Is.EqualTo(0), "A:Size");
Assert.That(p.SourceColumn, Is.Empty, "A:SourceColumn");
Assert.That(p.SourceVersion, Is.EqualTo(DataRowVersion.Current), "A:SourceVersion");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "A:NpgsqlDbType");
Assert.That(p.Value, Is.Null, "A:Value");
}
[Test]
//.ctor (String, NpgsqlDbType, Int32, String, ParameterDirection, bool, byte, byte, DataRowVersion, object)
public void Constructor7()
{
var p1 = new NpgsqlParameter("p1Name", NpgsqlDbType.Varchar, 20,
"srcCol", ParameterDirection.InputOutput, false, 0, 0,
DataRowVersion.Original, "foo");
Assert.That(p1.DbType, Is.EqualTo(DbType.String), "DbType");
Assert.That(p1.Direction, Is.EqualTo(ParameterDirection.InputOutput), "Direction");
Assert.That(p1.IsNullable, Is.EqualTo(false), "IsNullable");
//Assert.AreEqual (999, p1.LocaleId, "#");
Assert.That(p1.ParameterName, Is.EqualTo("p1Name"), "ParameterName");
Assert.That(p1.Precision, Is.EqualTo(0), "Precision");
Assert.That(p1.Scale, Is.EqualTo(0), "Scale");
Assert.That(p1.Size, Is.EqualTo(20), "Size");
Assert.That(p1.SourceColumn, Is.EqualTo("srcCol"), "SourceColumn");
Assert.That(p1.SourceColumnNullMapping, Is.EqualTo(false), "SourceColumnNullMapping");
Assert.That(p1.SourceVersion, Is.EqualTo(DataRowVersion.Original), "SourceVersion");
Assert.That(p1.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Varchar), "NpgsqlDbType");
//Assert.AreEqual (3210, p1.NpgsqlValue, "#");
Assert.That(p1.Value, Is.EqualTo("foo"), "Value");
//Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase, "XmlSchemaCollectionDatabase");
//Assert.AreEqual ("name", p1.XmlSchemaCollectionName, "XmlSchemaCollectionName");
//Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema, "XmlSchemaCollectionOwningSchema");
}
[Test]
public void Clone()
{
var expected = new NpgsqlParameter
{
Value = 42,
ParameterName = "TheAnswer",
DbType = DbType.Int32,
NpgsqlDbType = NpgsqlDbType.Integer,
DataTypeName = "integer",
Direction = ParameterDirection.InputOutput,
IsNullable = true,
Precision = 1,
Scale = 2,
Size = 4,
SourceVersion = DataRowVersion.Proposed,
SourceColumn = "source",
SourceColumnNullMapping = true,
};
var actual = expected.Clone();
Assert.That(actual.Value, Is.EqualTo(expected.Value));
Assert.That(actual.ParameterName, Is.EqualTo(expected.ParameterName));
Assert.That(actual.DbType, Is.EqualTo(expected.DbType));
Assert.That(actual.NpgsqlDbType, Is.EqualTo(expected.NpgsqlDbType));
Assert.That(actual.DataTypeName, Is.EqualTo(expected.DataTypeName));
Assert.That(actual.Direction, Is.EqualTo(expected.Direction));
Assert.That(actual.IsNullable, Is.EqualTo(expected.IsNullable));
Assert.That(actual.Precision, Is.EqualTo(expected.Precision));
Assert.That(actual.Scale, Is.EqualTo(expected.Scale));
Assert.That(actual.Size, Is.EqualTo(expected.Size));
Assert.That(actual.SourceVersion, Is.EqualTo(expected.SourceVersion));
Assert.That(actual.SourceColumn, Is.EqualTo(expected.SourceColumn));
Assert.That(actual.SourceColumnNullMapping, Is.EqualTo(expected.SourceColumnNullMapping));
}
[Test]
public void Clone_generic()
{
var expected = new NpgsqlParameter<int>
{
TypedValue = 42,
ParameterName = "TheAnswer",
DbType = DbType.Int32,
NpgsqlDbType = NpgsqlDbType.Integer,
DataTypeName = "integer",
Direction = ParameterDirection.InputOutput,
IsNullable = true,
Precision = 1,
Scale = 2,
Size = 4,
SourceVersion = DataRowVersion.Proposed,
SourceColumn ="source",
SourceColumnNullMapping = true,
};
var actual = (NpgsqlParameter<int>)expected.Clone();
Assert.That(actual.Value, Is.EqualTo(expected.Value));
Assert.That(actual.TypedValue, Is.EqualTo(expected.TypedValue));
Assert.That(actual.ParameterName, Is.EqualTo(expected.ParameterName));
Assert.That(actual.DbType, Is.EqualTo(expected.DbType));
Assert.That(actual.NpgsqlDbType, Is.EqualTo(expected.NpgsqlDbType));
Assert.That(actual.DataTypeName, Is.EqualTo(expected.DataTypeName));
Assert.That(actual.Direction, Is.EqualTo(expected.Direction));
Assert.That(actual.IsNullable, Is.EqualTo(expected.IsNullable));
Assert.That(actual.Precision, Is.EqualTo(expected.Precision));
Assert.That(actual.Scale, Is.EqualTo(expected.Scale));
Assert.That(actual.Size, Is.EqualTo(expected.Size));
Assert.That(actual.SourceVersion, Is.EqualTo(expected.SourceVersion));
Assert.That(actual.SourceColumn, Is.EqualTo(expected.SourceColumn));
Assert.That(actual.SourceColumnNullMapping, Is.EqualTo(expected.SourceColumnNullMapping));
}
#endregion
[Test] // bug #320196
public void Parameter_null()
{
var param = new NpgsqlParameter("param", NpgsqlDbType.Numeric);
Assert.That(param.Scale, Is.EqualTo(0), "#A1");
param.Value = DBNull.Value;
Assert.That(param.Scale, Is.EqualTo(0), "#A2");
param = new NpgsqlParameter("param", NpgsqlDbType.Integer);
Assert.That(param.Scale, Is.EqualTo(0), "#B1");
param.Value = DBNull.Value;
Assert.That(param.Scale, Is.EqualTo(0), "#B2");
}
[Test]
public void Parameter_type()
{
NpgsqlParameter p;
// If Type is not set, then type is inferred from the value
// assigned. The Type should be inferred everytime Value is assigned
// If value is null or DBNull, then the current Type should be reset to Unknown (DbType.Object and NpgsqlDbType.Unknown).
p = new NpgsqlParameter { Value = "" };
Assert.That(p.DbType, Is.EqualTo(DbType.String), "#A1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Text), "#A2");
p.Value = DBNull.Value;
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#B1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#B2");
p.Value = 1;
Assert.That(p.DbType, Is.EqualTo(DbType.Int32), "#C1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Integer), "#C2");
p.Value = DBNull.Value;
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#D1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#D2");
p.Value = new byte[] { 0x0a };
Assert.That(p.DbType, Is.EqualTo(DbType.Binary), "#E1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea), "#E2");
p.Value = null;
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#F1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#F2");
p.Value = DateTime.Now;
Assert.That(p.DbType, Is.EqualTo(DbType.DateTime2), "#G1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Timestamp), "#G2");
p.Value = null;
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#H1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#H2");
// If DbType is set, then the NpgsqlDbType should not be
// inferred from the value assigned.
p = new NpgsqlParameter();
p.DbType = DbType.DateTime;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.TimestampTz), "#I1");
p.Value = 1;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.TimestampTz), "#I2");
p.Value = null;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.TimestampTz), "#I3");
p.Value = DBNull.Value;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.TimestampTz), "#I4");
// If NpgsqlDbType is set, then the DbType should not be
// inferred from the value assigned.
p = new NpgsqlParameter();
p.NpgsqlDbType = NpgsqlDbType.Bytea;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea), "#J1");
p.Value = 1;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea), "#J2");
p.Value = null;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea), "#J3");
p.Value = DBNull.Value;
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Bytea), "#J4");
}
[Test, IssueLink("https://github.com/npgsql/npgsql/issues/5428")]
public async Task Match_param_index_case_insensitively()
{
await using var conn = await OpenConnectionAsync();
await using var cmd = new NpgsqlCommand("SELECT @p,@P", conn);
cmd.Parameters.AddWithValue("p", "Hello world");
await cmd.ExecuteNonQueryAsync();
}
[Test]
public void ParameterName()
{
var p = new NpgsqlParameter();
p.ParameterName = "name";
Assert.That(p.ParameterName, Is.EqualTo("name"), "#A:ParameterName");
Assert.That(p.SourceColumn, Is.Empty, "#A:SourceColumn");
p.ParameterName = null;
Assert.That(p.ParameterName, Is.Empty, "#B:ParameterName");
Assert.That(p.SourceColumn, Is.Empty, "#B:SourceColumn");
p.ParameterName = " ";
Assert.That(p.ParameterName, Is.EqualTo(" "), "#C:ParameterName");
Assert.That(p.SourceColumn, Is.Empty, "#C:SourceColumn");
p.ParameterName = " name ";
Assert.That(p.ParameterName, Is.EqualTo(" name "), "#D:ParameterName");
Assert.That(p.SourceColumn, Is.Empty, "#D:SourceColumn");
p.ParameterName = string.Empty;
Assert.That(p.ParameterName, Is.Empty, "#E:ParameterName");
Assert.That(p.SourceColumn, Is.Empty, "#E:SourceColumn");
}
[Test]
public void ResetDbType()
{
NpgsqlParameter p;
//Parameter with an assigned value but no DbType specified
p = new NpgsqlParameter("foo", 42);
p.ResetDbType();
Assert.That(p.DbType, Is.EqualTo(DbType.Int32), "#A:DbType");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Integer), "#A:NpgsqlDbType");
Assert.That(p.Value, Is.EqualTo(42), "#A:Value");
p.DbType = DbType.DateTime; //assigning a DbType
Assert.That(p.DbType, Is.EqualTo(DbType.DateTime), "#B:DbType1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.TimestampTz), "#B:SqlDbType1");
p.ResetDbType();
Assert.That(p.DbType, Is.EqualTo(DbType.Int32), "#B:DbType2");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Integer), "#B:SqlDbtype2");
//Parameter with an assigned NpgsqlDbType but no specified value
p = new NpgsqlParameter("foo", NpgsqlDbType.Integer);
p.ResetDbType();
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#C:DbType");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#C:NpgsqlDbType");
p.NpgsqlDbType = NpgsqlDbType.TimestampTz; //assigning a NpgsqlDbType
Assert.That(p.DbType, Is.EqualTo(DbType.DateTime), "#D:DbType1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.TimestampTz), "#D:SqlDbType1");
p.ResetDbType();
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#D:DbType2");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#D:SqlDbType2");
p = new NpgsqlParameter();
p.Value = DateTime.MaxValue;
Assert.That(p.DbType, Is.EqualTo(DbType.DateTime2), "#E:DbType1");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Timestamp), "#E:SqlDbType1");
p.Value = null;
p.ResetDbType();
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#E:DbType2");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#E:SqlDbType2");
p = new NpgsqlParameter("foo", NpgsqlDbType.Varchar);
p.Value = DateTime.MaxValue;
p.ResetDbType();
Assert.That(p.DbType, Is.EqualTo(DbType.DateTime2), "#F:DbType");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Timestamp), "#F:NpgsqlDbType");
Assert.That(p.Value, Is.EqualTo(DateTime.MaxValue), "#F:Value");
p = new NpgsqlParameter("foo", NpgsqlDbType.Varchar);
p.Value = DBNull.Value;
p.ResetDbType();
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#G:DbType");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#G:NpgsqlDbType");
Assert.That(p.Value, Is.EqualTo(DBNull.Value), "#G:Value");
p = new NpgsqlParameter("foo", NpgsqlDbType.Varchar);
p.Value = null;
p.ResetDbType();
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#G:DbType");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#G:NpgsqlDbType");
Assert.That(p.Value, Is.Null, "#G:Value");
}
[Test]
public void ParameterName_retains_prefix()
=> Assert.That(new NpgsqlParameter("@p", DbType.String).ParameterName, Is.EqualTo("@p"));
[Test]
public void SourceColumn()
{
var p = new NpgsqlParameter();
p.SourceColumn = "name";
Assert.That(p.ParameterName, Is.Empty, "#A:ParameterName");
Assert.That(p.SourceColumn, Is.EqualTo("name"), "#A:SourceColumn");
p.SourceColumn = null;
Assert.That(p.ParameterName, Is.Empty, "#B:ParameterName");
Assert.That(p.SourceColumn, Is.Empty, "#B:SourceColumn");
p.SourceColumn = " ";
Assert.That(p.ParameterName, Is.Empty, "#C:ParameterName");
Assert.That(p.SourceColumn, Is.EqualTo(" "), "#C:SourceColumn");
p.SourceColumn = " name ";
Assert.That(p.ParameterName, Is.Empty, "#D:ParameterName");
Assert.That(p.SourceColumn, Is.EqualTo(" name "), "#D:SourceColumn");
p.SourceColumn = string.Empty;
Assert.That(p.ParameterName, Is.Empty, "#E:ParameterName");
Assert.That(p.SourceColumn, Is.Empty, "#E:SourceColumn");
}
[Test]
public void Bug1011100_NpgsqlDbType()
{
var p = new NpgsqlParameter();
p.Value = DBNull.Value;
Assert.That(p.DbType, Is.EqualTo(DbType.Object), "#A:DbType");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Unknown), "#A:NpgsqlDbType");
// Now change parameter value.
// Note that as we didn't explicitly specified a dbtype, the dbtype property should change when
// the value changes...
p.Value = 8;
Assert.That(p.DbType, Is.EqualTo(DbType.Int32), "#A:DbType");
Assert.That(p.NpgsqlDbType, Is.EqualTo(NpgsqlDbType.Integer), "#A:NpgsqlDbType");
//Assert.AreEqual(3510, p.Value, "#A:Value");
//p.NpgsqlDbType = NpgsqlDbType.Varchar;
//Assert.AreEqual(DbType.String, p.DbType, "#B:DbType");
//Assert.AreEqual(NpgsqlDbType.Varchar, p.NpgsqlDbType, "#B:NpgsqlDbType");
//Assert.AreEqual(3510, p.Value, "#B:Value");
}
[Test]
public void NpgsqlParameter_Clone()
{
var param = new NpgsqlParameter();
param.Value = 5;
param.Precision = 1;
param.Scale = 1;
param.Size = 1;
param.Direction = ParameterDirection.Input;
param.IsNullable = true;
param.ParameterName = "parameterName";
param.SourceColumn = "source_column";
param.SourceVersion = DataRowVersion.Current;
param.NpgsqlValue = 5;
param.SourceColumnNullMapping = false;
var newParam = param.Clone();
Assert.That(newParam.Value, Is.EqualTo(param.Value));
Assert.That(newParam.Precision, Is.EqualTo(param.Precision));
Assert.That(newParam.Scale, Is.EqualTo(param.Scale));
Assert.That(newParam.Size, Is.EqualTo(param.Size));
Assert.That(newParam.Direction, Is.EqualTo(param.Direction));
Assert.That(newParam.IsNullable, Is.EqualTo(param.IsNullable));
Assert.That(newParam.ParameterName, Is.EqualTo(param.ParameterName));
Assert.That(newParam.TrimmedName, Is.EqualTo(param.TrimmedName));
Assert.That(newParam.SourceColumn, Is.EqualTo(param.SourceColumn));
Assert.That(newParam.SourceVersion, Is.EqualTo(param.SourceVersion));
Assert.That(newParam.NpgsqlValue, Is.EqualTo(param.NpgsqlValue));
Assert.That(newParam.SourceColumnNullMapping, Is.EqualTo(param.SourceColumnNullMapping));
Assert.That(newParam.NpgsqlValue, Is.EqualTo(param.NpgsqlValue));
}
[Test]
public void Precision_via_interface()
{
var parameter = new NpgsqlParameter();
var paramIface = (IDbDataParameter)parameter;
paramIface.Precision = 42;
Assert.That(paramIface.Precision, Is.EqualTo((byte)42));
}
[Test]
public void Precision_via_base_class()
{
var parameter = new NpgsqlParameter();
var paramBase = (DbParameter)parameter;
paramBase.Precision = 42;
Assert.That(paramBase.Precision, Is.EqualTo((byte)42));
}
[Test]
public void Scale_via_interface()
{
var parameter = new NpgsqlParameter();
var paramIface = (IDbDataParameter)parameter;
paramIface.Scale = 42;
Assert.That(paramIface.Scale, Is.EqualTo((byte)42));
}
[Test]
public void Scale_via_base_class()
{
var parameter = new NpgsqlParameter();
var paramBase = (DbParameter)parameter;
paramBase.Scale = 42;
Assert.That(paramBase.Scale, Is.EqualTo((byte)42));
}
[Test]
public void Null_value_throws()
{
using var connection = OpenConnection();
using var command = new NpgsqlCommand("SELECT @p", connection)
{
Parameters = { new NpgsqlParameter("p", null) }
};
Assert.That(() => command.ExecuteReader(), Throws.InvalidOperationException);
}
[Test]
public void Null_value_with_nullable_type()
{
using var connection = OpenConnection();
using var command = new NpgsqlCommand("SELECT @p", connection)
{
Parameters = { new NpgsqlParameter<int?>("p", null) }
};
using var reader = command.ExecuteReader();
Assert.That(reader.Read(), Is.True);
Assert.That(reader.GetFieldValue<int?>(0), Is.Null);
}
[Test]
public void DBNull_reuses_type_info([Values]bool generic)
{
var param = generic ? new NpgsqlParameter<object> { Value = "value" } : new NpgsqlParameter { Value = "value" };
param.ResolveTypeInfo(DataSource.CurrentReloadableState.SerializerOptions, null);
param.GetResolutionInfo(out var typeInfo, out _, out _);
Assert.That(typeInfo, Is.Not.Null);
// Make sure we don't reset the type info when setting DBNull.
param.Value = DBNull.Value;
param.GetResolutionInfo(out var secondTypeInfo, out _, out _);
Assert.That(secondTypeInfo, Is.SameAs(typeInfo));
// Make sure we don't resolve a different type info either.
param.ResolveTypeInfo(DataSource.CurrentReloadableState.SerializerOptions, null);
param.GetResolutionInfo(out var thirdTypeInfo, out _, out _);
Assert.That(thirdTypeInfo, Is.SameAs(secondTypeInfo));
}
[Test]
public void DBNull_followed_by_non_null_reresolves([Values]bool generic)
{
var param = generic ? new NpgsqlParameter<object> { Value = DBNull.Value } : new NpgsqlParameter { Value = DBNull.Value };
param.ResolveTypeInfo(DataSource.CurrentReloadableState.SerializerOptions, null);
param.GetResolutionInfo(out var typeInfo, out _, out var pgTypeId);
Assert.That(typeInfo, Is.Not.Null);
Assert.That(pgTypeId.IsUnspecified, Is.True);
param.Value = "value";
param.GetResolutionInfo(out var secondTypeInfo, out _, out _);
Assert.That(secondTypeInfo, Is.Null);
// Make sure we don't resolve the same type info either.
param.ResolveTypeInfo(DataSource.CurrentReloadableState.SerializerOptions, null);
param.GetResolutionInfo(out var thirdTypeInfo, out _, out _);
Assert.That(thirdTypeInfo, Is.Not.SameAs(typeInfo));
}
[Test]
public void Changing_value_type_reresolves([Values]bool generic)
{
var param = generic ? new NpgsqlParameter<object> { Value = "value" } : new NpgsqlParameter { Value = "value" };
param.ResolveTypeInfo(DataSource.CurrentReloadableState.SerializerOptions, null);
param.GetResolutionInfo(out var typeInfo, out _, out _);
Assert.That(typeInfo, Is.Not.Null);
param.Value = 1;
param.GetResolutionInfo(out var secondTypeInfo, out _, out _);
Assert.That(secondTypeInfo, Is.Null);
// Make sure we don't resolve a different type info either.
param.ResolveTypeInfo(DataSource.CurrentReloadableState.SerializerOptions, null);
param.GetResolutionInfo(out var thirdTypeInfo, out _, out _);
Assert.That(thirdTypeInfo, Is.Not.SameAs(typeInfo));
}
[Test]
public void DataTypeName_prioritized_over_NpgsqlDbType([Values]bool generic)
{
var param = generic ? new NpgsqlParameter<object>
{
NpgsqlDbType = NpgsqlDbType.Integer,
DataTypeName = "text",
Value = "value"
} : new NpgsqlParameter
{
NpgsqlDbType = NpgsqlDbType.Integer,
DataTypeName = "text",
Value = "value"
};
param.ResolveTypeInfo(DataSource.CurrentReloadableState.SerializerOptions, null);
param.GetResolutionInfo(out var typeInfo, out _, out _);
Assert.That(typeInfo, Is.Not.Null);
Assert.That(typeInfo.PgTypeId, Is.EqualTo(DataSource.CurrentReloadableState.SerializerOptions.TextPgTypeId));
}
#if NeedsPorting
[Test]
[Category ("NotWorking")]
public void InferType_Char()
{
Char value = 'X';
String string_value = "X";
NpgsqlParameter p = new NpgsqlParameter ();
p.Value = value;
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#A:NpgsqlDbType");
Assert.AreEqual (DbType.String, p.DbType, "#A:DbType");
Assert.AreEqual (string_value, p.Value, "#A:Value");
p = new NpgsqlParameter ();
p.Value = value;
Assert.AreEqual (value, p.Value, "#B:Value1");
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#B:NpgsqlDbType");
Assert.AreEqual (string_value, p.Value, "#B:Value2");
p = new NpgsqlParameter ();
p.Value = value;
Assert.AreEqual (value, p.Value, "#C:Value1");
Assert.AreEqual (DbType.String, p.DbType, "#C:DbType");
Assert.AreEqual (string_value, p.Value, "#C:Value2");
p = new NpgsqlParameter ("name", value);
Assert.AreEqual (value, p.Value, "#D:Value1");
Assert.AreEqual (DbType.String, p.DbType, "#D:DbType");
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#D:NpgsqlDbType");
Assert.AreEqual (string_value, p.Value, "#D:Value2");
p = new NpgsqlParameter ("name", 5);
p.Value = value;
Assert.AreEqual (value, p.Value, "#E:Value1");
Assert.AreEqual (DbType.String, p.DbType, "#E:DbType");
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#E:NpgsqlDbType");
Assert.AreEqual (string_value, p.Value, "#E:Value2");
p = new NpgsqlParameter ("name", NpgsqlDbType.Text);
p.Value = value;
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#F:NpgsqlDbType");
Assert.AreEqual (value, p.Value, "#F:Value");
}
[Test]
[Category ("NotWorking")]
public void InferType_CharArray()
{
Char[] value = new Char[] { 'A', 'X' };
String string_value = "AX";
NpgsqlParameter p = new NpgsqlParameter ();
p.Value = value;
Assert.AreEqual (value, p.Value, "#A:Value1");
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#A:NpgsqlDbType");
Assert.AreEqual (DbType.String, p.DbType, "#A:DbType");
Assert.AreEqual (string_value, p.Value, "#A:Value2");
p = new NpgsqlParameter ();
p.Value = value;
Assert.AreEqual (value, p.Value, "#B:Value1");
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#B:NpgsqlDbType");
Assert.AreEqual (string_value, p.Value, "#B:Value2");
p = new NpgsqlParameter ();
p.Value = value;
Assert.AreEqual (value, p.Value, "#C:Value1");
Assert.AreEqual (DbType.String, p.DbType, "#C:DbType");
Assert.AreEqual (string_value, p.Value, "#C:Value2");
p = new NpgsqlParameter ("name", value);
Assert.AreEqual (value, p.Value, "#D:Value1");
Assert.AreEqual (DbType.String, p.DbType, "#D:DbType");
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#D:NpgsqlDbType");
Assert.AreEqual (string_value, p.Value, "#D:Value2");
p = new NpgsqlParameter ("name", 5);
p.Value = value;
Assert.AreEqual (value, p.Value, "#E:Value1");
Assert.AreEqual (DbType.String, p.DbType, "#E:DbType");
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#E:NpgsqlDbType");
Assert.AreEqual (string_value, p.Value, "#E:Value2");
p = new NpgsqlParameter ("name", NpgsqlDbType.Text);
p.Value = value;
Assert.AreEqual (NpgsqlDbType.Text, p.NpgsqlDbType, "#F:NpgsqlDbType");
Assert.AreEqual (value, p.Value, "#F:Value");
}
[Test]
public void InferType_Object()
{
Object value = new Object();
NpgsqlParameter param = new NpgsqlParameter();
param.Value = value;
Assert.AreEqual(NpgsqlDbType.Variant, param.NpgsqlDbType, "#1");
Assert.AreEqual(DbType.Object, param.DbType, "#2");
}
[Test]
public void LocaleId ()
{
NpgsqlParameter parameter = new NpgsqlParameter ();
Assert.AreEqual (0, parameter.LocaleId, "#1");
parameter.LocaleId = 15;
Assert.AreEqual(15, parameter.LocaleId, "#2");
}
#endif
[OneTimeSetUp]
public async Task Bootstrap()
{
// Bootstrap datasource.
await using (var _ = await OpenConnectionAsync()) {}
}
}