OdbcDataReaderTest.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.Common;
  33. using System.Data.Odbc;
  34. using Mono.Data;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Data
  37. {
  38. [TestFixture]
  39. [Category ("odbc")]
  40. public class OdbcDataReaderTest
  41. {
  42. [Test]
  43. public void OutputParametersTest ()
  44. {
  45. IDbConnection conn = ConnectionManager.Singleton.Connection;
  46. try {
  47. ConnectionManager.Singleton.OpenConnection ();
  48. IDbCommand cmd = conn.CreateCommand ();
  49. cmd.CommandText = "call {? = sp_get_age (?,?)}";
  50. OdbcParameter ret = (OdbcParameter) new OdbcParameter ("ret", OdbcType.Int);
  51. cmd.Parameters.Add (ret);
  52. ret.Direction = ParameterDirection.ReturnValue;
  53. OdbcParameter name = (OdbcParameter) new OdbcParameter ("name", OdbcType.VarChar);
  54. cmd.Parameters.Add (name);
  55. name.Direction = ParameterDirection.Input;
  56. name.Value = "suresh";
  57. OdbcParameter age = (OdbcParameter) new OdbcParameter ("age", OdbcType.Int);
  58. cmd.Parameters.Add (age);
  59. name.Direction = ParameterDirection.Output;
  60. IDataReader reader = cmd.ExecuteReader ();
  61. reader.Close ();
  62. Assert.AreEqual (true, ((int) ret.Value) > 0, "#1");
  63. } finally {
  64. ConnectionManager.Singleton.CloseConnection ();
  65. }
  66. }
  67. [Test]
  68. public void LongTextTest ()
  69. {
  70. IDbConnection conn = new OdbcConnection (
  71. ConnectionManager.Singleton.ConnectionString);
  72. IDataReader rdr = null;
  73. try {
  74. conn.Open ();
  75. IDbCommand cmd = conn.CreateCommand ();
  76. cmd.CommandText = "Select type_text";
  77. cmd.CommandText += " from string_family where id=3";
  78. rdr = cmd.ExecuteReader ();
  79. rdr.Read ();
  80. rdr.GetValue (0);
  81. }finally {
  82. if (rdr != null)
  83. rdr.Close ();
  84. conn.Close ();
  85. }
  86. }
  87. #if NET_2_0
  88. [Test]
  89. public void GetDataTypeNameTest ()
  90. {
  91. IDbConnection conn = ConnectionManager.Singleton.Connection;
  92. IDataReader reader = null;
  93. try {
  94. ConnectionManager.Singleton.OpenConnection ();
  95. IDbCommand OdbcCmd = conn.CreateCommand ();
  96. OdbcCmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
  97. reader = OdbcCmd.ExecuteReader ();
  98. Assert.AreEqual ("integer", reader.GetDataTypeName (0), "#1 GetDataTypeName should return integer not Int");
  99. Assert.AreEqual ("varchar", reader.GetDataTypeName (2), "#2 GetDataTypeName should return varchar not VarChar");
  100. Assert.AreEqual ("datetime", reader.GetDataTypeName (4), "#3 GetDataTypeName should return datetime not DateTime");
  101. } finally {
  102. if (reader != null)
  103. reader.Close ();
  104. ConnectionManager.Singleton.CloseConnection ();
  105. }
  106. }
  107. [Test]
  108. public void GetDataTypeNameIndexOutOfRangeExceptionTest ()
  109. {
  110. IDbConnection conn = ConnectionManager.Singleton.Connection;
  111. IDataReader reader = null;
  112. try {
  113. ConnectionManager.Singleton.OpenConnection ();
  114. IDbCommand OdbcCmd = conn.CreateCommand ();
  115. OdbcCmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
  116. reader = OdbcCmd.ExecuteReader ();
  117. try {
  118. /*string tmp = */reader.GetDataTypeName (6);
  119. } catch (IndexOutOfRangeException) {
  120. return;
  121. } Assert.Fail ("Expected Exception IndexOutOfRangeException not thrown");
  122. } finally {
  123. if (reader != null)
  124. reader.Close ();
  125. ConnectionManager.Singleton.CloseConnection ();
  126. }
  127. }
  128. [Test]
  129. public void GetOrdinalTest ()
  130. {
  131. IDbConnection conn = ConnectionManager.Singleton.Connection;
  132. IDataReader reader = null;
  133. try {
  134. ConnectionManager.Singleton.OpenConnection ();
  135. IDbCommand OdbcCmd = conn.CreateCommand ();
  136. OdbcCmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
  137. reader = OdbcCmd.ExecuteReader ();
  138. Assert.AreEqual (0, reader.GetOrdinal ("id"), "#1 First column should be id");
  139. Assert.AreEqual (1, reader.GetOrdinal ("fname"), "#2 Second column should fname");
  140. Assert.AreEqual (2, reader.GetOrdinal ("lname"), "#3 Third column should lname");
  141. Assert.AreEqual (3, reader.GetOrdinal ("dob"), "#4 Fourth column should dob");
  142. Assert.AreEqual (4, reader.GetOrdinal ("doj"), "#5 Fifth column should doj");
  143. Assert.AreEqual (5, reader.GetOrdinal ("email"), "#6 Sixth column should email");
  144. } finally {
  145. if (reader != null)
  146. reader.Close ();
  147. ConnectionManager.Singleton.CloseConnection ();
  148. }
  149. }
  150. [Test]
  151. public void GetOrdinalIndexOutOfRangeExceptionTest ()
  152. {
  153. IDbConnection conn = ConnectionManager.Singleton.Connection;
  154. IDataReader reader = null;
  155. try {
  156. ConnectionManager.Singleton.OpenConnection ();
  157. IDbCommand OdbcCmd = conn.CreateCommand ();
  158. OdbcCmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
  159. reader = OdbcCmd.ExecuteReader ();
  160. try {
  161. /*int ord = */reader.GetOrdinal ("non_existing_column");
  162. } catch (IndexOutOfRangeException){
  163. return;
  164. } Assert.Fail("Expected Exception IndexOutOfRangeException not thrown");
  165. } finally {
  166. if (reader != null)
  167. reader.Close ();
  168. ConnectionManager.Singleton.CloseConnection ();
  169. }
  170. }
  171. [Test]
  172. public void GetFieldTypeTest ()
  173. {
  174. IDbConnection conn = ConnectionManager.Singleton.Connection;
  175. IDataReader reader = null;
  176. try {
  177. ConnectionManager.Singleton.OpenConnection ();
  178. IDbCommand OdbcCmd = conn.CreateCommand ();
  179. OdbcCmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
  180. reader = OdbcCmd.ExecuteReader ();
  181. Assert.AreEqual (typeof(int), reader.GetFieldType (0), "#1 Field type is not Integer");
  182. Assert.AreEqual (typeof(string), reader.GetFieldType (2), "#2 Field type is not Varchar");
  183. Assert.AreEqual (typeof(DateTime), reader.GetFieldType (4), "#3 Field type is not DateTime");
  184. } finally {
  185. if (reader != null)
  186. reader.Close ();
  187. ConnectionManager.Singleton.CloseConnection ();
  188. }
  189. }
  190. [Test]
  191. public void GetFieldTypeIndexOutOfRangeExceptionTest ()
  192. {
  193. IDbConnection conn = ConnectionManager.Singleton.Connection;
  194. IDataReader reader = null;;
  195. try {
  196. ConnectionManager.Singleton.OpenConnection ();
  197. IDbCommand OdbcCmd = conn.CreateCommand ();
  198. OdbcCmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
  199. reader = OdbcCmd.ExecuteReader ();
  200. try {
  201. /*String tmp = */reader.GetFieldType (6).ToString ();
  202. } catch (IndexOutOfRangeException){
  203. return;
  204. } Assert.Fail("Expected Exception IndexOutOfRangeException not thrown");
  205. } finally {
  206. if (reader != null)
  207. reader.Close ();
  208. ConnectionManager.Singleton.CloseConnection ();
  209. }
  210. }
  211. [Test]
  212. public void GetNameTest ()
  213. {
  214. IDbConnection conn = ConnectionManager.Singleton.Connection;
  215. IDataReader reader = null;
  216. try {
  217. ConnectionManager.Singleton.OpenConnection ();
  218. IDbCommand OdbcCmd = conn.CreateCommand ();
  219. OdbcCmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
  220. reader = OdbcCmd.ExecuteReader ();
  221. Assert.AreEqual ("id", reader.GetName (0), "#1 First column is not id");
  222. Assert.AreEqual ("fname", reader.GetName (1), "#2 Second column is not fname");
  223. Assert.AreEqual ("lname", reader.GetName (2), "#3 Third column is not lname");
  224. Assert.AreEqual ("dob", reader.GetName (3), "#4 Fourth column is not dob");
  225. Assert.AreEqual ("doj", reader.GetName (4), "#5 Fifth column is not doj");
  226. Assert.AreEqual ("email", reader.GetName (5), "#6 Sixth column is not email");
  227. } finally {
  228. if (reader != null)
  229. reader.Close ();
  230. ConnectionManager.Singleton.CloseConnection ();
  231. }
  232. }
  233. [Test]
  234. public void GetNameIndexOutOfRangeExceptionTest ()
  235. {
  236. IDbConnection conn = ConnectionManager.Singleton.Connection;
  237. IDataReader reader = null;
  238. try {
  239. ConnectionManager.Singleton.OpenConnection ();
  240. IDbCommand OdbcCmd = conn.CreateCommand ();
  241. OdbcCmd.CommandText = "SELECT * FROM employee WHERE lname='kumar'";
  242. reader = OdbcCmd.ExecuteReader ();
  243. try {
  244. /*String tmp = */reader.GetName (6);
  245. } catch (IndexOutOfRangeException){
  246. return;
  247. } Assert.Fail("Expected Exception IndexOutOfRangeException not thrown");
  248. } finally {
  249. if (reader != null)
  250. reader.Close ();
  251. ConnectionManager.Singleton.CloseConnection ();
  252. }
  253. }
  254. #endif
  255. }
  256. }