SqlCommandTest.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // SqlCommandTest.cs - NUnit Test Cases for testing the
  3. // SqlCommand class
  4. // Author:
  5. // Umadevi S ([email protected])
  6. //
  7. // Copyright (c) 2004 Novell Inc., and the individuals listed
  8. // on the ChangeLog entries.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Data;
  31. using System.Data.Common;
  32. using System.Data.SqlClient;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Data.SqlClient {
  35. [TestFixture]
  36. public class SqlCommandTest : MSSqlTestClient {
  37. [SetUp]
  38. public void GetReady () {
  39. OpenConnection ();
  40. }
  41. [TearDown]
  42. public void Clean () {
  43. CloseConnection ();
  44. }
  45. private void CreateInsertEmployeeSP() {
  46. string createquery = "CREATE PROCEDURE #Insert_Employee @TestPar1 varchar(50),@BirthDate datetime as insert into Employees(LastName,FirstName) VALUES('SSS','uuuu') ";
  47. SqlCommand cmd = new SqlCommand();
  48. cmd.Connection = conn;
  49. cmd.CommandText = createquery;
  50. cmd.ExecuteNonQuery();
  51. }
  52. private void CreateBug66630SP() {
  53. SqlCommand cmd = conn.CreateCommand();
  54. cmd.CommandType = CommandType.Text;
  55. cmd.CommandText = "CREATE PROCEDURE #Bug66630 ("
  56. + "@Status smallint = 7"
  57. + ")"
  58. + "AS" + Environment.NewLine
  59. + "BEGIN" + Environment.NewLine
  60. + "SELECT CAST(5 AS int), @Status" + Environment.NewLine
  61. + "END";
  62. cmd.ExecuteNonQuery();
  63. }
  64. [Test]
  65. public void ExecuteNonQueryTest () {
  66. // create temp sp here, should normally be created in Setup of test
  67. // case, but cannot be done right now because of ug #68978
  68. CreateInsertEmployeeSP();
  69. try {
  70. SqlCommand cmd = new SqlCommand();
  71. cmd.Connection = conn;
  72. cmd.CommandText = "#Insert_Employee";
  73. cmd.CommandType = CommandType.StoredProcedure;
  74. Object TestPar = DBNull.Value;
  75. cmd.Parameters.Add("@TestPar1", SqlDbType.Int);
  76. cmd.Parameters["@TestPar1"].Value = TestPar;
  77. cmd.Parameters.Add("@BirthDate", DateTime.Now);
  78. Assert.AreEqual(-1,cmd.ExecuteNonQuery());
  79. } catch (Exception e) {
  80. Assert.Fail("A#01 Got an exception");
  81. Console.WriteLine(e.Message);
  82. Console.WriteLine(e.StackTrace);
  83. } finally {
  84. // gracefully close connection
  85. CloseConnection ();
  86. }
  87. }
  88. /**
  89. * Verifies whether an enum value is converted to a numeric value when
  90. * used as value for a numeric parameter (bug #66630)
  91. */
  92. [Test]
  93. public void EnumParameterTest() {
  94. // create temp sp here, should normally be created in Setup of test
  95. // case, but cannot be done right now because of ug #68978
  96. CreateBug66630SP();
  97. SqlCommand cmd = new SqlCommand("#Bug66630", conn);
  98. cmd.CommandType = CommandType.StoredProcedure;
  99. cmd.Parameters.Add("@Status", SqlDbType.Int).Value = Status.Error;
  100. using (SqlDataReader dr = cmd.ExecuteReader()) {
  101. // one record should be returned
  102. Assert.IsTrue(dr.Read(), "EnumParameterTest#1");
  103. // we should get two field in the result
  104. Assert.AreEqual(2, dr.FieldCount, "EnumParameterTest#2");
  105. // field 1
  106. Assert.AreEqual("int", dr.GetDataTypeName(0), "EnumParameterTest#3");
  107. Assert.AreEqual(5, dr.GetInt32(0), "EnumParameterTest#4");
  108. // field 2
  109. Assert.AreEqual("smallint", dr.GetDataTypeName(1), "EnumParameterTest#5");
  110. Assert.AreEqual((short) Status.Error, dr.GetInt16(1), "EnumParameterTest#6");
  111. // only one record should be returned
  112. Assert.IsFalse(dr.Read(), "EnumParameterTest#7");
  113. }
  114. }
  115. [Test]
  116. /**
  117. * The below test does not need a connection but since the setup opens
  118. * the connection i will need to close it
  119. */
  120. public void CloneTest() {
  121. SqlCommand cmd = new SqlCommand();
  122. cmd.Connection = null;
  123. cmd.CommandText = "sp_insert";
  124. cmd.CommandType = CommandType.StoredProcedure;
  125. Object TestPar = DBNull.Value;
  126. cmd.Parameters.Add("@TestPar1", SqlDbType.Int);
  127. cmd.Parameters["@TestPar1"].Value = TestPar;
  128. cmd.Parameters.Add("@BirthDate", DateTime.Now);
  129. cmd.DesignTimeVisible = true;
  130. cmd.CommandTimeout = 100;
  131. Object clone1 = ((ICloneable)(cmd)).Clone();
  132. SqlCommand cmd1 = (SqlCommand) clone1;
  133. Assert.AreEqual(2, cmd1.Parameters.Count);
  134. Assert.AreEqual(100, cmd1.CommandTimeout);
  135. cmd1.Parameters.Add("@test", DateTime.Now);
  136. // to check that it is deep copy and not a shallow copy of the
  137. // parameter collection
  138. Assert.AreEqual(3, cmd1.Parameters.Count);
  139. Assert.AreEqual(2, cmd.Parameters.Count);
  140. }
  141. private enum Status {
  142. OK = 0,
  143. Error = 3
  144. }
  145. }
  146. }