OdbcDataReaderTest.cs 15 KB

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