OdbcCommandTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // OdbcCommandTest.cs - NUnit Test Cases for testing the
  2. // OdbcCommand class
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email protected])
  6. // Umadevi S ([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 NUnit.Framework;
  35. namespace MonoTests.System.Data.Connected.Odbc
  36. {
  37. [TestFixture]
  38. [Category ("odbc")]
  39. public class OdbcCommandTest
  40. {
  41. OdbcConnection conn;
  42. OdbcCommand cmd;
  43. [SetUp]
  44. public void SetUp ()
  45. {
  46. conn = ConnectionManager.Instance.Odbc.Connection;
  47. cmd = conn.CreateCommand ();
  48. }
  49. [TearDown]
  50. public void TearDown ()
  51. {
  52. if (cmd != null)
  53. cmd.Dispose ();
  54. ConnectionManager.Instance.Close ();
  55. }
  56. [Test]
  57. public void PrepareAndExecuteTest ()
  58. {
  59. OdbcDataReader reader = null;
  60. try {
  61. string tableName = DBHelper.GetRandomName ("PAE", 3);
  62. try {
  63. // setup table
  64. string query = "DROP TABLE " + tableName ;
  65. DBHelper.ExecuteNonQuery (conn, query);
  66. query = String.Format ("CREATE TABLE {0} ( id INT, small_id SMALLINT )",
  67. tableName);
  68. DBHelper.ExecuteNonQuery (conn, query);
  69. query = String.Format ("INSERT INTO {0} values (?, ?)", tableName);
  70. cmd = conn.CreateCommand ();
  71. cmd.CommandText = query;
  72. cmd.Prepare ();
  73. OdbcParameter param1 = cmd.Parameters.Add ("?", OdbcType.Int);
  74. OdbcParameter param2 = cmd.Parameters.Add ("?", OdbcType.SmallInt);
  75. param1.Value = 1;
  76. param2.Value = 5;
  77. cmd.ExecuteNonQuery ();
  78. param1.Value = 2;
  79. param2.Value = 6;
  80. cmd.ExecuteNonQuery ();
  81. cmd.CommandText = "select id, small_id from " + tableName + " order by id asc";
  82. reader = cmd.ExecuteReader ();
  83. Assert.IsTrue (reader.Read (), "#A1");
  84. Assert.AreEqual (1, reader.GetValue (0), "#A2");
  85. Assert.AreEqual (5, reader.GetValue (1), "#A3");
  86. Assert.IsTrue (reader.Read (), "#A4");
  87. Assert.AreEqual (2, reader.GetValue (0), "#A5");
  88. Assert.AreEqual (6, reader.GetValue (1), "#A6");
  89. Assert.IsFalse (reader.Read (), "#A7");
  90. reader.Close ();
  91. cmd.Dispose ();
  92. cmd = conn.CreateCommand ();
  93. cmd.CommandText = "select id, small_id from " + tableName + " order by id asc";
  94. reader = cmd.ExecuteReader ();
  95. Assert.IsTrue (reader.Read (), "#B1");
  96. Assert.AreEqual (1, reader.GetValue (0), "#B2");
  97. Assert.AreEqual (5, reader.GetValue (1), "#B3");
  98. Assert.IsTrue (reader.Read (), "#B4");
  99. Assert.AreEqual (2, reader.GetValue (0), "#B5");
  100. Assert.AreEqual (6, reader.GetValue (1), "#B6");
  101. Assert.IsFalse (reader.Read (), "#B7");
  102. } finally {
  103. DBHelper.ExecuteNonQuery (conn, "DROP TABLE " + tableName);
  104. }
  105. } finally {
  106. if (reader != null)
  107. reader.Close ();
  108. }
  109. }
  110. /// <summary>
  111. /// Test String parameters to ODBC Command
  112. /// </summary>
  113. [Test]
  114. public void ExecuteStringParameterTest()
  115. {
  116. cmd.CommandText = "select count(*) from employee where fname=?;";
  117. string colvalue = "suresh";
  118. OdbcParameter param = cmd.Parameters.Add("@un", OdbcType.VarChar);
  119. param.Value = colvalue;
  120. int count = Convert.ToInt32 (cmd.ExecuteScalar ());
  121. Assert.AreEqual (1, count, "#1 String parameter not passed correctly");
  122. }
  123. /// <summary>
  124. /// Test ExecuteNonQuery
  125. /// </summary>
  126. [Test]
  127. public void ExecuteNonQueryTest ()
  128. {
  129. int ret;
  130. cmd.CommandType = CommandType.Text;
  131. cmd.CommandText = "select count(*) from employee where id <= ?;";
  132. cmd.Parameters.Add ("@un", OdbcType.Int).Value = 3;
  133. ret = cmd.ExecuteNonQuery ();
  134. switch (ConnectionManager.Instance.Odbc.EngineConfig.Type) {
  135. case EngineType.SQLServer:
  136. Assert.AreEqual (-1, ret, "#1");
  137. break;
  138. case EngineType.MySQL:
  139. Assert.AreEqual (1, ret, "#1");
  140. break;
  141. default:
  142. Assert.Fail ("Engine type not supported.");
  143. break;
  144. }
  145. cmd = conn.CreateCommand ();
  146. cmd.CommandType = CommandType.Text;
  147. cmd.CommandText = "select * from employee where id <= ?;";
  148. cmd.Parameters.Add ("@un", OdbcType.Int).Value = 3;
  149. ret = cmd.ExecuteNonQuery ();
  150. switch (ConnectionManager.Instance.Odbc.EngineConfig.Type) {
  151. case EngineType.SQLServer:
  152. Assert.AreEqual (-1, ret, "#2");
  153. break;
  154. case EngineType.MySQL:
  155. Assert.AreEqual (3, ret, "#2");
  156. break;
  157. default:
  158. Assert.Fail ("Engine type not supported.");
  159. break;
  160. }
  161. cmd = conn.CreateCommand ();
  162. cmd.CommandType = CommandType.Text;
  163. cmd.CommandText = "select * from employee where id <= 3;";
  164. ret = cmd.ExecuteNonQuery ();
  165. switch (ConnectionManager.Instance.Odbc.EngineConfig.Type) {
  166. case EngineType.SQLServer:
  167. Assert.AreEqual (-1, ret, "#3");
  168. break;
  169. case EngineType.MySQL:
  170. Assert.AreEqual (3, ret, "#3");
  171. break;
  172. default:
  173. Assert.Fail ("Engine type not supported.");
  174. break;
  175. }
  176. try {
  177. // insert
  178. cmd = conn.CreateCommand ();
  179. cmd.CommandType = CommandType.Text;
  180. cmd.CommandText = "insert into employee (id, fname, dob, doj) values " +
  181. " (6001, 'tttt', '1999-01-22', '2005-02-11');";
  182. ret = cmd.ExecuteNonQuery ();
  183. Assert.AreEqual (1, ret, "#4");
  184. cmd = conn.CreateCommand ();
  185. cmd.CommandType = CommandType.Text;
  186. cmd.CommandText = "insert into employee (id, fname, dob, doj) values " +
  187. " (?, 'tttt', '1999-01-22', '2005-02-11');";
  188. cmd.Parameters.Add (new OdbcParameter ("id", OdbcType.Int));
  189. cmd.Parameters [0].Value = 6002;
  190. cmd.Prepare ();
  191. ret = cmd.ExecuteNonQuery ();
  192. Assert.AreEqual (1, ret, "#5");
  193. } finally {
  194. // delete
  195. cmd = (OdbcCommand) conn.CreateCommand ();
  196. cmd.CommandType = CommandType.Text;
  197. cmd.CommandText = "delete from employee where id > 6000";
  198. ret = cmd.ExecuteNonQuery ();
  199. Assert.AreEqual (2, ret, "#6");
  200. }
  201. }
  202. [Test]
  203. public void ExecuteNonQuery_Query_Invalid ()
  204. {
  205. cmd.CommandText = "select id1 from numeric_family"; ;
  206. try {
  207. cmd.ExecuteNonQuery ();
  208. Assert.Fail ("#A1");
  209. } catch (OdbcException ex) {
  210. // Invalid column name 'id1'
  211. Assert.AreEqual (typeof (OdbcException), ex.GetType (), "#A2");
  212. Assert.IsNull (ex.InnerException, "#A3");
  213. Assert.IsNotNull (ex.Message, "#A5");
  214. }
  215. // ensure connection is not closed after error
  216. int result;
  217. cmd.CommandText = "INSERT INTO numeric_family (id, type_int) VALUES (6100, 200)";
  218. result = cmd.ExecuteNonQuery ();
  219. Assert.AreEqual (1, result, "#B1");
  220. cmd.CommandText = "DELETE FROM numeric_family WHERE id = 6100";
  221. result = cmd.ExecuteNonQuery ();
  222. Assert.AreEqual (1, result, "#B1");
  223. }
  224. [Test]
  225. public void Dispose ()
  226. {
  227. OdbcTransaction trans = null;
  228. try {
  229. trans = conn.BeginTransaction ();
  230. cmd.CommandText = "SELECT 'a'";
  231. cmd.CommandTimeout = 67;
  232. cmd.CommandType = CommandType.StoredProcedure;
  233. cmd.DesignTimeVisible = false;
  234. cmd.Parameters.Add (new OdbcParameter ());
  235. cmd.Transaction = trans;
  236. cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;
  237. cmd.Dispose ();
  238. Assert.AreEqual (string.Empty, cmd.CommandText, "#1");
  239. Assert.AreEqual (67, cmd.CommandTimeout, "#2");
  240. Assert.AreEqual (CommandType.StoredProcedure, cmd.CommandType, "#3");
  241. Assert.IsNull (cmd.Connection, "#4");
  242. Assert.IsFalse (cmd.DesignTimeVisible, "#5");
  243. Assert.IsNotNull (cmd.Parameters, "#6");
  244. Assert.AreEqual (0, cmd.Parameters.Count, "#7");
  245. Assert.IsNull (cmd.Transaction, "#8");
  246. Assert.AreEqual (UpdateRowSource.OutputParameters, cmd.UpdatedRowSource, "#9");
  247. } finally {
  248. if (trans != null)
  249. trans.Rollback ();
  250. }
  251. }
  252. [Test] // bug #341743
  253. public void Dispose_Connection_Disposed ()
  254. {
  255. cmd.CommandText = "SELECT 'a'";
  256. cmd.ExecuteNonQuery ();
  257. conn.Dispose ();
  258. Assert.AreSame (conn, cmd.Connection, "#1");
  259. cmd.Dispose ();
  260. Assert.IsNull (cmd.Connection, "#2");
  261. }
  262. }
  263. }