SqlDataSourceTest.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. //
  2. // Tests for System.Web.UI.WebControls.SqlDataSource
  3. //
  4. // Author:
  5. // Chris Toshok ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 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.Data.SqlClient;
  35. using System.IO;
  36. using System.Globalization;
  37. using System.Web;
  38. using System.Web.UI;
  39. using System.Web.UI.WebControls;
  40. using System.Collections;
  41. using System.Text;
  42. using System.Data;
  43. namespace MonoTests.System.Web.UI.WebControls
  44. {
  45. class SqlPoker : SqlDataSource
  46. {
  47. public SqlPoker ()
  48. {
  49. TrackViewState ();
  50. }
  51. public object SaveToViewState ()
  52. {
  53. return SaveViewState ();
  54. }
  55. public void LoadFromViewState (object savedState)
  56. {
  57. LoadViewState (savedState);
  58. }
  59. public void DoRaiseDataSourceChangedEvent ()
  60. {
  61. base.RaiseDataSourceChangedEvent(new EventArgs());
  62. }
  63. }
  64. class CustomSqlDataSourceView : SqlDataSourceView
  65. {
  66. public CustomSqlDataSourceView (SqlDataSource owner,string name,HttpContext context):base(owner,name,context)
  67. {
  68. }
  69. public new int ExecuteDelete (global::System.Collections.IDictionary keys, global::System.Collections.IDictionary oldValues)
  70. {
  71. return base.ExecuteDelete (keys, oldValues);
  72. }
  73. public new int ExecuteInsert (global::System.Collections.IDictionary values)
  74. {
  75. return base.ExecuteInsert (values);
  76. }
  77. public new global::System.Collections.IEnumerable ExecuteSelect (DataSourceSelectArguments arguments)
  78. {
  79. return base.ExecuteSelect (arguments);
  80. }
  81. public new int ExecuteUpdate (global::System.Collections.IDictionary keys, global::System.Collections.IDictionary values, global::System.Collections.IDictionary oldValues)
  82. {
  83. return base.ExecuteUpdate (keys, values, oldValues);
  84. }
  85. }
  86. [TestFixture]
  87. public class SqlDataSourceTest
  88. {
  89. [SetUp]
  90. public void SetUp ()
  91. {
  92. SqlDataSourceTest.CustomEventParameterCollection = null;
  93. SqlDataSourceTest.PassedParameters = "";
  94. }
  95. [Test]
  96. public void Defaults ()
  97. {
  98. SqlPoker sql = new SqlPoker ();
  99. Assert.AreEqual ("", sql.CacheKeyDependency, "A1");
  100. Assert.IsTrue (sql.CancelSelectOnNullParameter, "A2");
  101. Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "A3");
  102. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "A4");
  103. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "A5");
  104. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "A6");
  105. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "A7");
  106. Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "A8");
  107. Assert.AreEqual ("", sql.SqlCacheDependency, "A9");
  108. Assert.AreEqual ("", sql.SortParameterName, "A10");
  109. Assert.AreEqual (0, sql.CacheDuration, "A11");
  110. Assert.AreEqual (DataSourceCacheExpiry.Absolute, sql.CacheExpirationPolicy, "A12");
  111. Assert.IsFalse (sql.EnableCaching, "A13");
  112. Assert.AreEqual ("", sql.ProviderName, "A14");
  113. Assert.AreEqual ("", sql.ConnectionString, "A15");
  114. Assert.AreEqual (SqlDataSourceMode.DataSet, sql.DataSourceMode, "A16");
  115. Assert.AreEqual ("", sql.DeleteCommand, "A17");
  116. Assert.IsNotNull (sql.DeleteParameters, "A18");
  117. Assert.AreEqual (0, sql.DeleteParameters.Count, "A18.1");
  118. Assert.IsNotNull (sql.FilterParameters, "A19");
  119. Assert.AreEqual (0, sql.FilterParameters.Count, "A19.1");
  120. Assert.AreEqual ("", sql.InsertCommand, "A20");
  121. Assert.IsNotNull (sql.InsertParameters, "A21");
  122. Assert.AreEqual (0, sql.InsertParameters.Count, "A21.1");
  123. Assert.AreEqual ("", sql.SelectCommand, "A22");
  124. Assert.IsNotNull (sql.SelectParameters, "A23");
  125. Assert.AreEqual (0, sql.SelectParameters.Count, "A23.1");
  126. Assert.AreEqual ("", sql.UpdateCommand, "A24");
  127. Assert.IsNotNull (sql.UpdateParameters, "A25");
  128. Assert.AreEqual (0, sql.UpdateParameters.Count, "A25.1");
  129. Assert.AreEqual ("", sql.FilterExpression, "A26");
  130. }
  131. [Test]
  132. public void ViewState ()
  133. {
  134. SqlPoker sql = new SqlPoker ();
  135. sql.CacheKeyDependency = "hi";
  136. sql.CancelSelectOnNullParameter = false;
  137. sql.ConflictDetection = ConflictOptions.CompareAllValues;
  138. sql.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
  139. sql.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
  140. sql.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
  141. sql.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
  142. sql.OldValuesParameterFormatString = "{1}";
  143. sql.SqlCacheDependency = "hi";
  144. sql.SortParameterName = "hi";
  145. sql.CacheDuration = 1;
  146. sql.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
  147. sql.EnableCaching = true;
  148. sql.DataSourceMode = SqlDataSourceMode.DataReader;
  149. sql.DeleteCommand = "DELETE foo";
  150. sql.InsertCommand = "INSERT foo";
  151. sql.SelectCommand = "SELECT foo";
  152. sql.UpdateCommand = "UPDATE foo";
  153. sql.FilterExpression = "hi";
  154. Assert.AreEqual ("hi", sql.CacheKeyDependency, "A1");
  155. Assert.IsFalse (sql.CancelSelectOnNullParameter, "A2");
  156. Assert.AreEqual (ConflictOptions.CompareAllValues, sql.ConflictDetection, "A3");
  157. Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.DeleteCommandType, "A4");
  158. Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.InsertCommandType, "A5");
  159. Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.SelectCommandType, "A6");
  160. Assert.AreEqual (SqlDataSourceCommandType.StoredProcedure, sql.UpdateCommandType, "A7");
  161. Assert.AreEqual ("{1}", sql.OldValuesParameterFormatString, "A8");
  162. Assert.AreEqual ("hi", sql.SqlCacheDependency, "A9");
  163. Assert.AreEqual ("hi", sql.SortParameterName, "A10");
  164. Assert.AreEqual (1, sql.CacheDuration, "A11");
  165. Assert.AreEqual (DataSourceCacheExpiry.Sliding, sql.CacheExpirationPolicy, "A12");
  166. Assert.IsTrue (sql.EnableCaching, "A13");
  167. Assert.AreEqual (SqlDataSourceMode.DataReader, sql.DataSourceMode, "A16");
  168. Assert.AreEqual ("DELETE foo", sql.DeleteCommand, "A17");
  169. Assert.AreEqual ("INSERT foo", sql.InsertCommand, "A20");
  170. Assert.AreEqual ("SELECT foo", sql.SelectCommand, "A22");
  171. Assert.AreEqual ("UPDATE foo", sql.UpdateCommand, "A24");
  172. Assert.AreEqual ("hi", sql.FilterExpression, "A26");
  173. object state = sql.SaveToViewState ();
  174. Assert.IsNull (state, "ViewState is null");
  175. sql = new SqlPoker ();
  176. sql.LoadFromViewState (state);
  177. Assert.AreEqual ("", sql.CacheKeyDependency, "B1");
  178. Assert.IsTrue (sql.CancelSelectOnNullParameter, "B2");
  179. Assert.AreEqual (ConflictOptions.OverwriteChanges, sql.ConflictDetection, "B3");
  180. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.DeleteCommandType, "B4");
  181. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.InsertCommandType, "B5");
  182. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.SelectCommandType, "B6");
  183. Assert.AreEqual (SqlDataSourceCommandType.Text, sql.UpdateCommandType, "B7");
  184. Assert.AreEqual ("{0}", sql.OldValuesParameterFormatString, "B8");
  185. Assert.AreEqual ("", sql.SqlCacheDependency, "B9");
  186. Assert.AreEqual ("", sql.SortParameterName, "B10");
  187. Assert.AreEqual (0, sql.CacheDuration, "B11");
  188. Assert.AreEqual (DataSourceCacheExpiry.Absolute, sql.CacheExpirationPolicy, "B12");
  189. Assert.IsFalse (sql.EnableCaching, "B13");
  190. Assert.AreEqual (SqlDataSourceMode.DataSet, sql.DataSourceMode, "B16");
  191. Assert.AreEqual ("", sql.DeleteCommand, "B17");
  192. Assert.IsNotNull (sql.DeleteParameters, "B18");
  193. Assert.AreEqual (0, sql.DeleteParameters.Count, "B18.1");
  194. Assert.IsNotNull (sql.FilterParameters, "B19");
  195. Assert.AreEqual (0, sql.FilterParameters.Count, "B19.1");
  196. Assert.AreEqual ("", sql.InsertCommand, "B20");
  197. Assert.IsNotNull (sql.InsertParameters, "B21");
  198. Assert.AreEqual (0, sql.InsertParameters.Count, "B21.1");
  199. Assert.AreEqual ("", sql.SelectCommand, "B22");
  200. Assert.IsNotNull (sql.SelectParameters, "B23");
  201. Assert.AreEqual (0, sql.SelectParameters.Count, "B23.1");
  202. Assert.AreEqual ("", sql.UpdateCommand, "B24");
  203. Assert.IsNotNull (sql.UpdateParameters, "B25");
  204. Assert.AreEqual (0, sql.UpdateParameters.Count, "B25.1");
  205. Assert.AreEqual ("", sql.FilterExpression, "B26");
  206. }
  207. // Help parameter for Asserts
  208. private static SqlParameterCollection CustomEventParameterCollection;
  209. private static string PassedParameters;
  210. [Test]
  211. public void ReturnValueParameter ()
  212. {
  213. SqlPoker sql = new SqlPoker ();
  214. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  215. sql.ProviderName = "System.Data.SqlClient";
  216. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  217. view.SelectCommandType = SqlDataSourceCommandType.Text;
  218. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  219. view.OldValuesParameterFormatString = "origin_{0}";
  220. view.SelectParameters.Add (new Parameter ("ProductID", TypeCode.Int32, "10"));
  221. Parameter myReturn = new Parameter ("myReturn", TypeCode.Int32);
  222. myReturn.Direction = ParameterDirection.ReturnValue;
  223. view.SelectParameters.Add (myReturn);
  224. view.Selecting += new SqlDataSourceSelectingEventHandler (view_Selecting);
  225. view.Select (new DataSourceSelectArguments ());
  226. Assert.IsNotNull (CustomEventParameterCollection, "Select event not fired");
  227. Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
  228. Assert.IsNotNull (CustomEventParameterCollection ["@myReturn"], "Parameter name");
  229. }
  230. [Test]
  231. public void ExecuteSelect ()
  232. {
  233. SqlPoker sql = new SqlPoker();
  234. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  235. sql.ProviderName = "System.Data.SqlClient";
  236. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  237. view.SelectCommandType = SqlDataSourceCommandType.Text;
  238. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  239. view.OldValuesParameterFormatString = "origin_{0}";
  240. view.SelectParameters.Add (new Parameter ("ProductID", TypeCode.Int32, "10"));
  241. view.Selecting += new SqlDataSourceSelectingEventHandler (view_Selecting);
  242. view.Select (new DataSourceSelectArguments ());
  243. Assert.IsNotNull (CustomEventParameterCollection, "Select event not fired");
  244. Assert.AreEqual (1, CustomEventParameterCollection.Count, "Parameter count");
  245. Assert.AreEqual ("@ProductID", CustomEventParameterCollection[0].ParameterName, "Parameter name");
  246. Assert.AreEqual (10, CustomEventParameterCollection[0].Value, "Parameter value");
  247. }
  248. [Test]
  249. public void ExecuteSelect2 ()
  250. {
  251. SqlPoker sql = new SqlPoker ();
  252. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  253. sql.ProviderName = "System.Data.SqlClient";
  254. sql.DataSourceMode = SqlDataSourceMode.DataReader;
  255. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  256. view.SelectCommandType = SqlDataSourceCommandType.Text;
  257. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  258. view.OldValuesParameterFormatString = "origin_{0}";
  259. view.SelectParameters.Add (new Parameter ("ProductID", TypeCode.Int32, "10"));
  260. view.Selecting += new SqlDataSourceSelectingEventHandler (view_Selecting);
  261. view.Select (new DataSourceSelectArguments ());
  262. Assert.IsNotNull (CustomEventParameterCollection, "Select event not fired");
  263. Assert.AreEqual (1, CustomEventParameterCollection.Count, "Parameter count");
  264. Assert.AreEqual ("@ProductID", CustomEventParameterCollection [0].ParameterName, "Parameter name");
  265. Assert.AreEqual (10, CustomEventParameterCollection [0].Value, "Parameter value");
  266. }
  267. [Test]
  268. public void ExecuteUpdate ()
  269. {
  270. SqlPoker sql = new SqlPoker ();
  271. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  272. sql.ProviderName = "System.Data.SqlClient";
  273. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  274. view.SelectCommandType = SqlDataSourceCommandType.Text;
  275. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  276. view.UpdateCommandType = SqlDataSourceCommandType.Text;
  277. view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
  278. view.OldValuesParameterFormatString = "origin_{0}";
  279. view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
  280. view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
  281. view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
  282. view.Update (null, null, null);
  283. Assert.IsNotNull (CustomEventParameterCollection, "Update event not fired");
  284. Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
  285. Assert.AreEqual ("@UserName", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
  286. Assert.AreEqual ("TestUser", CustomEventParameterCollection[0].Value, "Parameter value#1");
  287. Assert.AreEqual ("@UserId", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
  288. Assert.AreEqual (1, CustomEventParameterCollection[1].Value, "Parameter value#2");
  289. }
  290. [Test]
  291. public void ExecuteInsert ()
  292. {
  293. SqlPoker sql = new SqlPoker ();
  294. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  295. sql.ProviderName = "System.Data.SqlClient";
  296. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  297. view.SelectCommandType = SqlDataSourceCommandType.Text;
  298. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  299. view.InsertCommandType = SqlDataSourceCommandType.Text;
  300. view.InsertCommand = "INSERT INTO Table1 (UserId, UserName) VALUES ({0},{1})";
  301. view.InsertParameters.Add (new Parameter ("UserId", TypeCode.Int32, "15"));
  302. view.InsertParameters.Add (new Parameter ("UserName", TypeCode.String, "newuser"));
  303. view.OldValuesParameterFormatString = "origin_{0}";
  304. view.Inserting += new SqlDataSourceCommandEventHandler (view_Inserting);
  305. view.Insert (null);
  306. Assert.IsNotNull (CustomEventParameterCollection, "Insert event not fired");
  307. Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
  308. Assert.AreEqual ("@UserId", CustomEventParameterCollection[0].ParameterName, "Parameter name#2");
  309. Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#2");
  310. Assert.AreEqual ("@UserName", CustomEventParameterCollection[1].ParameterName, "Parameter name#1");
  311. Assert.AreEqual ("newuser", CustomEventParameterCollection[1].Value, "Parameter value#1");
  312. }
  313. [Test]
  314. public void ExecuteInsertWithCollection ()
  315. {
  316. SqlPoker sql = new SqlPoker ();
  317. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  318. sql.ProviderName = "System.Data.SqlClient";
  319. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  320. view.SelectCommandType = SqlDataSourceCommandType.Text;
  321. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  322. view.InsertCommandType = SqlDataSourceCommandType.Text;
  323. view.InsertCommand = "INSERT INTO products (UserId, UserName) VALUES ({0},{1})";
  324. view.InsertParameters.Add (new Parameter ("UserId", TypeCode.Int32, "15"));
  325. view.InsertParameters.Add (new Parameter ("UserName", TypeCode.String, "newuser"));
  326. view.OldValuesParameterFormatString = "origin_{0}";
  327. view.Inserting += new SqlDataSourceCommandEventHandler (view_Inserting);
  328. Hashtable value = new Hashtable ();
  329. value.Add ("Description", "TestDescription");
  330. view.Insert (value);
  331. Assert.IsNotNull (CustomEventParameterCollection, "Insert event not fired");
  332. Assert.AreEqual (3, CustomEventParameterCollection.Count, "Parameter count");
  333. Assert.AreEqual ("@UserId", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
  334. Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#1");
  335. Assert.AreEqual ("@UserName", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
  336. Assert.AreEqual ("newuser", CustomEventParameterCollection[1].Value, "Parameter value#2");
  337. Assert.AreEqual ("@Description", CustomEventParameterCollection[2].ParameterName, "Parameter name#3");
  338. Assert.AreEqual ("TestDescription", CustomEventParameterCollection[2].Value, "Parameter value#3");
  339. }
  340. [Test]
  341. public void ExecuteDelete ()
  342. {
  343. SqlPoker sql = new SqlPoker ();
  344. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  345. sql.ProviderName = "System.Data.SqlClient";
  346. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  347. view.SelectCommandType = SqlDataSourceCommandType.Text;
  348. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  349. view.DeleteCommandType = SqlDataSourceCommandType.Text;
  350. view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
  351. view.DeleteParameters.Add (new Parameter ("ProductId", TypeCode.Int32, "15"));
  352. view.OldValuesParameterFormatString = "origin_{0}";
  353. view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
  354. view.Delete (null, null);
  355. Assert.IsNotNull (CustomEventParameterCollection, "Delete event not fired");
  356. Assert.AreEqual (1, CustomEventParameterCollection.Count, "Parameter count");
  357. Assert.AreEqual ("@ProductId", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
  358. Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#1");
  359. }
  360. [Test]
  361. public void ExecuteDeleteWithOldValues ()
  362. {
  363. SqlPoker sql = new SqlPoker ();
  364. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  365. sql.ProviderName = "System.Data.SqlClient";
  366. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  367. view.SelectCommandType = SqlDataSourceCommandType.Text;
  368. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  369. view.DeleteCommandType = SqlDataSourceCommandType.Text;
  370. view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
  371. view.DeleteParameters.Add (new Parameter ("ProductID", TypeCode.Int32, "15"));
  372. view.OldValuesParameterFormatString = "origin_{0}";
  373. view.ConflictDetection = ConflictOptions.CompareAllValues;
  374. view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
  375. Hashtable oldvalue = new Hashtable ();
  376. oldvalue.Add ("ProductID", 10);
  377. view.Delete (null,oldvalue );
  378. Assert.IsNotNull (CustomEventParameterCollection, "Delete event not fired");
  379. Assert.AreEqual (1, CustomEventParameterCollection.Count, "Parameter count");
  380. Assert.AreEqual ("@origin_ProductID", CustomEventParameterCollection[0].ParameterName, "Parameter name#2");
  381. Assert.AreEqual (10, CustomEventParameterCollection[0].Value, "Parameter value#2");
  382. }
  383. [Test]
  384. public void ExecuteDeleteWithMergedOldValues ()
  385. {
  386. SqlPoker sql = new SqlPoker ();
  387. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  388. sql.ProviderName = "System.Data.SqlClient";
  389. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  390. view.SelectCommandType = SqlDataSourceCommandType.Text;
  391. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  392. view.DeleteCommandType = SqlDataSourceCommandType.Text;
  393. view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
  394. view.DeleteParameters.Add (new Parameter ("ProductId", TypeCode.Int32, "15"));
  395. view.OldValuesParameterFormatString = "origin_{0}";
  396. view.ConflictDetection = ConflictOptions.CompareAllValues;
  397. view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
  398. Hashtable oldvalue = new Hashtable ();
  399. oldvalue.Add ("Desc", "Description");
  400. view.Delete (null, oldvalue);
  401. Assert.IsNotNull (CustomEventParameterCollection, "Delete event not fired");
  402. Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
  403. Assert.AreEqual ("@ProductId", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
  404. Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#1");
  405. Assert.AreEqual ("@origin_Desc", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
  406. Assert.AreEqual ("Description", CustomEventParameterCollection[1].Value, "Parameter value#2");
  407. }
  408. [Test]
  409. public void ExecuteDeleteWithMergedValues ()
  410. {
  411. SqlPoker sql = new SqlPoker ();
  412. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  413. sql.ProviderName = "System.Data.SqlClient";
  414. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  415. view.SelectCommandType = SqlDataSourceCommandType.Text;
  416. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  417. view.DeleteCommandType = SqlDataSourceCommandType.Text;
  418. view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
  419. view.DeleteParameters.Add (new Parameter ("ProductId", TypeCode.Int32, "15"));
  420. view.OldValuesParameterFormatString = "origin_{0}";
  421. view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
  422. Hashtable value = new Hashtable ();
  423. value.Add ("Desc", "Description");
  424. view.Delete (value, null);
  425. Assert.IsNotNull (CustomEventParameterCollection, "Delete event not fired");
  426. Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
  427. Assert.AreEqual ("@ProductId", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
  428. Assert.AreEqual (15, CustomEventParameterCollection[0].Value, "Parameter value#1");
  429. Assert.AreEqual ("@origin_Desc", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
  430. Assert.AreEqual ("Description", CustomEventParameterCollection[1].Value, "Parameter value#2");
  431. }
  432. [Test]
  433. public void ExecuteDelete_KeysAndOldValues_OverwriteChanges ()
  434. {
  435. SqlPoker sql = new SqlPoker ();
  436. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  437. sql.ProviderName = "System.Data.SqlClient";
  438. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  439. IDictionary keys;
  440. IDictionary values;
  441. IDictionary oldValues;
  442. InitializeView (view, out keys, out values, out oldValues);
  443. view.ConflictDetection = ConflictOptions.OverwriteChanges;
  444. view.Delete (keys, oldValues);
  445. Assert.IsNotNull (CustomEventParameterCollection, "KeysAndOldValues_OverwriteChanges");
  446. Assert.AreEqual ("String:@origin_ProductID=k_10", PassedParameters, "KeysAndOldValues_OverwriteChanges Values");
  447. }
  448. [Test]
  449. public void ExecuteDelete_KeysAndOldValues_CompareAllValues ()
  450. {
  451. SqlPoker sql = new SqlPoker ();
  452. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  453. sql.ProviderName = "System.Data.SqlClient";
  454. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  455. IDictionary keys;
  456. IDictionary values;
  457. IDictionary oldValues;
  458. InitializeView (view, out keys, out values, out oldValues);
  459. view.ConflictDetection = ConflictOptions.CompareAllValues;
  460. view.Delete (keys, oldValues);
  461. Assert.IsNotNull (CustomEventParameterCollection, "KeysAndOldValues_CompareAllValues");
  462. Assert.AreEqual ("String:@origin_ProductID=ov_10, String:@origin_Description=ov_Beautifull, String:@origin_Name=ov_ColorTV", PassedParameters, "KeysAndOldValues_CompareAllValues Values");
  463. }
  464. [Test]
  465. public void ExecuteDelete_KeysAndOldValues_CompareAllValues2 ()
  466. {
  467. SqlPoker sql = new SqlPoker ();
  468. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  469. sql.ProviderName = "System.Data.SqlClient";
  470. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  471. IDictionary keys;
  472. IDictionary values;
  473. IDictionary oldValues;
  474. InitializeView (view, out keys, out values, out oldValues);
  475. view.DeleteParameters.Add ("origin_ProductID", "po_10");
  476. view.ConflictDetection = ConflictOptions.CompareAllValues;
  477. view.Delete (keys, oldValues);
  478. Assert.IsNotNull (CustomEventParameterCollection, "ExecuteDelete_KeysAndOldValues_CompareAllValues2");
  479. string [] expectedParams = new string []
  480. {
  481. "String:@origin_ProductID=ov_10",
  482. "String:@origin_Description=ov_Beautifull",
  483. "String:@origin_Name=ov_ColorTV"
  484. };
  485. string [] actualValues = PassedParameters.Split (new string [] { ", " }, StringSplitOptions.RemoveEmptyEntries);
  486. Assert.AreEqual (expectedParams.Length, actualValues.Length, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 Params count");
  487. ValidatePassedParams (expectedParams, actualValues, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 expecte '{0}'");
  488. }
  489. private static void ValidatePassedParams (string [] expectedParams, string [] actualValues, string errorMessageFormat)
  490. {
  491. foreach (string eps in expectedParams) {
  492. bool found = false;
  493. foreach (string aps in actualValues) {
  494. if (eps == aps) {
  495. found = true;
  496. break;
  497. }
  498. }
  499. Assert.IsTrue (found, String.Format (errorMessageFormat, eps));
  500. }
  501. }
  502. private void InitializeView (CustomSqlDataSourceView view, out IDictionary keys, out IDictionary values, out IDictionary oldValues)
  503. {
  504. view.SelectCommandType = SqlDataSourceCommandType.Text;
  505. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  506. view.SelectParameters.Add (new Parameter ("ProductID", TypeCode.String, "p_10"));
  507. view.Selecting += new SqlDataSourceSelectingEventHandler (view_Selecting);
  508. view.DeleteCommandType = SqlDataSourceCommandType.Text;
  509. view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
  510. view.DeleteParameters.Add (new Parameter ("ProductID", TypeCode.String, "p_10"));
  511. view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
  512. view.InsertCommandType = SqlDataSourceCommandType.Text;
  513. view.InsertCommand = "INSERT INTO products (ProductID, Name, Description) VALUES (@ProductID, @Name, @Description)";
  514. view.InsertParameters.Add (new Parameter ("ProductID", TypeCode.String, "p_15"));
  515. view.InsertParameters.Add (new Parameter ("Name", TypeCode.String, "p_NewProduct"));
  516. view.InsertParameters.Add (new Parameter ("Description", TypeCode.String, "p_Description"));
  517. view.Inserting += new SqlDataSourceCommandEventHandler (view_Inserting);
  518. view.UpdateCommandType = SqlDataSourceCommandType.Text;
  519. view.UpdateCommand = "UPDATE products SET Name = @Name, Description = @Description WHERE ProductID = @ProductID";
  520. view.UpdateParameters.Add (new Parameter ("ProductID", TypeCode.String, "p_15"));
  521. view.UpdateParameters.Add (new Parameter ("Name", TypeCode.String, "p_UpdatedProduct"));
  522. view.UpdateParameters.Add (new Parameter ("Description", TypeCode.String, "p_UpdatedDescription"));
  523. view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
  524. view.OldValuesParameterFormatString = "origin_{0}";
  525. keys = new Hashtable ();
  526. values = new Hashtable ();
  527. oldValues = new Hashtable ();
  528. keys.Add ("ProductID", "k_10");
  529. values.Add ("ProductID", "n_10");
  530. values.Add ("Name", "n_ColorTV");
  531. values.Add ("Description", "n_Beautifull");
  532. oldValues.Add ("ProductID", "ov_10");
  533. oldValues.Add ("Name", "ov_ColorTV");
  534. oldValues.Add ("Description", "ov_Beautifull");
  535. }
  536. [Test]
  537. public void ExecuteUpdateWithOldValues ()
  538. {
  539. SqlPoker sql = new SqlPoker ();
  540. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  541. sql.ProviderName = "System.Data.SqlClient";
  542. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  543. view.SelectCommandType = SqlDataSourceCommandType.Text;
  544. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  545. view.UpdateCommandType = SqlDataSourceCommandType.Text;
  546. view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
  547. view.OldValuesParameterFormatString = "origin_{0}";
  548. view.ConflictDetection = ConflictOptions.CompareAllValues;
  549. view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
  550. view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
  551. view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
  552. Hashtable oldvalue = new Hashtable ();
  553. oldvalue.Add ("UserId", 2);
  554. view.Update (null, null, oldvalue);
  555. Assert.IsNotNull (CustomEventParameterCollection, "Update event not fired");
  556. Assert.AreEqual (3, CustomEventParameterCollection.Count, "Parameter count");
  557. Assert.AreEqual ("@UserName", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
  558. Assert.AreEqual ("TestUser", CustomEventParameterCollection[0].Value, "Parameter value#1");
  559. Assert.AreEqual ("@UserId", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
  560. Assert.AreEqual (1, CustomEventParameterCollection[1].Value, "Parameter value#2");
  561. Assert.AreEqual ("@origin_UserId", CustomEventParameterCollection[2].ParameterName, "Parameter name#3");
  562. Assert.AreEqual (2, CustomEventParameterCollection[2].Value, "Parameter value#3");
  563. }
  564. [Test]
  565. public void ExecuteUpdateWithOverwriteParameters ()
  566. {
  567. SqlPoker sql = new SqlPoker ();
  568. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  569. sql.ProviderName = "System.Data.SqlClient";
  570. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  571. view.SelectCommandType = SqlDataSourceCommandType.Text;
  572. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  573. view.UpdateCommandType = SqlDataSourceCommandType.Text;
  574. view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
  575. view.OldValuesParameterFormatString = "origin_{0}";
  576. view.ConflictDetection = ConflictOptions.OverwriteChanges;
  577. view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
  578. view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
  579. view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
  580. Hashtable value = new Hashtable ();
  581. value.Add ("UserId", 2);
  582. view.Update (value, null, null);
  583. Assert.IsNotNull (CustomEventParameterCollection, "Update event not fired");
  584. Assert.AreEqual (2, CustomEventParameterCollection.Count, "Parameter count");
  585. Assert.AreEqual ("@UserName", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
  586. Assert.AreEqual ("TestUser", CustomEventParameterCollection[0].Value, "Parameter value#1");
  587. Assert.AreEqual ("@origin_UserId", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
  588. Assert.AreEqual (2, CustomEventParameterCollection[1].Value, "Parameter value#2");
  589. }
  590. [Test]
  591. public void ExecuteUpdateWithMargeParameters ()
  592. {
  593. SqlPoker sql = new SqlPoker ();
  594. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  595. sql.ProviderName = "System.Data.SqlClient";
  596. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  597. view.SelectCommandType = SqlDataSourceCommandType.Text;
  598. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  599. view.UpdateCommandType = SqlDataSourceCommandType.Text;
  600. view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
  601. view.OldValuesParameterFormatString = "origin_{0}";
  602. view.ConflictDetection = ConflictOptions.OverwriteChanges;
  603. view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
  604. view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
  605. view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
  606. Hashtable value = new Hashtable ();
  607. value.Add ("UserLName", "TestLName");
  608. view.Update (null, value, null);
  609. Assert.IsNotNull (CustomEventParameterCollection, "Update event not fired");
  610. Assert.AreEqual (3, CustomEventParameterCollection.Count, "Parameter count");
  611. Assert.AreEqual ("@UserName", CustomEventParameterCollection[0].ParameterName, "Parameter name#1");
  612. Assert.AreEqual ("TestUser", CustomEventParameterCollection[0].Value, "Parameter value#1");
  613. Assert.AreEqual ("@UserId", CustomEventParameterCollection[1].ParameterName, "Parameter name#2");
  614. Assert.AreEqual (1, CustomEventParameterCollection[1].Value, "Parameter value#2");
  615. Assert.AreEqual ("@UserLName", CustomEventParameterCollection[2].ParameterName, "Parameter name#3");
  616. Assert.AreEqual ("TestLName", CustomEventParameterCollection[2].Value, "Parameter value#3");
  617. }
  618. [Test]
  619. public void ExecuteUpdate_KeysValuesAndOldValues_OverwriteChanges ()
  620. {
  621. SqlPoker sql = new SqlPoker ();
  622. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  623. sql.ProviderName = "System.Data.SqlClient";
  624. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  625. IDictionary keys;
  626. IDictionary values;
  627. IDictionary oldValues;
  628. InitializeView (view, out keys, out values, out oldValues);
  629. view.ConflictDetection = ConflictOptions.OverwriteChanges;
  630. view.Update (keys, values, oldValues);
  631. Assert.IsNotNull (CustomEventParameterCollection, "ExecuteUpdate_KeysValuesAndOldValues_OverwriteChanges");
  632. string [] expectedParams = new string []
  633. {
  634. "String:@ProductID=n_10",
  635. "String:@Name=n_ColorTV",
  636. "String:@Description=n_Beautifull",
  637. "String:@origin_ProductID=k_10"
  638. };
  639. string [] actualValues = PassedParameters.Split (new string [] { ", " }, StringSplitOptions.RemoveEmptyEntries);
  640. Assert.AreEqual (expectedParams.Length, actualValues.Length, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 Params count");
  641. ValidatePassedParams (expectedParams, actualValues, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 expecte '{0}'");
  642. }
  643. [Test]
  644. public void ExecuteUpdate_KeysValuesAndOldValues_CompareAllValues ()
  645. {
  646. SqlPoker sql = new SqlPoker ();
  647. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  648. sql.ProviderName = "System.Data.SqlClient";
  649. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  650. IDictionary keys;
  651. IDictionary values;
  652. IDictionary oldValues;
  653. InitializeView (view, out keys, out values, out oldValues);
  654. view.ConflictDetection = ConflictOptions.CompareAllValues;
  655. view.Update (keys, values, oldValues);
  656. Assert.IsNotNull (CustomEventParameterCollection, "ExecuteUpdate_KeysValuesAndOldValues_CompareAllValues");
  657. string [] expectedParams = new string []
  658. {
  659. "String:@ProductID=n_10",
  660. "String:@Name=n_ColorTV",
  661. "String:@Description=n_Beautifull",
  662. "String:@origin_ProductID=ov_10",
  663. "String:@origin_Name=ov_ColorTV",
  664. "String:@origin_Description=ov_Beautifull",
  665. };
  666. string [] actualValues = PassedParameters.Split (new string [] { ", " }, StringSplitOptions.RemoveEmptyEntries);
  667. Assert.AreEqual (expectedParams.Length, actualValues.Length, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 Params count");
  668. ValidatePassedParams (expectedParams, actualValues, "ExecuteDelete_KeysAndOldValues_CompareAllValues2 expecte '{0}'");
  669. }
  670. void view_Updating (object sender, SqlDataSourceCommandEventArgs e)
  671. {
  672. SqlDataSourceTest.CustomEventParameterCollection = (SqlParameterCollection) e.Command.Parameters;
  673. SqlDataSourceTest.PassedParameters = FormatParameters (SqlDataSourceTest.CustomEventParameterCollection);
  674. e.Cancel = true;
  675. }
  676. void view_Selecting (object sender, SqlDataSourceSelectingEventArgs e)
  677. {
  678. SqlDataSourceTest.CustomEventParameterCollection = (SqlParameterCollection) e.Command.Parameters;
  679. SqlDataSourceTest.PassedParameters = FormatParameters (SqlDataSourceTest.CustomEventParameterCollection);
  680. e.Cancel = true;
  681. }
  682. void view_Inserting (object sender, SqlDataSourceCommandEventArgs e)
  683. {
  684. SqlDataSourceTest.CustomEventParameterCollection = (SqlParameterCollection) e.Command.Parameters;
  685. SqlDataSourceTest.PassedParameters = FormatParameters (SqlDataSourceTest.CustomEventParameterCollection);
  686. e.Cancel = true;
  687. }
  688. void view_Deleting (object sender, SqlDataSourceCommandEventArgs e)
  689. {
  690. SqlDataSourceTest.CustomEventParameterCollection = (SqlParameterCollection) e.Command.Parameters;
  691. SqlDataSourceTest.PassedParameters = FormatParameters (SqlDataSourceTest.CustomEventParameterCollection);
  692. e.Cancel = true;
  693. }
  694. private string FormatParameters (SqlParameterCollection sqlParameterCollection)
  695. {
  696. StringBuilder sb = new StringBuilder ();
  697. foreach (SqlParameter p in sqlParameterCollection) {
  698. if (sb.Length > 0) {
  699. sb.Append (", ");
  700. }
  701. sb.AppendFormat ("{0}:{1}={2}", p.DbType, p.ParameterName, p.Value);
  702. }
  703. return sb.ToString ();
  704. }
  705. #region help_results
  706. class eventAssert
  707. {
  708. private static int _testcounter;
  709. private static bool _eventChecker;
  710. private eventAssert ()
  711. {
  712. _testcounter = 0;
  713. }
  714. public static bool eventChecker
  715. {
  716. get
  717. {
  718. throw new NotImplementedException ();
  719. }
  720. set
  721. {
  722. _eventChecker = value;
  723. }
  724. }
  725. static private void testAdded ()
  726. {
  727. _testcounter++;
  728. _eventChecker = false;
  729. }
  730. public static void IsTrue (string msg)
  731. {
  732. Assert.IsTrue (_eventChecker, msg + "#" + _testcounter);
  733. testAdded ();
  734. }
  735. public static void IsFalse (string msg)
  736. {
  737. Assert.IsFalse (_eventChecker, msg + "#" + _testcounter);
  738. testAdded ();
  739. }
  740. }
  741. #endregion
  742. [Test]
  743. public void SqlDataSource_DataSourceViewChanged ()
  744. {
  745. SqlPoker sql = new SqlPoker ();
  746. ((IDataSource) sql).DataSourceChanged += new EventHandler (SqlDataSourceTest_DataSourceChanged);
  747. sql.DoRaiseDataSourceChangedEvent ();
  748. eventAssert.IsTrue ("SqlDataSourceView"); // Assert include counter the first is zero
  749. sql.CacheKeyDependency = "hi";
  750. eventAssert.IsFalse ("SqlDataSourceView");
  751. sql.CancelSelectOnNullParameter = false;
  752. eventAssert.IsFalse ("SqlDataSourceView");
  753. sql.ConflictDetection = ConflictOptions.CompareAllValues;
  754. eventAssert.IsFalse ("SqlDataSourceView");
  755. sql.DeleteCommandType = SqlDataSourceCommandType.StoredProcedure;
  756. eventAssert.IsFalse ("SqlDataSourceView");
  757. sql.InsertCommandType = SqlDataSourceCommandType.StoredProcedure;
  758. eventAssert.IsFalse ("SqlDataSourceView");
  759. sql.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
  760. eventAssert.IsFalse ("SqlDataSourceView");
  761. sql.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;
  762. eventAssert.IsFalse ("SqlDataSourceView");
  763. sql.OldValuesParameterFormatString = "{1}";
  764. eventAssert.IsFalse ("SqlDataSourceView");
  765. sql.SqlCacheDependency = "hi";
  766. eventAssert.IsFalse ("SqlDataSourceView");
  767. sql.SortParameterName = "hi";
  768. eventAssert.IsFalse ("SqlDataSourceView");
  769. sql.CacheDuration = 1;
  770. eventAssert.IsFalse ("SqlDataSourceView");
  771. sql.CacheExpirationPolicy = DataSourceCacheExpiry.Sliding;
  772. eventAssert.IsFalse ("SqlDataSourceView");
  773. sql.EnableCaching = true;
  774. eventAssert.IsFalse ("SqlDataSourceView");
  775. sql.DataSourceMode = SqlDataSourceMode.DataReader;
  776. eventAssert.IsTrue ("SqlDataSourceView");
  777. sql.DeleteCommand = "DELETE foo";
  778. eventAssert.IsFalse ("SqlDataSourceView");
  779. sql.InsertCommand = "INSERT foo";
  780. eventAssert.IsFalse ("SqlDataSourceView");
  781. sql.SelectCommand = "SELECT foo";
  782. eventAssert.IsFalse ("SqlDataSourceView");
  783. sql.UpdateCommand = "UPDATE foo";
  784. eventAssert.IsFalse ("SqlDataSourceView");
  785. sql.FilterExpression = "hi";
  786. eventAssert.IsFalse ("SqlDataSourceView");
  787. }
  788. void SqlDataSourceTest_DataSourceChanged (object sender, EventArgs e)
  789. {
  790. eventAssert.eventChecker = true;
  791. }
  792. //exceptions
  793. [Test]
  794. [ExpectedException (typeof (NotSupportedException))]
  795. public void ExecuteUpdateException ()
  796. {
  797. SqlPoker sql = new SqlPoker ();
  798. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  799. sql.ProviderName = "System.Data.SqlClient";
  800. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  801. view.Update (null, null, null);
  802. }
  803. [Test]
  804. [ExpectedException (typeof (NotSupportedException))]
  805. public void ExecuteDeleteException ()
  806. {
  807. SqlPoker sql = new SqlPoker ();
  808. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  809. sql.ProviderName = "System.Data.SqlClient";
  810. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  811. view.Delete (null, null);
  812. }
  813. [Test]
  814. [ExpectedException (typeof (NotSupportedException))]
  815. public void ExecuteInsertException ()
  816. {
  817. SqlPoker sql = new SqlPoker ();
  818. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  819. sql.ProviderName = "System.Data.SqlClient";
  820. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  821. view.SelectCommandType = SqlDataSourceCommandType.Text;
  822. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  823. view.Insert (null);
  824. }
  825. [Test] //ConflictOptions.CompareAllValues must include old value collection
  826. [ExpectedException (typeof (InvalidOperationException))]
  827. public void ExecuteUpdateWithOldValuesException ()
  828. {
  829. SqlPoker sql = new SqlPoker ();
  830. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  831. sql.ProviderName = "System.Data.SqlClient";
  832. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  833. view.SelectCommandType = SqlDataSourceCommandType.Text;
  834. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  835. view.UpdateCommandType = SqlDataSourceCommandType.Text;
  836. view.UpdateCommand = "UPDATE Table1 SET UserName = @UserName WHERE UserId = @UserId";
  837. view.OldValuesParameterFormatString = "origin_{0}";
  838. view.ConflictDetection = ConflictOptions.CompareAllValues;
  839. view.Updating += new SqlDataSourceCommandEventHandler (view_Updating);
  840. view.UpdateParameters.Add (new Parameter ("UserName", TypeCode.String, "TestUser"));
  841. view.UpdateParameters.Add (new Parameter ("UserId", TypeCode.Int32, "1"));
  842. view.Update (null, null, null);
  843. }
  844. [Test] //ConflictOptions.CompareAllValues must include old value collection
  845. [ExpectedException (typeof (InvalidOperationException))]
  846. public void ExecuteDeleteWithOldValuesException ()
  847. {
  848. SqlPoker sql = new SqlPoker ();
  849. sql.ConnectionString = "Data Source=fake\\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa";
  850. sql.ProviderName = "System.Data.SqlClient";
  851. CustomSqlDataSourceView view = new CustomSqlDataSourceView (sql, "TestView", null);
  852. view.SelectCommandType = SqlDataSourceCommandType.Text;
  853. view.SelectCommand = "SELECT * FROM products WHERE ProductID = @ProductID;";
  854. view.DeleteCommandType = SqlDataSourceCommandType.Text;
  855. view.DeleteCommand = "DELETE * FROM products WHERE ProductID = @ProductID;";
  856. view.DeleteParameters.Add (new Parameter ("ProductId", TypeCode.Int32, "15"));
  857. view.OldValuesParameterFormatString = "origin_{0}";
  858. view.ConflictDetection = ConflictOptions.CompareAllValues;
  859. view.Deleting += new SqlDataSourceCommandEventHandler (view_Deleting);
  860. Hashtable oldvalue = new Hashtable ();
  861. oldvalue.Add ("ProductID", 10);
  862. view.Delete (null, null);
  863. }
  864. }
  865. }
  866. #endif