OdbcCommandTest.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. [Test]
  43. public void PrepareAndExecuteTest ()
  44. {
  45. IDbConnection conn = ConnectionManager.Singleton.Connection;
  46. try {
  47. ConnectionManager.Singleton.OpenConnection ();
  48. string tableName = DBHelper.GetRandomName ("PAE", 3);
  49. try {
  50. // setup table
  51. string query = "DROP TABLE " + tableName ;
  52. DBHelper.ExecuteNonQuery (conn, query);
  53. query = String.Format ("CREATE TABLE {0} ( id INT, small_id SMALLINT )",
  54. tableName);
  55. DBHelper.ExecuteNonQuery (conn, query);
  56. query = String.Format ("INSERT INTO {0} values (?, ?)", tableName);
  57. OdbcCommand cmd = (OdbcCommand) conn.CreateCommand ();
  58. cmd.CommandText = query;
  59. cmd.Prepare ();
  60. OdbcParameter param1 = cmd.Parameters.Add ("?", OdbcType.Int);
  61. OdbcParameter param2 = cmd.Parameters.Add ("?", OdbcType.SmallInt);
  62. param1.Value = 1;
  63. param2.Value = 5;
  64. cmd.ExecuteNonQuery ();
  65. param1.Value = 2;
  66. param2.Value = 6;
  67. cmd.ExecuteNonQuery ();
  68. cmd.CommandText = "select * from " + tableName;
  69. cmd.Parameters.Clear ();
  70. OdbcDataReader reader = cmd.ExecuteReader ();
  71. int count = 0;
  72. while (reader.Read ()){
  73. count++;
  74. }
  75. reader.Close ();
  76. Assert.AreEqual (2, count, "#1");
  77. } finally {
  78. DBHelper.ExecuteNonQuery (conn, "DROP TABLE " + tableName);
  79. }
  80. } finally {
  81. ConnectionManager.Singleton.CloseConnection ();
  82. }
  83. }
  84. /// <summary>
  85. /// Test String parameters to ODBC Command
  86. /// </summary>
  87. [Test]
  88. public void ExecuteStringParameterTest()
  89. {
  90. IDbConnection conn = ConnectionManager.Singleton.Connection;
  91. try {
  92. ConnectionManager.Singleton.OpenConnection ();
  93. OdbcCommand dbcmd = (OdbcCommand) conn.CreateCommand ();
  94. dbcmd.CommandType = CommandType.Text;
  95. dbcmd.CommandText = "select count(*) from employee where fname=?;";
  96. string colvalue = "suresh";
  97. OdbcParameter param = dbcmd.Parameters.Add("@un", OdbcType.VarChar);
  98. param.Value = colvalue;
  99. int count = Convert.ToInt32 (dbcmd.ExecuteScalar ());
  100. Assert.AreEqual (1, count, "#1 String parameter not passed correctly");
  101. } finally {
  102. ConnectionManager.Singleton.CloseConnection ();
  103. }
  104. }
  105. /// <summary>
  106. /// Test ExecuteNonQuery
  107. /// </summary>
  108. [Test]
  109. public void ExecuteNonQueryTest ()
  110. {
  111. IDbConnection conn = ConnectionManager.Singleton.Connection;
  112. try {
  113. ConnectionManager.Singleton.OpenConnection ();
  114. OdbcCommand dbcmd = (OdbcCommand) conn.CreateCommand ();
  115. dbcmd.CommandType = CommandType.Text;
  116. dbcmd.CommandText = "select count(*) from employee where id <= ?;";
  117. int value = 3;
  118. dbcmd.Parameters.Add("@un", OdbcType.Int).Value = value;
  119. int ret = dbcmd.ExecuteNonQuery();
  120. Assert.AreEqual (-1, ret, "#1 ExecuteNonQuery not working");
  121. try {
  122. // insert
  123. dbcmd = (OdbcCommand) conn.CreateCommand ();
  124. dbcmd.CommandType = CommandType.Text;
  125. dbcmd.CommandText = "insert into employee (id, fname, dob, doj) values " +
  126. " (6001, 'tttt', '1999-01-22', '2005-02-11');";
  127. ret = dbcmd.ExecuteNonQuery();
  128. Assert.AreEqual (1, ret, "#2 ExecuteNonQuery not working");
  129. } finally {
  130. // delete
  131. dbcmd = (OdbcCommand) conn.CreateCommand ();
  132. dbcmd.CommandType = CommandType.Text;
  133. dbcmd.CommandText = "delete from employee where id > 6000";
  134. ret = dbcmd.ExecuteNonQuery();
  135. Assert.AreEqual (true, ret > 0, "#3 ExecuteNonQuery for deletion not working");
  136. }
  137. } finally {
  138. ConnectionManager.Singleton.CloseConnection ();
  139. }
  140. }
  141. }
  142. }