New test.
[mono.git] / mcs / class / System.Web / Test / System.Web.UI.WebControls / SqlDataSourceViewTest.cs
1 //
2 // Tests for System.Web.UI.WebControls.SqlDataSourceView
3 //
4 // Author:
5 //      Chris Toshok (toshok@novell.com)
6 //
7
8 //
9 // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
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
31 #if NET_2_0
32
33 using NUnit.Framework;
34 using System;
35 using System.Configuration;
36 using System.Data.Common;
37 using System.IO;
38 using System.Globalization;
39 using System.Web;
40 using System.Web.UI;
41 using System.Web.UI.WebControls;
42
43 namespace MonoTests.System.Web.UI.WebControls
44 {
45         class SqlViewPoker : SqlDataSourceView {
46                 public SqlViewPoker (SqlDataSource ds, string name, HttpContext context)
47                         : base (ds, name, context)
48                 {
49                         TrackViewState ();
50                 }
51
52                 public object SaveToViewState ()
53                 {
54                         return SaveViewState ();
55                 }
56
57                 public void LoadFromViewState (object savedState)
58                 {
59                         LoadViewState (savedState);
60                 }
61         }
62
63         [TestFixture]
64         public class SqlDataSourceViewTest {
65                 [Test]
66                 public void Defaults ()
67                 {
68                         SqlDataSource ds = new SqlDataSource ();
69                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
70
71                         Assert.IsTrue (sql.CancelSelectOnNullParameter, "A1");
72                         Assert.IsFalse (sql.CanDelete,"A2");
73                         Assert.IsFalse (sql.CanInsert,"A3");
74                         Assert.IsFalse (sql.CanPage,"A4");
75                         Assert.IsFalse (sql.CanRetrieveTotalRowCount,"A5");
76                         Assert.IsTrue (sql.CanSort,"A6");
77                         Assert.IsFalse (sql.CanUpdate,"A7");
78                         Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "A8");
79                         Assert.AreEqual ("", sql.DeleteCommand, "A9");
80                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "A10");
81                         Assert.IsNotNull (sql.DeleteParameters, "A11");
82                         Assert.AreEqual (0, sql.DeleteParameters.Count, "A12");
83                         Assert.AreEqual ("", sql.FilterExpression, "A13");
84                         Assert.IsNotNull (sql.FilterParameters, "A14");
85                         Assert.AreEqual (0, sql.FilterParameters.Count, "A15");
86                         Assert.AreEqual ("", sql.InsertCommand, "A16");
87                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "A17");
88                         Assert.IsNotNull (sql.InsertParameters, "A18");
89                         Assert.AreEqual (0, sql.InsertParameters.Count, "A19");
90                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A20");
91                         Assert.AreEqual ("", sql.SelectCommand, "A21");
92                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "A22");
93                         Assert.IsNotNull (sql.SelectParameters, "A23");
94                         Assert.AreEqual (0, sql.SelectParameters.Count, "A24");
95                         Assert.AreEqual ("", sql.SortParameterName, "A25");
96                         Assert.AreEqual ("", sql.UpdateCommand, "A26");
97                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "A27");
98                         Assert.IsNotNull (sql.UpdateParameters, "A28");
99                         Assert.AreEqual (0, sql.UpdateParameters.Count, "A29");
100                 }
101
102                 [Test]
103         [Category ("NotWorking")]
104         public void ViewState ()
105                 {
106                         SqlDataSource ds = new SqlDataSource ();
107                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
108
109                         /* XXX test parameters */
110
111                         sql.CancelSelectOnNullParameter = false;
112                         sql.ConflictDetection = ConflictOptions.CompareAllValues;
113                         sql.DeleteCommandType = SqlDataSourceCommandType.Text;
114                         sql.DeleteCommand = "delete command";
115                         sql.FilterExpression = "filter expression";
116                         sql.InsertCommand = "insert command";
117                         sql.InsertCommandType = SqlDataSourceCommandType.Text;
118                         sql.OldValuesParameterFormatString = "{1}";
119                         sql.SelectCommand = "select command";
120                         sql.SelectCommandType = SqlDataSourceCommandType.Text;
121                         sql.SortParameterName = "sort parameter";
122                         sql.UpdateCommand = "update command";
123                         sql.UpdateCommandType = SqlDataSourceCommandType.Text;
124
125                         Assert.IsFalse (sql.CancelSelectOnNullParameter, "A1");
126                         Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "A2");
127                         Assert.AreEqual ("delete command", sql.DeleteCommand, "A3");
128                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "A4");
129                         Assert.AreEqual ("filter expression", sql.FilterExpression, "A5");
130                         Assert.AreEqual ("insert command", sql.InsertCommand, "A6");
131                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "A7");
132                         Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "A8");
133                         Assert.AreEqual ("select command", sql.SelectCommand, "A9");
134                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "A10");
135                         Assert.AreEqual ("sort parameter", sql.SortParameterName, "A11");
136                         Assert.AreEqual ("update command", sql.UpdateCommand, "A12");
137                         Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "A13");
138
139                         object state = sql.SaveToViewState();
140
141                         sql = new SqlViewPoker (ds, "DefaultView", null);
142                         sql.LoadFromViewState (state);
143
144             Assert.IsTrue(sql.CancelSelectOnNullParameter, "B1");
145             Assert.IsFalse(sql.CanDelete, "B2");
146             Assert.IsFalse(sql.CanInsert, "B3");
147             Assert.IsFalse(sql.CanPage, "B4");
148             Assert.IsFalse(sql.CanRetrieveTotalRowCount, "B5");
149             Assert.IsTrue(sql.CanSort, "B6");
150             Assert.IsFalse(sql.CanUpdate, "B7");
151             Assert.AreEqual(ConflictOptions.OverwriteChanges, sql.ConflictDetection, "B8");
152             Assert.AreEqual("", sql.DeleteCommand, "B9");
153             Assert.AreEqual(SqlDataSourceCommandType.Text, sql.DeleteCommandType, "B10");
154             Assert.IsNotNull(sql.DeleteParameters, "B11");
155             Assert.AreEqual(0, sql.DeleteParameters.Count, "B12");
156             Assert.AreEqual("", sql.FilterExpression, "B13");
157             Assert.IsNotNull(sql.FilterParameters, "B14");
158             Assert.AreEqual(0, sql.FilterParameters.Count, "B15");
159             Assert.AreEqual("", sql.InsertCommand, "B16");
160             Assert.AreEqual(SqlDataSourceCommandType.Text, sql.InsertCommandType, "B17");
161             Assert.IsNotNull(sql.InsertParameters, "B18");
162             Assert.AreEqual(0, sql.InsertParameters.Count, "B19");
163             Assert.AreEqual("{0}", sql.OldValuesParameterFormatString, "B20");
164             Assert.AreEqual("", sql.SelectCommand, "B21");
165             Assert.AreEqual(SqlDataSourceCommandType.Text, sql.SelectCommandType, "B22");
166             Assert.IsNotNull(sql.SelectParameters, "B23");
167             Assert.AreEqual(0, sql.SelectParameters.Count, "B24");
168             Assert.AreEqual("", sql.SortParameterName, "B25");
169             Assert.AreEqual("", sql.UpdateCommand, "B26");
170             Assert.AreEqual(SqlDataSourceCommandType.Text, sql.UpdateCommandType, "B27");
171             Assert.IsNotNull(sql.UpdateParameters, "B28");
172             Assert.AreEqual(0, sql.UpdateParameters.Count, "B29");
173                 }
174
175                 [Test]
176                 public void CanDelete ()
177                 {
178                         SqlDataSource ds = new SqlDataSource ();
179                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
180
181                         sql.DeleteCommand = "DELETE from foo";
182                         Assert.IsTrue (sql.CanDelete, "A1");
183
184                         sql.DeleteCommand = "";
185                         Assert.IsFalse (sql.CanDelete, "A2");
186
187                         sql.DeleteCommand = null;
188                         Assert.IsFalse (sql.CanDelete, "A3");
189                 }
190
191                 [Test]
192                 public void CanInsert ()
193                 {
194                         SqlDataSource ds = new SqlDataSource ();
195                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
196
197                         sql.InsertCommand = "INSERT into foo";
198                         Assert.IsTrue (sql.CanInsert, "A1");
199
200                         sql.InsertCommand = "";
201                         Assert.IsFalse (sql.CanInsert, "A2");
202
203                         sql.InsertCommand = null;
204                         Assert.IsFalse (sql.CanInsert, "A3");
205                 }
206
207                 [Test]
208                 public void CanUpdate ()
209                 {
210                         SqlDataSource ds = new SqlDataSource ();
211                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
212
213                         sql.UpdateCommand = "UPDATE foo";
214                         Assert.IsTrue (sql.CanUpdate, "A1");
215
216                         sql.UpdateCommand = "";
217                         Assert.IsFalse (sql.CanUpdate, "A2");
218
219                         sql.UpdateCommand = null;
220                         Assert.IsFalse (sql.CanUpdate, "A3");
221                 }
222
223                 [Test]
224                 public void CanSort ()
225                 {
226                         SqlDataSource ds = new SqlDataSource ();
227                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
228
229                         sql.SortParameterName = "foo";
230                         Assert.IsTrue (sql.CanSort, "A1");
231
232                         sql.SortParameterName = null;
233                         Assert.IsTrue (sql.CanSort, "A2");
234
235                         sql.SortParameterName = "";
236                         Assert.IsTrue (sql.CanSort, "A3");
237
238                         sql.SortParameterName = "foo";
239
240                         ds.DataSourceMode = SqlDataSourceMode.DataReader;
241                         Assert.IsTrue (sql.CanSort, "A4");
242
243                         ds.DataSourceMode = SqlDataSourceMode.DataSet;
244                         Assert.IsTrue (sql.CanSort, "A5");
245
246                         sql.SortParameterName = "";
247
248                         ds.DataSourceMode = SqlDataSourceMode.DataReader;
249                         Assert.IsFalse (sql.CanSort, "A6");
250
251                         ds.DataSourceMode = SqlDataSourceMode.DataSet;
252                         Assert.IsTrue (sql.CanSort, "A7");
253                 }
254
255                 [Test]
256                 public void OldValuesParameterFormatString ()
257                 {
258                         SqlDataSource ds = new SqlDataSource ();
259                         
260                         Assert.AreEqual ("{0}", ds.OldValuesParameterFormatString, "A1");
261
262                         ds.OldValuesParameterFormatString = "hi {0}";
263
264                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
265
266                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A2");
267
268                         ds.OldValuesParameterFormatString = "hi {0}";
269
270                         Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A3");
271
272                         ds.OldValuesParameterFormatString = "{0}";
273                         sql.OldValuesParameterFormatString = "hi {0}";
274
275                         Assert.AreEqual ("{0}", ds.OldValuesParameterFormatString, "A4");
276                 }
277
278                 [Test]
279                 public void CancelSelectOnNullParameter ()
280                 {
281                         SqlDataSource ds = new SqlDataSource ();
282
283                         ds.CancelSelectOnNullParameter = false;
284
285                         SqlViewPoker sql = new SqlViewPoker (ds, "DefaultView", null);
286
287                         Assert.IsTrue (sql.CancelSelectOnNullParameter, "A1");
288
289                         ds.CancelSelectOnNullParameter = true;
290                         sql.CancelSelectOnNullParameter = false;
291
292                         Assert.IsTrue (ds.CancelSelectOnNullParameter, "A2");
293
294                         sql.CancelSelectOnNullParameter = false;
295                         ds.CancelSelectOnNullParameter = true;
296                         Assert.IsFalse (sql.CancelSelectOnNullParameter, "A3");
297                 }
298         }
299
300 }
301
302 #endif