OdbcCommandTest.cs 9.4 KB

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