OdbcCommandTest.cs 8.8 KB

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