2
0

SqlDataReaderTest.cs 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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 GetBytes_Binary ()
  393. {
  394. cmd.CommandText = "Select type_binary,type_varbinary,type_blob ";
  395. cmd.CommandText += "from binary_family where id=1";
  396. reader = cmd.ExecuteReader ();
  397. reader.Read ();
  398. byte[] binary = (byte[])reader.GetValue (0);
  399. byte[] varbinary = (byte[])reader.GetValue (1);
  400. byte[] image = (byte[])reader.GetValue (2);
  401. reader.Close ();
  402. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  403. reader.Read ();
  404. int len = 0;
  405. byte[] arr ;
  406. len = (int)reader.GetBytes (0,0,null,0,0);
  407. Assert.AreEqual (binary.Length, len, "#1");
  408. arr = new byte [len];
  409. reader.GetBytes (0,0,arr,0,len);
  410. for (int i=0; i<len; ++i)
  411. Assert.AreEqual (binary[i], arr[i], "#2");
  412. len = (int)reader.GetBytes (1,0,null,0,0);
  413. Assert.AreEqual (varbinary.Length, len, "#1");
  414. arr = new byte [len];
  415. reader.GetBytes (1,0,arr,0,len);
  416. for (int i=0; i<len; ++i)
  417. Assert.AreEqual (varbinary[i], arr[i], "#2");
  418. len = (int)reader.GetBytes (2,0,null,0,0);
  419. Assert.AreEqual (image.Length, len, "#1");
  420. arr = new byte [len];
  421. reader.GetBytes (2,0,arr,0,len);
  422. for (int i=0; i<len; ++i)
  423. Assert.AreEqual (image[i], arr[i], "#2");
  424. reader.Close ();
  425. cmd.CommandText = "Select type_binary,type_varbinary,type_blob ";
  426. cmd.CommandText += "from binary_family where id=1";
  427. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  428. reader.Read ();
  429. len = (int)reader.GetBytes (0,0,null,0,0);
  430. arr = new byte [100];
  431. for (int i=0; i<len; ++i) {
  432. Assert.AreEqual (len-i, reader.GetBytes (0, i, null, 0, 0), "#1_"+i);
  433. Assert.AreEqual (1, reader.GetBytes (0, i, arr, 0, 1), "#2_"+i);
  434. Assert.AreEqual (binary [i], arr [0], "#3_"+i);
  435. }
  436. Assert.AreEqual (0, reader.GetBytes (0, len+10, null, 0, 0));
  437. reader.Close ();
  438. }
  439. [Test]
  440. public void GetChars ()
  441. {
  442. cmd.CommandText = "Select type_char, type_varchar,type_text, type_ntext ";
  443. cmd.CommandText += "from string_family where id=1";
  444. reader = cmd.ExecuteReader ();
  445. reader.Read ();
  446. string charstring = reader.GetString (0);
  447. //string ncharstring = reader.GetString (1);
  448. string varcharstring = reader.GetString (1);
  449. //string nvarcharstring = reader.GetString (2);
  450. string textstring = reader.GetString (2);
  451. string ntextstring = reader.GetString (3);
  452. reader.Close ();
  453. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  454. reader.Read ();
  455. int len = 0;
  456. char[] arr;
  457. len = (int)reader.GetChars (0,0,null,0,0);
  458. Assert.AreEqual (charstring.Length, len, "#1");
  459. arr = new char [len];
  460. reader.GetChars (0,0,arr,0,len);
  461. Assert.AreEqual (0, charstring.CompareTo (new String (arr)), "#2");
  462. len = (int)reader.GetChars (1,0,null,0,0);
  463. Assert.AreEqual (varcharstring.Length, len, "#3");
  464. arr = new char [len];
  465. reader.GetChars (1,0,arr,0,len);
  466. Assert.AreEqual (0, varcharstring.CompareTo (new String (arr)), "#4");
  467. len = (int)reader.GetChars (2,0,null,0,0);
  468. Assert.AreEqual (textstring.Length, len, "#5");
  469. arr = new char [len];
  470. reader.GetChars (2,0,arr,0,len);
  471. Assert.AreEqual (0, textstring.CompareTo (new String (arr)), "#6");
  472. len = (int)reader.GetChars (3,0,null,0,0);
  473. Assert.AreEqual (ntextstring.Length, len, "#7");
  474. arr = new char [len];
  475. reader.GetChars (3,0,arr,0,len);
  476. Assert.AreEqual (0, ntextstring.CompareTo (new String (arr)), "#8");
  477. reader.Close ();
  478. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  479. reader.Read ();
  480. len = (int)reader.GetChars (0,0,null,0,0);
  481. arr = new char [10];
  482. for (int i=0; i<len; ++i) {
  483. Assert.AreEqual (len-i, reader.GetChars (0, i, null, 0, 0), "#9_"+i);
  484. Assert.AreEqual (1, reader.GetChars (0, i, arr, 0, 1), "#10_"+i);
  485. Assert.AreEqual (charstring [i], arr [0], "#11_"+i);
  486. }
  487. Assert.AreEqual (0, reader.GetChars (0, len+10, null, 0, 0));
  488. reader.Close ();
  489. }
  490. [Test]
  491. public void GetStringTest ()
  492. {
  493. cmd.CommandText = "Select type_varchar,10,convert(varchar,null)";
  494. cmd.CommandText += "from string_family where id=1";
  495. reader = cmd.ExecuteReader ();
  496. reader.Read ();
  497. // Test for standard exceptions
  498. GetMethodTests("String");
  499. // Test if data is returned correctly
  500. Assert.AreEqual (stringRow["type_varchar"], reader.GetString(0),
  501. "#2 DataValidation Failed");
  502. // Test for standard exceptions
  503. GetMethodTests("SqlString");
  504. // Test if data is returned correctly
  505. Assert.AreEqual (stringRow["type_varchar"], reader.GetSqlString(0).Value,
  506. "#4 DataValidation Failed");
  507. reader.Close();
  508. }
  509. [Test]
  510. public void GetSqlBinaryTest ()
  511. {
  512. cmd.CommandText = "Select type_binary ,10 ,convert(binary,null)";
  513. cmd.CommandText += "from binary_family where id=1";
  514. reader = cmd.ExecuteReader ();
  515. reader.Read ();
  516. // Test for standard exceptions
  517. GetMethodTests ("SqlBinary");
  518. // Test if data is returned correctly
  519. Assert.AreEqual (binaryRow["type_binary"], reader.GetSqlBinary(0).Value,
  520. "#2 DataValidation Failed");
  521. reader.Close ();
  522. }
  523. [Test]
  524. public void GetGuidTest ()
  525. {
  526. cmd.CommandText = "Select type_guid,id,convert(uniqueidentifier,null)";
  527. cmd.CommandText += "from string_family where id=1";
  528. reader = cmd.ExecuteReader ();
  529. reader.Read ();
  530. // Test for standard exceptions
  531. GetMethodTests("Guid");
  532. // Test if data is returned correctly
  533. Assert.AreEqual (stringRow["type_guid"], reader.GetGuid(0),
  534. "#2 DataValidation Failed");
  535. // Test for standard exceptions
  536. GetMethodTests("SqlGuid");
  537. // Test if data is returned correctly
  538. Assert.AreEqual (stringRow["type_guid"], reader.GetSqlGuid(0).Value,
  539. "#4 DataValidation Failed");
  540. reader.Close ();
  541. }
  542. [Test]
  543. public void GetDateTimeTest ()
  544. {
  545. cmd.CommandText = "Select type_datetime,10,convert(datetime,null)";
  546. cmd.CommandText += "from datetime_family where id=1";
  547. reader = cmd.ExecuteReader ();
  548. reader.Read ();
  549. // Test for standard exceptions
  550. GetMethodTests("DateTime");
  551. // Test if data is returned correctly
  552. Assert.AreEqual (datetimeRow["type_datetime"], reader.GetDateTime(0),
  553. "#2 DataValidation Failed");
  554. // Test for standard exceptions
  555. GetMethodTests("SqlDateTime");
  556. // Test if data is returned correctly
  557. Assert.AreEqual (datetimeRow["type_datetime"], reader.GetSqlDateTime(0).Value,
  558. "#2 DataValidation Failed");
  559. reader.Close ();
  560. }
  561. [Test]
  562. [Ignore ("Not Supported by msdotnet")]
  563. public void GetCharTest ()
  564. {
  565. cmd.CommandText = "Select type_char,type_guid,convert(char,null)";
  566. cmd.CommandText += "from string_family where id=1";
  567. reader = cmd.ExecuteReader ();
  568. reader.Read ();
  569. // Test for standard exceptions
  570. GetMethodTests ("Char");
  571. reader.Close ();
  572. }
  573. [Test]
  574. public void GetValueTest ()
  575. {
  576. cmd.CommandText = "Select id, null from numeric_family where id=1";
  577. reader = cmd.ExecuteReader ();
  578. reader.Read ();
  579. object obj = null;
  580. obj = reader.GetValue (0);
  581. Assert.AreEqual ((byte)1, obj, "#1 Shud return the value of id");
  582. obj = reader.GetValue (1);
  583. Assert.AreEqual (DBNull.Value, obj, "#2 shud return DBNull");
  584. reader.Close ();
  585. }
  586. [Test]
  587. public void GetSqlValueTest ()
  588. {
  589. cmd.CommandText = "Select id, type_tinyint, null from numeric_family where id=1";
  590. reader = cmd.ExecuteReader ();
  591. reader.Read ();
  592. Assert.AreEqual ((byte)255, ((SqlByte) reader.GetSqlValue(1)).Value, "#1");
  593. //Assert.AreEqual (DBNull.Value, reader.GetSqlValue(2), "#2");
  594. reader.Close ();
  595. }
  596. [Test]
  597. public void GetValuesTest ()
  598. {
  599. cmd.CommandText = "Select 10,20,30 from numeric_family where id=1";
  600. reader = cmd.ExecuteReader ();
  601. reader.Read ();
  602. object[] arr = null;
  603. int count = 0;
  604. arr = new object[1];
  605. count = reader.GetValues (arr);
  606. Assert.AreEqual (10, (int)arr[0], "#1 Only first object shud be copied");
  607. Assert.AreEqual (1, count, "#1 return value shud equal objects copied");
  608. arr = new object[3];
  609. count = reader.GetValues (arr);
  610. Assert.AreEqual (3, count, "#2 return value shud equal objects copied");
  611. arr = new object[5];
  612. count = reader.GetValues (arr);
  613. Assert.AreEqual (3, count, "#3 return value shud equal objects copied");
  614. Assert.IsNull (arr[3], "#4 Only 3 objects shud be copied");
  615. reader.Close ();
  616. }
  617. [Test]
  618. public void GetSqlValuesTest ()
  619. {
  620. cmd.CommandText = "Select 10,20,30 from numeric_family where id=1";
  621. reader = cmd.ExecuteReader ();
  622. reader.Read ();
  623. object[] arr = null;
  624. int count = 0;
  625. arr = new object[1];
  626. count = reader.GetSqlValues (arr);
  627. // Something is wrong with types ... gotta figure it out
  628. //Assert.AreEqual (10, arr[0], "#1 Only first object shud be copied");
  629. Assert.AreEqual (1, count, "#1 return value shud equal objects copied");
  630. arr = new object[3];
  631. count = reader.GetSqlValues (arr);
  632. Assert.AreEqual (3, count, "#2 return value shud equal objects copied");
  633. arr = new object[5];
  634. count = reader.GetSqlValues (arr);
  635. Assert.AreEqual (3, count, "#3 return value shud equal objects copied");
  636. Assert.IsNull (arr[3], "#4 Only 3 objects shud be copied");
  637. reader.Close ();
  638. }
  639. [Test]
  640. public void isDBNullTest ()
  641. {
  642. cmd.CommandText = "select id , null from numeric_family where id=1";
  643. reader = cmd.ExecuteReader ();
  644. reader.Read ();
  645. Assert.IsFalse (reader.IsDBNull (0), "#1");
  646. Assert.IsTrue (reader.IsDBNull (1) , "#2");
  647. try {
  648. reader.IsDBNull (10);
  649. Assert.Fail ("#1 Invalid Argument");
  650. }catch (AssertionException e) {
  651. throw e;
  652. }catch (Exception e) {
  653. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  654. "#1 Incorrect Exception : " + e);
  655. }
  656. }
  657. [Test]
  658. public void ReadTest ()
  659. {
  660. cmd.CommandText = "select id, type_bit from numeric_family where id=1" ;
  661. reader = cmd.ExecuteReader ();
  662. Assert.IsTrue (reader.Read () , "#1");
  663. Assert.IsFalse (reader.Read (), "#2");
  664. reader.Close ();
  665. try {
  666. reader.Read ();
  667. Assert.Fail ("#3 Exception shud be thrown : Reader is closed");
  668. }catch (AssertionException e) {
  669. throw e;
  670. }catch (Exception e) {
  671. Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
  672. "#4 Incorrect Exception : " + e);
  673. }
  674. }
  675. [Test]
  676. public void NextResultTest ()
  677. {
  678. cmd.CommandText = "Select id from numeric_family where id=1";
  679. reader = cmd.ExecuteReader ();
  680. Assert.IsFalse (reader.NextResult (), "#1");
  681. reader.Close ();
  682. cmd.CommandText = "select id from numeric_family where id=1;";
  683. cmd.CommandText += "select type_bit from numeric_family where id=2;";
  684. reader = cmd.ExecuteReader ();
  685. Assert.IsTrue (reader.NextResult (), "#2");
  686. Assert.IsFalse (reader.NextResult (), "#3");
  687. reader.Close ();
  688. try {
  689. reader.NextResult ();
  690. Assert.Fail ("#4 Exception shud be thrown : Reader is closed");
  691. }catch (AssertionException e) {
  692. throw e;
  693. }catch (Exception e) {
  694. Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
  695. "#5 Incorrect Exception : " + e);
  696. }
  697. }
  698. [Test]
  699. public void GetNameTest ()
  700. {
  701. cmd.CommandText = "Select id,10 as gen,20 from numeric_family where id=1";
  702. reader = cmd.ExecuteReader ();
  703. Assert.AreEqual ("id" , reader.GetName(0) , "#1");
  704. Assert.AreEqual ("gen" , reader.GetName(1) , "#2");
  705. try {
  706. reader.GetName (3);
  707. Assert.Fail ("#4 Exception shud be thrown");
  708. }catch (AssertionException e) {
  709. throw e;
  710. }catch (Exception e) {
  711. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  712. "#5 Incorrect Exception : " + e);
  713. }
  714. }
  715. [Test]
  716. public void GetOrdinalTest ()
  717. {
  718. //what is kana-width insensitive ?????
  719. cmd.CommandText = "Select id,10 as gen,20 from numeric_family where id=1";
  720. reader = cmd.ExecuteReader ();
  721. Assert.AreEqual (0, reader.GetOrdinal ("id"), "#1");
  722. Assert.AreEqual (0, reader.GetOrdinal ("ID"), "#2");
  723. Assert.AreEqual (1, reader.GetOrdinal ("gen"), "#3");
  724. // Would expect column1,columnn2 etc for unnamed columns,
  725. // but msdotnet return empty string for unnamed columns
  726. Assert.AreEqual (2, reader.GetOrdinal (""), "#4");
  727. try {
  728. reader.GetOrdinal ("invalidname");
  729. }catch (AssertionException e) {
  730. throw e;
  731. }catch (Exception e) {
  732. Assert.AreEqual (typeof (IndexOutOfRangeException),
  733. e.GetType(), "#4 Incorrect Exception : " + e);
  734. }
  735. }
  736. [Test]
  737. public void GetSchemaTableTest ()
  738. {
  739. cmd.CommandText = "Select type_decimal as decimal,id,10 ";
  740. cmd.CommandText += "from numeric_family where id=1";
  741. reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
  742. DataTable schemaTable = reader.GetSchemaTable ();
  743. DataRow row0 = schemaTable.Rows[0];
  744. DataRow row1 = schemaTable.Rows[1];
  745. Assert.AreEqual ("decimal", row0["ColumnName"], "#1");
  746. Assert.AreEqual ("", schemaTable.Rows[2]["ColumnName"], "#2");
  747. Assert.AreEqual (0, row0["ColumnOrdinal"], "#2");
  748. Assert.AreEqual (17, row0["ColumnSize"], "#3");
  749. Assert.AreEqual (38, row0["NumericPrecision"], "#4");
  750. Assert.AreEqual (0, row0["NumericScale"], "#5");
  751. Assert.AreEqual (false, row0["IsUnique"], "#6");
  752. // msdotnet returns IsUnique as false for Primary key
  753. // even though table consists of a single Primary Key
  754. //Assert.AreEqual (true, row1["IsUnique"], "#7");
  755. Assert.AreEqual (false, row0["IsKey"], "#8");
  756. Assert.AreEqual (true, row1["IsKey"], "#9");
  757. //Assert.AreEqual ("servername", row0["BaseServerName"], "#10");
  758. //Assert.AreEqual ("monotest", row0["BaseCatalogName"], "#11");
  759. Assert.AreEqual ("type_decimal", row0["BaseColumnName"], "#12");
  760. //Assert.IsNull(row0["BaseSchemaName"], "#13");
  761. Assert.AreEqual ("numeric_family", row0["BaseTableName"], "#14");
  762. Assert.AreEqual (typeof (Decimal), row0["DataType"], "#15");
  763. Assert.AreEqual (true, row0["AllowDBNull"], "#16");
  764. Assert.AreEqual (false, row1["AllowDBNull"], "#17");
  765. //Assert.IsNull(row0["ProviderType"], "#18");
  766. Assert.AreEqual (true, row0["IsAliased"], "#19");
  767. Assert.AreEqual (false, row1["IsAliased"], "#20");
  768. Assert.AreEqual (false, row0["IsExpression"], "#21");
  769. Assert.AreEqual (false, row0["IsIdentity"], "#22");
  770. Assert.AreEqual (false, row0["IsAutoIncrement"], "#23");
  771. Assert.AreEqual (false, row0["IsRowVersion"], "#24");
  772. Assert.AreEqual (false, row0["IsHidden"], "#25");
  773. Assert.AreEqual (false, row0["IsLong"], "#26");
  774. Assert.AreEqual (false, row0["IsReadOnly"], "#27");
  775. Assert.AreEqual (true, schemaTable.Rows[2]["IsReadOnly"], "#27");
  776. // Test Exception is thrown when reader is closed
  777. reader.Close ();
  778. try {
  779. reader.GetSchemaTable ();
  780. Assert.Fail ("#28 Exception shud be thrown" );
  781. }catch (AssertionException e) {
  782. throw e;
  783. }catch (Exception e) {
  784. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  785. "#29 Incorrect Exception");
  786. }
  787. }
  788. [Test]
  789. public void GetDataTypeNameTest ()
  790. {
  791. cmd.CommandText = "Select id, type_tinyint, 10,null from numeric_family where id=1";
  792. reader = cmd.ExecuteReader ();
  793. Assert.AreEqual ("tinyint", reader.GetDataTypeName(1), "#1");
  794. Assert.AreEqual ("int", reader.GetDataTypeName(2), "#2");
  795. //need check on windows
  796. Assert.AreEqual ("int", reader.GetDataTypeName(3), "#3");
  797. try {
  798. reader.GetDataTypeName (10);
  799. Assert.Fail ("#4 Exception shud be thrown");
  800. }catch (AssertionException e) {
  801. throw e;
  802. }catch (Exception e) {
  803. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  804. "#5 Incorrect Exception : " + e);
  805. }
  806. }
  807. [Test]
  808. public void GetFieldTypeTest ()
  809. {
  810. cmd.CommandText = "Select id , type_tinyint, 10 , null from numeric_family where id=1";
  811. reader = cmd.ExecuteReader ();
  812. Assert.AreEqual ("tinyint", reader.GetDataTypeName(1), "#1");
  813. Assert.AreEqual ("int", reader.GetDataTypeName(2) , "#2");
  814. Assert.AreEqual ("int", reader.GetDataTypeName(3), "#3");
  815. try {
  816. reader.GetDataTypeName (10);
  817. Assert.Fail ("#4 Exception shud be thrown");
  818. }catch (AssertionException e) {
  819. throw e;
  820. }catch (Exception e) {
  821. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  822. "#5 Incorrect Exception : " + e);
  823. }
  824. }
  825. // Need to populate the data from a config file
  826. // Will be replaced later
  827. void validateData(string sqlQuery, DataTable table)
  828. {
  829. string fmt = "#TAB[{0}] ROW[{1}] COL[{2}] Data Mismatch";
  830. int noOfColumns = table.Columns.Count ;
  831. int i=0;
  832. cmd.CommandText = sqlQuery ;
  833. reader = cmd.ExecuteReader ();
  834. while (reader.Read ()){
  835. for (int j=1; j< noOfColumns ; ++j)
  836. Assert.AreEqual (table.Rows[i][j], reader[j],
  837. String.Format (fmt, table.TableName, i+1, j));
  838. i++;
  839. }
  840. reader.Close ();
  841. }
  842. [Test]
  843. public void NumericDataValidation ()
  844. {
  845. validateData ("select * from numeric_family order by id ASC",
  846. numericDataTable);
  847. }
  848. [Test]
  849. public void StringDataValidation ()
  850. {
  851. validateData ("select * from string_family order by id ASC",
  852. stringDataTable);
  853. }
  854. [Test]
  855. public void BinaryDataValidation ()
  856. {
  857. validateData ("select * from binary_family order by id ASC",
  858. binaryDataTable);
  859. }
  860. [Test]
  861. public void DatetimeDataValidation ()
  862. {
  863. validateData ("select * from datetime_family order by id ASC",
  864. datetimeDataTable);
  865. }
  866. }
  867. }