1 // DataReaderTest.cs - NUnit Test Cases for testing the
2 // DataReader family of classes
5 // Sureshkumar T (tsureshkumar@novell.com)
7 // Copyright (c) 2004 Novell Inc., and the individuals listed on the
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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
33 using System.Data.Common;
36 using NUnit.Framework;
38 namespace MonoTests.System.Data
41 [Category ("odbc"), Category ("sqlserver")]
42 public class DataReaderTest
45 public void GetSchemaTableTest ()
47 IDbConnection conn = ConnectionManager.Singleton.Connection;
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 ();
55 Assert.AreEqual (3, schema.Rows.Count, "#1");
58 DataRow pkRow = schema.Select ("ColumnName = 'id'") [0];
59 Assert.AreEqual (false, pkRow.IsNull ("IsKey"), "#2");
60 Assert.AreEqual (true, (bool) pkRow ["IsKey"], "#3");
62 ConnectionManager.Singleton.CloseConnection ();
68 public void GetNameTest ()
70 IDbConnection conn = ConnectionManager.Singleton.Connection;
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");
80 ConnectionManager.Singleton.CloseConnection ();
85 public void NumericTest()
87 IDbConnection conn = ConnectionManager.Singleton.Connection;
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 ()) {
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");
98 Assert.Fail ("#3 does not have test data");
101 ConnectionManager.Singleton.CloseConnection ();
106 public void TinyIntTest ()
108 IDbConnection conn = ConnectionManager.Singleton.Connection;
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 ()) {
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");
119 Assert.Fail ("#3 does not have test data");
122 ConnectionManager.Singleton.CloseConnection ();
127 public void GetByteTest ()
129 IDbConnection conn = ConnectionManager.Singleton.Connection;
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!");
143 ConnectionManager.Singleton.CloseConnection ();
148 public void GetValueBinaryTest ()
150 IDbConnection conn = ConnectionManager.Singleton.Connection;
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!");
161 Assert.Fail ("#2 test data not available");
164 ConnectionManager.Singleton.CloseConnection ();
169 public void GetBytesNullBufferTest ()
171 IDbConnection conn = ConnectionManager.Singleton.Connection;
173 ConnectionManager.Singleton.OpenConnection ();
174 IDbCommand cmd = conn.CreateCommand ();
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");
183 Assert.Fail ("#2 No test data");
184 // for null value, length in bytes should return 0
186 Assert.AreEqual (0, reader.GetBytes (0, 0, null, 0, 0),
187 "#3 on null value, it should return -1");
189 Assert.Fail ("#4 No test data");
192 ConnectionManager.Singleton.CloseConnection ();
197 public void GetBytesTest ()
199 IDbConnection conn = ConnectionManager.Singleton.Connection;
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);
214 byte [] val = new byte [totalsize];
216 ret = reader.GetBytes (0, offset, val, offset, buffsize);
219 } while (count < totalsize);
220 Assert.AreEqual (8, count,
221 "#1 The assembled value length does not match");
223 Assert.Fail ("#2 no test data");
226 ConnectionManager.Singleton.CloseConnection ();
231 public void GetSchemaTableTest_AutoIncrement ()
233 IDbConnection conn = ConnectionManager.Singleton.Connection;
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");
247 ConnectionManager.Singleton.CloseConnection ();