OdbcDataReaderTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. }
  219. }