//
// SqlDataReaderTest.cs - NUnit Test Cases for testing the
// SqlDataReader class
// Author:
// Umadevi S (sumadevi@novell.com)
// Kornél Pál
// 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.Text;
using System.Data.SqlTypes;
using System.Data.Common;
using System.Data.SqlClient;
using NUnit.Framework;
namespace MonoTests.System.Data.SqlClient
{
[TestFixture]
[Category ("sqlserver")]
public class SqlDataReaderTest
{
SqlConnection conn = null;
SqlCommand cmd = null;
SqlDataReader reader = null;
String query = "Select type_{0},type_{1},convert({0},null) from numeric_family where id=1";
DataSet sqlDataset = null;
DataTable numericDataTable =null;
DataTable stringDataTable =null;
DataTable binaryDataTable =null;
DataTable datetimeDataTable =null;
DataRow numericRow = null;
DataRow stringRow = null;
DataRow binaryRow = null;
DataRow datetimeRow = null;
[TestFixtureSetUp]
public void init ()
{
conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
cmd = conn.CreateCommand ();
sqlDataset = (new DataProvider()).GetDataSet ();
numericDataTable = sqlDataset.Tables["numeric_family"];
stringDataTable = sqlDataset.Tables["string_family"];
binaryDataTable = sqlDataset.Tables["binary_family"];
datetimeDataTable = sqlDataset.Tables["datetime_family"];
numericRow = numericDataTable.Select ("id=1")[0];
stringRow = stringDataTable.Select ("id=1")[0];
binaryRow = binaryDataTable.Select ("id=1")[0];
datetimeRow = datetimeDataTable.Select ("id=1")[0];
}
[SetUp]
public void Setup ()
{
conn.Open ();
}
[TearDown]
public void TearDown ()
{
if (reader != null)
reader.Close ();
conn.Close ();
}
[Test]
public void ReadEmptyNTextFieldTest () {
try {
DBHelper.ExecuteNonQuery (conn, "create table #tmp_monotest (name ntext)");
DBHelper.ExecuteNonQuery (conn, "insert into #tmp_monotest values ('')");
SqlCommand cmd = (SqlCommand) conn.CreateCommand ();
cmd.CommandText = "select * from #tmp_monotest";
SqlDataReader dr = cmd.ExecuteReader ();
if (dr.Read()) {
Assert.AreEqual("System.String",dr["NAME"].GetType().FullName);
}
Assert.AreEqual (false, dr.Read (), "#2");
} finally {
ConnectionManager.Singleton.CloseConnection ();
}
}
[Test]
public void ReadBingIntTest()
{
try {
string query = "SELECT CAST(548967465189498 AS bigint) AS Value";
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = query;
SqlDataReader r = cmd.ExecuteReader();
using (r) {
Assert.AreEqual (true, r.Read(), "#1");
long id = r.GetInt64(0);
Assert.AreEqual(548967465189498, id, "#2");
id = r.GetSqlInt64(0).Value;
Assert.AreEqual(548967465189498, id, "#3");
}
} finally {
ConnectionManager.Singleton.CloseConnection ();
}
}
// This method just helps in Calling common tests among all the Get* Methods
// without replicating code
void CallGetMethod (string s, int i)
{
switch (s) {
case "Boolean" : reader.GetBoolean (i) ; break;
case "SqlBoolean": reader.GetSqlBoolean (i); break;
case "Int16" : reader.GetInt16 (i); break;
case "SqlInt16" : reader.GetSqlInt16 (i); break;
case "Int32" : reader.GetInt32 (i);break;
case "SqlInt32" : reader.GetSqlInt32(i);break;
case "Int64" : reader.GetInt64 (i);break;
case "SqlInt64" : reader.GetSqlInt64(i); break;
case "Decimal" : reader.GetDecimal(i);break;
case "SqlDecimal" : reader.GetSqlDecimal (i);break;
case "SqlMoney" : reader.GetSqlMoney (i);break;
case "Float" : reader.GetFloat (i);break;
case "SqlSingle" : reader.GetSqlSingle(i);break;
case "Double" : reader.GetDouble (i);break;
case "SqlDouble" : reader.GetSqlDouble(i);break;
case "Guid" : reader.GetGuid(i);break;
case "SqlGuid" : reader.GetSqlGuid(i);break;
case "String" : reader.GetString(i);break;
case "SqlString" : reader.GetSqlString(i);break;
case "Char" : reader.GetChar(i);break;
case "Byte" : reader.GetByte (i);break;
case "SqlByte" : reader.GetSqlByte(i); break;
case "DateTime" : reader.GetDateTime(i); break;
case "SqlDateTime" : reader.GetSqlDateTime(i); break;
case "SqlBinary" : reader.GetSqlBinary(i); break;
default : Console.WriteLine ("OOOOPSSSSSS {0}",s);break;
}
}
// This method just helps in Calling common tests among all the Get* Methods
// without replicating code
void GetMethodTests (string s)
{
try {
CallGetMethod (s, 1);
Assert.Fail ("#1[Get"+s+"] InvalidCastException must be thrown");
}catch (AssertionException e) {
throw e;
}catch (Exception e) {
Assert.AreEqual (typeof(InvalidCastException), e.GetType(),
"#2[Get"+s+"] Incorrect Exception : " + e);
}
// GetSql* Methods do not throw SqlNullValueException
// So, Testimg only for Get* Methods
if (!s.StartsWith("Sql")) {
try {
CallGetMethod (s, 2);
Assert.Fail ("#3[Get"+s+"] Exception must be thrown");
}catch (AssertionException e) {
throw e;
}catch (Exception e){
Assert.AreEqual (typeof(SqlNullValueException),e.GetType(),
"#4[Get"+s+"] Incorrect Exception : " + e);
}
}
try {
CallGetMethod (s, 3);
Assert.Fail ("#5[Get"+s+"] IndexOutOfRangeException must be thrown");
}catch (AssertionException e) {
throw e;
}catch (Exception e){
Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
"#6[Get"+s+"] Incorrect Exception : " + e);
}
}
[Test]
public void GetBooleanTest ()
{
cmd.CommandText = string.Format (query, "bit", "int");
reader = cmd.ExecuteReader ();
reader.Read ();
// Test for standard exceptions
GetMethodTests("Boolean");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_bit"], reader.GetBoolean(0),
"#2 DataValidation Failed");
// Test for standard exceptions
GetMethodTests("SqlBoolean");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_bit"], reader.GetSqlBoolean(0).Value,
"#4 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetByteTest ()
{
cmd.CommandText = string.Format (query, "tinyint", "int");
reader = cmd.ExecuteReader ();
reader.Read ();
// Test for standard exceptions
GetMethodTests("Byte");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_tinyint"], reader.GetByte(0),
"#2 DataValidation Failed");
// Test for standard exceptions
GetMethodTests("SqlByte");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_tinyint"], reader.GetSqlByte(0).Value,
"#4 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetInt16Test ()
{
cmd.CommandText = string.Format (query, "smallint", "int");
reader = cmd.ExecuteReader();
reader.Read ();
// Test for standard exceptions
GetMethodTests("Int16");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_smallint"], reader.GetInt16(0),
"#2 DataValidation Failed");
// Test for standard exceptions
GetMethodTests("SqlInt16");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_smallint"], reader.GetSqlInt16(0).Value,
"#4 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetInt32Test ()
{
cmd.CommandText = string.Format (query, "int", "bigint");
reader = cmd.ExecuteReader ();
reader.Read ();
// Test for standard exceptions
GetMethodTests("Int32");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_int"], reader.GetInt32(0),
"#2 DataValidation Failed");
// Test for standard exceptions
GetMethodTests("SqlInt32");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_int"], reader.GetSqlInt32(0).Value,
"#4 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetInt64Test ()
{
cmd.CommandText = string.Format (query, "bigint", "int");
reader = cmd.ExecuteReader ();
reader.Read ();
// Test for standard exceptions
GetMethodTests("Int64");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_bigint"], reader.GetInt64(0),
"#2 DataValidation Failed");
// Test for standard exceptions
GetMethodTests("SqlInt64");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_bigint"], reader.GetSqlInt64(0).Value,
"#4 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetDecimalTest ()
{
cmd.CommandText = string.Format (query, "decimal", "int");
reader = cmd.ExecuteReader ();
reader.Read ();
// Test for standard exceptions
GetMethodTests("Decimal");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_decimal"], reader.GetDecimal(0),
"#2 DataValidation Failed");
// Test for standard exceptions
GetMethodTests("SqlDecimal");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_decimal"], reader.GetSqlDecimal(0).Value,
"#4 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetSqlMoneyTest ()
{
cmd.CommandText = string.Format (query, "money", "int");
reader = cmd.ExecuteReader ();
reader.Read ();
// Test for standard exceptions
GetMethodTests("SqlMoney");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_money"], reader.GetSqlMoney(0).Value,
"#2 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetFloatTest ()
{
cmd.CommandText = "select type_float,type_double,convert(real,null)";
cmd.CommandText += "from numeric_family where id=1";
reader = cmd.ExecuteReader ();
reader.Read ();
// Test for standard exceptions
GetMethodTests("Float");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_float"], reader.GetFloat(0),
"#2 DataValidation Failed");
// Test for standard exceptions
GetMethodTests("SqlSingle");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_float"], reader.GetSqlSingle(0).Value,
"#2 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetDoubleTest ()
{
cmd.CommandText = "select type_double,type_float,convert(float,null)";
cmd.CommandText += " from numeric_family where id=1";
reader = cmd.ExecuteReader ();
reader.Read ();
// Test for standard exceptions
GetMethodTests("Double");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_double"], reader.GetDouble(0),
"#2 DataValidation Failed");
// Test for standard exceptions
GetMethodTests("SqlDouble");
// Test if data is returned correctly
Assert.AreEqual (numericRow["type_double"], reader.GetSqlDouble(0).Value,
"#4 DataValidation Failed");
reader.Close ();
}
[Test]
public void GetBytesTest ()
{
cmd.CommandText = "Select type_text,type_ntext,convert(text,null) ";
cmd.CommandText += "from string_family where id=1";
reader = cmd.ExecuteReader ();
reader.Read ();
try {
reader.GetBytes (0,0,null,0,0);
Assert.Fail ("#1 GetBytes shud be used only wth Sequential Access");
}catch (AssertionException e) {
throw e;
}catch (Exception e) {
Assert.AreEqual (typeof(InvalidCastException), e.GetType (),
"#2 Incorrect Exception : " + e);
}
reader.Close ();
byte[] asciiArray = (new ASCIIEncoding ()).GetBytes ("text");
byte[] unicodeArray = (new UnicodeEncoding ()).GetBytes ("ntext");
byte[] buffer = null ;
long size = 0;
reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
reader.Read ();
size = reader.GetBytes (0,0,null,0,0);
Assert.AreEqual (asciiArray.Length, size, "#3 Data Incorrect");
buffer = new byte[size];
size = reader.GetBytes (0,0,buffer,0,(int)size);
for (int i=0;i