SqlCommandTest.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // SqlCommandTest.cs - NUnit Test Cases for testing
  3. // System.Data.SqlClient.SqlCommand
  4. //
  5. // Author:
  6. // Gert Driesen ([email protected])
  7. //
  8. // Copyright (c) 2007 Gert Driesen
  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.Data;
  30. using System.Data.SqlClient;
  31. using NUnit.Framework;
  32. namespace MonoTests.System.Data.SqlClient
  33. {
  34. [TestFixture]
  35. public class SqlCommandTest
  36. {
  37. const string COMMAND_TEXT = "SELECT * FROM Authors";
  38. [Test] // SqlCommand ()
  39. public void Constructor1 ()
  40. {
  41. SqlCommand cmd = new SqlCommand ();
  42. Assert.AreEqual (string.Empty, cmd.CommandText, "#1");
  43. Assert.AreEqual (30, cmd.CommandTimeout, "#2");
  44. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#3");
  45. Assert.IsNull (cmd.Connection, "#4");
  46. Assert.IsNull (cmd.Container, "#5");
  47. Assert.IsTrue (cmd.DesignTimeVisible, "#6");
  48. #if NET_2_0
  49. Assert.IsNull (cmd.Notification, "#7");
  50. Assert.IsTrue (cmd.NotificationAutoEnlist, "#8");
  51. #endif
  52. Assert.IsNotNull (cmd.Parameters, "#9");
  53. Assert.AreEqual (0, cmd.Parameters.Count, "#10");
  54. Assert.IsNull (cmd.Site, "#11");
  55. Assert.IsNull (cmd.Transaction, "#11");
  56. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#12");
  57. }
  58. [Test] // SqlCommand (string)
  59. public void Constructor2 ()
  60. {
  61. SqlCommand cmd = new SqlCommand (COMMAND_TEXT);
  62. Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
  63. Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
  64. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
  65. Assert.IsNull (cmd.Connection, "#A4");
  66. Assert.IsNull (cmd.Container, "#A5");
  67. Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
  68. #if NET_2_0
  69. Assert.IsNull (cmd.Notification, "#A7");
  70. Assert.IsTrue (cmd.NotificationAutoEnlist, "#A8");
  71. #endif
  72. Assert.IsNotNull (cmd.Parameters, "#A9");
  73. Assert.AreEqual (0, cmd.Parameters.Count, "#A10");
  74. Assert.IsNull (cmd.Site, "#A11");
  75. Assert.IsNull (cmd.Transaction, "#A12");
  76. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A13");
  77. cmd = new SqlCommand ((string) null);
  78. Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
  79. Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
  80. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
  81. Assert.IsNull (cmd.Connection, "#B4");
  82. Assert.IsNull (cmd.Container, "#B5");
  83. Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
  84. #if NET_2_0
  85. Assert.IsNull (cmd.Notification, "#B7");
  86. Assert.IsTrue (cmd.NotificationAutoEnlist, "#B8");
  87. #endif
  88. Assert.IsNotNull (cmd.Parameters, "#B9");
  89. Assert.AreEqual (0, cmd.Parameters.Count, "#B10");
  90. Assert.IsNull (cmd.Site, "#B11");
  91. Assert.IsNull (cmd.Transaction, "#B12");
  92. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B13");
  93. }
  94. [Test] // SqlCommand (string, SqlConnection)
  95. public void Constructor3 ()
  96. {
  97. SqlConnection conn = new SqlConnection ();
  98. SqlCommand cmd;
  99. cmd = new SqlCommand (COMMAND_TEXT, conn);
  100. Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
  101. Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
  102. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
  103. Assert.AreSame (conn, cmd.Connection, "#A4");
  104. Assert.IsNull (cmd.Container, "#A5");
  105. Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
  106. #if NET_2_0
  107. Assert.IsNull (cmd.Notification, "#A7");
  108. Assert.IsTrue (cmd.NotificationAutoEnlist, "#A8");
  109. #endif
  110. Assert.IsNotNull (cmd.Parameters, "#A9");
  111. Assert.AreEqual (0, cmd.Parameters.Count, "#A10");
  112. Assert.IsNull (cmd.Site, "#A11");
  113. Assert.IsNull (cmd.Transaction, "#A12");
  114. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A13");
  115. cmd = new SqlCommand ((string) null, conn);
  116. Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
  117. Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
  118. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
  119. Assert.AreSame (conn, cmd.Connection, "#B4");
  120. Assert.IsNull (cmd.Container, "#B5");
  121. Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
  122. #if NET_2_0
  123. Assert.IsNull (cmd.Notification, "#B7");
  124. Assert.IsTrue (cmd.NotificationAutoEnlist, "#B8");
  125. #endif
  126. Assert.IsNotNull (cmd.Parameters, "#B9");
  127. Assert.AreEqual (0, cmd.Parameters.Count, "#B10");
  128. Assert.IsNull (cmd.Site, "#B11");
  129. Assert.IsNull (cmd.Transaction, "#B12");
  130. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B13");
  131. cmd = new SqlCommand (COMMAND_TEXT, (SqlConnection) null);
  132. Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#C1");
  133. Assert.AreEqual (30, cmd.CommandTimeout, "#C2");
  134. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#C3");
  135. Assert.IsNull (cmd.Connection, "#C4");
  136. Assert.IsNull (cmd.Container, "#C5");
  137. Assert.IsTrue (cmd.DesignTimeVisible, "#C6");
  138. #if NET_2_0
  139. Assert.IsNull (cmd.Notification, "#C7");
  140. Assert.IsTrue (cmd.NotificationAutoEnlist, "#C8");
  141. #endif
  142. Assert.IsNotNull (cmd.Parameters, "#C9");
  143. Assert.AreEqual (0, cmd.Parameters.Count, "#C10");
  144. Assert.IsNull (cmd.Site, "#C11");
  145. Assert.IsNull (cmd.Transaction, "#C12");
  146. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#C13");
  147. }
  148. [Test] // SqlCommand (string, SqlConnection, SqlTransaction)
  149. public void Constructor4 ()
  150. {
  151. SqlConnection conn = new SqlConnection ();
  152. SqlCommand cmd;
  153. cmd = new SqlCommand (COMMAND_TEXT, conn, (SqlTransaction) null);
  154. Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#A1");
  155. Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
  156. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
  157. Assert.AreSame (conn, cmd.Connection, "#A4");
  158. Assert.IsNull (cmd.Container, "#A5");
  159. Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
  160. #if NET_2_0
  161. Assert.IsNull (cmd.Notification, "#A7");
  162. Assert.IsTrue (cmd.NotificationAutoEnlist, "#A8");
  163. #endif
  164. Assert.IsNotNull (cmd.Parameters, "#A9");
  165. Assert.AreEqual (0, cmd.Parameters.Count, "#A10");
  166. Assert.IsNull (cmd.Site, "#A11");
  167. Assert.IsNull (cmd.Transaction, "#A12");
  168. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A13");
  169. cmd = new SqlCommand ((string) null, conn, (SqlTransaction) null);
  170. Assert.AreEqual (string.Empty, cmd.CommandText, "#B1");
  171. Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
  172. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
  173. Assert.AreSame (conn, cmd.Connection, "#B4");
  174. Assert.IsNull (cmd.Container, "#B5");
  175. Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
  176. #if NET_2_0
  177. Assert.IsNull (cmd.Notification, "#B7");
  178. Assert.IsTrue (cmd.NotificationAutoEnlist, "#B8");
  179. #endif
  180. Assert.IsNotNull (cmd.Parameters, "#B9");
  181. Assert.AreEqual (0, cmd.Parameters.Count, "#B10");
  182. Assert.IsNull (cmd.Site, "#B11");
  183. Assert.IsNull (cmd.Transaction, "#B12");
  184. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B13");
  185. cmd = new SqlCommand (COMMAND_TEXT, (SqlConnection) null, (SqlTransaction) null);
  186. Assert.AreEqual (COMMAND_TEXT, cmd.CommandText, "#C1");
  187. Assert.AreEqual (30, cmd.CommandTimeout, "#C2");
  188. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#C3");
  189. Assert.IsNull (cmd.Connection, "#C4");
  190. Assert.IsNull (cmd.Container, "#C5");
  191. Assert.IsTrue (cmd.DesignTimeVisible, "#C6");
  192. #if NET_2_0
  193. Assert.IsNull (cmd.Notification, "#C7");
  194. Assert.IsTrue (cmd.NotificationAutoEnlist, "#C8");
  195. #endif
  196. Assert.IsNotNull (cmd.Parameters, "#C9");
  197. Assert.AreEqual (0, cmd.Parameters.Count, "#C10");
  198. Assert.IsNull (cmd.Site, "#C11");
  199. Assert.IsNull (cmd.Transaction, "#C12");
  200. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#C13");
  201. }
  202. [Test] // bug #81710
  203. public void Dispose ()
  204. {
  205. string connectionString = "Initial Catalog=a;Server=b;User ID=c;"
  206. + "Password=d";
  207. SqlConnection connection = new SqlConnection (connectionString);
  208. SqlCommand command = connection.CreateCommand ();
  209. command.Dispose ();
  210. Assert.AreEqual (connectionString, connection.ConnectionString);
  211. }
  212. [Test]
  213. public void CommandText ()
  214. {
  215. SqlCommand cmd = new SqlCommand ();
  216. cmd.CommandText = COMMAND_TEXT;
  217. Assert.AreSame (COMMAND_TEXT, cmd.CommandText, "#1");
  218. cmd.CommandText = null;
  219. Assert.AreEqual (string.Empty, cmd.CommandText, "#2");
  220. cmd.CommandText = COMMAND_TEXT;
  221. Assert.AreSame (COMMAND_TEXT, cmd.CommandText, "#3");
  222. cmd.CommandText = string.Empty;
  223. Assert.AreEqual (string.Empty, cmd.CommandText, "#4");
  224. }
  225. }
  226. }