forked from npgsql/npgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNpgsqlParameterTests.cs
More file actions
923 lines (819 loc) · 39.9 KB
/
NpgsqlParameterTests.cs
File metadata and controls
923 lines (819 loc) · 39.9 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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
#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
#define NET_2_0
using System;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Threading;
using System.Xml;
using Npgsql;
using NpgsqlTypes;
using NUnit.Framework;
namespace Npgsql.Tests
{
[TestFixture]
public class NpgsqlParameterTest
{
[Test, Description("Makes sure that when NpgsqlDbType or Value/NpgsqlValue are set, DbType and NpgsqlDbType are set accordingly")]
public void ImplicitSettingOfDbTypes()
{
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));
}
// Older tests
/// <summary>
/// Test which validates that Clear() indeed cleans up the parameters in a command so they can be added to other commands safely.
/// </summary>
[Test]
public void NpgsqlParameterCollectionClearTest()
{
var p = new NpgsqlParameter();
var c1 = new NpgsqlCommand();
var c2 = new NpgsqlCommand();
c1.Parameters.Add(p);
Assert.AreEqual(1, c1.Parameters.Count);
Assert.AreEqual(0, c2.Parameters.Count);
c1.Parameters.Clear();
Assert.AreEqual(0, c1.Parameters.Count);
c2.Parameters.Add(p);
Assert.AreEqual(0, c1.Parameters.Count);
Assert.AreEqual(1, c2.Parameters.Count);
}
#region Constructors
[Test]
public void Constructor1()
{
var p = new NpgsqlParameter();
Assert.AreEqual(DbType.Object, p.DbType, "DbType");
Assert.AreEqual(ParameterDirection.Input, p.Direction, "Direction");
Assert.IsFalse(p.IsNullable, "IsNullable");
#if NET_2_0
//Assert.AreEqual (0, p.LocaleId, "LocaleId");
#endif
Assert.AreEqual(string.Empty, p.ParameterName, "ParameterName");
Assert.AreEqual(0, p.Precision, "Precision");
Assert.AreEqual(0, p.Scale, "Scale");
Assert.AreEqual(0, p.Size, "Size");
Assert.AreEqual(string.Empty, p.SourceColumn, "SourceColumn");
#if NET_2_0
Assert.IsFalse(p.SourceColumnNullMapping, "SourceColumnNullMapping");
#endif
Assert.AreEqual(DataRowVersion.Current, p.SourceVersion, "SourceVersion");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "NpgsqlDbType");
#if NET_2_0
Assert.IsNull(p.NpgsqlValue, "NpgsqlValue");
#endif
Assert.IsNull(p.Value, "Value");
#if NET_2_0
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionDatabase, "XmlSchemaCollectionDatabase");
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionName, "XmlSchemaCollectionName");
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionOwningSchema, "XmlSchemaCollectionOwningSchema");
#endif
}
[Test]
public void Constructor2_Value_DateTime()
{
var value = new DateTime(2004, 8, 24);
var p = new NpgsqlParameter("address", value);
Assert.AreEqual(DbType.DateTime, p.DbType, "B:DbType");
Assert.AreEqual(ParameterDirection.Input, p.Direction, "B:Direction");
Assert.IsFalse(p.IsNullable, "B:IsNullable");
#if NET_2_0
//Assert.AreEqual (0, p.LocaleId, "B:LocaleId");
#endif
Assert.AreEqual("address", p.ParameterName, "B:ParameterName");
Assert.AreEqual(0, p.Precision, "B:Precision");
Assert.AreEqual(0, p.Scale, "B:Scale");
//Assert.AreEqual (0, p.Size, "B:Size");
Assert.AreEqual(string.Empty, p.SourceColumn, "B:SourceColumn");
#if NET_2_0
Assert.IsFalse(p.SourceColumnNullMapping, "B:SourceColumnNullMapping");
#endif
Assert.AreEqual(DataRowVersion.Current, p.SourceVersion, "B:SourceVersion");
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "B:NpgsqlDbType");
#if NET_2_0
// FIXME
//Assert.AreEqual (new SqlDateTime (value), p.NpgsqlValue, "B:NpgsqlValue");
#endif
Assert.AreEqual(value, p.Value, "B:Value");
#if NET_2_0
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionDatabase, "B:XmlSchemaCollectionDatabase");
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionName, "B:XmlSchemaCollectionName");
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionOwningSchema, "B:XmlSchemaCollectionOwningSchema");
#endif
}
[Test]
public void Constructor2_Value_DBNull()
{
var p = new NpgsqlParameter("address", DBNull.Value);
Assert.AreEqual(DbType.Object, p.DbType, "B:DbType");
Assert.AreEqual(ParameterDirection.Input, p.Direction, "B:Direction");
Assert.IsFalse(p.IsNullable, "B:IsNullable");
#if NET_2_0
//Assert.AreEqual (0, p.LocaleId, "B:LocaleId");
#endif
Assert.AreEqual("address", p.ParameterName, "B:ParameterName");
Assert.AreEqual(0, p.Precision, "B:Precision");
Assert.AreEqual(0, p.Scale, "B:Scale");
Assert.AreEqual(0, p.Size, "B:Size");
Assert.AreEqual(string.Empty, p.SourceColumn, "B:SourceColumn");
#if NET_2_0
Assert.IsFalse(p.SourceColumnNullMapping, "B:SourceColumnNullMapping");
#endif
Assert.AreEqual(DataRowVersion.Current, p.SourceVersion, "B:SourceVersion");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "B:NpgsqlDbType");
#if NET_2_0
// FIXME
//Assert.AreEqual (SqlString.Null, p.NpgsqlValue, "B:NpgsqlValue");
#endif
Assert.AreEqual(DBNull.Value, p.Value, "B:Value");
#if NET_2_0
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionDatabase, "B:XmlSchemaCollectionDatabase");
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionName, "B:XmlSchemaCollectionName");
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionOwningSchema, "B:XmlSchemaCollectionOwningSchema");
#endif
}
[Test]
public void Constructor2_Value_Null()
{
var p = new NpgsqlParameter("address", (Object) null);
Assert.AreEqual(DbType.Object, p.DbType, "A:DbType");
Assert.AreEqual(ParameterDirection.Input, p.Direction, "A:Direction");
Assert.IsFalse(p.IsNullable, "A:IsNullable");
#if NET_2_0
//Assert.AreEqual (0, p.LocaleId, "A:LocaleId");
#endif
Assert.AreEqual("address", p.ParameterName, "A:ParameterName");
Assert.AreEqual(0, p.Precision, "A:Precision");
Assert.AreEqual(0, p.Scale, "A:Scale");
Assert.AreEqual(0, p.Size, "A:Size");
Assert.AreEqual(string.Empty, p.SourceColumn, "A:SourceColumn");
#if NET_2_0
Assert.IsFalse(p.SourceColumnNullMapping, "A:SourceColumnNullMapping");
#endif
Assert.AreEqual(DataRowVersion.Current, p.SourceVersion, "A:SourceVersion");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "A:NpgsqlDbType");
#if NET_2_0
Assert.IsNull(p.NpgsqlValue, "A:NpgsqlValue");
#endif
Assert.IsNull(p.Value, "A:Value");
#if NET_2_0
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionDatabase, "A:XmlSchemaCollectionDatabase");
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionName, "A:XmlSchemaCollectionName");
//Assert.AreEqual (string.Empty, p.XmlSchemaCollectionOwningSchema, "A:XmlSchemaCollectionOwningSchema");
#endif
}
#if NET_2_0
[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.AreEqual(DbType.String, p1.DbType, "DbType");
Assert.AreEqual(ParameterDirection.InputOutput, p1.Direction, "Direction");
Assert.AreEqual(false, p1.IsNullable, "IsNullable");
//Assert.AreEqual (999, p1.LocaleId, "#");
Assert.AreEqual("p1Name", p1.ParameterName, "ParameterName");
Assert.AreEqual(0, p1.Precision, "Precision");
Assert.AreEqual(0, p1.Scale, "Scale");
Assert.AreEqual(20, p1.Size, "Size");
Assert.AreEqual("srcCol", p1.SourceColumn, "SourceColumn");
Assert.AreEqual(false, p1.SourceColumnNullMapping, "SourceColumnNullMapping");
Assert.AreEqual(DataRowVersion.Original, p1.SourceVersion, "SourceVersion");
Assert.AreEqual(NpgsqlDbType.Varchar, p1.NpgsqlDbType, "NpgsqlDbType");
//Assert.AreEqual (3210, p1.NpgsqlValue, "#");
Assert.AreEqual("foo", p1.Value, "Value");
//Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase, "XmlSchemaCollectionDatabase");
//Assert.AreEqual ("name", p1.XmlSchemaCollectionName, "XmlSchemaCollectionName");
//Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema, "XmlSchemaCollectionOwningSchema");
}
#endif
#endregion
#if NeedsPorting
[Test]
#if NET_2_0
[Category ("NotWorking")]
#endif
public void InferType_Char()
{
Char value = 'X';
#if NET_2_0
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");
#else
NpgsqlParameter p = new NpgsqlParameter();
try
{
p.Value = value;
Assert.Fail("#1");
}
catch (ArgumentException ex)
{
// The parameter data type of Char is invalid
Assert.AreEqual(typeof(ArgumentException), ex.GetType(), "#2");
Assert.IsNull(ex.InnerException, "#3");
Assert.IsNotNull(ex.Message, "#4");
Assert.IsNull(ex.ParamName, "#5");
}
#endif
}
[Test]
#if NET_2_0
[Category ("NotWorking")]
#endif
public void InferType_CharArray()
{
Char[] value = new Char[] { 'A', 'X' };
#if NET_2_0
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");
#else
NpgsqlParameter p = new NpgsqlParameter();
try
{
p.Value = value;
Assert.Fail("#1");
}
catch (FormatException)
{
// appears to be bug in .NET 1.1 while constructing
// exception message
}
catch (ArgumentException ex)
{
// The parameter data type of Char[] is invalid
Assert.AreEqual(typeof(ArgumentException), ex.GetType(), "#2");
Assert.IsNull(ex.InnerException, "#3");
Assert.IsNotNull(ex.Message, "#4");
Assert.IsNull(ex.ParamName, "#5");
}
#endif
}
#endif
[Test]
[Ignore]
#if NET_2_0
[Category("NotWorking")]
#endif
public void InferType_Invalid()
{
var notsupported = new object[]
{
UInt16.MaxValue,
UInt32.MaxValue,
UInt64.MaxValue,
SByte.MaxValue,
new NpgsqlParameter()
};
var param = new NpgsqlParameter();
for (var i = 0; i < notsupported.Length; i++)
{
#if NET_2_0
param.Value = notsupported[i];
try
{
var type = param.NpgsqlDbType;
Assert.Fail("#A1:" + i + " (" + type + ")");
}
catch (ArgumentException ex)
{
// The parameter data type of ... is invalid
Assert.AreEqual(typeof (ArgumentException), ex.GetType(), "#A2");
Assert.IsNull(ex.InnerException, "#A3");
Assert.IsNotNull(ex.Message, "#A4");
Assert.IsNull(ex.ParamName, "#A5");
}
try
{
var type = param.DbType;
Assert.Fail("#B1:" + i + " (" + type + ")");
}
catch (ArgumentException ex)
{
// The parameter data type of ... is invalid
Assert.AreEqual(typeof (ArgumentException), ex.GetType(), "#B2");
Assert.IsNull(ex.InnerException, "#B3");
Assert.IsNotNull(ex.Message, "#B4");
Assert.IsNull(ex.ParamName, "#B5");
}
#else
try
{
param.Value = notsupported[i];
Assert.Fail("#A1:" + i);
}
catch (FormatException)
{
// appears to be bug in .NET 1.1 while
// constructing exception message
}
catch (ArgumentException ex)
{
// The parameter data type of ... is invalid
Assert.AreEqual(typeof(ArgumentException), ex.GetType(), "#A2");
Assert.IsNull(ex.InnerException, "#A3");
Assert.IsNotNull(ex.Message, "#A4");
Assert.IsNull(ex.ParamName, "#A5");
}
#endif
}
}
#if NeedsPorting
[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");
}
#endif
#if NeedsPorting
#if NET_2_0
[Test]
public void LocaleId ()
{
NpgsqlParameter parameter = new NpgsqlParameter ();
Assert.AreEqual (0, parameter.LocaleId, "#1");
parameter.LocaleId = 15;
Assert.AreEqual(15, parameter.LocaleId, "#2");
}
#endif
#endif
[Test] // bug #320196
public void ParameterNullTest()
{
var param = new NpgsqlParameter("param", NpgsqlDbType.Numeric);
Assert.AreEqual(0, param.Scale, "#A1");
param.Value = DBNull.Value;
Assert.AreEqual(0, param.Scale, "#A2");
param = new NpgsqlParameter("param", NpgsqlDbType.Integer);
Assert.AreEqual(0, param.Scale, "#B1");
param.Value = DBNull.Value;
Assert.AreEqual(0, param.Scale, "#B2");
}
[Test]
[Ignore]
public void ParameterType()
{
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 Text.
p = new NpgsqlParameter();
Assert.AreEqual(DbType.String, p.DbType, "#A1");
Assert.AreEqual(NpgsqlDbType.Text, p.NpgsqlDbType, "#A2");
p.Value = DBNull.Value;
Assert.AreEqual(DbType.String, p.DbType, "#B1");
Assert.AreEqual(NpgsqlDbType.Text, p.NpgsqlDbType, "#B2");
p.Value = 1;
Assert.AreEqual(DbType.Int32, p.DbType, "#C1");
Assert.AreEqual(NpgsqlDbType.Integer, p.NpgsqlDbType, "#C2");
p.Value = DBNull.Value;
#if NET_2_0
Assert.AreEqual(DbType.String, p.DbType, "#D1");
Assert.AreEqual(NpgsqlDbType.Text, p.NpgsqlDbType, "#D2");
#else
Assert.AreEqual(DbType.Int32, p.DbType, "#D1");
Assert.AreEqual(NpgsqlDbType.Integer, p.NpgsqlDbType, "#D2");
#endif
p.Value = new byte[] {0x0a};
Assert.AreEqual(DbType.Binary, p.DbType, "#E1");
Assert.AreEqual(NpgsqlDbType.Bytea, p.NpgsqlDbType, "#E2");
p.Value = null;
#if NET_2_0
Assert.AreEqual(DbType.String, p.DbType, "#F1");
Assert.AreEqual(NpgsqlDbType.Text, p.NpgsqlDbType, "#F2");
#else
Assert.AreEqual(DbType.Binary, p.DbType, "#F1");
Assert.AreEqual(NpgsqlDbType.VarBinary, p.NpgsqlDbType, "#F2");
#endif
p.Value = DateTime.Now;
Assert.AreEqual(DbType.DateTime, p.DbType, "#G1");
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#G2");
p.Value = null;
#if NET_2_0
Assert.AreEqual(DbType.String, p.DbType, "#H1");
Assert.AreEqual(NpgsqlDbType.Text, p.NpgsqlDbType, "#H2");
#else
Assert.AreEqual(DbType.DateTime, p.DbType, "#H1");
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#H2");
#endif
// If DbType is set, then the NpgsqlDbType should not be
// inferred from the value assigned.
p = new NpgsqlParameter();
p.DbType = DbType.DateTime;
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#I1");
p.Value = 1;
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#I2");
p.Value = null;
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#I3");
p.Value = DBNull.Value;
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#I4");
// If NpgsqlDbType is set, then the DbType should not be
// inferred from the value assigned.
p = new NpgsqlParameter();
p.NpgsqlDbType = NpgsqlDbType.Bytea;
Assert.AreEqual(NpgsqlDbType.Bytea, p.NpgsqlDbType, "#J1");
p.Value = 1;
Assert.AreEqual(NpgsqlDbType.Bytea, p.NpgsqlDbType, "#J2");
p.Value = null;
Assert.AreEqual(NpgsqlDbType.Bytea, p.NpgsqlDbType, "#J3");
p.Value = DBNull.Value;
Assert.AreEqual(NpgsqlDbType.Bytea, p.NpgsqlDbType, "#J4");
}
[Test]
[Ignore]
public void ParameterName()
{
var p = new NpgsqlParameter();
p.ParameterName = "name";
Assert.AreEqual("name", p.ParameterName, "#A:ParameterName");
Assert.AreEqual(string.Empty, p.SourceColumn, "#A:SourceColumn");
p.ParameterName = null;
Assert.AreEqual(string.Empty, p.ParameterName, "#B:ParameterName");
Assert.AreEqual(string.Empty, p.SourceColumn, "#B:SourceColumn");
p.ParameterName = " ";
Assert.AreEqual(" ", p.ParameterName, "#C:ParameterName");
Assert.AreEqual(string.Empty, p.SourceColumn, "#C:SourceColumn");
p.ParameterName = " name ";
Assert.AreEqual(" name ", p.ParameterName, "#D:ParameterName");
Assert.AreEqual(string.Empty, p.SourceColumn, "#D:SourceColumn");
p.ParameterName = string.Empty;
Assert.AreEqual(string.Empty, p.ParameterName, "#E:ParameterName");
Assert.AreEqual(string.Empty, p.SourceColumn, "#E:SourceColumn");
}
#if NET_2_0
[Test]
public void ResetDbType()
{
NpgsqlParameter p;
//Parameter with an assigned value but no DbType specified
p = new NpgsqlParameter("foo", 42);
p.ResetDbType();
Assert.AreEqual(DbType.Int32, p.DbType, "#A:DbType");
Assert.AreEqual(NpgsqlDbType.Integer, p.NpgsqlDbType, "#A:NpgsqlDbType");
Assert.AreEqual(42, p.Value, "#A:Value");
p.DbType = DbType.DateTime; //assigning a DbType
Assert.AreEqual(DbType.DateTime, p.DbType, "#B:DbType1");
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#B:SqlDbType1");
p.ResetDbType();
Assert.AreEqual(DbType.Int32, p.DbType, "#B:DbType2");
Assert.AreEqual(NpgsqlDbType.Integer, p.NpgsqlDbType, "#B:SqlDbtype2");
//Parameter with an assigned NpgsqlDbType but no specified value
p = new NpgsqlParameter("foo", NpgsqlDbType.Integer);
p.ResetDbType();
Assert.AreEqual(DbType.Object, p.DbType, "#C:DbType");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "#C:NpgsqlDbType");
p.DbType = DbType.DateTime; //assigning a NpgsqlDbType
Assert.AreEqual(DbType.DateTime, p.DbType, "#D:DbType1");
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#D:SqlDbType1");
p.ResetDbType();
Assert.AreEqual(DbType.Object, p.DbType, "#D:DbType2");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "#D:SqlDbType2");
p = new NpgsqlParameter();
p.Value = DateTime.MaxValue;
Assert.AreEqual(DbType.DateTime, p.DbType, "#E:DbType1");
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#E:SqlDbType1");
p.Value = null;
p.ResetDbType();
Assert.AreEqual(DbType.Object, p.DbType, "#E:DbType2");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "#E:SqlDbType2");
p = new NpgsqlParameter("foo", NpgsqlDbType.Varchar);
p.Value = DateTime.MaxValue;
p.ResetDbType();
Assert.AreEqual(DbType.DateTime, p.DbType, "#F:DbType");
Assert.AreEqual(NpgsqlDbType.Timestamp, p.NpgsqlDbType, "#F:NpgsqlDbType");
Assert.AreEqual(DateTime.MaxValue, p.Value, "#F:Value");
p = new NpgsqlParameter("foo", NpgsqlDbType.Varchar);
p.Value = DBNull.Value;
p.ResetDbType();
Assert.AreEqual(DbType.Object, p.DbType, "#G:DbType");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "#G:NpgsqlDbType");
Assert.AreEqual(DBNull.Value, p.Value, "#G:Value");
p = new NpgsqlParameter("foo", NpgsqlDbType.Varchar);
p.Value = null;
p.ResetDbType();
Assert.AreEqual(DbType.Object, p.DbType, "#G:DbType");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "#G:NpgsqlDbType");
Assert.IsNull(p.Value, "#G:Value");
}
#if NeedsPorting
[Test]
public void ResetSqlDbType ()
{
//Parameter with an assigned value but no NpgsqlDbType specified
NpgsqlParameter p1 = new NpgsqlParameter ("foo", 42);
Assert.AreEqual (42, p1.Value, "#1");
Assert.AreEqual (DbType.Int32, p1.DbType, "#2");
Assert.AreEqual (NpgsqlDbType.Integer, p1.NpgsqlDbType, "#3");
p1.ResetSqlDbType ();
Assert.AreEqual (DbType.Int32, p1.DbType, "#4 The parameter with value 42 must have DbType as Int32");
Assert.AreEqual (NpgsqlDbType.Integer, p1.NpgsqlDbType, "#5 The parameter with value 42 must have NpgsqlDbType as Int");
p1.NpgsqlDbType = NpgsqlDbType.Timestamp; //assigning a NpgsqlDbType
Assert.AreEqual (DbType.DateTime, p1.DbType, "#6");
Assert.AreEqual (NpgsqlDbType.Timestamp, p1.NpgsqlDbType, "#7");
p1.ResetSqlDbType (); //Resetting NpgsqlDbType
Assert.AreEqual (DbType.Int32, p1.DbType, "#8 Resetting NpgsqlDbType must infer the type from the value");
Assert.AreEqual (NpgsqlDbType.Integer, p1.NpgsqlDbType, "#9 Resetting NpgsqlDbType must infer the type from the value");
//Parameter with an assigned NpgsqlDbType but no specified value
NpgsqlParameter p2 = new NpgsqlParameter ("foo", NpgsqlDbType.Integer);
Assert.AreEqual (null, p2.Value, "#10");
Assert.AreEqual (DbType.Int32, p2.DbType, "#11");
Assert.AreEqual (NpgsqlDbType.Integer, p2.NpgsqlDbType, "#12");
//Although a NpgsqlDbType is specified, calling ResetSqlDbType resets
//the NpgsqlDbType and DbType properties to default values
p2.ResetSqlDbType ();
Assert.AreEqual (DbType.String, p2.DbType, "#13 Resetting NpgsqlDbType must infer the type from the value");
Assert.AreEqual (NpgsqlDbType.Text, p2.NpgsqlDbType, "#14 Resetting NpgsqlDbType must infer the type from the value");
p2.NpgsqlDbType = NpgsqlDbType.Timestamp; //assigning a NpgsqlDbType
Assert.AreEqual (DbType.DateTime, p2.DbType, "#15");
Assert.AreEqual (NpgsqlDbType.Timestamp, p2.NpgsqlDbType, "#16");
p2.ResetSqlDbType (); //Resetting NpgsqlDbType
Assert.AreEqual (DbType.String, p2.DbType, "#17 Resetting NpgsqlDbType must infer the type from the value");
Assert.AreEqual (NpgsqlDbType.Text, p2.NpgsqlDbType, "#18 Resetting NpgsqlDbType must infer the type from the value");
}
#endif
#endif
[Test]
[Ignore]
public void SourceColumn()
{
var p = new NpgsqlParameter();
p.SourceColumn = "name";
Assert.AreEqual(string.Empty, p.ParameterName, "#A:ParameterName");
Assert.AreEqual("name", p.SourceColumn, "#A:SourceColumn");
p.SourceColumn = null;
Assert.AreEqual(string.Empty, p.ParameterName, "#B:ParameterName");
Assert.AreEqual(string.Empty, p.SourceColumn, "#B:SourceColumn");
p.SourceColumn = " ";
Assert.AreEqual(string.Empty, p.ParameterName, "#C:ParameterName");
Assert.AreEqual(" ", p.SourceColumn, "#C:SourceColumn");
p.SourceColumn = " name ";
Assert.AreEqual(string.Empty, p.ParameterName, "#D:ParameterName");
Assert.AreEqual(" name ", p.SourceColumn, "#D:SourceColumn");
p.SourceColumn = string.Empty;
Assert.AreEqual(string.Empty, p.ParameterName, "#E:ParameterName");
Assert.AreEqual(string.Empty, p.SourceColumn, "#E:SourceColumn");
}
#if NET_2_0
[Test]
public void SourceColumnNullMapping()
{
var p = new NpgsqlParameter();
Assert.IsFalse(p.SourceColumnNullMapping, "#1");
p.SourceColumnNullMapping = true;
Assert.IsTrue(p.SourceColumnNullMapping, "#2");
p.SourceColumnNullMapping = false;
Assert.IsFalse(p.SourceColumnNullMapping, "#3");
}
#endif
[Test]
public void Bug1011100NpgsqlDbTypeTest()
{
var p = new NpgsqlParameter();
p.Value = DBNull.Value;
Assert.AreEqual(DbType.Object, p.DbType, "#A:DbType");
Assert.AreEqual(NpgsqlDbType.Unknown, p.NpgsqlDbType, "#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.AreEqual(DbType.Int32, p.DbType, "#A:DbType");
Assert.AreEqual(NpgsqlDbType.Integer, p.NpgsqlDbType, "#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");
}
#if NET_2_0
#if NeedsPorting
[Test]
public void XmlSchemaTest ()
{
NpgsqlParameter p1 = new NpgsqlParameter ();
//Testing default values
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
"#1 Default value for XmlSchemaCollectionDatabase is an empty string");
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionName,
"#2 Default value for XmlSchemaCollectionName is an empty string");
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
"#3 Default value for XmlSchemaCollectionOwningSchema is an empty string");
//Changing one property should not affect the remaining two properties
p1.XmlSchemaCollectionDatabase = "database";
Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
"#4 Default value for XmlSchemaCollectionDatabase is an empty string");
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionName,
"#5 Default value for XmlSchemaCollectionName is an empty string");
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
"#6 Default value for XmlSchemaCollectionOwningSchema is an empty string");
p1.XmlSchemaCollectionName = "name";
Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
"#7 Default value for XmlSchemaCollectionDatabase is an empty string");
Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
"#8 Default value for XmlSchemaCollectionName is an empty string");
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionOwningSchema,
"#9 Default value for XmlSchemaCollectionOwningSchema is an empty string");
p1.XmlSchemaCollectionOwningSchema = "schema";
Assert.AreEqual ("database", p1.XmlSchemaCollectionDatabase,
"#10 Default value for XmlSchemaCollectionDatabase is an empty string");
Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
"#11 Default value for XmlSchemaCollectionName is an empty string");
Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
"#12 Default value for XmlSchemaCollectionOwningSchema is an empty string");
//assigning null value stores default empty string
p1.XmlSchemaCollectionDatabase = null;
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
"#13 Default value for XmlSchemaCollectionDatabase is an empty string");
Assert.AreEqual ("name", p1.XmlSchemaCollectionName,
"#14 Default value for XmlSchemaCollectionName is an empty string");
Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
"#15 Default value for XmlSchemaCollectionOwningSchema is an empty string");
p1.XmlSchemaCollectionName = "";
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
"#16 Default value for XmlSchemaCollectionDatabase is an empty string");
Assert.AreEqual ("", p1.XmlSchemaCollectionName,
"#17 Default value for XmlSchemaCollectionName is an empty string");
Assert.AreEqual ("schema", p1.XmlSchemaCollectionOwningSchema,
"#18 Default value for XmlSchemaCollectionOwningSchema is an empty string");
//values are not trimmed
p1.XmlSchemaCollectionOwningSchema = " a ";
Assert.AreEqual (String.Empty, p1.XmlSchemaCollectionDatabase,
"#19 Default value for XmlSchemaCollectionDatabase is an empty string");
Assert.AreEqual ("", p1.XmlSchemaCollectionName,
"#20 Default value for XmlSchemaCollectionName is an empty string");
Assert.AreEqual (" a ", p1.XmlSchemaCollectionOwningSchema,
"#21 Default value for XmlSchemaCollectionOwningSchema is an empty string");
}
#endif
#endif
[Test]
public void ParameterCollectionHashLookupParameterRenameBug()
{
using (var command = new NpgsqlCommand())
{
// Put plenty of parameters in the collection to turn on hash lookup functionality.
for (int i = 0 ; i < 10 ; i++)
{
command.Parameters.AddWithValue(string.Format("p{0:00}", i + 1), NpgsqlDbType.Text, string.Format("String parameter value {0}", i + 1));
}
// Make sure both hash lookups have been generated.
Assert.AreEqual(command.Parameters["p03"].ParameterName, "p03");
Assert.AreEqual(command.Parameters["P03"].ParameterName, "p03");
// Rename the target parameter.
command.Parameters["p03"].ParameterName = "a_new_name";
try
{
// Try to exploit the hash lookup bug.
// If the bug exists, the hash lookups will be out of sync with the list, and be unable
// to find the parameter by its new name.
Assert.IsTrue(command.Parameters.IndexOf("a_new_name") >= 0);
}
catch (Exception e)
{
throw new Exception("NpgsqlParameterCollection hash lookup/parameter rename bug detected", e);
}
}
}
[Test]
public void NpgsqlParameterCloneTest()
{
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.AreEqual(param.Value, newParam.Value);
Assert.AreEqual(param.Precision, newParam.Precision);
Assert.AreEqual(param.Scale, newParam.Scale);
Assert.AreEqual(param.Size, newParam.Size);
Assert.AreEqual(param.Direction, newParam.Direction);
Assert.AreEqual(param.IsNullable, newParam.IsNullable);
Assert.AreEqual(param.ParameterName, newParam.ParameterName);
Assert.AreEqual(param.SourceColumn, newParam.SourceColumn);
Assert.AreEqual(param.SourceVersion, newParam.SourceVersion);
Assert.AreEqual(param.NpgsqlValue, newParam.NpgsqlValue);
Assert.AreEqual(param.SourceColumnNullMapping, newParam.SourceColumnNullMapping);
Assert.AreEqual(param.NpgsqlValue, newParam.NpgsqlValue);
}
[Test]
public void CleanName()
{
var param = new NpgsqlParameter();
var command = new NpgsqlCommand();
command.Parameters.Add(param);
param.ParameterName = "";
// These should not throw exceptions
Assert.AreEqual(0, command.Parameters.IndexOf(""));
Assert.AreEqual("", param.CleanName);
}
}
}