SqlDataSourceViewTest.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // Tests for System.Web.UI.WebControls.SqlDataSourceView
  3. //
  4. // Author:
  5. // Chris Toshok ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  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. #if NET_2_0
  30. using NUnit.Framework;
  31. using System;
  32. using System.Configuration;
  33. using System.Data.Common;
  34. using System.IO;
  35. using System.Globalization;
  36. using System.Web;
  37. using System.Web.UI;
  38. using System.Web.UI.WebControls;
  39. namespace MonoTests.System.Web.UI.WebControls
  40. {
  41. class SqlViewPoker : SqlDataSourceView {
  42. public SqlViewPoker (SqlDataSource ds, string name, HttpContext context)
  43. : base (ds, name, context)
  44. {
  45. TrackViewState ();
  46. }
  47. public object SaveToViewState ()
  48. {
  49. return SaveViewState ();
  50. }
  51. public void LoadFromViewState (object savedState)
  52. {
  53. LoadViewState (savedState);
  54. }
  55. }
  56. [TestFixture]
  57. public class SqlDataSourceViewTest {
  58. [Test]
  59. public void Defaults ()
  60. {
  61. SqlDataSource ds = new SqlDataSource ();
  62. SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
  63. Assert.IsTrue (sql.CancelSelectOnNullParameter, "A1");
  64. Assert.IsFalse (sql.CanDelete,"A2");
  65. Assert.IsFalse (sql.CanInsert,"A3");
  66. Assert.IsFalse (sql.CanPage,"A4");
  67. Assert.IsFalse (sql.CanRetrieveTotalRowCount,"A5");
  68. Assert.IsTrue (sql.CanSort,"A6");
  69. Assert.IsFalse (sql.CanUpdate,"A7");
  70. Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "A8");
  71. Assert.AreEqual ("", sql.DeleteCommand, "A9");
  72. Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.DeleteCommandType, "A10");
  73. Assert.IsNotNull (sql.DeleteParameters, "A11");
  74. Assert.AreEqual (0, sql.DeleteParameters.Count, "A12");
  75. Assert.AreEqual ("", sql.FilterExpression, "A13");
  76. Assert.IsNotNull (sql.FilterParameters, "A14");
  77. Assert.AreEqual (0, sql.FilterParameters.Count, "A15");
  78. Assert.AreEqual ("", sql.InsertCommand, "A16");
  79. Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.InsertCommandType, "A17");
  80. Assert.IsNotNull (sql.InsertParameters, "A18");
  81. Assert.AreEqual (0, sql.InsertParameters.Count, "A19");
  82. Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A20");
  83. Assert.AreEqual ("", sql.SelectCommand, "A21");
  84. Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.SelectCommandType, "A22");
  85. Assert.IsNotNull (sql.SelectParameters, "A23");
  86. Assert.AreEqual (0, sql.SelectParameters.Count, "A24");
  87. Assert.AreEqual ("", sql.SortParameterName, "A25");
  88. Assert.AreEqual ("", sql.UpdateCommand, "A26");
  89. Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.UpdateCommandType, "A27");
  90. Assert.IsNotNull (sql.UpdateParameters, "A28");
  91. Assert.AreEqual (0, sql.UpdateParameters.Count, "A29");
  92. }
  93. [Test]
  94. [Category ("NotWorking")]
  95. public void ViewState ()
  96. {
  97. SqlDataSource ds = new SqlDataSource ();
  98. SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
  99. /* XXX test parameters */
  100. sql.CancelSelectOnNullParameter = false;
  101. sql.ConflictDetection = ConflictOptions.CompareAllValues;
  102. sql.DeleteCommandType = SqlDataSourceCommandType.Text;
  103. sql.DeleteCommand = "delete command";
  104. sql.FilterExpression = "filter expression";
  105. sql.InsertCommand = "insert command";
  106. sql.InsertCommandType = SqlDataSourceCommandType.Text;
  107. sql.OldValuesParameterFormatString = "{1}";
  108. sql.SelectCommand = "select command";
  109. sql.SelectCommandType = SqlDataSourceCommandType.Text;
  110. sql.SortParameterName = "sort parameter";
  111. sql.UpdateCommand = "update command";
  112. sql.UpdateCommandType = SqlDataSourceCommandType.Text;
  113. Assert.IsFalse (sql.CancelSelectOnNullParameter, "A1");
  114. Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "A2");
  115. Assert.AreEqual ("delete command", sql.DeleteCommand, "A3");
  116. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "A4");
  117. Assert.AreEqual ("filter expression", sql.FilterExpression, "A5");
  118. Assert.AreEqual ("insert command", sql.InsertCommand, "A6");
  119. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "A7");
  120. Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "A8");
  121. Assert.AreEqual ("select command", sql.SelectCommand, "A9");
  122. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "A10");
  123. Assert.AreEqual ("sort parameter", sql.SortParameterName, "A11");
  124. Assert.AreEqual ("update command", sql.UpdateCommand, "A12");
  125. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "A13");
  126. object state = sql.SaveToViewState();
  127. sql = new SqlViewPoker (ds, "DefaultView", null);
  128. sql.LoadFromViewState (state);
  129. Assert.IsFalse (sql.CancelSelectOnNullParameter, "B1");
  130. Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "B2");
  131. Assert.AreEqual ("delete command", sql.DeleteCommand, "B3");
  132. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "B4");
  133. Assert.AreEqual ("filter expression", sql.FilterExpression, "B5");
  134. Assert.AreEqual ("insert command", sql.InsertCommand, "B6");
  135. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "B7");
  136. Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "B8");
  137. Assert.AreEqual ("select command", sql.SelectCommand, "B9");
  138. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "B10");
  139. Assert.AreEqual ("sort parameter", sql.SortParameterName, "B11");
  140. Assert.AreEqual ("update command", sql.UpdateCommand, "B12");
  141. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "B13");
  142. }
  143. [Test]
  144. public void CanDelete ()
  145. {
  146. SqlDataSource ds = new SqlDataSource ();
  147. SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
  148. sql.DeleteCommand = "DELETE from foo";
  149. Assert.IsTrue (sql.CanDelete, "A1");
  150. sql.DeleteCommand = "";
  151. Assert.IsFalse (sql.CanDelete, "A2");
  152. sql.DeleteCommand = null;
  153. Assert.IsFalse (sql.CanDelete, "A3");
  154. }
  155. [Test]
  156. public void CanInsert ()
  157. {
  158. SqlDataSource ds = new SqlDataSource ();
  159. SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
  160. sql.InsertCommand = "INSERT into foo";
  161. Assert.IsTrue (sql.CanInsert, "A1");
  162. sql.InsertCommand = "";
  163. Assert.IsFalse (sql.CanInsert, "A2");
  164. sql.InsertCommand = null;
  165. Assert.IsFalse (sql.CanInsert, "A3");
  166. }
  167. [Test]
  168. public void CanUpdate ()
  169. {
  170. SqlDataSource ds = new SqlDataSource ();
  171. SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
  172. sql.UpdateCommand = "UPDATE foo";
  173. Assert.IsTrue (sql.CanUpdate, "A1");
  174. sql.UpdateCommand = "";
  175. Assert.IsFalse (sql.CanUpdate, "A2");
  176. sql.UpdateCommand = null;
  177. Assert.IsFalse (sql.CanUpdate, "A3");
  178. }
  179. [Test]
  180. public void CanSort ()
  181. {
  182. SqlDataSource ds = new SqlDataSource ();
  183. SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
  184. sql.SortParameterName = "foo";
  185. Assert.IsTrue (sql.CanSort, "A1");
  186. sql.SortParameterName = null;
  187. Assert.IsTrue (sql.CanSort, "A2");
  188. sql.SortParameterName = "";
  189. Assert.IsTrue (sql.CanSort, "A3");
  190. sql.SortParameterName = "foo";
  191. ds.DataSourceMode = SqlDataSourceMode.DataReader;
  192. Assert.IsTrue (sql.CanSort, "A4");
  193. ds.DataSourceMode = SqlDataSourceMode.DataSet;
  194. Assert.IsTrue (sql.CanSort, "A5");
  195. sql.SortParameterName = "";
  196. ds.DataSourceMode = SqlDataSourceMode.DataReader;
  197. Assert.IsFalse (sql.CanSort, "A6");
  198. ds.DataSourceMode = SqlDataSourceMode.DataSet;
  199. Assert.IsTrue (sql.CanSort, "A7");
  200. }
  201. [Test]
  202. public void OldValuesParameterFormatString ()
  203. {
  204. SqlDataSource ds = new SqlDataSource ();
  205. Assert.AreEqual ("{0}", ds.OldValuesParameterFormatString, "A1");
  206. ds.OldValuesParameterFormatString = "hi {0}";
  207. SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
  208. Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A2");
  209. ds.OldValuesParameterFormatString = "hi {0}";
  210. Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A3");
  211. ds.OldValuesParameterFormatString = "{0}";
  212. sql.OldValuesParameterFormatString = "hi {0}";
  213. Assert.AreEqual ("{0}", ds.OldValuesParameterFormatString, "A4");
  214. }
  215. [Test]
  216. public void CancelSelectOnNullParameter ()
  217. {
  218. SqlDataSource ds = new SqlDataSource ();
  219. ds.CancelSelectOnNullParameter = false;
  220. SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
  221. Assert.IsTrue (sql.CancelSelectOnNullParameter, "A1");
  222. ds.CancelSelectOnNullParameter = true;
  223. sql.CancelSelectOnNullParameter = false;
  224. Assert.IsTrue (ds.CancelSelectOnNullParameter, "A2");
  225. sql.CancelSelectOnNullParameter = false;
  226. ds.CancelSelectOnNullParameter = true;
  227. Assert.IsFalse (sql.CancelSelectOnNullParameter, "A3");
  228. }
  229. }
  230. }
  231. #endif