2 // Tests for System.Web.UI.WebControls.SqlDataSource
5 // Chris Toshok (toshok@novell.com)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using NUnit.Framework;
34 using System.Configuration;
35 using System.Data.Common;
36 using System.Data.SqlClient;
38 using System.Globalization;
41 using System.Web.UI.WebControls;
42 using System.Collections;
46 using MonoTests.SystemWeb.Framework;
47 using MonoTests.stand_alone.WebHarness;
49 namespace MonoTests.System.Web.UI.WebControls
51 class SqlPoker : SqlDataSource
58 public object SaveToViewState ()
60 return SaveViewState ();
63 public void LoadFromViewState (object savedState)
65 LoadViewState (savedState);
68 public void DoRaiseDataSourceChangedEvent ()
70 base.RaiseDataSourceChangedEvent(new EventArgs());
74 class CustomSqlDataSourceView : SqlDataSourceView
76 public CustomSqlDataSourceView (SqlDataSource owner,string name,HttpContext context):base(owner,name,context)
80 public new int ExecuteDelete (global::System.Collections.IDictionary keys, global::System.Collections.IDictionary oldValues)
82 return base.ExecuteDelete (keys, oldValues);
85 public new int ExecuteInsert (global::System.Collections.IDictionary values)
87 return base.ExecuteInsert (values);
90 public new global::System.Collections.IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
92 return base.ExecuteSelect (arguments);
95 public new int ExecuteUpdate (global::System.Collections.IDictionary keys, global::System.Collections.IDictionary values, global::System.Collections.IDictionary oldValues)
97 return base.ExecuteUpdate (keys, values, oldValues);
104 public class SqlDataSourceTest
109 SqlDataSourceTest.CustomEventParameterCollection = null;
110 SqlDataSourceTest.PassedParameters = "";
112 WebTest.CopyResource (GetType (), "SqlDataSource_OnInit_Bug572781.aspx", "SqlDataSource_OnInit_Bug572781.aspx");
116 public void Defaults ()
118 SqlPoker sql = new SqlPoker ();
119 Assert.AreEqual ("", sql.CacheKeyDependency, "A1");
120 Assert.IsTrue (sql.CancelSelectOnNullParameter, "A2");
121 Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "A3");
122 Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "A4");
123 Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "A5");
124 Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "A6");
125 Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "A7");
126 Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A8");
127 Assert.AreEqual ("", sql.SqlCacheDependency, "A9");
128 Assert.AreEqual ("", sql.SortParameterName, "A10");
129 Assert.AreEqual (0, sql.CacheDuration, "A11");
130 Assert.AreEqual (DataSourceCacheExpiry.Absolute, sql.CacheExpirationPolicy, "A12");
131 Assert.IsFalse (sql.EnableCaching, "A13");
132 Assert.AreEqual ("", sql.ProviderName, "A14");
133 Assert.AreEqual ("", sql.ConnectionString, "A15");
134 Assert.AreEqual (SqlDataSourceMode.DataSet, sql.DataSourceMode, "A16");
135 Assert.AreEqual ("", sql.DeleteCommand, "A17");
136 Assert.IsNotNull (sql.DeleteParameters, "A18");
137 Assert.AreEqual (0, sql.DeleteParameters.Count, "A18.1");
138 Assert.IsNotNull (sql.FilterParameters, "A19");
139 Assert.AreEqual (0, sql.FilterParameters.Count, "A19.1");
140 Assert.AreEqual ("", sql.InsertCommand, "A20");
141 Assert.IsNotNull (sql.InsertParameters, "A21");
142 Assert.AreEqual (0, sql.InsertParameters.Count, "A21.1");
143 Assert.AreEqual ("", sql.SelectCommand, "A22");
144 Assert.IsNotNull (sql.SelectParameters, "A23");
145 Assert.AreEqual (0, sql.SelectParameters.Count, "A23.1");
146 Assert.AreEqual ("", sql.UpdateCommand, "A24");
147 Assert.IsNotNull (sql.UpdateParameters, "A25");
148 Assert.AreEqual (0, sql.UpdateParameters.Count, "A25.1");
149 Assert.AreEqual ("", sql.FilterExpression, "A26");
153 public void ViewState ()
155 SqlPoker sql = new SqlPoker ();
157 sql.CacheKeyDependency = "hi";
158 sql.CancelSelectOnNullParameter = false;
159 sql.ConflictDetection = ConflictOptions.CompareAllValues;
160 sql.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
161 sql.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
162 sql.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
163 sql.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
164 sql.OldValuesParameterFormatString = "{1}";
165 sql.SqlCacheDependency = "hi";
166 sql.SortParameterName = "hi";
167 sql.CacheDuration = 1;
168 sql.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
169 sql.EnableCaching = true;
170 sql.DataSourceMode = SqlDataSourceMode.DataReader;
171 sql.DeleteCommand = "DELETE foo";
172 sql.InsertCommand = "INSERT foo";
173 sql.SelectCommand = "SELECT foo";
174 sql.UpdateCommand = "UPDATE foo";
175 sql.FilterExpression = "hi";
177 Assert.AreEqual ("hi", sql.CacheKeyDependency, "A1");
178 Assert.IsFalse (sql.CancelSelectOnNullParameter, "A2");
179 Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "A3");
180 Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.DeleteCommandType, "A4");
181 Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.InsertCommandType, "A5");
182 Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.SelectCommandType, "A6");
183 Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.UpdateCommandType, "A7");
184 Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "A8");
185 Assert.AreEqual ("hi", sql.SqlCacheDependency, "A9");
186 Assert.AreEqual ("hi", sql.SortParameterName, "A10");
187 Assert.AreEqual (1, sql.CacheDuration, "A11");
188 Assert.AreEqual (DataSourceCacheExpiry.Sliding, sql.CacheExpirationPolicy, "A12");
189 Assert.IsTrue (sql.EnableCaching, "A13");
190 Assert.AreEqual (SqlDataSourceMode.DataReader, sql.DataSourceMode, "A16");
191 Assert.AreEqual ("DELETE foo", sql.DeleteCommand, "A17");
192 Assert.AreEqual ("INSERT foo", sql.InsertCommand, "A20");
193 Assert.AreEqual ("SELECT foo", sql.SelectCommand, "A22");
194 Assert.AreEqual ("UPDATE foo", sql.UpdateCommand, "A24");
195 Assert.AreEqual ("hi", sql.FilterExpression, "A26");
197 object state = sql.SaveToViewState ();
198 Assert.IsNull (state, "ViewState is null");
200 sql = new SqlPoker ();
201 sql.LoadFromViewState (state);
203 Assert.AreEqual ("", sql.CacheKeyDependency, "B1");
204 Assert.IsTrue (sql.CancelSelectOnNullParameter, "B2");
205 Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "B3");
206 Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "B4");
207 Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "B5");
208 Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "B6");
209 Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "B7");
210 Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "B8");
211 Assert.AreEqual ("", sql.SqlCacheDependency, "B9");
212 Assert.AreEqual ("", sql.SortParameterName, "B10");
213 Assert.AreEqual (0, sql.CacheDuration, "B11");
214 Assert.AreEqual (DataSourceCacheExpiry.Absolute, sql.CacheExpirationPolicy, "B12");
215 Assert.IsFalse (sql.EnableCaching, "B13");
216 Assert.AreEqual (SqlDataSourceMode.DataSet, sql.DataSourceMode, "B16");
217 Assert.AreEqual ("", sql.DeleteCommand, "B17");
218 Assert.IsNotNull (sql.DeleteParameters, "B18");
219 Assert.AreEqual (0, sql.DeleteParameters.Count, "B18.1");
220 Assert.IsNotNull (sql.FilterParameters, "B19");
221 Assert.AreEqual (0, sql.FilterParameters.Count, "B19.1");
222 Assert.AreEqual ("", sql.InsertCommand, "B20");
223 Assert.IsNotNull (sql.InsertParameters, "B21");
224 Assert.AreEqual (0, sql.InsertParameters.Count, "B21.1");
225 Assert.AreEqual ("", sql.SelectCommand, "B22");
226 Assert.IsNotNull (sql.SelectParameters, "B23");
227 Assert.AreEqual (0, sql.SelectParameters.Count, "B23.1");
228 Assert.AreEqual ("", sql.UpdateCommand, "B24");
229 Assert.IsNotNull (sql.UpdateParameters, "B25");
230 Assert.AreEqual (0, sql.UpdateParameters.Count, "B25.1");
231 Assert.AreEqual ("", sql.FilterExpression, "B26");
234 // Help parameter for Asserts
235 private static SqlParameterCollection CustomEventParameterCollection;
236 private static string PassedParameters;
239 public void ReturnValueParameter ()
241 SqlPoker sql = new SqlPoker ();
242 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
243 sql.ProviderName = "System.Data.SqlClient";
244 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
245 view.SelectCommandType = SqlDataSourceCommandType.Text;
246 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
247 view.OldValuesParameterFormatString = "origin_{0}";
249 view.SelectParameters.Add (new Parameter ("ProductID", TypeCode.Int32, "10"));
250 Parameter myReturn = new Parameter ("myReturn", TypeCode.Int32);
251 myReturn.Direction = ParameterDirection.ReturnValue;
252 view.SelectParameters.Add (myReturn);
254 view.Selecting += new SqlDataSourceSelectingEventHandler (view_Selecting);
255 view.Select (new DataSourceSelectArguments ());
257 Assert.IsNotNull (CustomEventParameterCollection, "Select event not fired");
258 Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
259 Assert.IsNotNull (CustomEventParameterCollection ["@myReturn"], "Parameter name");
263 public void ExecuteSelect ()
265 SqlPoker sql = new SqlPoker();
266 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
267 sql.ProviderName = "System.Data.SqlClient";
268 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
269 view.SelectCommandType = SqlDataSourceCommandType.Text;
270 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
271 view.OldValuesParameterFormatString = "origin_{0}";
273 view.SelectParameters.Add (new Parameter ("ProductID", TypeCode.Int32, "10"));
274 view.Selecting += new SqlDataSourceSelectingEventHandler (view_Selecting);
275 view.Select (new DataSourceSelectArguments ());
276 Assert.IsNotNull (CustomEventParameterCollection, "Select event not fired");
277 Assert.AreEqual (1, CustomEventParameterCollection.Count, "Parameter count");
278 Assert.AreEqual ("@ProductID", CustomEventParameterCollection[0].ParameterName, "Parameter name");
279 Assert.AreEqual (10, CustomEventParameterCollection[0].Value, "Parameter value");
283 public void ExecuteSelect2 ()
285 SqlPoker sql = new SqlPoker ();
286 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
287 sql.ProviderName = "System.Data.SqlClient";
288 sql.DataSourceMode = SqlDataSourceMode.DataReader;
289 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
290 view.SelectCommandType = SqlDataSourceCommandType.Text;
291 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
292 view.OldValuesParameterFormatString = "origin_{0}";
294 view.SelectParameters.Add (new Parameter ("ProductID", TypeCode.Int32, "10"));
295 view.Selecting += new SqlDataSourceSelectingEventHandler (view_Selecting);
296 view.Select (new DataSourceSelectArguments ());
297 Assert.IsNotNull (CustomEventParameterCollection, "Select event not fired");
298 Assert.AreEqual (1, CustomEventParameterCollection.Count, "Parameter count");
299 Assert.AreEqual ("@ProductID", CustomEventParameterCollection [0].ParameterName, "Parameter name");
300 Assert.AreEqual (10, CustomEventParameterCollection [0].Value, "Parameter value");
304 public void ExecuteUpdate ()
306 SqlPoker sql = new SqlPoker ();
307 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
308 sql.ProviderName = "System.Data.SqlClient";
309 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
310 view.SelectCommandType = SqlDataSourceCommandType.Text;
311 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
312 view.UpdateCommandType = SqlDataSourceCommandType.Text;
313 view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
314 view.OldValuesParameterFormatString = "origin_{0}";
315 view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
316 view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
317 view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
318 view.Update (null, null, null);
319 Assert.IsNotNull (CustomEventParameterCollection, "Update event not fired");
320 Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
321 Assert.AreEqual ("@UserName", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
322 Assert.AreEqual ("TestUser", CustomEventParameterCollection[0].Value, "Parameter value#1");
323 Assert.AreEqual ("@UserId", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
324 Assert.AreEqual (1, CustomEventParameterCollection[1].Value, "Parameter value#2");
328 public void ExecuteInsert ()
330 SqlPoker sql = new SqlPoker ();
331 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
332 sql.ProviderName = "System.Data.SqlClient";
333 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
334 view.SelectCommandType = SqlDataSourceCommandType.Text;
335 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
336 view.InsertCommandType = SqlDataSourceCommandType.Text;
337 view.InsertCommand = "INSERT INTO Table1 (UserId, UserName) VALUES ({0},{1})";
338 view.InsertParameters.Add (new Parameter ("UserId", TypeCode.Int32, "15"));
339 view.InsertParameters.Add (new Parameter ("UserName", TypeCode.String, "newuser"));
340 view.OldValuesParameterFormatString = "origin_{0}";
341 view.Inserting += new SqlDataSourceCommandEventHandler (view_Inserting);
343 Assert.IsNotNull (CustomEventParameterCollection, "Insert event not fired");
344 Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
345 Assert.AreEqual ("@UserId", CustomEventParameterCollection[0].ParameterName, "Parameter name#2");
346 Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#2");
347 Assert.AreEqual ("@UserName", CustomEventParameterCollection[1].ParameterName, "Parameter name#1");
348 Assert.AreEqual ("newuser", CustomEventParameterCollection[1].Value, "Parameter value#1");
352 public void ExecuteInsertWithCollection ()
354 SqlPoker sql = new SqlPoker ();
355 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
356 sql.ProviderName = "System.Data.SqlClient";
357 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
358 view.SelectCommandType = SqlDataSourceCommandType.Text;
359 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
360 view.InsertCommandType = SqlDataSourceCommandType.Text;
361 view.InsertCommand = "INSERT INTO products (UserId, UserName) VALUES ({0},{1})";
362 view.InsertParameters.Add (new Parameter ("UserId", TypeCode.Int32, "15"));
363 view.InsertParameters.Add (new Parameter ("UserName", TypeCode.String, "newuser"));
364 view.OldValuesParameterFormatString = "origin_{0}";
365 view.Inserting += new SqlDataSourceCommandEventHandler (view_Inserting);
366 Hashtable value = new Hashtable ();
367 value.Add ("Description", "TestDescription");
369 Assert.IsNotNull (CustomEventParameterCollection, "Insert event not fired");
370 Assert.AreEqual (3, CustomEventParameterCollection.Count, "Parameter count");
371 Assert.AreEqual ("@UserId", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
372 Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#1");
373 Assert.AreEqual ("@UserName", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
374 Assert.AreEqual ("newuser", CustomEventParameterCollection[1].Value, "Parameter value#2");
375 Assert.AreEqual ("@Description", CustomEventParameterCollection[2].ParameterName, "Parameter name#3");
376 Assert.AreEqual ("TestDescription", CustomEventParameterCollection[2].Value, "Parameter value#3");
380 public void ExecuteDelete ()
382 SqlPoker sql = new SqlPoker ();
383 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
384 sql.ProviderName = "System.Data.SqlClient";
385 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
386 view.SelectCommandType = SqlDataSourceCommandType.Text;
387 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
388 view.DeleteCommandType = SqlDataSourceCommandType.Text;
389 view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
390 view.DeleteParameters.Add (new Parameter ("ProductId", TypeCode.Int32, "15"));
391 view.OldValuesParameterFormatString = "origin_{0}";
392 view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
393 view.Delete (null, null);
394 Assert.IsNotNull (CustomEventParameterCollection, "Delete event not fired");
395 Assert.AreEqual (1, CustomEventParameterCollection.Count, "Parameter count");
396 Assert.AreEqual ("@ProductId", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
397 Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#1");
401 public void ExecuteDeleteWithOldValues ()
403 SqlPoker sql = new SqlPoker ();
404 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
405 sql.ProviderName = "System.Data.SqlClient";
406 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
407 view.SelectCommandType = SqlDataSourceCommandType.Text;
408 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
409 view.DeleteCommandType = SqlDataSourceCommandType.Text;
410 view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
411 view.DeleteParameters.Add (new Parameter ("ProductID", TypeCode.Int32, "15"));
412 view.OldValuesParameterFormatString = "origin_{0}";
413 view.ConflictDetection = ConflictOptions.CompareAllValues;
414 view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
415 Hashtable oldvalue = new Hashtable ();
416 oldvalue.Add ("ProductID", 10);
417 view.Delete (null,oldvalue );
418 Assert.IsNotNull (CustomEventParameterCollection, "Delete event not fired");
419 Assert.AreEqual (1, CustomEventParameterCollection.Count, "Parameter count");
420 Assert.AreEqual ("@origin_ProductID", CustomEventParameterCollection[0].ParameterName, "Parameter name#2");
421 Assert.AreEqual (10, CustomEventParameterCollection[0].Value, "Parameter value#2");
425 public void ExecuteDeleteWithMergedOldValues ()
427 SqlPoker sql = new SqlPoker ();
428 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
429 sql.ProviderName = "System.Data.SqlClient";
430 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
431 view.SelectCommandType = SqlDataSourceCommandType.Text;
432 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
433 view.DeleteCommandType = SqlDataSourceCommandType.Text;
434 view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
435 view.DeleteParameters.Add (new Parameter ("ProductId", TypeCode.Int32, "15"));
436 view.OldValuesParameterFormatString = "origin_{0}";
437 view.ConflictDetection = ConflictOptions.CompareAllValues;
438 view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
439 Hashtable oldvalue = new Hashtable ();
440 oldvalue.Add ("Desc", "Description");
441 view.Delete (null, oldvalue);
442 Assert.IsNotNull (CustomEventParameterCollection, "Delete event not fired");
443 Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
444 Assert.AreEqual ("@ProductId", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
445 Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#1");
446 Assert.AreEqual ("@origin_Desc", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
447 Assert.AreEqual ("Description", CustomEventParameterCollection[1].Value, "Parameter value#2");
451 public void ExecuteDeleteWithMergedValues ()
453 SqlPoker sql = new SqlPoker ();
454 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
455 sql.ProviderName = "System.Data.SqlClient";
456 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
457 view.SelectCommandType = SqlDataSourceCommandType.Text;
458 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
459 view.DeleteCommandType = SqlDataSourceCommandType.Text;
460 view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
461 view.DeleteParameters.Add (new Parameter ("ProductId", TypeCode.Int32, "15"));
462 view.OldValuesParameterFormatString = "origin_{0}";
463 view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
464 Hashtable value = new Hashtable ();
465 value.Add ("Desc", "Description");
466 view.Delete (value, null);
467 Assert.IsNotNull (CustomEventParameterCollection, "Delete event not fired");
468 Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
469 Assert.AreEqual ("@ProductId", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
470 Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#1");
471 Assert.AreEqual ("@origin_Desc", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
472 Assert.AreEqual ("Description", CustomEventParameterCollection[1].Value, "Parameter value#2");
476 public void ExecuteDelete_KeysAndOldValues_OverwriteChanges ()
478 SqlPoker sql = new SqlPoker ();
479 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
480 sql.ProviderName = "System.Data.SqlClient";
481 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
484 IDictionary oldValues;
485 InitializeView (view, out keys, out values, out oldValues);
487 view.ConflictDetection = ConflictOptions.OverwriteChanges;
489 view.Delete (keys, oldValues);
491 Assert.IsNotNull (CustomEventParameterCollection, "KeysAndOldValues_OverwriteChanges");
492 Assert.AreEqual ("String:@origin_ProductID=k_10", PassedParameters, "KeysAndOldValues_OverwriteChanges Values");
496 public void ExecuteDelete_KeysAndOldValues_CompareAllValues ()
498 SqlPoker sql = new SqlPoker ();
499 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
500 sql.ProviderName = "System.Data.SqlClient";
501 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
504 IDictionary oldValues;
505 InitializeView (view, out keys, out values, out oldValues);
507 view.ConflictDetection = ConflictOptions.CompareAllValues;
509 view.Delete (keys, oldValues);
511 Assert.IsNotNull (CustomEventParameterCollection, "ExecuteDelete_KeysAndOldValues_CompareAllValues");
512 string [] expectedParams = new string []
514 "String:@origin_ProductID=ov_10",
515 "String:@origin_Name=ov_ColorTV",
516 "String:@origin_Description=ov_Beautifull"
518 string [] actualValues = PassedParameters.Split (new string [] { ", " }, StringSplitOptions.RemoveEmptyEntries);
519 Assert.AreEqual (expectedParams.Length, actualValues.Length, "ExecuteDelete_KeysAndOldValues_CompareAllValues Params count");
520 ValidatePassedParams (expectedParams, actualValues, "ExecuteDelete_KeysAndOldValues_CompareAllValues expecte '{0}'");
524 public void ExecuteDelete_KeysAndOldValues_CompareAllValues2 ()
526 SqlPoker sql = new SqlPoker ();
527 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
528 sql.ProviderName = "System.Data.SqlClient";
529 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
532 IDictionary oldValues;
533 InitializeView (view, out keys, out values, out oldValues);
534 view.DeleteParameters.Add ("origin_ProductID", "po_10");
536 view.ConflictDetection = ConflictOptions.CompareAllValues;
538 view.Delete (keys, oldValues);
540 Assert.IsNotNull (CustomEventParameterCollection, "ExecuteDelete_KeysAndOldValues_CompareAllValues2");
541 string [] expectedParams = new string []
543 "String:@origin_ProductID=ov_10",
544 "String:@origin_Name=ov_ColorTV",
545 "String:@origin_Description=ov_Beautifull"
547 string [] actualValues = PassedParameters.Split (new string [] { ", " }, StringSplitOptions.RemoveEmptyEntries);
548 Assert.AreEqual (expectedParams.Length, actualValues.Length, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 Params count");
549 ValidatePassedParams (expectedParams, actualValues, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 expecte '{0}'");
552 private static void ValidatePassedParams (string [] expectedParams, string [] actualValues, string errorMessageFormat)
554 foreach (string eps in expectedParams) {
556 foreach (string aps in actualValues) {
562 Assert.IsTrue (found, String.Format (errorMessageFormat, eps));
566 private void InitializeView (CustomSqlDataSourceView view, out IDictionary keys, out IDictionary values, out IDictionary oldValues)
568 view.SelectCommandType = SqlDataSourceCommandType.Text;
569 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
570 view.SelectParameters.Add (new Parameter ("ProductID", TypeCode.String, "p_10"));
571 view.Selecting += new SqlDataSourceSelectingEventHandler (view_Selecting);
573 view.DeleteCommandType = SqlDataSourceCommandType.Text;
574 view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
575 view.DeleteParameters.Add (new Parameter ("ProductID", TypeCode.String, "p_10"));
576 view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
578 view.InsertCommandType = SqlDataSourceCommandType.Text;
579 view.InsertCommand = "INSERT INTO products (ProductID, Name, Description) VALUES (@ProductID, @Name, @Description)";
580 view.InsertParameters.Add (new Parameter ("ProductID", TypeCode.String, "p_15"));
581 view.InsertParameters.Add (new Parameter ("Name", TypeCode.String, "p_NewProduct"));
582 view.InsertParameters.Add (new Parameter ("Description", TypeCode.String, "p_Description"));
583 view.Inserting += new SqlDataSourceCommandEventHandler (view_Inserting);
585 view.UpdateCommandType = SqlDataSourceCommandType.Text;
586 view.UpdateCommand = "UPDATE products SET Name = @Name, Description = @Description WHERE ProductID = @ProductID";
587 view.UpdateParameters.Add (new Parameter ("ProductID", TypeCode.String, "p_15"));
588 view.UpdateParameters.Add (new Parameter ("Name", TypeCode.String, "p_UpdatedProduct"));
589 view.UpdateParameters.Add (new Parameter ("Description", TypeCode.String, "p_UpdatedDescription"));
590 view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
592 view.OldValuesParameterFormatString = "origin_{0}";
594 keys = new Hashtable ();
595 values = new Hashtable ();
596 oldValues = new Hashtable ();
598 keys.Add ("ProductID", "k_10");
600 values.Add ("ProductID", "n_10");
601 values.Add ("Name", "n_ColorTV");
602 values.Add ("Description", "n_Beautifull");
604 oldValues.Add ("ProductID", "ov_10");
605 oldValues.Add ("Name", "ov_ColorTV");
606 oldValues.Add ("Description", "ov_Beautifull");
610 public void ExecuteUpdateWithOldValues ()
612 SqlPoker sql = new SqlPoker ();
613 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
614 sql.ProviderName = "System.Data.SqlClient";
615 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
616 view.SelectCommandType = SqlDataSourceCommandType.Text;
617 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
618 view.UpdateCommandType = SqlDataSourceCommandType.Text;
619 view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
620 view.OldValuesParameterFormatString = "origin_{0}";
621 view.ConflictDetection = ConflictOptions.CompareAllValues;
622 view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
623 view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
624 view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
625 Hashtable oldvalue = new Hashtable ();
626 oldvalue.Add ("UserId", 2);
627 view.Update (null, null, oldvalue);
628 Assert.IsNotNull (CustomEventParameterCollection, "Update event not fired");
629 Assert.AreEqual (3, CustomEventParameterCollection.Count, "Parameter count");
630 Assert.AreEqual ("@UserName", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
631 Assert.AreEqual ("TestUser", CustomEventParameterCollection[0].Value, "Parameter value#1");
632 Assert.AreEqual ("@UserId", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
633 Assert.AreEqual (1, CustomEventParameterCollection[1].Value, "Parameter value#2");
634 Assert.AreEqual ("@origin_UserId", CustomEventParameterCollection[2].ParameterName, "Parameter name#3");
635 Assert.AreEqual (2, CustomEventParameterCollection[2].Value, "Parameter value#3");
639 public void ExecuteUpdateWithOverwriteParameters ()
641 SqlPoker sql = new SqlPoker ();
642 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
643 sql.ProviderName = "System.Data.SqlClient";
644 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
645 view.SelectCommandType = SqlDataSourceCommandType.Text;
646 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
647 view.UpdateCommandType = SqlDataSourceCommandType.Text;
648 view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
649 view.OldValuesParameterFormatString = "origin_{0}";
650 view.ConflictDetection = ConflictOptions.OverwriteChanges;
651 view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
652 view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
653 view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
654 Hashtable value = new Hashtable ();
655 value.Add ("UserId", 2);
656 view.Update (value, null, null);
657 Assert.IsNotNull (CustomEventParameterCollection, "Update event not fired");
658 Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
659 Assert.AreEqual ("@UserName", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
660 Assert.AreEqual ("TestUser", CustomEventParameterCollection[0].Value, "Parameter value#1");
661 Assert.AreEqual ("@origin_UserId", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
662 Assert.AreEqual (2, CustomEventParameterCollection[1].Value, "Parameter value#2");
666 public void ExecuteUpdateWithMargeParameters ()
668 SqlPoker sql = new SqlPoker ();
669 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
670 sql.ProviderName = "System.Data.SqlClient";
671 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
672 view.SelectCommandType = SqlDataSourceCommandType.Text;
673 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
674 view.UpdateCommandType = SqlDataSourceCommandType.Text;
675 view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
676 view.OldValuesParameterFormatString = "origin_{0}";
677 view.ConflictDetection = ConflictOptions.OverwriteChanges;
678 view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
679 view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
680 view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
681 Hashtable value = new Hashtable ();
682 value.Add ("UserLName", "TestLName");
683 view.Update (null, value, null);
684 Assert.IsNotNull (CustomEventParameterCollection, "Update event not fired");
685 Assert.AreEqual (3, CustomEventParameterCollection.Count, "Parameter count");
686 Assert.AreEqual ("@UserName", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
687 Assert.AreEqual ("TestUser", CustomEventParameterCollection[0].Value, "Parameter value#1");
688 Assert.AreEqual ("@UserId", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
689 Assert.AreEqual (1, CustomEventParameterCollection[1].Value, "Parameter value#2");
690 Assert.AreEqual ("@UserLName", CustomEventParameterCollection[2].ParameterName, "Parameter name#3");
691 Assert.AreEqual ("TestLName", CustomEventParameterCollection[2].Value, "Parameter value#3");
695 public void ExecuteUpdate_KeysValuesAndOldValues_OverwriteChanges ()
697 SqlPoker sql = new SqlPoker ();
698 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
699 sql.ProviderName = "System.Data.SqlClient";
700 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
703 IDictionary oldValues;
704 InitializeView (view, out keys, out values, out oldValues);
706 view.ConflictDetection = ConflictOptions.OverwriteChanges;
708 view.Update (keys, values, oldValues);
710 Assert.IsNotNull (CustomEventParameterCollection, "ExecuteUpdate_KeysValuesAndOldValues_OverwriteChanges");
711 string [] expectedParams = new string []
713 "String:@ProductID=n_10",
714 "String:@Name=n_ColorTV",
715 "String:@Description=n_Beautifull",
716 "String:@origin_ProductID=k_10"
718 string [] actualValues = PassedParameters.Split (new string [] { ", " }, StringSplitOptions.RemoveEmptyEntries);
719 Assert.AreEqual (expectedParams.Length, actualValues.Length, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 Params count");
720 ValidatePassedParams (expectedParams, actualValues, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 expecte '{0}'");
724 public void ExecuteUpdate_KeysValuesAndOldValues_CompareAllValues ()
726 SqlPoker sql = new SqlPoker ();
727 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
728 sql.ProviderName = "System.Data.SqlClient";
729 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
732 IDictionary oldValues;
733 InitializeView (view, out keys, out values, out oldValues);
735 view.ConflictDetection = ConflictOptions.CompareAllValues;
737 view.Update (keys, values, oldValues);
739 Assert.IsNotNull (CustomEventParameterCollection, "ExecuteUpdate_KeysValuesAndOldValues_CompareAllValues");
740 string [] expectedParams = new string []
742 "String:@ProductID=n_10",
743 "String:@Name=n_ColorTV",
744 "String:@Description=n_Beautifull",
745 "String:@origin_ProductID=ov_10",
746 "String:@origin_Name=ov_ColorTV",
747 "String:@origin_Description=ov_Beautifull",
749 string [] actualValues = PassedParameters.Split (new string [] { ", " }, StringSplitOptions.RemoveEmptyEntries);
750 Assert.AreEqual (expectedParams.Length, actualValues.Length, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 Params count");
751 ValidatePassedParams (expectedParams, actualValues, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 expecte '{0}'");
754 void view_Updating (object sender, SqlDataSourceCommandEventArgs e)
756 SqlDataSourceTest.CustomEventParameterCollection = (SqlParameterCollection) e.Command.Parameters;
757 SqlDataSourceTest.PassedParameters = FormatParameters (SqlDataSourceTest.CustomEventParameterCollection);
761 void view_Selecting (object sender, SqlDataSourceSelectingEventArgs e)
763 SqlDataSourceTest.CustomEventParameterCollection = (SqlParameterCollection) e.Command.Parameters;
764 SqlDataSourceTest.PassedParameters = FormatParameters (SqlDataSourceTest.CustomEventParameterCollection);
768 void view_Inserting (object sender, SqlDataSourceCommandEventArgs e)
770 SqlDataSourceTest.CustomEventParameterCollection = (SqlParameterCollection) e.Command.Parameters;
771 SqlDataSourceTest.PassedParameters = FormatParameters (SqlDataSourceTest.CustomEventParameterCollection);
775 void view_Deleting (object sender, SqlDataSourceCommandEventArgs e)
777 SqlDataSourceTest.CustomEventParameterCollection = (SqlParameterCollection) e.Command.Parameters;
778 SqlDataSourceTest.PassedParameters = FormatParameters (SqlDataSourceTest.CustomEventParameterCollection);
782 private string FormatParameters (SqlParameterCollection sqlParameterCollection)
784 StringBuilder sb = new StringBuilder ();
785 foreach (SqlParameter p in sqlParameterCollection) {
789 sb.AppendFormat ("{0}:{1}={2}", p.DbType, p.ParameterName, p.Value);
791 return sb.ToString ();
797 private static int _testcounter;
798 private static bool _eventChecker;
799 private eventAssert ()
804 public static bool eventChecker
808 throw new NotImplementedException ();
812 _eventChecker = value;
816 static private void testAdded ()
819 _eventChecker = false;
822 public static void IsTrue (string msg)
824 Assert.IsTrue (_eventChecker, msg + "#" + _testcounter);
829 public static void IsFalse (string msg)
831 Assert.IsFalse (_eventChecker, msg + "#" + _testcounter);
838 public void SqlDataSource_DataSourceViewChanged ()
840 SqlPoker sql = new SqlPoker ();
841 ((IDataSource) sql).DataSourceChanged += new EventHandler (SqlDataSourceTest_DataSourceChanged);
843 sql.DoRaiseDataSourceChangedEvent ();
844 eventAssert.IsTrue ("SqlDataSourceView"); // Assert include counter the first is zero
845 sql.CacheKeyDependency = "hi";
846 eventAssert.IsFalse ("SqlDataSourceView");
847 sql.CancelSelectOnNullParameter = false;
848 eventAssert.IsFalse ("SqlDataSourceView");
849 sql.ConflictDetection = ConflictOptions.CompareAllValues;
850 eventAssert.IsFalse ("SqlDataSourceView");
851 sql.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
852 eventAssert.IsFalse ("SqlDataSourceView");
853 sql.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
854 eventAssert.IsFalse ("SqlDataSourceView");
855 sql.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
856 eventAssert.IsFalse ("SqlDataSourceView");
857 sql.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
858 eventAssert.IsFalse ("SqlDataSourceView");
859 sql.OldValuesParameterFormatString = "{1}";
860 eventAssert.IsFalse ("SqlDataSourceView");
861 sql.SqlCacheDependency = "hi";
862 eventAssert.IsFalse ("SqlDataSourceView");
863 sql.SortParameterName = "hi";
864 eventAssert.IsFalse ("SqlDataSourceView");
865 sql.CacheDuration = 1;
866 eventAssert.IsFalse ("SqlDataSourceView");
867 sql.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
868 eventAssert.IsFalse ("SqlDataSourceView");
869 sql.EnableCaching = true;
870 eventAssert.IsFalse ("SqlDataSourceView");
871 sql.DataSourceMode = SqlDataSourceMode.DataReader;
872 eventAssert.IsTrue ("SqlDataSourceView");
873 sql.DeleteCommand = "DELETE foo";
874 eventAssert.IsFalse ("SqlDataSourceView");
875 sql.InsertCommand = "INSERT foo";
876 eventAssert.IsFalse ("SqlDataSourceView");
877 sql.SelectCommand = "SELECT foo";
878 eventAssert.IsFalse ("SqlDataSourceView");
879 sql.UpdateCommand = "UPDATE foo";
880 eventAssert.IsFalse ("SqlDataSourceView");
881 sql.FilterExpression = "hi";
882 eventAssert.IsFalse ("SqlDataSourceView");
885 void SqlDataSourceTest_DataSourceChanged (object sender, EventArgs e)
887 eventAssert.eventChecker = true;
892 [ExpectedException (typeof (NotSupportedException))]
893 public void ExecuteUpdateException ()
895 SqlPoker sql = new SqlPoker ();
896 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
897 sql.ProviderName = "System.Data.SqlClient";
898 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
899 view.Update (null, null, null);
903 [ExpectedException (typeof (NotSupportedException))]
904 public void ExecuteDeleteException ()
906 SqlPoker sql = new SqlPoker ();
907 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
908 sql.ProviderName = "System.Data.SqlClient";
909 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
910 view.Delete (null, null);
914 [ExpectedException (typeof (NotSupportedException))]
915 public void ExecuteInsertException ()
917 SqlPoker sql = new SqlPoker ();
918 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
919 sql.ProviderName = "System.Data.SqlClient";
920 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
921 view.SelectCommandType = SqlDataSourceCommandType.Text;
922 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
926 [Test] //ConflictOptions.CompareAllValues must include old value collection
927 [ExpectedException (typeof (InvalidOperationException))]
928 public void ExecuteUpdateWithOldValuesException ()
930 SqlPoker sql = new SqlPoker ();
931 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
932 sql.ProviderName = "System.Data.SqlClient";
933 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
934 view.SelectCommandType = SqlDataSourceCommandType.Text;
935 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
936 view.UpdateCommandType = SqlDataSourceCommandType.Text;
937 view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
938 view.OldValuesParameterFormatString = "origin_{0}";
939 view.ConflictDetection = ConflictOptions.CompareAllValues;
940 view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
941 view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
942 view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
943 view.Update (null, null, null);
946 [Test] //ConflictOptions.CompareAllValues must include old value collection
947 [ExpectedException (typeof (InvalidOperationException))]
948 public void ExecuteDeleteWithOldValuesException ()
950 SqlPoker sql = new SqlPoker ();
951 sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
952 sql.ProviderName = "System.Data.SqlClient";
953 CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
954 view.SelectCommandType = SqlDataSourceCommandType.Text;
955 view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
956 view.DeleteCommandType = SqlDataSourceCommandType.Text;
957 view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
958 view.DeleteParameters.Add (new Parameter ("ProductId", TypeCode.Int32, "15"));
959 view.OldValuesParameterFormatString = "origin_{0}";
960 view.ConflictDetection = ConflictOptions.CompareAllValues;
961 view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
962 Hashtable oldvalue = new Hashtable ();
963 oldvalue.Add ("ProductID", 10);
964 view.Delete (null, null);
968 public void SqlDataSource_OnInit_Bug572781 ()
970 WebTest t = new WebTest ("SqlDataSource_OnInit_Bug572781.aspx");
971 string origHtmlFirst = @" Init: button1. Init: sqlDataSource1<input type=""submit"" name=""button1"" value=""Click me!"" id=""button1"" />";
972 string origHtmlSecond = @" Init: button1. Init: sqlDataSource1<input type=""submit"" name=""button1"" value=""You clicked me"" id=""button1"" />";
977 renderedHtml = HtmlDiff.GetControlFromPageHtml (html);
978 HtmlDiff.AssertAreEqual (origHtmlFirst, renderedHtml, "#A1");
980 FormRequest fr = new FormRequest (t.Response, "form1");
981 fr.Controls.Add ("button1");
982 fr.Controls ["button1"].Value = "Click me!";
986 renderedHtml = HtmlDiff.GetControlFromPageHtml (html);
987 HtmlDiff.AssertAreEqual (origHtmlSecond, renderedHtml, "#A1");