Make a copy of the old ZipLib
[mono.git] / mcs / class / System.Data / Test / ProviderTests / System.Data.SqlClient / SqlDataReaderTest.cs
1 //
2 // SqlDataReaderTest.cs - NUnit Test Cases for testing the
3 //                          SqlDataReader class
4 // Author:
5 //      Umadevi S (sumadevi@novell.com)
6 //      Kornél Pál <http://www.kornelpal.hu/>
7 //      Sureshkumar T (tsureshkumar@novell.com)
8 //      Senganal T (tsenganal@novell.com)
9 //
10 // Copyright (c) 2004 Novell Inc., and the individuals listed
11 // on the ChangeLog entries.
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System;
34 using System.Data;
35 using System.Text;
36 using System.Data.SqlTypes;
37 using System.Data.Common;
38 using System.Data.SqlClient;
39
40 using NUnit.Framework;
41
42 namespace MonoTests.System.Data.SqlClient
43 {
44
45         [TestFixture]
46         [Category ("sqlserver")]
47         public class SqlDataReaderTest 
48         {
49                 SqlConnection conn = null; 
50                 SqlCommand cmd = null;
51                 SqlDataReader reader = null; 
52                 String query = "Select type_{0},type_{1},convert({0},null) from numeric_family where id=1";
53                 DataSet sqlDataset = null; 
54
55                 DataTable numericDataTable =null;
56                 DataTable stringDataTable =null;
57                 DataTable binaryDataTable =null;
58                 DataTable datetimeDataTable =null;
59
60                 DataRow numericRow = null; 
61                 DataRow stringRow = null; 
62                 DataRow binaryRow = null; 
63                 DataRow datetimeRow = null; 
64
65                 [TestFixtureSetUp]
66                 public void init ()
67                 {
68                         conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
69                         cmd = conn.CreateCommand ();
70                         
71                         sqlDataset = (new DataProvider()).GetDataSet ();
72
73                         numericDataTable = sqlDataset.Tables["numeric_family"];
74                         stringDataTable = sqlDataset.Tables["string_family"];
75                         binaryDataTable = sqlDataset.Tables["binary_family"];
76                         datetimeDataTable = sqlDataset.Tables["datetime_family"];
77
78                         numericRow = numericDataTable.Select ("id=1")[0];
79                         stringRow = stringDataTable.Select ("id=1")[0];
80                         binaryRow = binaryDataTable.Select ("id=1")[0];
81                         datetimeRow = datetimeDataTable.Select ("id=1")[0];
82                 }
83
84                 [SetUp]
85                 public void Setup ()
86                 {
87                         conn.Open ();
88                 }
89                 [TearDown]
90                 public void TearDown ()
91                 {
92                         if (reader != null)
93                                 reader.Close ();
94
95                         conn.Close ();
96                 }
97                 
98                 [Test]
99                 public void ReadEmptyNTextFieldTest () {
100                         try {
101                                 DBHelper.ExecuteNonQuery (conn, "create table #tmp_monotest (name ntext)");
102                                 DBHelper.ExecuteNonQuery (conn, "insert into #tmp_monotest values ('')");
103                                 
104                                 SqlCommand cmd = (SqlCommand) conn.CreateCommand ();
105                                 cmd.CommandText = "select * from #tmp_monotest";
106                                 SqlDataReader dr = cmd.ExecuteReader ();
107                                 if (dr.Read()) {
108                                         Assert.AreEqual("System.String",dr["NAME"].GetType().FullName);
109                                 }
110                                 Assert.AreEqual (false, dr.Read (), "#2");
111                         } finally {
112                                 ConnectionManager.Singleton.CloseConnection ();
113                         }
114                 }               
115
116                 [Test]
117                 public void ReadBingIntTest() 
118                 {
119                         try {
120                                 string query = "SELECT CAST(548967465189498 AS bigint) AS Value";
121                                 SqlCommand cmd = new SqlCommand();
122                                 cmd.Connection = conn;
123                                 cmd.CommandText = query;
124                                 SqlDataReader r = cmd.ExecuteReader();
125                                 using (r) {
126                                         Assert.AreEqual (true, r.Read(), "#1");
127                                         long id = r.GetInt64(0);
128                                         Assert.AreEqual(548967465189498, id, "#2");
129                                         id = r.GetSqlInt64(0).Value;
130                                         Assert.AreEqual(548967465189498, id, "#3");
131                                 }
132                         } finally {
133                                 ConnectionManager.Singleton.CloseConnection ();
134                         }
135                 }
136
137
138
139                 // This method just helps in Calling common tests among all the Get* Methods 
140                 // without replicating code 
141
142                 void CallGetMethod (string s, int i)
143                 {
144                         switch (s) {
145                         case "Boolean" : reader.GetBoolean (i) ; break; 
146                         case "SqlBoolean": reader.GetSqlBoolean (i); break;
147                         case "Int16" : reader.GetInt16 (i); break;
148                         case "SqlInt16" : reader.GetSqlInt16 (i); break;
149                         case "Int32" : reader.GetInt32 (i);break;
150                         case "SqlInt32" : reader.GetSqlInt32(i);break;
151                         case "Int64" : reader.GetInt64 (i);break;
152                         case "SqlInt64" : reader.GetSqlInt64(i); break;
153                         case "Decimal" : reader.GetDecimal(i);break;
154                         case "SqlDecimal" : reader.GetSqlDecimal (i);break;
155                         case "SqlMoney" : reader.GetSqlMoney (i);break;
156                         case "Float" : reader.GetFloat (i);break;
157                         case "SqlSingle" : reader.GetSqlSingle(i);break;
158                         case "Double" : reader.GetDouble (i);break;
159                         case "SqlDouble" : reader.GetSqlDouble(i);break;
160                         case "Guid" : reader.GetGuid(i);break;
161                         case "SqlGuid" : reader.GetSqlGuid(i);break;
162                         case "String" : reader.GetString(i);break;
163                         case "SqlString" : reader.GetSqlString(i);break;
164                         case "Char" : reader.GetChar(i);break;
165                         case "Byte" : reader.GetByte (i);break;
166                         case "SqlByte" : reader.GetSqlByte(i); break;
167                         case "DateTime" : reader.GetDateTime(i); break;
168                         case "SqlDateTime" : reader.GetSqlDateTime(i); break;
169                         case "SqlBinary" : reader.GetSqlBinary(i); break;
170                         default : Console.WriteLine ("OOOOPSSSSSS {0}",s);break;
171                         }
172                 }
173
174                 // This method just helps in Calling common tests among all the Get* Methods 
175                 // without replicating code 
176                 void GetMethodTests (string s)
177                 {
178
179                         try {
180                                 CallGetMethod (s, 1);
181                                 Assert.Fail ("#1[Get"+s+"] InvalidCastException must be thrown");       
182                         }catch (AssertionException e) {
183                                 throw e;                
184                         }catch (Exception e) {
185                                 Assert.AreEqual (typeof(InvalidCastException), e.GetType(),
186                                         "#2[Get"+s+"] Incorrect Exception : " + e);
187                         }
188                 
189                         // GetSql* Methods do not throw SqlNullValueException   
190                         // So, Testimg only for Get* Methods 
191                         if (!s.StartsWith("Sql")) {
192                                 try {
193                                         CallGetMethod (s, 2);
194                                         Assert.Fail ("#3[Get"+s+"] Exception must be thrown");  
195                                 }catch (AssertionException e) {
196                                         throw e;
197                                 }catch (Exception e){
198                                         Assert.AreEqual (typeof(SqlNullValueException),e.GetType(),
199                                                 "#4[Get"+s+"] Incorrect Exception : " + e);
200                                 }
201                         }
202
203                         try {
204                                 CallGetMethod (s, 3);
205                                 Assert.Fail ("#5[Get"+s+"] IndexOutOfRangeException must be thrown");
206                         }catch (AssertionException e) {
207                                 throw e;
208                         }catch (Exception e){
209                                 Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
210                                         "#6[Get"+s+"] Incorrect Exception : " + e);
211                         }
212                 }
213
214                 [Test]
215                 public void GetBooleanTest ()
216                 {
217                         cmd.CommandText = string.Format (query, "bit", "int");
218                         reader = cmd.ExecuteReader ();
219                         reader.Read ();
220                         // Test for standard exceptions 
221                         GetMethodTests("Boolean");
222
223                         // Test if data is returned correctly
224                         Assert.AreEqual (numericRow["type_bit"], reader.GetBoolean(0),
225                                                 "#2 DataValidation Failed");
226                         
227                         // Test for standard exceptions 
228                         GetMethodTests("SqlBoolean");
229
230                         // Test if data is returned correctly
231                         Assert.AreEqual (numericRow["type_bit"], reader.GetSqlBoolean(0).Value,
232                                 "#4 DataValidation Failed");    
233                         reader.Close ();
234                 }
235
236                 [Test]
237                 public void GetByteTest ()
238                 {
239                         cmd.CommandText = string.Format (query, "tinyint", "int");
240                         reader = cmd.ExecuteReader ();
241                         reader.Read ();
242                         // Test for standard exceptions 
243                         GetMethodTests("Byte");
244
245                         // Test if data is returned correctly
246                         Assert.AreEqual (numericRow["type_tinyint"], reader.GetByte(0),
247                                                 "#2 DataValidation Failed");
248
249                         // Test for standard exceptions 
250                         GetMethodTests("SqlByte");
251
252                         // Test if data is returned correctly
253                         Assert.AreEqual (numericRow["type_tinyint"], reader.GetSqlByte(0).Value,
254                                                 "#4 DataValidation Failed");
255                         reader.Close ();
256                 }
257
258                 [Test]
259                 public void GetInt16Test ()
260                 {
261                         cmd.CommandText = string.Format (query, "smallint", "int");
262                         reader = cmd.ExecuteReader();
263                         reader.Read ();
264                         // Test for standard exceptions 
265                         GetMethodTests("Int16");
266
267                         // Test if data is returned correctly
268                         Assert.AreEqual (numericRow["type_smallint"], reader.GetInt16(0),
269                                                 "#2 DataValidation Failed");
270
271                         // Test for standard exceptions 
272                         GetMethodTests("SqlInt16");
273
274                         // Test if data is returned correctly
275                         Assert.AreEqual (numericRow["type_smallint"], reader.GetSqlInt16(0).Value,
276                                                 "#4 DataValidation Failed");
277                         reader.Close ();
278                 }
279
280                 [Test]
281                 public void GetInt32Test ()
282                 {
283                         cmd.CommandText = string.Format (query, "int", "bigint");
284                         reader = cmd.ExecuteReader ();
285                         reader.Read ();
286                         // Test for standard exceptions 
287                         GetMethodTests("Int32");
288
289                         // Test if data is returned correctly
290                         Assert.AreEqual (numericRow["type_int"], reader.GetInt32(0),
291                                 "#2 DataValidation Failed");
292
293                         // Test for standard exceptions 
294                         GetMethodTests("SqlInt32");
295
296                         // Test if data is returned correctly
297                         Assert.AreEqual (numericRow["type_int"], reader.GetSqlInt32(0).Value,
298                                 "#4 DataValidation Failed");
299                         reader.Close ();
300                 }
301
302                 [Test]
303                 public void GetInt64Test ()
304                 {
305                         cmd.CommandText = string.Format (query, "bigint", "int");
306                         reader = cmd.ExecuteReader ();
307                         reader.Read ();
308
309                         // Test for standard exceptions 
310                         GetMethodTests("Int64");
311
312                         // Test if data is returned correctly
313                         Assert.AreEqual (numericRow["type_bigint"], reader.GetInt64(0),
314                                 "#2 DataValidation Failed");
315
316                         // Test for standard exceptions 
317                         GetMethodTests("SqlInt64");
318
319                         // Test if data is returned correctly
320                         Assert.AreEqual (numericRow["type_bigint"], reader.GetSqlInt64(0).Value,
321                                 "#4 DataValidation Failed");
322                         reader.Close ();
323                 }
324
325                 [Test]
326                 public void GetDecimalTest ()
327                 {
328                         cmd.CommandText = string.Format (query, "decimal", "int");
329                         reader = cmd.ExecuteReader ();
330                         reader.Read ();
331                         // Test for standard exceptions 
332                         GetMethodTests("Decimal");
333
334                         // Test if data is returned correctly
335                         Assert.AreEqual (numericRow["type_decimal"], reader.GetDecimal(0),
336                                 "#2 DataValidation Failed");
337
338                         // Test for standard exceptions 
339                         GetMethodTests("SqlDecimal");
340
341                         // Test if data is returned correctly
342                         Assert.AreEqual (numericRow["type_decimal"], reader.GetSqlDecimal(0).Value,
343                                 "#4 DataValidation Failed");
344                         reader.Close ();
345                 }
346
347                 [Test]
348                 public void GetSqlMoneyTest ()
349                 {
350                         cmd.CommandText = string.Format (query, "money", "int");
351                         reader = cmd.ExecuteReader ();
352                         reader.Read ();
353                         // Test for standard exceptions 
354                         GetMethodTests("SqlMoney");
355
356                         // Test if data is returned correctly
357                         Assert.AreEqual (numericRow["type_money"], reader.GetSqlMoney(0).Value,
358                                 "#2 DataValidation Failed");
359                         reader.Close ();
360                 }
361
362                 [Test]
363                 public void GetFloatTest ()
364                 {
365                         cmd.CommandText = "select type_float,type_double,convert(real,null)";
366                         cmd.CommandText += "from numeric_family where id=1"; 
367                         reader = cmd.ExecuteReader ();
368                         reader.Read ();
369                         // Test for standard exceptions 
370                         GetMethodTests("Float");
371
372                         // Test if data is returned correctly
373                         Assert.AreEqual (numericRow["type_float"], reader.GetFloat(0),
374                                 "#2 DataValidation Failed");
375
376                         // Test for standard exceptions 
377                         GetMethodTests("SqlSingle");
378
379                         // Test if data is returned correctly
380                         Assert.AreEqual (numericRow["type_float"], reader.GetSqlSingle(0).Value,
381                                 "#2 DataValidation Failed");
382                         reader.Close ();
383                 } 
384
385                 [Test]
386                 public void GetDoubleTest ()
387                 {
388                         cmd.CommandText = "select type_double,type_float,convert(float,null)";
389                         cmd.CommandText += " from numeric_family where id=1"; 
390                         reader = cmd.ExecuteReader ();
391                         reader.Read ();
392                         // Test for standard exceptions 
393                         GetMethodTests("Double");
394
395                         // Test if data is returned correctly
396                         Assert.AreEqual (numericRow["type_double"], reader.GetDouble(0),
397                                 "#2 DataValidation Failed");
398
399                         // Test for standard exceptions 
400                         GetMethodTests("SqlDouble");
401
402                         // Test if data is returned correctly
403                         Assert.AreEqual (numericRow["type_double"], reader.GetSqlDouble(0).Value,
404                                 "#4 DataValidation Failed");
405                         reader.Close ();
406                 }
407
408                 [Test]
409                 public void GetBytesTest ()
410                 {
411                         cmd.CommandText = "Select type_text,type_ntext,convert(text,null) ";
412                         cmd.CommandText += "from string_family where id=1";
413                         reader = cmd.ExecuteReader ();
414                         reader.Read ();
415                         try {
416                                 reader.GetBytes (0,0,null,0,0); 
417                                 Assert.Fail ("#1 GetBytes shud be used only wth Sequential Access");
418                         }catch (AssertionException e) {
419                                 throw e;
420                         }catch (Exception e) {
421                                 Assert.AreEqual (typeof(InvalidCastException), e.GetType (),
422                                         "#2 Incorrect Exception : " + e);
423                         }
424                         reader.Close ();
425                         
426                         byte[] asciiArray = (new ASCIIEncoding ()).GetBytes ("text");
427                         byte[] unicodeArray = (new UnicodeEncoding ()).GetBytes ("ntext");
428                         byte[] buffer = null ;
429                         long size = 0; 
430
431                         reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);  
432                         reader.Read ();
433                         size = reader.GetBytes (0,0,null,0,0);
434                         Assert.AreEqual (asciiArray.Length, size, "#3 Data Incorrect");
435
436                         buffer = new byte[size];
437                         size = reader.GetBytes (0,0,buffer,0,(int)size);
438                         for (int i=0;i<size; i++)
439                                 Assert.AreEqual (asciiArray[i], buffer[i], "#4 Data Incorrect");
440
441                         size = reader.GetBytes (1,0,null,0,0);
442                         Assert.AreEqual (unicodeArray.Length, size, "#5 Data Incorrect");
443                         buffer = new byte[size];
444                         size = reader.GetBytes (1,0,buffer,0,(int)size);
445                         for (int i=0;i<size; i++)
446                                 Assert.AreEqual (unicodeArray[i], buffer[i], "#6 Data Incorrect");
447                         
448                         // Test if msdotnet behavior s followed when null value 
449                         // is read using GetBytes 
450                         Assert.AreEqual (0, reader.GetBytes (2,0,null,0,0), "#7");
451                         reader.GetBytes (2,0,buffer,0,10);
452
453                         reader.Close ();
454                         // do i need to test for image/binary values also ??? 
455                 }
456
457                 [Test]
458                 public void GetStringTest ()
459                 {
460                         cmd.CommandText = "Select type_varchar,10,convert(varchar,null)";
461                         cmd.CommandText += "from string_family where id=1";
462                         reader = cmd.ExecuteReader ();
463                         reader.Read ();
464                         // Test for standard exceptions 
465                         GetMethodTests("String");
466
467                         // Test if data is returned correctly
468                         Assert.AreEqual (stringRow["type_varchar"], reader.GetString(0),
469                                 "#2 DataValidation Failed");
470
471                         // Test for standard exceptions 
472                         GetMethodTests("SqlString");
473
474                         // Test if data is returned correctly
475                         Assert.AreEqual (stringRow["type_varchar"], reader.GetSqlString(0).Value,
476                                 "#4 DataValidation Failed");
477                         reader.Close();
478                 }
479
480                 [Test]
481                 public void GetSqlBinaryTest ()
482                 {
483                         cmd.CommandText = "Select type_binary ,10 ,convert(binary,null)";
484                         cmd.CommandText += "from binary_family where id=1";
485                         reader = cmd.ExecuteReader ();
486                         reader.Read ();
487                         // Test for standard exceptions         
488                         GetMethodTests ("SqlBinary");
489
490                         // Test if data is returned correctly
491                         Assert.AreEqual (binaryRow["type_binary"], reader.GetSqlBinary(0).Value,
492                                 "#2 DataValidation Failed");
493                         reader.Close ();
494                 }
495
496                 [Test]
497                 public void GetGuidTest ()
498                 {
499                         cmd.CommandText = "Select type_guid,id,convert(uniqueidentifier,null)";
500                         cmd.CommandText += "from string_family where id=1";
501                         reader = cmd.ExecuteReader ();
502                         reader.Read ();
503
504                         // Test for standard exceptions 
505                         GetMethodTests("Guid");
506
507                         // Test if data is returned correctly
508                         Assert.AreEqual (stringRow["type_guid"], reader.GetGuid(0),
509                                 "#2 DataValidation Failed");
510
511                         // Test for standard exceptions 
512                         GetMethodTests("SqlGuid");
513
514                         // Test if data is returned correctly
515                         Assert.AreEqual (stringRow["type_guid"], reader.GetSqlGuid(0).Value,
516                                 "#4 DataValidation Failed");
517                         reader.Close ();
518                 }
519
520                 [Test]
521                 public void GetDateTimeTest ()
522                 {
523                         cmd.CommandText = "Select type_datetime,10,convert(datetime,null)";
524                         cmd.CommandText += "from datetime_family where id=1";
525                         reader = cmd.ExecuteReader ();
526                         reader.Read ();
527
528                         // Test for standard exceptions 
529                         GetMethodTests("DateTime");
530
531                         // Test if data is returned correctly
532                         Assert.AreEqual (datetimeRow["type_datetime"], reader.GetDateTime(0),
533                                 "#2 DataValidation Failed");
534
535                         // Test for standard exceptions 
536                         GetMethodTests("SqlDateTime");
537
538                         // Test if data is returned correctly
539                         Assert.AreEqual (datetimeRow["type_datetime"], reader.GetSqlDateTime(0).Value,
540                                 "#2 DataValidation Failed");
541                         reader.Close ();
542                 }
543
544                 [Test]
545                 [Ignore ("Not Supported by msdotnet")]
546                 public void GetCharTest ()
547                 {
548                         cmd.CommandText = "Select type_char,type_guid,convert(char,null)"; 
549                         cmd.CommandText += "from string_family where id=1";
550                         reader = cmd.ExecuteReader ();
551                         reader.Read ();
552                         // Test for standard exceptions
553                         GetMethodTests ("Char");
554                         reader.Close ();
555                 }
556
557                 [Test]
558                 public void GetValueTest ()
559                 {
560                         cmd.CommandText = "Select id, null from numeric_family where id=1";
561                         reader = cmd.ExecuteReader ();
562                         reader.Read ();
563
564                         object obj = null; 
565                         obj = reader.GetValue (0);
566                         Assert.AreEqual ((byte)1, obj, "#1 Shud return the value of id");
567                         obj = reader.GetValue (1);
568                         Assert.AreEqual (DBNull.Value, obj, "#2 shud return DBNull");
569                         reader.Close ();
570                 }
571
572                 [Test]
573                 public void GetSqlValueTest ()
574                 {
575                         cmd.CommandText = "Select id, type_tinyint, null from numeric_family where id=1";
576                         reader = cmd.ExecuteReader ();
577                         reader.Read ();
578
579                         Assert.AreEqual ((byte)255, ((SqlByte) reader.GetSqlValue(1)).Value, "#1");
580                         //Assert.AreEqual (DBNull.Value, reader.GetSqlValue(2), "#2");
581
582                         reader.Close ();
583                 }
584
585                 [Test]
586                 public void GetValuesTest ()
587                 {
588                         cmd.CommandText = "Select 10,20,30 from numeric_family where id=1";
589                         reader = cmd.ExecuteReader ();
590                         reader.Read ();
591                         object[] arr = null;
592                         int count = 0; 
593
594                         arr = new object[1];
595                         count = reader.GetValues (arr);
596                         Assert.AreEqual (10, (int)arr[0], "#1 Only first object shud be copied");
597                         Assert.AreEqual (1, count, "#1 return value shud equal objects copied");
598
599                         arr = new object[3];
600                         count = reader.GetValues (arr);
601                         Assert.AreEqual (3, count, "#2 return value shud equal objects copied");
602
603                         arr = new object[5];
604                         count = reader.GetValues (arr);
605                         Assert.AreEqual (3, count, "#3 return value shud equal objects copied");
606                         Assert.IsNull (arr[3], "#4 Only 3 objects shud be copied");
607
608                         reader.Close ();
609                 }
610
611                 [Test]
612                 public void GetSqlValuesTest ()
613                 {
614                         cmd.CommandText = "Select 10,20,30 from numeric_family where id=1";
615                         reader = cmd.ExecuteReader ();
616                         reader.Read ();
617                         object[] arr = null;
618                         int count = 0; 
619
620                         arr = new object[1];
621                         count = reader.GetSqlValues (arr);
622                         // Something is wrong with types ... gotta figure it out 
623                         //Assert.AreEqual (10, arr[0], "#1 Only first object shud be copied");
624                         Assert.AreEqual (1, count, "#1 return value shud equal objects copied");
625
626                         arr = new object[3];
627                         count = reader.GetSqlValues (arr);
628                         Assert.AreEqual (3, count, "#2 return value shud equal objects copied");
629
630                         arr = new object[5];
631                         count = reader.GetSqlValues (arr);
632                         Assert.AreEqual (3, count, "#3 return value shud equal objects copied");
633                         Assert.IsNull (arr[3], "#4 Only 3 objects shud be copied");
634
635                         reader.Close ();
636                 }
637
638                 [Test]
639                 public void isDBNullTest ()
640                 {
641                         cmd.CommandText = "select id , null from numeric_family where id=1";
642                         reader = cmd.ExecuteReader ();
643                         reader.Read ();
644
645                         Assert.IsFalse (reader.IsDBNull (0), "#1");
646                         Assert.IsTrue (reader.IsDBNull (1) , "#2");
647
648                         try {
649                                 reader.IsDBNull (10);
650                                 Assert.Fail ("#1 Invalid Argument");
651                         }catch (AssertionException e) {
652                                 throw e;
653                         }catch (Exception e) {
654                                 Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
655                                         "#1 Incorrect Exception : " + e); 
656                         }
657                 }
658
659                 [Test]
660                 public void ReadTest ()
661                 {
662                         cmd.CommandText = "select id, type_bit from numeric_family where id=1" ;
663                         reader = cmd.ExecuteReader ();
664                         Assert.IsTrue (reader.Read () , "#1");
665                         Assert.IsFalse (reader.Read (), "#2");
666                         reader.Close ();
667
668                         try {
669                                 reader.Read ();
670                                 Assert.Fail ("#3 Exception shud be thrown : Reader is closed");
671                         }catch (AssertionException e) {
672                                 throw e;
673                         }catch (Exception e) {
674                                 Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
675                                         "#4 Incorrect Exception : " + e);
676                         }
677                 }
678
679                 [Test]
680                 public void NextResultTest ()
681                 {
682                         cmd.CommandText = "Select id from numeric_family where id=1";
683                         reader = cmd.ExecuteReader ();
684                         Assert.IsFalse (reader.NextResult (), "#1");
685                         reader.Close ();
686
687                         cmd.CommandText = "select id from numeric_family where id=1;";
688                         cmd.CommandText += "select type_bit from numeric_family where id=2;";
689                         reader = cmd.ExecuteReader ();
690                         Assert.IsTrue (reader.NextResult (), "#2");
691                         Assert.IsFalse (reader.NextResult (), "#3");
692                         reader.Close ();
693
694                         try {
695                                 reader.NextResult ();
696                                 Assert.Fail ("#4 Exception shud be thrown : Reader is closed");
697                         }catch (AssertionException e) {
698                                 throw e;
699                         }catch (Exception e) {
700                                 Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
701                                         "#5 Incorrect Exception : " + e);
702                         }
703                 }
704
705                 [Test]
706                 public void GetNameTest ()
707                 {
708                         cmd.CommandText = "Select id,10 as gen,20 from numeric_family where id=1";
709                         reader = cmd.ExecuteReader ();
710
711                         Assert.AreEqual ("id" , reader.GetName(0) , "#1");
712                         Assert.AreEqual ("gen" , reader.GetName(1) , "#2");
713
714                         try {
715                                 reader.GetName (3);
716                                 Assert.Fail ("#4 Exception shud be thrown");
717                         }catch (AssertionException e) {
718                                 throw e;
719                         }catch (Exception e) {
720                                 Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
721                                         "#5 Incorrect Exception : " + e);
722                         }
723                 }
724
725                 [Test]
726                 public void GetOrdinalTest ()
727                 {
728                         //what is kana-width insensitive ????? 
729                         cmd.CommandText = "Select id,10 as gen,20 from numeric_family where id=1";
730                         reader = cmd.ExecuteReader ();
731
732                         Assert.AreEqual (0, reader.GetOrdinal ("id"), "#1");
733                         Assert.AreEqual (0, reader.GetOrdinal ("ID"), "#2");
734                         Assert.AreEqual (1, reader.GetOrdinal ("gen"), "#3");
735                         // Would expect column1,columnn2 etc for unnamed columns,
736                         // but msdotnet return empty string for unnamed columns
737                         Assert.AreEqual (2, reader.GetOrdinal (""), "#4");
738
739                         try {
740                                 reader.GetOrdinal ("invalidname");
741                         }catch (AssertionException e) {
742                                 throw e;
743                         }catch (Exception e) {
744                                 Assert.AreEqual (typeof (IndexOutOfRangeException),
745                                         e.GetType(), "#4 Incorrect Exception : " + e);
746                         }
747                 }
748
749                 [Test]
750                 public void GetSchemaTableTest ()
751                 {
752                         cmd.CommandText = "Select type_decimal as decimal,id,10 ";
753                         cmd.CommandText += "from numeric_family where id=1";
754                         reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
755                         DataTable schemaTable  = reader.GetSchemaTable ();
756                         DataRow row0 = schemaTable.Rows[0]; 
757                         DataRow row1 = schemaTable.Rows[1]; 
758                 
759                         Assert.AreEqual ("decimal", row0["ColumnName"], "#1");
760                         Assert.AreEqual ("", schemaTable.Rows[2]["ColumnName"], "#2");
761
762                         Assert.AreEqual (0, row0["ColumnOrdinal"], "#2");
763                         Assert.AreEqual (17, row0["ColumnSize"], "#3");
764                         Assert.AreEqual (38, row0["NumericPrecision"], "#4"); 
765                         Assert.AreEqual (0, row0["NumericScale"], "#5");
766
767                         Assert.AreEqual (false, row0["IsUnique"], "#6"); 
768                         // msdotnet returns IsUnique as false for Primary key
769                         // even though table consists of a single Primary Key
770                         //Assert.AreEqual (true, row1["IsUnique"], "#7"); 
771                         Assert.AreEqual (false, row0["IsKey"], "#8"); 
772                         Assert.AreEqual (true, row1["IsKey"], "#9"); 
773
774                         //Assert.AreEqual ("servername", row0["BaseServerName"], "#10");
775                         //Assert.AreEqual ("monotest", row0["BaseCatalogName"], "#11");  
776                         Assert.AreEqual ("type_decimal", row0["BaseColumnName"], "#12");
777                         //Assert.IsNull(row0["BaseSchemaName"], "#13");
778                         Assert.AreEqual ("numeric_family", row0["BaseTableName"], "#14");
779                         Assert.AreEqual (typeof (Decimal), row0["DataType"], "#15"); 
780                         Assert.AreEqual (true, row0["AllowDBNull"], "#16");
781                         Assert.AreEqual (false, row1["AllowDBNull"], "#17");
782                         //Assert.IsNull(row0["ProviderType"], "#18");
783                         Assert.AreEqual (true, row0["IsAliased"], "#19");
784                         Assert.AreEqual (false, row1["IsAliased"], "#20");
785
786                         Assert.AreEqual (false, row0["IsExpression"], "#21"); 
787                         Assert.AreEqual (false, row0["IsIdentity"], "#22"); 
788                         Assert.AreEqual (false, row0["IsAutoIncrement"], "#23");
789                         Assert.AreEqual (false, row0["IsRowVersion"], "#24"); 
790                         Assert.AreEqual (false, row0["IsHidden"], "#25"); 
791                         Assert.AreEqual (false, row0["IsLong"], "#26"); 
792                         Assert.AreEqual (false, row0["IsReadOnly"], "#27"); 
793                         Assert.AreEqual (true, schemaTable.Rows[2]["IsReadOnly"], "#27"); 
794
795                         // Test Exception is thrown when reader is closed
796                         reader.Close ();
797                         try {
798                                 reader.GetSchemaTable ();
799                                 Assert.Fail ("#28 Exception shud be thrown" );
800                         }catch (AssertionException e) {
801                                 throw e;
802                         }catch (Exception e) {
803                                 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
804                                         "#29 Incorrect Exception");
805                         }
806                 }
807
808                 [Test]
809                 public void GetDataTypeNameTest ()
810                 {
811                         cmd.CommandText = "Select id, type_tinyint, 10,null from numeric_family where id=1";
812                         reader = cmd.ExecuteReader ();
813
814                         Assert.AreEqual ("tinyint", reader.GetDataTypeName(1), "#1");
815                         Assert.AreEqual ("int", reader.GetDataTypeName(2), "#2");
816                         //need check on windows 
817                         Assert.AreEqual ("int", reader.GetDataTypeName(3), "#3");
818                         try {
819                                 reader.GetDataTypeName (10);
820                                 Assert.Fail ("#4 Exception shud be thrown");
821                         }catch (AssertionException e) {
822                                 throw e;
823                         }catch (Exception e) {
824                                 Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
825                                         "#5 Incorrect Exception : " + e);
826                         }
827                 }
828
829                 [Test]
830                 public void GetFieldTypeTest ()
831                 {
832                         cmd.CommandText = "Select id , type_tinyint, 10 , null from numeric_family where id=1";
833                         reader = cmd.ExecuteReader ();
834
835                         Assert.AreEqual ("tinyint", reader.GetDataTypeName(1), "#1");
836                         Assert.AreEqual ("int", reader.GetDataTypeName(2) , "#2");
837                         Assert.AreEqual ("int", reader.GetDataTypeName(3), "#3");
838                         try {
839                                 reader.GetDataTypeName (10);
840                                 Assert.Fail ("#4 Exception shud be thrown");
841                         }catch (AssertionException e) {
842                                 throw e;
843                         }catch (Exception e) {
844                                 Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
845                                         "#5 Incorrect Exception : " + e);
846                         }
847                 }
848
849                 // Need to populate the data from a config file
850                 // Will be replaced later 
851                 void validateData(string sqlQuery, DataTable table)
852                 {
853                         string fmt = "#TAB[{0}] ROW[{1}] COL[{2}] Data Mismatch";
854
855                         int noOfColumns = table.Columns.Count ;
856                         int i=0;
857
858                         cmd.CommandText = sqlQuery ;
859                         reader = cmd.ExecuteReader ();
860
861                         while (reader.Read ()){
862                                 for (int j=1; j< noOfColumns ; ++j)
863                                         Assert.AreEqual (table.Rows[i][j], reader[j],
864                                                 String.Format (fmt, table.TableName, i+1, j));
865                                 
866                                 i++;
867                         }
868                         reader.Close ();
869                 }
870
871                 [Test]
872                 public void NumericDataValidation ()
873                 {
874                         validateData ("select * from numeric_family order by id ASC",
875                                 numericDataTable);
876                 }
877                 
878                 [Test]
879                 public void StringDataValidation ()
880                 {
881                         validateData ("select * from string_family order by id ASC",
882                                 stringDataTable);
883                 }
884
885                 [Test]
886                 public void BinaryDataValidation ()
887                 {
888                         validateData ("select * from binary_family order by id ASC",
889                                 binaryDataTable);
890                 }
891
892                 [Test]
893                 public void DatetimeDataValidation ()
894                 {
895                         validateData ("select * from datetime_family order by id ASC",
896                                 datetimeDataTable);
897                 }
898
899         }
900 }