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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
|
//
// SqlDataAdapterTest.cs - NUnit Test Cases for testing the
// SqlDataAdapter class
// Author:
// Umadevi S (sumadevi@novell.com)
// Sureshkumar T (tsureshkumar@novell.com)
// Senganal T (tsenganal@novell.com)
//
// Copyright (c) 2004 Novell Inc., and the individuals listed
// on the ChangeLog entries.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Mono.Data;
using System.Configuration;
using NUnit.Framework;
namespace MonoTests.System.Data.SqlClient
{
[TestFixture]
[Category ("sqlserver")]
public class SqlDataAdapterTest
{
SqlDataAdapter adapter;
SqlDataReader dr;
DataSet data;
string connectionString = ConnectionManager.Singleton.ConnectionString;
SqlConnection conn;
EngineConfig engine;
[SetUp]
public void SetUp ()
{
engine = ConnectionManager.Singleton.Engine;
}
[TearDown]
public void TearDown ()
{
if (adapter != null) {
adapter.Dispose ();
adapter = null;
}
if (dr != null) {
dr.Close ();
dr = null;
}
if (conn != null) {
conn.Close ();
conn = null;
}
}
[Test]
public void Update_DeleteRow ()
{
conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
conn.Open ();
DataTable dt = new DataTable ();
adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
adapter.DeleteCommand = builder.GetDeleteCommand ();
adapter.Fill (dt);
DateTime now = DateTime.Now;
DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
now.Minute, now.Second);
DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
now.Minute, now.Second);
dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
try {
DataRow newRow = dt.NewRow ();
newRow ["id"] = 6002;
newRow ["fname"] = "boston";
newRow ["dob"] = dob;
newRow ["doj"] = doj;
newRow ["email"] = "mono@novell.com";
dt.Rows.Add (newRow);
adapter.Update (dt);
foreach (DataRow row in dt.Rows)
if (((int) row ["id"]) == 6002)
row.Delete ();
adapter.Update (dt);
SqlCommand cmd = conn.CreateCommand ();
cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
dr = cmd.ExecuteReader ();
Assert.IsFalse (dr.Read ());
dr.Close ();
} finally {
DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
}
}
[Test]
public void Update_InsertRow ()
{
conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
conn.Open ();
DataTable dt = new DataTable ();
adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
adapter.InsertCommand = builder.GetInsertCommand ();
adapter.Fill (dt);
DateTime now = DateTime.Now;
DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
now.Minute, now.Second);
DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
now.Minute, now.Second);
dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
try {
DataRow newRow = dt.NewRow ();
newRow ["id"] = 6002;
newRow ["fname"] = "boston";
newRow ["dob"] = dob;
newRow ["doj"] = doj;
newRow ["email"] = "mono@novell.com";
dt.Rows.Add (newRow);
adapter.Update (dt);
SqlCommand cmd = conn.CreateCommand ();
cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
dr = cmd.ExecuteReader ();
Assert.IsTrue (dr.Read (), "#A1");
Assert.AreEqual (6002, dr.GetValue (0), "#A2");
Assert.AreEqual ("boston", dr.GetValue (1), "#A3");
Assert.AreEqual (DBNull.Value, dr.GetValue (2), "#A4");
Assert.AreEqual (dob, dr.GetValue (3), "#A5");
Assert.AreEqual (doj, dr.GetValue (4), "#A6");
Assert.AreEqual ("mono@novell.com", dr.GetValue (5), "#A7");
Assert.IsFalse (dr.Read (), "#A8");
dr.Close ();
} finally {
DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
}
}
[Test]
public void Update_UpdateRow ()
{
conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
conn.Open ();
DataTable dt = new DataTable ();
adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
adapter.UpdateCommand = builder.GetUpdateCommand ();
adapter.Fill (dt);
DateTime now = DateTime.Now;
DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
now.Minute, now.Second);
DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
now.Minute, now.Second);
dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
try {
DataRow newRow = dt.NewRow ();
newRow ["id"] = 6002;
newRow ["fname"] = "boston";
newRow ["dob"] = dob;
newRow ["doj"] = doj;
newRow ["email"] = "mono@novell.com";
dt.Rows.Add (newRow);
adapter.Update (dt);
foreach (DataRow row in dt.Rows)
if (((int) row ["id"]) == 6002)
row ["lname"] = "de Icaza";
adapter.Update (dt);
SqlCommand cmd = conn.CreateCommand ();
cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
dr = cmd.ExecuteReader ();
Assert.IsTrue (dr.Read (), "#A1");
Assert.AreEqual (6002, dr.GetValue (0), "#A2");
Assert.AreEqual ("boston", dr.GetValue (1), "#A3");
Assert.AreEqual ("de Icaza", dr.GetValue (2), "#A4");
Assert.AreEqual (dob, dr.GetValue (3), "#A5");
Assert.AreEqual (doj, dr.GetValue (4), "#A6");
Assert.AreEqual ("mono@novell.com", dr.GetValue (5), "#A7");
Assert.IsFalse (dr.Read (), "#A8");
dr.Close ();
} finally {
DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
}
}
/**
The below test will not run everytime, since the region id column is unique
so change the regionid if you want the test to pass.
**/
/*
[Test]
public void UpdateTest () {
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
try {
ConnectionManager.Singleton.OpenConnection ();
DataTable dt = new DataTable();
SqlDataAdapter da = null;
da = new SqlDataAdapter("Select * from employee", conn);
//SqlCommandBuilder cb = new SqlCommandBuilder (da);
da.Fill(dt);
DataRow dr = dt.NewRow();
dr ["id"] = 6002;
dr ["fname"] = "boston";
dr ["dob"] = DateTime.Now.Subtract (new TimeSpan (20*365, 0, 0, 0));
dr ["doj"] = DateTime.Now;
dt.Rows.Add(dr);
da.Update(dt);
} finally {
DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
ConnectionManager.Singleton.CloseConnection ();
}
}
private static void OnRowUpdatedTest (object sender, SqlRowUpdatedEventArgs e)
{
rowUpdated = true;
}
private static void OnRowUpdatingTest (object sender, SqlRowUpdatingEventArgs e)
{
rowUpdating = true;
}
private static bool rowUpdated = false;
private static bool rowUpdating = false;
[Test]
public void RowUpdatedTest () {
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
try {
ConnectionManager.Singleton.OpenConnection ();
DataTable dt = null;
DataSet ds = new DataSet ();
SqlDataAdapter da = null;
da = new SqlDataAdapter("Select * from employee", conn);
//SqlCommandBuilder cb = new SqlCommandBuilder (da);
rowUpdated = false;
rowUpdating = false;
da.RowUpdated += new SqlRowUpdatedEventHandler (OnRowUpdatedTest);
da.RowUpdating += new SqlRowUpdatingEventHandler (OnRowUpdatingTest);
da.Fill (ds);
dt = ds.Tables [0];
dt.Rows[0][0] = 200;
da.UpdateCommand = new SqlCommand ("Update employee set id = @id");
da.Update (dt);
dt.Rows[0][0] = 1;
da.Update (dt);
da.RowUpdated -= new SqlRowUpdatedEventHandler (OnRowUpdatedTest);
da.RowUpdating -= new SqlRowUpdatingEventHandler (OnRowUpdatingTest);
Assert.AreEqual (true, rowUpdated, "RowUpdated");
Assert.AreEqual (true, rowUpdating, "RowUpdating");
} finally {
DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
ConnectionManager.Singleton.CloseConnection ();
}
}
*/
/**
This needs a errortable created as follows
id uniqueidentifier,name char(10) , with values
Guid name
{A12...} NULL
NULL bbbbbb
**/
[Test]
public void NullGuidTest()
{
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
try {
ConnectionManager.Singleton.OpenConnection ();
DBHelper.ExecuteNonQuery (conn, "create table #tmp_guid_table ( " +
" id uniqueidentifier default newid (), " +
" name char (10))");
DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (name) values (null)");
DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (id, name) values (null, 'bbbb')");
SqlDataAdapter da = new SqlDataAdapter("select * from #tmp_guid_table", conn);
DataSet ds = new DataSet();
da.Fill(ds);
Assert.AreEqual (1, ds.Tables.Count, "#1");
Assert.AreEqual (DBNull.Value, ds.Tables [0].Rows [1] ["id"], "#2");
} finally {
ConnectionManager.Singleton.CloseConnection ();
}
// the bug 68804 - is that the fill hangs!
Assert.AreEqual("Done","Done");
}
[Test]
public void DefaultConstructorTest ()
{
adapter = new SqlDataAdapter ();
Assert.AreEqual (MissingMappingAction.Passthrough,
adapter.MissingMappingAction,
"#1 Missing Mapping acttion default to Passthrough");
Assert.AreEqual (MissingSchemaAction.Add,
adapter.MissingSchemaAction,
"#2 Missing Schme action default to Add");
}
[Test]
public void OverloadedConstructorsTest ()
{
SqlCommand selCmd = new SqlCommand ("Select * from numeric_family");
adapter = new SqlDataAdapter (selCmd);
Assert.AreEqual (MissingMappingAction.Passthrough,
adapter.MissingMappingAction,
"#1 Missing Mapping acttion default to Passthrough");
Assert.AreEqual (MissingSchemaAction.Add,
adapter.MissingSchemaAction,
"#2 Missing Schme action default to Add");
Assert.AreSame (selCmd, adapter.SelectCommand,
"#3 Select Command shud be a ref to the arg passed");
conn = new SqlConnection (connectionString);
String selStr = "Select * from numeric_family";
adapter = new SqlDataAdapter (selStr, conn);
Assert.AreEqual (MissingMappingAction.Passthrough,
adapter.MissingMappingAction,
"#4 Missing Mapping acttion default to Passthrough");
Assert.AreEqual (MissingSchemaAction.Add,
adapter.MissingSchemaAction,
"#5 Missing Schme action default to Add");
Assert.AreSame (selStr, adapter.SelectCommand.CommandText,
"#6 Select Command shud be a ref to the arg passed");
Assert.AreSame (conn, adapter.SelectCommand.Connection,
"#7 cmd.connection shud be t ref to connection obj");
selStr = "Select * from numeric_family";
adapter = new SqlDataAdapter (selStr, connectionString);
Assert.AreEqual (MissingMappingAction.Passthrough,
adapter.MissingMappingAction,
"#8 Missing Mapping action shud default to Passthrough");
Assert.AreEqual (MissingSchemaAction.Add,
adapter.MissingSchemaAction,
"#9 Missing Schema action shud default to Add");
Assert.AreSame (selStr,
adapter.SelectCommand.CommandText,
"#10");
Assert.AreEqual (connectionString,
adapter.SelectCommand.Connection.ConnectionString,
"#11 ");
}
[Test]
public void Fill_Test_ConnState ()
{
//Check if Connection State is maintained correctly ..
data = new DataSet ("test1");
adapter = new SqlDataAdapter ("select id from numeric_family where id=1",
connectionString);
SqlCommand cmd = adapter.SelectCommand ;
Assert.AreEqual (ConnectionState.Closed,
cmd.Connection.State, "#1 Connection shud be in closed state");
adapter.Fill (data);
Assert.AreEqual (1, data.Tables.Count, "#2 One table shud be populated");
Assert.AreEqual (ConnectionState.Closed, cmd.Connection.State,
"#3 Connection shud be closed state");
data = new DataSet ("test2");
cmd.Connection.Open ();
Assert.AreEqual (ConnectionState.Open, cmd.Connection.State,
"#3 Connection shud be open");
adapter.Fill (data);
Assert.AreEqual (1, data.Tables.Count, "#4 One table shud be populated");
Assert.AreEqual (ConnectionState.Open, cmd.Connection.State,
"#5 Connection shud be open");
cmd.Connection.Close ();
// Test if connection is closed when exception occurs
cmd.CommandText = "select id1 from numeric_family";
try {
adapter.Fill (data);
} catch {
if (cmd.Connection.State == ConnectionState.Open) {
cmd.Connection.Close ();
Assert.Fail ("# Connection Shud be Closed");
}
}
}
[Test]
public void Fill_Test_Data ()
{
//Check if a table is created for each resultset
String batchQuery = "Select id,type_bit,type_int from numeric_family;";
batchQuery += "Select type_bit from numeric_family";
adapter = new SqlDataAdapter (batchQuery, connectionString);
data = new DataSet ("test1");
adapter.Fill (data);
Assert.AreEqual (2, data.Tables.Count,"#1 2 Table shud be created");
//Check if Table and Col are named correctly for unnamed columns
string query = "Select 10,20 from numeric_family;" ;
query += "Select 10,20 from numeric_family";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test2");
adapter.Fill (data);
Assert.AreEqual (2, data.Tables.Count,
"#2 2 Tables shud be created");
Assert.AreEqual ("Table", data.Tables[0].TableName, "#3");
Assert.AreEqual ("Table1", data.Tables[1].TableName, "#4");
Assert.AreEqual ("Column1", data.Tables[0].Columns[0].ColumnName, "#5");
Assert.AreEqual ("Column2", data.Tables[0].Columns[1].ColumnName, "#6");
Assert.AreEqual ("Column1", data.Tables[1].Columns[0].ColumnName, "#7");
Assert.AreEqual ("Column2", data.Tables[1].Columns[1].ColumnName, "#8");
//Check if dup columns are named correctly
query = "select A.id ,B.id , C.id from numeric_family A, ";
query += "numeric_family B , numeric_family C";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test3");
adapter.Fill (data);
// NOTE msdotnet contradicts documented behavior
// as per documentation the column names should be
// id1,id2,id3 .. but msdotnet returns id,id1,id2
Assert.AreEqual ("id", data.Tables[0].Columns[0].ColumnName,
"#9 if colname is duplicated ,shud be col,col1,col2 etc");
Assert.AreEqual ("id1", data.Tables[0].Columns[1].ColumnName,
"#10 if colname is duplicated ,shud be col,col1,col2 etc");
Assert.AreEqual ("id2", data.Tables[0].Columns[2].ColumnName,
"#11 if colname is duplicated ,shud be col,col1,col2 etc");
// Test if tables are created and named accordingly ,
// but only for those queries returning result sets
query = "update numeric_family set id=100 where id=50;";
query += "select * from numeric_family";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test4");
adapter.Fill (data);
Assert.AreEqual (1 ,data.Tables.Count,
"#12 Tables shud be named only for queries returning a resultset");
Assert.AreEqual ("Table", data.Tables[0].TableName,
"#13 The first resutlset shud have 'Table' as its name");
// Test behavior with an outerjoin
query = "select A.id,B.type_bit from numeric_family A LEFT OUTER JOIN ";
query += "numeric_family B on A.id = B.type_bit";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test5");
adapter.Fill (data);
Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
"#14 Primary Key shudnt be set if an outer join is performed");
Assert.AreEqual (0, data.Tables[0].Constraints.Count,
"#15 Constraints shudnt be set if an outer join is performed");
adapter = new SqlDataAdapter ("select id from numeric_family",
connectionString);
data = new DataSet ("test6");
adapter.Fill (data, 1, 1, "numeric_family");
Assert.AreEqual (1, data.Tables[0].Rows.Count, "#16");
Assert.AreEqual (2, data.Tables[0].Rows[0][0], "#17");
// only one test for DataTable.. DataSet tests covers others
adapter = new SqlDataAdapter ("select id from numeric_family",
connectionString);
DataTable table = new DataTable ("table1");
adapter.Fill (table);
Assert.AreEqual (4, table.Rows.Count , "#18");
}
[Test]
public void Fill_Test_PriKey ()
{
// Test if Primary Key & Constraints Collection is correct
adapter = new SqlDataAdapter ("select id,type_bit from numeric_family",
connectionString);
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
data = new DataSet ("test1");
adapter.Fill (data);
Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
"#1 Primary Key shud be set");
Assert.AreEqual (1, data.Tables[0].Constraints.Count,
"#2 Constraints shud be set");
Assert.AreEqual (4, data.Tables[0].Rows.Count,
"#3 No Of Rows shud be 4");
// Test if data is correctly merged
adapter.Fill (data);
Assert.AreEqual (4, data.Tables[0].Rows.Count,
"#4 No of Row shud still be 4");
// Test if rows are appended and not merged
// when primary key is not returned in the result-set
string query = "Select type_int from numeric_family";
adapter.SelectCommand.CommandText = query;
data = new DataSet ("test2");
adapter.Fill (data);
Assert.AreEqual (4, data.Tables[0].Rows.Count,
"#5 No of Rows shud be 4");
adapter.Fill (data);
Assert.AreEqual (8, data.Tables[0].Rows.Count,
"#6 No of Rows shud double now");
}
[Test]
public void Fill_Test_Exceptions ()
{
adapter = new SqlDataAdapter ("select * from numeric_family",
connectionString);
data = new DataSet ("test1");
try {
adapter.Fill (data, -1, 0, "numeric_family");
Assert.Fail ("#1 Exception shud be thrown:Incorrect Arguments");
}catch (AssertionException e){
throw e;
}catch (Exception e){
Assert.AreEqual (typeof(ArgumentException), e.GetType(),
"#2 Incorrect Exception : " + e);
}
// conn is not closed due to a bug..
// can be removed later
adapter.SelectCommand.Connection.Close ();
try {
adapter.Fill (data , 0 , -1 , "numeric_family");
Assert.Fail ("#3 Exception shud be thrown:Incorrect Arguments");
}catch (AssertionException e){
throw e;
}catch (Exception e){
Assert.AreEqual (typeof(ArgumentException), e.GetType(),
"#4 Incorrect Exception : " + e);
}
// conn is curr not closed.. can be removed later
adapter.SelectCommand.Connection.Close ();
/*
// NOTE msdotnet contradicts documented behavior
// InvalidOperationException is expected if table is not valid
try {
adapter.Fill (data , 0 , 0 , "invalid_talbe_name");
}catch (InvalidOperationException e) {
ex= e;
}catch (Exception e){
Assert.Fail ("#5 Exception shud be thrown : incorrect arugments ");
}
Assert.IsNotNull (ex , "#6 Exception shud be thrown : incorrect args ");
adapter.SelectCommand.Connection.Close (); // tmp .. can be removed once the bug if fixed
ex=null;
*/
try {
adapter.Fill ( null , 0 , 0 , "numeric_family");
Assert.Fail ( "#7 Exception shud be thrown : Invalid Dataset");
}catch (AssertionException e){
throw e ;
}catch (ArgumentNullException) {
}catch (Exception e) {
Assert.AreEqual (typeof(SystemException), e.GetType(),
"#8 Incorrect Exception : " + e);
}
// conn is currently not being closed..
//need to be removed once behavior is fixed
adapter.SelectCommand.Connection.Close ();
adapter.SelectCommand.Connection = null;
try {
adapter.Fill (data);
Assert.Fail ("#9 Exception shud be thrown : Invalid Connection");
}catch (AssertionException e){
throw e;
}catch (Exception e){
Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
"#10 Incorrect Exception : " + e);
}
}
bool FillErrorContinue = false;
[Test]
public void Fill_Test_FillErrorTest ()
{
string query = "select type_int from numeric_family where id=1 or id=4 ";
DataSet ds = new DataSet ();
DataTable table = ds.Tables.Add ("test");
table.Columns.Add ("col", typeof (short));
adapter = new SqlDataAdapter (query, connectionString);
DataTableMapping mapping = adapter.TableMappings.Add ("numeric_family", "test");
mapping.ColumnMappings.Add ("type_int", "col");
try {
adapter.Fill (ds, "numeric_family");
Assert.Fail ("#A1");
} catch (OverflowException) {
} catch (ArgumentException ex) {
// System.OverflowException: Value was either too large or too
// small for an Int16
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
Assert.IsNotNull (ex.InnerException, "#A3");
Assert.IsNotNull (ex.Message, "#A4");
Assert.IsNull (ex.ParamName, "#A5");
OverflowException inner = ex.InnerException as OverflowException;
Assert.IsNotNull (inner, "#A6");
Assert.AreEqual (typeof (OverflowException), inner.GetType (), "#A7");
Assert.IsNull (inner.InnerException, "#A8");
Assert.IsNotNull (inner.Message, "#A9");
}
Assert.AreEqual (0, ds.Tables [0].Rows.Count, "#A10");
adapter.FillError += new FillErrorEventHandler (ErrorHandler);
FillErrorContinue = false;
try {
adapter.Fill (ds, "numeric_family");
Assert.Fail ("#B1");
} catch (OverflowException) {
} catch (ArgumentException ex) {
// System.OverflowException: Value was either too large or too
// small for an Int16
Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
Assert.IsNotNull (ex.InnerException, "#B3");
Assert.IsNotNull (ex.Message, "#B4");
Assert.IsNull (ex.ParamName, "#B5");
OverflowException inner = ex.InnerException as OverflowException;
Assert.IsNotNull (inner, "#B6");
Assert.AreEqual (typeof (OverflowException), inner.GetType (), "#B7");
Assert.IsNull (inner.InnerException, "#B8");
Assert.IsNotNull (inner.Message, "#B9");
}
Assert.AreEqual (0, ds.Tables [0].Rows.Count, "#B10");
FillErrorContinue = true;
int count = adapter.Fill (ds, "numeric_family");
Assert.AreEqual (1, ds.Tables [0].Rows.Count, "#C1");
Assert.AreEqual (1, count, "#C2");
}
void ErrorHandler (object sender, FillErrorEventArgs args)
{
args.Continue = FillErrorContinue;
}
[Test]
public void GetFillParametersTest ()
{
string query = "select id, type_bit from numeric_family where id > @param1";
adapter = new SqlDataAdapter (query, connectionString);
IDataParameter[] param = adapter.GetFillParameters ();
Assert.AreEqual (0, param.Length, "#1 size shud be 0");
SqlParameter param1 = new SqlParameter ();
param1.ParameterName = "@param1";
param1.Value = 2;
adapter.SelectCommand.Parameters.Add (param1);
param = adapter.GetFillParameters ();
Assert.AreEqual (1, param.Length, "#2 count shud be 1");
Assert.AreEqual (param1, param[0], "#3 Params shud be equal");
}
[Test]
public void FillSchemaTest ()
{
string query;
// Test if connection is closed if excepton occurs during fill schema
query = "select * from invalid_table";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test");
try {
adapter.FillSchema (data , SchemaType.Source);
} catch {
if (adapter.SelectCommand.Connection.State != ConnectionState.Closed) {
Assert.Fail ("#0 Conn shud be closed if exception occurs");
adapter.SelectCommand.Connection.Close();
}
}
// Test Primary Key is set (since primary key column returned)
query = "select id, type_int from numeric_family where id=1";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test1");
adapter.FillSchema (data , SchemaType.Source);
Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
"#1 Primary Key property must be set");
// Test Primary Key is not set (since primary key column is returned)
query = "select type_bit, type_int from numeric_family where id=1";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test2");
adapter.FillSchema (data, SchemaType.Source);
Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
"#2 Primary Key property should not be set");
// Test multiple tables are created for a batch query
query = "Select id ,type_bit from numeric_family;" ;
query += "Select id,type_bit,type_int from numeric_family;";
data = new DataSet ("test3");
adapter = new SqlDataAdapter (query, connectionString);
adapter.FillSchema (data , SchemaType.Source);
Assert.AreEqual (2 , data.Tables.Count , "#3 A table shud be created for each Result Set");
Assert.AreEqual (2 , data.Tables[0].Columns.Count , "#4 should have 2 columns");
Assert.AreEqual (3 , data.Tables[1].Columns.Count , "#5 Should have 3 columns");
// Test if table names and column names are filled correctly
query = "select 10,20 from numeric_family;" ;
query += "select 10,20 from numeric_family;";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test4");
try {
adapter.FillSchema (data , SchemaType.Source);
}catch (Exception e){
Assert.Fail ("#3 Unexpected Exception : " + e);
}
Assert.AreEqual ( "Table", data.Tables[0].TableName);
Assert.AreEqual ( "Table1", data.Tables[1].TableName);
Assert.AreEqual ( "Column1", data.Tables[0].Columns[0].ColumnName,
"#6 Unnamed col shud be named as 'ColumnN'");
Assert.AreEqual ( "Column2", data.Tables[0].Columns[1].ColumnName,
"#7 Unnamed col shud be named as 'ColumnN'");
Assert.AreEqual ( "Column1", data.Tables[1].Columns[0].ColumnName,
"#8 Unnamed col shud be named as 'ColumnN'");
Assert.AreEqual ( "Column2", data.Tables[1].Columns[1].ColumnName,
"#9 Unnamed col shud be named as 'ColumnN'");
Assert.AreEqual (ConnectionState.Closed, adapter.SelectCommand.Connection.State,
"#10 Connection shud be closed");
// Test if mapping works correctly
// doesent work in both mono and msdotnet
// gotto check if something is wrong
/*
query = "select id,type_bit from numeric_family";
adapter = new SqlDataAdapter (query, connectionString);
data = new DataSet ("test");
DataTable table = data.Tables.Add ("numeric_family_1");
table.Columns.Add ("id");
table.Columns.Add ("type_bit");
DataTableMapping map = adapter.TableMappings.Add("numeric_family_1",
"numeric_family");
map.ColumnMappings.Add ("id", "id_1");
map.ColumnMappings.Add ("type_bit", "type_bit_1");
adapter.FillSchema (data, SchemaType.Source, "numeric_family");
foreach (DataTable tab in data.Tables){
Console.WriteLine ("Table == {0}",tab.TableName);
foreach (DataColumn col in tab.Columns)
Console.WriteLine (" Col = {0} " , col.ColumnName);
}
*/
}
[Test]
public void MissingSchemaActionTest ()
{
adapter = new SqlDataAdapter (
"select id,type_bit,type_int from numeric_family where id<=4",
connectionString);
data = new DataSet ();
Assert.AreEqual (MissingSchemaAction.Add, adapter.MissingSchemaAction,
"#1 Default Value");
adapter.Fill (data);
Assert.AreEqual (1, data.Tables.Count , "#1 One table shud be populated");
Assert.AreEqual (3, data.Tables[0].Columns.Count, "#2 Missing cols are added");
Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length, "#3 Default Value");
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
data.Reset();
adapter.Fill (data);
Assert.AreEqual (3, data.Tables[0].Columns.Count,
"#4 Missing cols are added");
Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length, "#5 Default Value");
adapter.MissingSchemaAction = MissingSchemaAction.Ignore ;
data.Reset ();
adapter.Fill (data);
Assert.AreEqual (0, data.Tables.Count, "#6 Data shud be ignored");
adapter.MissingSchemaAction = MissingSchemaAction.Error ;
data.Reset();
try {
adapter.Fill (data);
Assert.Fail ("#8 Exception shud be thrown: Schema Mismatch");
} catch (InvalidOperationException ex) {
Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(),
"#9");
}
// Test for invalid MissingSchema Value
try {
adapter.MissingSchemaAction = (MissingSchemaAction)(-5000);
Assert.Fail ("#10 Exception shud be thrown: Invalid Value");
} catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#11");
}
// Tests if Data is filled correctly if schema is defined
// manually and MissingSchemaAction.Error is set
adapter.MissingSchemaAction = MissingSchemaAction.Error;
data.Reset();
DataTable table = data.Tables.Add ("Table");
table.Columns.Add ("id");
table.Columns.Add ("type_bit");
table.Columns.Add ("type_int");
adapter.Fill (data);
Assert.AreEqual (1, data.Tables.Count, "#12");
Assert.AreEqual (4, data.Tables[0].Rows.Count, "#13");
}
[Test]
public void MissingMappingActionTest ()
{
adapter = new SqlDataAdapter ("select id,type_bit from numeric_family where id=1",
connectionString);
data = new DataSet ();
Assert.AreEqual (adapter.MissingMappingAction,
MissingMappingAction.Passthrough,
"#1 Default Value");
adapter.Fill(data);
Assert.AreEqual (1, data.Tables.Count,
"#2 One Table shud be created");
Assert.AreEqual (2, data.Tables[0].Columns.Count,
"#3 Two Cols shud be created");
adapter.MissingMappingAction = MissingMappingAction.Ignore;
data.Reset ();
adapter.Fill (data);
Assert.AreEqual (0, data.Tables.Count, "#4 No table shud be created");
adapter.MissingMappingAction = MissingMappingAction.Error;
data.Reset ();
try {
adapter.Fill (data);
Assert.Fail ("#5 Exception shud be thrown : Mapping is missing");
} catch (InvalidOperationException ex) {
Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(),
"#6");
}
try {
adapter.MissingMappingAction = (MissingMappingAction)(-5000);
Assert.Fail ("#7 Exception shud be thrown : Invalid Value");
} catch (ArgumentOutOfRangeException ex) {
Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (),
"#8");
}
// Test if mapping the column and table names works correctly
adapter.MissingMappingAction = MissingMappingAction.Error;
data.Reset ();
DataTable table = data.Tables.Add ("numeric_family_1");
table.Columns.Add ("id_1");
table.Columns.Add ("type_bit_1");
table.Columns.Add ("type_int_1");
DataTableMapping tableMap = adapter.TableMappings.Add ("numeric_family",
"numeric_family_1");
tableMap.ColumnMappings.Add ("id", "id_1");
tableMap.ColumnMappings.Add ("type_bit", "type_bit_1");
tableMap.ColumnMappings.Add ("type_int", "type_int_1");
adapter.Fill (data,"numeric_family");
Assert.AreEqual (1, data.Tables.Count ,
"#8 The DataTable shud be correctly mapped");
Assert.AreEqual (3, data.Tables[0].Columns.Count,
"#9 The DataColumns shud be corectly mapped");
Assert.AreEqual (1, data.Tables[0].Rows.Count,
"#10 Data shud be populated if mapping is correct");
}
[Test] // bug #76433
public void FillSchema_ValuesTest()
{
using (SqlConnection conn = new SqlConnection(connectionString)) {
conn.Open();
IDbCommand command = conn.CreateCommand();
// Create Temp Table
String cmd = "Create Table #tmp_TestTable (" ;
cmd += "Field1 DECIMAL (10) NOT NULL,";
cmd += "Field2 DECIMAL(19))";
command.CommandText = cmd;
command.ExecuteNonQuery();
DataSet dataSet = new DataSet();
string selectString = "SELECT * FROM #tmp_TestTable";
IDbDataAdapter dataAdapter = new SqlDataAdapter (
selectString, conn);
dataAdapter.FillSchema(dataSet, SchemaType.Mapped);
Assert.AreEqual (1, dataSet.Tables.Count, "#1");
Assert.IsFalse (dataSet.Tables[0].Columns[0].AllowDBNull,"#2");
Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull,"#3");
}
}
[Test]
public void Fill_CheckSchema ()
{
using (SqlConnection conn = new SqlConnection(connectionString)) {
conn.Open();
IDbCommand command = conn.CreateCommand();
// Create Temp Table
String cmd = "Create Table #tmp_TestTable (" ;
cmd += "id int primary key,";
cmd += "field int not null)";
command.CommandText = cmd;
command.ExecuteNonQuery();
DataSet dataSet = new DataSet();
string selectString = "SELECT * from #tmp_TestTable";
IDbDataAdapter dataAdapter = new SqlDataAdapter (
selectString,conn);
dataAdapter.Fill (dataSet);
Assert.AreEqual (1, dataSet.Tables.Count, "#A1");
Assert.AreEqual (2, dataSet.Tables [0].Columns.Count, "#A2");
Assert.IsTrue (dataSet.Tables [0].Columns [1].AllowDBNull, "#A3");
Assert.AreEqual (0, dataSet.Tables [0].PrimaryKey.Length, "#A4");
dataSet.Reset ();
dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dataAdapter.Fill (dataSet);
Assert.AreEqual (1, dataSet.Tables.Count, "#B1");
Assert.AreEqual (2, dataSet.Tables [0].Columns.Count, "#B2");
Assert.IsFalse (dataSet.Tables [0].Columns [1].AllowDBNull, "#B3");
if (ClientVersion == 7)
Assert.AreEqual (0, dataSet.Tables [0].PrimaryKey.Length, "#B4");
else
Assert.AreEqual (1, dataSet.Tables [0].PrimaryKey.Length, "#B4");
}
}
[Test]
public void FillSchema_CheckSchema ()
{
using (SqlConnection conn = new SqlConnection(connectionString)) {
conn.Open();
IDbCommand command = conn.CreateCommand();
// Create Temp Table
String cmd = "Create Table #tmp_TestTable (" ;
cmd += "id int primary key,";
cmd += "field int not null)";
command.CommandText = cmd;
command.ExecuteNonQuery();
DataSet dataSet = new DataSet();
string selectString = "SELECT * from #tmp_TestTable";
IDbDataAdapter dataAdapter = new SqlDataAdapter (
selectString,conn);
dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
dataSet.Reset ();
dataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#2");
dataSet.Reset ();
dataAdapter.MissingSchemaAction = MissingSchemaAction.Ignore;
dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
Assert.AreEqual (0, dataSet.Tables.Count, "#3");
dataSet.Reset ();
dataAdapter.MissingSchemaAction = MissingSchemaAction.Error;
try {
dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
Assert.Fail ("#4 Error should be thrown");
} catch (InvalidOperationException ex) {
Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#4");
}
}
}
[Test]
public void CreateViewSSPITest ()
{
SqlConnection conn = new SqlConnection (ConfigurationSettings.AppSettings ["SSPIConnString"]);
conn.Open ();
string sql = "create view MONO_TEST_VIEW as select * from Numeric_family";
SqlCommand dbcmd = new SqlCommand( sql, conn );
dbcmd.ExecuteNonQuery();
sql = "drop view MONO_TEST_VIEW";
dbcmd = new SqlCommand( sql, conn );
dbcmd.ExecuteNonQuery();
conn.Close();
}
[Test]
public void Fill_RelatedTables ()
{
SqlConnection conn = new SqlConnection(connectionString);
using (conn) {
conn.Open();
IDbCommand command = conn.CreateCommand();
DataSet dataSet = new DataSet();
string selectString = "SELECT id, type_int from numeric_family where id < 3";
DbDataAdapter dataAdapter = new SqlDataAdapter (selectString,conn);
DataTable table2 = dataSet.Tables.Add ("table2");
DataColumn ccol1 = table2.Columns.Add ("id", typeof (int));
DataColumn ccol2 = table2.Columns.Add ("type_int", typeof (int));
DataTable table1 = dataSet.Tables.Add ("table1");
DataColumn pcol1 = table1.Columns.Add ("id", typeof (int));
DataColumn pcol2 = table1.Columns.Add ("type_int", typeof (int));
table2.Constraints.Add ("fk", pcol1, ccol1);
//table1.Constraints.Add ("fk1", pcol2, ccol2);
dataSet.EnforceConstraints = false;
dataAdapter.Fill (dataSet, "table1");
dataAdapter.Fill (dataSet, "table2");
//Should not throw an exception
dataSet.EnforceConstraints = true;
Assert.AreEqual (2, table1.Rows.Count, "#1");
Assert.AreEqual (2, table2.Rows.Count, "#2");
}
}
[Test]
public void UpdateBatchSizeTest ()
{
adapter = new SqlDataAdapter();
Assert.AreEqual (1, adapter.UpdateBatchSize, "#1 The default value should be 1");
adapter.UpdateBatchSize = 3;
Assert.AreEqual (3, adapter.UpdateBatchSize, "#2 The value should be 3 after setting the property UpdateBatchSize to 3");
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void UpdateBatchSizeArgumentOutOfRangeTest ()
{
adapter = new SqlDataAdapter();
adapter.UpdateBatchSize = -2;
}
int ClientVersion {
get {
return (engine.ClientVersion);
}
}
}
[TestFixture]
[Category ("sqlserver")]
public class SqlDataAdapterInheritTest : DbDataAdapter
{
SqlConnection conn = null;
[Test]
public void FillDataAdapterTest () {
conn = (SqlConnection) ConnectionManager.Singleton.Connection;
try {
ConnectionManager.Singleton.OpenConnection ();
DataTable dt = new DataTable();
SqlCommand command = new SqlCommand ();
command.CommandText = "Select * from employee;";
command.Connection = conn;
SelectCommand = command;
Fill (dt, command.ExecuteReader ());
Assert.AreEqual (4, dt.Rows.Count, "#1");
Assert.AreEqual (6, dt.Columns.Count, "#2");
} finally {
DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
ConnectionManager.Singleton.CloseConnection ();
}
}
}
}
|