2
0

SqlCommandTest.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // SqlCommandTest.cs - NUnit Test Cases for testing the
  3. // SqlCommand class
  4. // Author:
  5. // Umadevi S ([email protected])
  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.SqlClient;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Data.SqlClient
  36. {
  37. [TestFixture]
  38. [Category ("sqlserver")]
  39. public class SqlCommandTest
  40. {
  41. public SqlConnection conn;
  42. [Test]
  43. public void ExecuteNonQueryTempProcedureTest () {
  44. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  45. try {
  46. ConnectionManager.Singleton.OpenConnection ();
  47. // create temp sp here, should normally be created in Setup of test
  48. // case, but cannot be done right now because of ug #68978
  49. DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
  50. SqlCommand cmd = new SqlCommand();
  51. cmd.Connection = conn;
  52. cmd.CommandText = "#sp_temp_insert_employee";
  53. cmd.CommandType = CommandType.StoredProcedure;
  54. Object TestPar = "test";
  55. cmd.Parameters.Add("@fname", SqlDbType.VarChar);
  56. cmd.Parameters ["@fname"].Value = TestPar;
  57. Assert.AreEqual(-1,cmd.ExecuteNonQuery());
  58. } finally {
  59. DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
  60. DBHelper.ExecuteSimpleSP (conn, "sp_clean_person_table");
  61. ConnectionManager.Singleton.CloseConnection ();
  62. }
  63. }
  64. /**
  65. * Verifies whether an enum value is converted to a numeric value when
  66. * used as value for a numeric parameter (bug #66630)
  67. */
  68. [Test]
  69. public void EnumParameterTest() {
  70. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  71. try {
  72. ConnectionManager.Singleton.OpenConnection ();
  73. // create temp sp here, should normally be created in Setup of test
  74. // case, but cannot be done right now because of ug #68978
  75. DBHelper.ExecuteNonQuery (conn, "CREATE PROCEDURE #Bug66630 ("
  76. + "@Status smallint = 7"
  77. + ")"
  78. + "AS" + Environment.NewLine
  79. + "BEGIN" + Environment.NewLine
  80. + "SELECT CAST(5 AS int), @Status" + Environment.NewLine
  81. + "END");
  82. SqlCommand cmd = new SqlCommand("#Bug66630", conn);
  83. cmd.CommandType = CommandType.StoredProcedure;
  84. cmd.Parameters.Add("@Status", SqlDbType.Int).Value = Status.Error;
  85. using (SqlDataReader dr = cmd.ExecuteReader()) {
  86. // one record should be returned
  87. Assert.IsTrue(dr.Read(), "EnumParameterTest#1");
  88. // we should get two field in the result
  89. Assert.AreEqual(2, dr.FieldCount, "EnumParameterTest#2");
  90. // field 1
  91. Assert.AreEqual("int", dr.GetDataTypeName(0), "EnumParameterTest#3");
  92. Assert.AreEqual(5, dr.GetInt32(0), "EnumParameterTest#4");
  93. // field 2
  94. Assert.AreEqual("smallint", dr.GetDataTypeName(1), "EnumParameterTest#5");
  95. Assert.AreEqual((short) Status.Error, dr.GetInt16(1), "EnumParameterTest#6");
  96. // only one record should be returned
  97. Assert.IsFalse(dr.Read(), "EnumParameterTest#7");
  98. }
  99. } finally {
  100. DBHelper.ExecuteNonQuery (conn, "if exists (select name from sysobjects " +
  101. " where name like '#temp_Bug66630' and type like 'P') " +
  102. " drop procedure #temp_Bug66630; ");
  103. ConnectionManager.Singleton.CloseConnection ();
  104. }
  105. }
  106. /**
  107. * The below test does not need a connection but since the setup opens
  108. * the connection i will need to close it
  109. */
  110. [Test]
  111. public void CloneTest() {
  112. ConnectionManager.Singleton.OpenConnection ();
  113. SqlCommand cmd = new SqlCommand();
  114. cmd.Connection = null;
  115. cmd.CommandText = "sp_insert";
  116. cmd.CommandType = CommandType.StoredProcedure;
  117. Object TestPar = DBNull.Value;
  118. cmd.Parameters.Add("@TestPar1", SqlDbType.Int);
  119. cmd.Parameters["@TestPar1"].Value = TestPar;
  120. cmd.Parameters.Add("@BirthDate", DateTime.Now);
  121. cmd.DesignTimeVisible = true;
  122. cmd.CommandTimeout = 100;
  123. Object clone1 = ((ICloneable)(cmd)).Clone();
  124. SqlCommand cmd1 = (SqlCommand) clone1;
  125. Assert.AreEqual(2, cmd1.Parameters.Count);
  126. Assert.AreEqual(100, cmd1.CommandTimeout);
  127. cmd1.Parameters.Add("@test", DateTime.Now);
  128. // to check that it is deep copy and not a shallow copy of the
  129. // parameter collection
  130. Assert.AreEqual(3, cmd1.Parameters.Count);
  131. Assert.AreEqual(2, cmd.Parameters.Count);
  132. }
  133. private enum Status {
  134. OK = 0,
  135. Error = 3
  136. }
  137. private readonly string CREATE_TMP_SP_TEMP_INSERT_PERSON = ("create procedure #sp_temp_insert_employee ( " + Environment.NewLine +
  138. "@fname varchar (20), " + Environment.NewLine +
  139. "as " + Environment.NewLine +
  140. "begin" + Environment.NewLine +
  141. "declare @id int;" + Environment.NewLine +
  142. "select @id = max (id) from employee;" + Environment.NewLine +
  143. "set @id = @id + 6000 + 1;" + Environment.NewLine +
  144. "insert into employee (id, fname, dob, doj) values (@id, @fname, '1980-02-11', getdate ());" + Environment.NewLine +
  145. "return @id;" + Environment.NewLine +
  146. "end");
  147. private readonly string DROP_TMP_SP_TEMP_INSERT_PERSON = ("if exists (select name from sysobjects where " + Environment.NewLine +
  148. "name = '#sp_temp_insert_employee' and type = 'P') " + Environment.NewLine +
  149. "drop procedure #sp_temp_insert_employee; ");
  150. }
  151. }