SqlDataReaderTest.cs 27 KB

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