OdbcDataReaderTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //
  2. // OdbcDataReaderTest.cs - NUnit Test Cases for testing the
  3. // OdbcDataReader class
  4. //
  5. // Author:
  6. // Sureshkumar T ([email protected])
  7. //
  8. // Copyright (c) 2004 Novell Inc., and the individuals listed
  9. // on the ChangeLog entries.
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // 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 BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Data;
  32. using System.Data.Odbc;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Data.Odbc
  35. {
  36. [TestFixture]
  37. public class OdbcDataReaderTest : MySqlOdbcBaseClient
  38. {
  39. [SetUp]
  40. public void GetReady () {
  41. OpenConnection ();
  42. CreateTestSetup (); // create database & test tables
  43. }
  44. [TearDown]
  45. public void Clean () {
  46. CleanTestSetup (); // clean test database;
  47. CloseConnection ();
  48. }
  49. /// <summary>
  50. /// Tests the return value of GetByte method of OdbcDataReader
  51. /// </summary>
  52. [Test]
  53. public void GetByteTest ()
  54. {
  55. OdbcDataReader reader = null;
  56. try {
  57. // For this Test, you must create sample table
  58. // called test, with a column of name 'col_int'.
  59. // and the table with atleast a row with a minimum value for col_int as 0xff
  60. // This tries to read a int column using GetByte method
  61. OdbcCommand cmd = conn.CreateCommand ();
  62. string query = "select col_int from test order by col_int;";
  63. cmd.CommandText = query;
  64. reader = cmd.ExecuteReader ();
  65. if (reader.Read ()) {
  66. byte b = reader.GetByte (0);
  67. Assertion.AssertEquals ("GetByte returns wrong result!", 0xff, b);
  68. } else // This should not happen while testing
  69. Assertion.AssertEquals ("test table doens not have a test data!", true, true);
  70. } finally { // try/catch is necessary to gracefully close connections
  71. if (reader != null && reader.IsClosed)
  72. reader.Close ();
  73. CleanTestSetup ();
  74. CloseConnection ();
  75. }
  76. }
  77. /// <summary>
  78. /// Tests the return column type of data reader
  79. /// To test the bugzilla id 49340
  80. /// </summary>
  81. [Test]
  82. public void ColumnDataTypeTest ()
  83. {
  84. OdbcCommand dbcmd = conn.CreateCommand ();
  85. string sql = "SELECT * from test";
  86. dbcmd.CommandText = sql;
  87. IDataReader reader = dbcmd.ExecuteReader ();
  88. try {
  89. Assertion.AssertEquals ("GetDataTypeName returns invalid Type for column #1",
  90. "TinyInt", reader.GetDataTypeName (0));
  91. Assertion.AssertEquals ("GetDataTypeName returns invalid Type for column #2",
  92. "VarChar", reader.GetDataTypeName (1));
  93. // Test via method GetFieldType.ToString
  94. Assertion.AssertEquals ("GetFieldType returns invalid Type for column #1",
  95. "System.Byte", reader.GetFieldType (0).ToString ());
  96. Assertion.AssertEquals ("GetFieldType returns invalid Type for column #2",
  97. "System.String", reader.GetFieldType (1).ToString ());
  98. // Test via method GetSchemaTable
  99. reader = dbcmd.ExecuteReader ();
  100. DataTable schemaTable = reader.GetSchemaTable ();
  101. Assertion.AssertEquals ("GetSchemaTable.ColumnDataType failes for column #1",
  102. typeof (System.Byte), schemaTable.Rows [0]["DataType"]);
  103. Assertion.AssertEquals ("GetSchemaTable.ColumnDataType failes for column #1",
  104. typeof (System.String), schemaTable.Rows [1]["DataType"]);
  105. } finally {
  106. // clean up
  107. if (reader != null && !reader.IsClosed)
  108. reader.Close ();
  109. reader = null;
  110. CleanTestSetup ();
  111. CloseConnection ();
  112. }
  113. }
  114. [Test]
  115. public void GetNameTest ()
  116. {
  117. OdbcCommand dbcmd = conn.CreateCommand ();
  118. string sql = "SELECT * from test";
  119. dbcmd.CommandText = sql;
  120. OdbcDataReader reader = dbcmd.ExecuteReader ();
  121. try {
  122. Assertion.AssertEquals ("GetName failes ", "pk_tint", reader.GetName (0));
  123. } finally {
  124. // clean up
  125. if (reader != null && !reader.IsClosed)
  126. reader.Close ();
  127. reader = null;
  128. CleanTestSetup ();
  129. CloseConnection ();
  130. }
  131. }
  132. [Test]
  133. public void GetBytesTest ()
  134. {
  135. OdbcCommand cmd = conn.CreateCommand ();
  136. string sql = "SELECT * FROM test";
  137. cmd.CommandText = sql;
  138. OdbcDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  139. try {
  140. if (reader.Read ()) {
  141. // Get By Parts for the column blob
  142. int totalsize = 100;
  143. int buffsize = 5;
  144. int buffstart = 0;
  145. long retval = 0;
  146. long start = 0;
  147. byte [] val = new byte [totalsize];
  148. retval = reader.GetBytes (3, start, val, buffstart, buffsize);
  149. while (retval == buffsize) {
  150. start += buffsize;
  151. buffstart += buffsize;
  152. retval = reader.GetBytes (3, start, val, buffstart, buffsize);
  153. }
  154. buffstart += (int) retval;
  155. // assemble here.
  156. string col = System.Text.Encoding.Default.GetString (val, 0, buffstart);
  157. Assertion.AssertEquals ("The assembled value length does not match",
  158. 39, col.Length);
  159. }
  160. } finally {
  161. // clean up
  162. if (reader != null && !reader.IsClosed)
  163. reader.Close ();
  164. reader = null;
  165. CleanTestSetup ();
  166. CloseConnection ();
  167. }
  168. }
  169. [Test]
  170. public void GetBytesNullBufferTest ()
  171. {
  172. OdbcCommand cmd = conn.CreateCommand ();
  173. string sql = "SELECT * FROM test";
  174. cmd.CommandText = sql;
  175. OdbcDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  176. try {
  177. if (reader.Read ()) {
  178. Assertion.AssertEquals ("GetBytes on a fixed length column does not work!",
  179. 11, reader.GetBytes (1,0,null,0,0));
  180. Assertion.AssertEquals ("GetBytes with non null column does not work!",
  181. 39, reader.GetBytes (3,0,null,0,0));
  182. }
  183. // for null value, length in bytes should return 0
  184. if (reader.Read ())
  185. Assertion.AssertEquals ("GetBytes with null column does not return -1" ,
  186. -1, reader.GetBytes (3,0,null,0,0));
  187. } finally {
  188. // clean up
  189. if (reader != null && !reader.IsClosed )
  190. reader.Close ();
  191. reader = null;
  192. CleanTestSetup ();
  193. CloseConnection ();
  194. }
  195. }
  196. [Test]
  197. public void GetValueBinaryTest ()
  198. {
  199. OdbcCommand cmd = conn.CreateCommand ();
  200. string sql = "SELECT * FROM test";
  201. cmd.CommandText = sql;
  202. OdbcDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  203. try {
  204. if (reader.Read ()) {
  205. object ob = reader.GetValue (3);
  206. Assertion.AssertEquals ("Type of binary column is wrong!",
  207. "System.Byte[]", ob.GetType ().ToString () );
  208. }
  209. } finally {
  210. // clean up
  211. if (reader != null && !reader.IsClosed )
  212. reader.Close ();
  213. reader = null;
  214. CleanTestSetup ();
  215. CloseConnection ();
  216. }
  217. }
  218. [Test]
  219. public void GetDateTimeTest ()
  220. {
  221. OdbcCommand cmd = conn.CreateCommand ();
  222. string sql = "SELECT * FROM test";
  223. cmd.CommandText = sql;
  224. OdbcDataReader reader = cmd.ExecuteReader (CommandBehavior.Default);
  225. try {
  226. if (reader.Read ()) {
  227. object ob = reader["col_datetime"];
  228. Assertion.AssertEquals ("Type of datetime column is wrong!",
  229. "System.DateTime", ob.GetType ().ToString () );
  230. ob = reader["col_date"];
  231. Assertion.AssertEquals ("Type of date column is wrong!",
  232. "System.DateTime", ob.GetType ().ToString () );
  233. // FIXME : Once TIME data type is fixed, enable this check
  234. //ob = reader["col_time"];
  235. //Assertion.AssertEquals ("Type of time column is wrong!",
  236. //"System.DateTime", ob.GetType ().ToString () );
  237. DateTime dt = reader.GetDateTime (4);
  238. Assertion.AssertEquals ("DateValue (SQL_TIMESTAMP) is wrong", new DateTime (2004, 8, 22, 0, 0, 0), dt);
  239. dt = reader.GetDateTime (5);
  240. Assertion.AssertEquals ("DateValue (SQL_DATE) is wrong", new DateTime (2004, 8, 22, 0, 0, 0), dt);
  241. // FIXME : Once TIME data type is fixed, enable this check
  242. //dt = reader.GetDateTime (7);
  243. //Assertion.AssertEquals ("DateValue is wrong", "2004-08-22", dt.ToString ());
  244. }
  245. } finally {
  246. // clean up
  247. if (reader != null && !reader.IsClosed )
  248. reader.Close ();
  249. reader = null;
  250. CleanTestSetup ();
  251. CloseConnection ();
  252. }
  253. }
  254. [Test]
  255. public void NumericTest()
  256. {
  257. using(IDbConnection dbConnection = new OdbcConnection
  258. (connectionString))
  259. {
  260. dbConnection.Open();
  261. IDbCommand dbCommand = dbConnection.CreateCommand();
  262. //note this will fail if the table already exists, ie if the test has failed.
  263. dbCommand.CommandText = "CREATE TABLE NumericTable (NumericField NUMERIC(10) NOT NULL)";
  264. dbCommand.ExecuteNonQuery();
  265. dbCommand.CommandText = "INSERT INTO NumericTable (NumericField) VALUES (125)";
  266. dbCommand.ExecuteNonQuery();
  267. dbCommand.CommandText = "SELECT * FROM NumericTable";
  268. using(IDataReader reader = dbCommand.ExecuteReader())
  269. {
  270. while(reader.Read())
  271. {
  272. for(int index = 0; index < reader.FieldCount; index++)
  273. {
  274. Object dataValue = reader.GetValue(index);
  275. Assert.AreEqual("System.Decimal",dataValue.GetType().ToString());
  276. Assert.AreEqual("125", dataValue.ToString());
  277. }
  278. }
  279. }
  280. dbCommand.CommandText = "DROP TABLE NumericTable";
  281. dbCommand.ExecuteNonQuery();
  282. }
  283. }
  284. /// <summary>
  285. /// This test for the return type &amp; value for GetValue
  286. /// in case of Odbc Data type TINYINT
  287. /// </summary>
  288. [Test]
  289. public void TinyIntTest ()
  290. {
  291. OdbcCommand cmd = conn.CreateCommand ();
  292. string sql = "SELECT * FROM test";
  293. cmd.CommandText = sql;
  294. OdbcDataReader reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  295. try {
  296. if (reader.Read ()) {
  297. object ob = reader.GetValue (0);
  298. Assertion.AssertEquals ("Type of tinyInt column is wrong!",
  299. "System.Byte", ob.GetType ().ToString () );
  300. Assertion.AssertEquals ("Value of tinyInt column is wrong!",
  301. 1, System.Convert.ToInt16(ob) );
  302. }
  303. } finally {
  304. // clean up
  305. if (reader != null && !reader.IsClosed )
  306. reader.Close ();
  307. reader = null;
  308. CleanTestSetup ();
  309. CloseConnection ();
  310. }
  311. }
  312. }
  313. }