In .:
[mono.git] / mcs / class / System.Data / Test / ProviderTests / ProviderIndependant / DataReaderTest.cs
1 // DataReaderTest.cs - NUnit Test Cases for testing the
2 // DataReader family of classes
3 //
4 // Authors:
5 //      Sureshkumar T (tsureshkumar@novell.com)
6 // 
7 // Copyright (c) 2004 Novell Inc., and the individuals listed on the
8 // ChangeLog entries.
9 //
10 //
11 // Permission is hereby granted, free of charge, to any person
12 // obtaining a copy of this software and associated documentation
13 // files (the "Software"), to deal in the Software without
14 // restriction, including without limitation the rights to use, copy,
15 // modify, merge, publish, distribute, sublicense, and/or sell copies
16 // of the Software, and to permit persons to whom the Software is
17 // furnished to do so, subject to the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
26 // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 // SOFTWARE.
30
31 using System;
32 using System.Data;
33 using System.Data.Common;
34 using Mono.Data;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Data
39 {
40         [TestFixture]
41         [Category ("odbc"), Category ("sqlserver")]
42         public class DataReaderTest
43         {
44                 [Test]
45                 public void GetSchemaTableTest ()
46                 {
47                         IDbConnection conn = ConnectionManager.Singleton.Connection;
48                         try {
49                                 ConnectionManager.Singleton.OpenConnection ();
50                                 IDbCommand cmd = conn.CreateCommand ();
51                                 cmd.CommandText = "select id, fname, id + 20 as plustwenty from employee";
52                                 IDataReader reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo);
53                                 DataTable schema = reader.GetSchemaTable ();
54                                 reader.Close ();
55                                 Assert.AreEqual (3, schema.Rows.Count, "#1");
56
57                                 // Primary Key Test
58                                 DataRow pkRow = schema.Select ("ColumnName = 'id'") [0];
59                                 Assert.AreEqual (false, pkRow.IsNull ("IsKey"), "#2");
60                                 Assert.AreEqual (true, (bool) pkRow ["IsKey"], "#3");
61                         } finally {
62                                 ConnectionManager.Singleton.CloseConnection ();
63                         }
64                 }
65
66                 
67                 [Test]
68                 public void GetNameTest () 
69                 {
70                         IDbConnection conn = ConnectionManager.Singleton.Connection;
71                         try {
72                                 ConnectionManager.Singleton.OpenConnection ();
73                                 IDbCommand dbcmd = conn.CreateCommand ();
74                                 string sql = "SELECT  type_tinyint from numeric_family";
75                                 dbcmd.CommandText = sql;
76                                 using (IDataReader reader = dbcmd.ExecuteReader ()) {
77                                         Assert.AreEqual ("type_tinyint", reader.GetName (0), "#1 GetName failes");
78                                 }
79                         } finally {
80                                 ConnectionManager.Singleton.CloseConnection ();
81                         }
82                 }
83
84                 [Test]
85                 public void NumericTest()
86                 {
87                         IDbConnection conn = ConnectionManager.Singleton.Connection;
88                         try {
89                                 ConnectionManager.Singleton.OpenConnection ();
90                                 IDbCommand dbCommand = conn.CreateCommand ();
91                                 dbCommand.CommandText = "select type_numeric from numeric_family where id = 1;";
92                                 using(IDataReader reader = dbCommand.ExecuteReader ()) {
93                                         if (reader.Read()) {
94                                                 object value = reader.GetValue(0);
95                                                 Assert.AreEqual (typeof (decimal), value.GetType(), "#1 wrong type");
96                                                 Assert.AreEqual ( (decimal) 1000, value, "#2 value is wrong");   
97                                         } else
98                                                 Assert.Fail ("#3 does not have test data");
99                                 }
100                         } finally {
101                                 ConnectionManager.Singleton.CloseConnection ();
102                         }
103                 }
104
105                 [Test]
106                 public void TinyIntTest ()
107                 {
108                         IDbConnection conn = ConnectionManager.Singleton.Connection;
109                         try {
110                                 ConnectionManager.Singleton.OpenConnection ();
111                                 IDbCommand dbCommand = conn.CreateCommand ();
112                                 dbCommand.CommandText = "select type_tinyint from numeric_family where id = 1;";
113                                 using(IDataReader reader = dbCommand.ExecuteReader ()) {
114                                         if (reader.Read()) {
115                                                 object value = reader.GetValue (0);
116                                                 Assert.AreEqual (typeof (byte), value.GetType(), "#1 wrong type");
117                                                 Assert.AreEqual (255, value, "#2 value is wrong");   
118                                         } else
119                                                 Assert.Fail ("#3 does not have test data");
120                                 }
121                         } finally {
122                                 ConnectionManager.Singleton.CloseConnection ();
123                         }
124                 }
125                 
126                 [Test]
127                 public void GetByteTest () 
128                 {
129                         IDbConnection conn = ConnectionManager.Singleton.Connection;
130                         try {
131                                 ConnectionManager.Singleton.OpenConnection ();
132                                 IDbCommand cmd = conn.CreateCommand ();
133                                 string query = "select type_tinyint from numeric_family where id = 1";
134                                 cmd.CommandText = query;
135                                 using (IDataReader reader = cmd.ExecuteReader ()) {
136                                         if (reader.Read ()) {
137                                                 byte b = reader.GetByte (0); 
138                                                 Assert.AreEqual (255, b, "GetByte returns wrong result");
139                                         } else // This should not happen while testing
140                                                 Assert.Fail ("test table does not have a test data!");
141                                 }
142                         } finally {
143                                 ConnectionManager.Singleton.CloseConnection ();
144                         }                       
145                 }
146
147                 [Test]
148                 public void GetValueBinaryTest ()
149                 {
150                         IDbConnection conn = ConnectionManager.Singleton.Connection;
151                         try {
152                                 ConnectionManager.Singleton.OpenConnection ();
153                                 IDbCommand cmd = conn.CreateCommand ();
154                                 string sql = "select type_binary from binary_family where id = 1";
155                                 cmd.CommandText = sql;
156                                 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
157                                         if (reader.Read ()) {
158                                                 object ob = reader.GetValue (0);
159                                                 Assert.AreEqual (typeof (byte []), ob.GetType (), "#1 Type of binary column is wrong!");
160                                         } else
161                                                 Assert.Fail ("#2 test data not available");
162                                 }
163                         } finally {
164                                 ConnectionManager.Singleton.CloseConnection ();
165                         }      
166                 }
167                 
168                 [Test]
169                 public void GetBytesNullBufferTest ()
170                 {
171                         IDbConnection conn = ConnectionManager.Singleton.Connection;
172                         try {
173                                 ConnectionManager.Singleton.OpenConnection ();
174                                 IDbCommand cmd = conn.CreateCommand ();
175                                 // 4th row is null
176                                 string sql = "SELECT type_blob FROM binary_family where id = 1 or id = 4 order by id";
177                                 cmd.CommandText = sql;
178                                 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
179                                         if (reader.Read ()) {
180                                                 Assert.AreEqual (8, reader.GetBytes (0, 0, null, 0, 0), 
181                                                                  "#1 getbytes should return length of column");
182                                         } else
183                                                 Assert.Fail ("#2 No test data");
184                                         // for null value, length in bytes should return 0
185                                         if (reader.Read ()) 
186                                                 Assert.AreEqual (0, reader.GetBytes (0, 0, null, 0, 0), 
187                                                                  "#3 on null value, it should return -1");
188                                         else
189                                                 Assert.Fail ("#4 No test data");
190                                 }
191                         } finally {
192                                 ConnectionManager.Singleton.CloseConnection ();
193                         }
194                 }
195
196                 [Test]
197                 public void GetBytesTest ()
198                 {
199                         IDbConnection conn = ConnectionManager.Singleton.Connection;
200                         try {
201                                 ConnectionManager.Singleton.OpenConnection ();
202                                 IDbCommand cmd = conn.CreateCommand ();
203                                 string sql = "SELECT type_blob FROM binary_family where id = 1";
204                                 cmd.CommandText = sql;
205                                 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess)) {
206                                         if (reader.Read ()) {
207                                                 // Get By Parts for the column blob
208                                                 long totalsize = reader.GetBytes (0, 0, null, 0, 0);
209                                                 int buffsize = 5;
210                                                 int start = 0;
211                                                 int offset = 0;
212                                                 long ret = 0;
213                                                 long count = 0;
214                                                 byte [] val = new byte [totalsize];
215                                                 do {
216                                                         ret = reader.GetBytes (0, offset, val, offset, buffsize);
217                                                         offset += (int) ret;
218                                                         count += ret;
219                                                 } while (count < totalsize);
220                                                 Assert.AreEqual (8, count, 
221                                                                  "#1 The assembled value length does not match");
222                                         } else
223                                                 Assert.Fail ("#2 no test data");
224                                 }
225                         } finally {
226                                 ConnectionManager.Singleton.CloseConnection ();
227                         }
228                 }
229
230                 [Test]
231                 public void GetSchemaTableTest_AutoIncrement ()
232                 {
233                         IDbConnection conn = ConnectionManager.Singleton.Connection;
234                         try {
235                                 ConnectionManager.Singleton.OpenConnection ();
236                                 IDbCommand cmd = conn.CreateCommand ();
237                                 cmd.CommandText = "create table #tmp_table (id int identity(1,1))";
238                                 cmd.ExecuteNonQuery ();
239                                 cmd.CommandText = "select * from #tmp_table";
240                                 using (IDataReader reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly)) {
241                                         DataTable schemaTable = reader.GetSchemaTable ();
242                                         Assert.IsTrue ((bool)schemaTable.Rows [0]["IsAutoIncrement"], "#1");
243                                         if (schemaTable.Columns.Contains ("IsIdentity"))
244                                                 Assert.IsTrue ((bool)schemaTable.Rows [0]["IsIdentity"], "#2");
245                                 }
246                         } finally {
247                                 ConnectionManager.Singleton.CloseConnection ();
248                         }
249                 }
250         }
251 }