DataViewTest2.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. // Authors:
  2. // Rafael Mizrahi <[email protected]>
  3. // Erez Lotan <[email protected]>
  4. // Oren Gurfinkel <[email protected]>
  5. // Ofer Borstein
  6. //
  7. // Copyright (c) 2004 Mainsoft Co.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.IO;
  31. using System.ComponentModel;
  32. using System.Data;
  33. using MonoTests.System.Data.Utils;
  34. namespace MonoTests_System.Data
  35. {
  36. [TestFixture] public class DataViewTest2
  37. {
  38. private EventProperties evProp = null;
  39. class EventProperties //hold the event properties to be checked
  40. {
  41. public System.ComponentModel.ListChangedType lstType ;
  42. public int NewIndex;
  43. public int OldIndex;
  44. }
  45. [Test] public void AddNew()
  46. {
  47. //create the source datatable
  48. DataTable dt = DataProvider.CreateChildDataTable();
  49. //create the dataview for the table
  50. DataView dv = new DataView(dt);
  51. int CountView = dv.Count ;
  52. int CountTable= dt.Rows.Count ;
  53. DataRowView drv = null;
  54. // AddNew - DataView Row Count
  55. drv = dv.AddNew();
  56. Assert.AreEqual(dv.Count , CountView+1, "DV1");
  57. // AddNew - Table Row Count
  58. Assert.AreEqual(dt.Rows.Count , CountTable, "DV2");
  59. // AddNew - new row in DataTable
  60. drv.EndEdit();
  61. Assert.AreEqual(dt.Rows.Count , CountTable+1, "DV3");
  62. // AddNew - new row != null
  63. Assert.AreEqual(true, drv!=null, "DV4");
  64. // AddNew - check table
  65. Assert.AreEqual(dt, drv.Row.Table, "DV5");
  66. }
  67. [Test] public void AllowDelete()
  68. {
  69. DataTable dt = DataProvider.CreateParentDataTable();
  70. DataView dv = new DataView(dt);
  71. // AllowDelete - default value
  72. Assert.AreEqual(true , dv.AllowDelete , "DV6");
  73. // AllowDelete - true
  74. dv.AllowDelete = true;
  75. Assert.AreEqual(true, dv.AllowDelete , "DV7");
  76. // AllowDelete - false
  77. dv.AllowDelete = false;
  78. Assert.AreEqual(false, dv.AllowDelete , "DV8");
  79. dv.AllowDelete = false;
  80. // AllowDelete false- Exception
  81. try
  82. {
  83. dv.Delete(0);
  84. Assert.Fail("DV9: Delete Failed to throw DataException");
  85. }
  86. catch (DataException) {}
  87. catch (AssertionException exc) {throw exc;}
  88. catch (Exception exc)
  89. {
  90. Assert.Fail("DV10: Delete. Wrong exception type. Got:" + exc);
  91. }
  92. dv.AllowDelete = true;
  93. int RowsCount = dv.Count ;
  94. // AllowDelete true- Exception
  95. dv.Delete(0);
  96. Assert.AreEqual(RowsCount-1, dv.Count , "DV11");
  97. }
  98. [Test] public void AllowEdit()
  99. {
  100. DataTable dt = DataProvider.CreateParentDataTable();
  101. DataView dv = new DataView(dt);
  102. // AllowEdit - default value
  103. Assert.AreEqual(true , dv.AllowEdit , "DV12");
  104. // AllowEdit - true
  105. dv.AllowEdit = true;
  106. Assert.AreEqual(true, dv.AllowEdit , "DV13");
  107. // AllowEdit - false
  108. dv.AllowEdit = false;
  109. Assert.AreEqual(false, dv.AllowEdit , "DV14");
  110. dv.AllowEdit=false;
  111. // AllowEdit false - exception
  112. try
  113. {
  114. dv[0][2] = "aaa";
  115. Assert.Fail("DV15: Indexer Failed to throw DataException");
  116. }
  117. catch (DataException) {}
  118. catch (AssertionException exc) {throw exc;}
  119. catch (Exception exc)
  120. {
  121. Assert.Fail("DV16: Indexer. Wrong exception type. Got:" + exc);
  122. }
  123. dv.AllowEdit=true;
  124. // AllowEdit true- exception
  125. dv[0][2] = "aaa";
  126. Assert.AreEqual("aaa", dv[0][2] , "DV17");
  127. }
  128. [Test] public void AllowNew()
  129. {
  130. DataTable dt = DataProvider.CreateParentDataTable();
  131. DataView dv = new DataView(dt);
  132. // AllowNew - default value
  133. Assert.AreEqual(true , dv.AllowNew , "DV18");
  134. // AllowNew - true
  135. dv.AllowNew = true;
  136. Assert.AreEqual(true, dv.AllowNew , "DV19");
  137. // AllowNew - false
  138. dv.AllowNew = false;
  139. Assert.AreEqual(false, dv.AllowNew , "DV20");
  140. // AllowNew - exception
  141. try
  142. {
  143. dv.AddNew();
  144. Assert.Fail("DV21: AddNew Failed to throw DataException");
  145. }
  146. catch (DataException) {}
  147. catch (AssertionException exc) {throw exc;}
  148. catch (Exception exc)
  149. {
  150. Assert.Fail("DV22: AddNew. Wrong exception type. Got:" + exc);
  151. }
  152. dv.AllowNew=true;
  153. int RowsCount = dv.Count ;
  154. // AllowNew - exception
  155. dv.AddNew();
  156. Assert.AreEqual(RowsCount+1, dv.Count , "DV23");
  157. }
  158. [Test] public void ApplyDefaultSort()
  159. {
  160. DataTable dt = DataProvider.CreateParentDataTable();
  161. DataView dv = new DataView(dt);
  162. // ApplyDefaultSort - default value
  163. Assert.AreEqual(false , dv.ApplyDefaultSort , "DV24");
  164. // ApplyDefaultSort - true
  165. dv.ApplyDefaultSort = true;
  166. Assert.AreEqual(true, dv.ApplyDefaultSort , "DV25");
  167. // ApplyDefaultSort - false
  168. dv.ApplyDefaultSort = false;
  169. Assert.AreEqual(false, dv.ApplyDefaultSort , "DV26");
  170. }
  171. [Test] public void CopyTo()
  172. {
  173. //create the source datatable
  174. DataTable dt = DataProvider.CreateChildDataTable();
  175. //create the dataview for the table
  176. DataView dv = new DataView(dt);
  177. DataRowView[] drvExpected = null;
  178. DataRowView[] drvResult = null;
  179. // ------- Copy from Index=0
  180. drvExpected = new DataRowView[dv.Count];
  181. for (int i=0; i < dv.Count ;i++)
  182. {
  183. drvExpected[i] = dv[i];
  184. }
  185. drvResult = new DataRowView[dv.Count];
  186. // CopyTo from index 0
  187. dv.CopyTo(drvResult,0);
  188. Assert.AreEqual(drvResult, drvExpected , "DV27");
  189. // ------- Copy from Index=3
  190. drvExpected = new DataRowView[dv.Count+3];
  191. for (int i=0; i < dv.Count ;i++)
  192. {
  193. drvExpected[i+3] = dv[i];
  194. }
  195. drvResult = new DataRowView[dv.Count+3];
  196. // CopyTo from index 3
  197. dv.CopyTo(drvResult,3);
  198. Assert.AreEqual(drvResult , drvExpected , "DV28");
  199. // ------- Copy from Index=3,larger array
  200. drvExpected = new DataRowView[dv.Count+9];
  201. for (int i=0; i < dv.Count ;i++)
  202. {
  203. drvExpected[i+3] = dv[i];
  204. }
  205. drvResult = new DataRowView[dv.Count+9];
  206. // CopyTo from index 3,larger array
  207. dv.CopyTo(drvResult,3);
  208. Assert.AreEqual(drvResult, drvExpected , "DV29");
  209. // ------- CopyTo smaller array, check exception
  210. drvResult = new DataRowView[dv.Count-1];
  211. // CopyTo smaller array, check exception
  212. try
  213. {
  214. dv.CopyTo(drvResult,0);
  215. Assert.Fail("DV30: CopyTo Failed to throw IndexOutOfRangeException");
  216. }
  217. catch (IndexOutOfRangeException) {}
  218. catch (AssertionException exc) {throw exc;}
  219. catch (Exception exc)
  220. {
  221. Assert.Fail("DV31: CopyTo. Wrong exception type. Got:" + exc);
  222. }
  223. }
  224. [Test] public void Delete()
  225. {
  226. //create the source datatable
  227. DataTable dt = DataProvider.CreateChildDataTable();
  228. //create the dataview for the table
  229. DataView dv = new DataView(dt);
  230. int CountView = dv.Count ;
  231. int CountTable= dt.Rows.Count ;
  232. DataRowView drv = dv[0];
  233. // Delete - DataView Row Count
  234. dv.Delete(0);
  235. Assert.AreEqual(dv.Count , CountView-1, "DV32");
  236. // Delete - Table Row Count
  237. Assert.AreEqual(dt.Rows.Count , CountTable, "DV33");
  238. // Delete - check table
  239. Assert.AreEqual(dt, drv.Row.Table, "DV34");
  240. }
  241. [Test] public void FindRows_ByKey()
  242. {
  243. DataRowView[] dvArr = null;
  244. //create the source datatable
  245. DataTable dt = DataProvider.CreateChildDataTable();
  246. //create the dataview for the table
  247. DataView dv = new DataView(dt);
  248. // FindRows ,no sort - exception
  249. try
  250. {
  251. dvArr = dv.FindRows(3);
  252. Assert.Fail("DV35: FindRows Failed to throw ArgumentException");
  253. }
  254. catch (ArgumentException) {}
  255. catch (AssertionException exc) {throw exc;}
  256. catch (Exception exc)
  257. {
  258. Assert.Fail("DV36: FindRows. Wrong exception type. Got:" + exc);
  259. }
  260. dv.Sort = "String1";
  261. // Find = wrong sort, can not find
  262. dvArr = dv.FindRows(3);
  263. Assert.AreEqual(0, dvArr.Length , "DV37");
  264. dv.Sort = "ChildId";
  265. //get expected results
  266. DataRow[] drExpected = dt.Select("ChildId=3");
  267. // FindRows - check count
  268. dvArr = dv.FindRows(3);
  269. Assert.AreEqual(drExpected.Length , dvArr.Length, "DV38");
  270. // FindRows - check data
  271. //check that result is ok
  272. bool Succeed = true;
  273. for (int i=0; i<dvArr.Length ; i++)
  274. {
  275. Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
  276. if (!Succeed) break;
  277. }
  278. Assert.AreEqual(true, Succeed , "DV39");
  279. }
  280. [Test] public void FindRows_ByKeys()
  281. {
  282. DataRowView[] dvArr = null;
  283. //create the source datatable
  284. DataTable dt = DataProvider.CreateChildDataTable();
  285. //create the dataview for the table
  286. DataView dv = new DataView(dt);
  287. // FindRows ,no sort - exception
  288. try
  289. {
  290. dvArr = dv.FindRows(new object[] {"3","3-String1"});
  291. Assert.Fail("DV40: FindRows Failed to throw ArgumentException");
  292. }
  293. catch (ArgumentException) {}
  294. catch (AssertionException exc) {throw exc;}
  295. catch (Exception exc)
  296. {
  297. Assert.Fail("DV41: FindRows. Wrong exception type. Got:" + exc);
  298. }
  299. dv.Sort = "String1,ChildId";
  300. // Find = wrong sort, can not find
  301. dvArr = dv.FindRows(new object[] {"3","3-String1"});
  302. Assert.AreEqual(0, dvArr.Length , "DV42");
  303. dv.Sort = "ChildId,String1";
  304. //get expected results
  305. DataRow[] drExpected = dt.Select("ChildId=3 and String1='3-String1'");
  306. // FindRows - check count
  307. dvArr = dv.FindRows(new object[] {"3","3-String1"});
  308. Assert.AreEqual(drExpected.Length , dvArr.Length, "DV43");
  309. // FindRows - check data
  310. //check that result is ok
  311. bool Succeed = true;
  312. for (int i=0; i<dvArr.Length ; i++)
  313. {
  314. Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
  315. if (!Succeed) break;
  316. }
  317. Assert.AreEqual(true, Succeed , "DV44");
  318. }
  319. //Activate This Construntor to log All To Standard output
  320. //public TestClass():base(true){}
  321. //Activate this constructor to log Failures to a log file
  322. //public TestClass(System.IO.TextWriter tw):base(tw, false){}
  323. //Activate this constructor to log All to a log file
  324. //public TestClass(System.IO.TextWriter tw):base(tw, true){}
  325. //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
  326. [Test] public void Find_ByObject()
  327. {
  328. int FindResult,ExpectedResult=-1;
  329. //create the source datatable
  330. DataTable dt = DataProvider.CreateParentDataTable();
  331. //create the dataview for the table
  332. DataView dv = new DataView(dt);
  333. for (int i=0; i<dt.Rows.Count ; i++)
  334. {
  335. if ((int)dt.Rows[i]["ParentId"] == 3)
  336. {
  337. ExpectedResult = i;
  338. break;
  339. }
  340. }
  341. // Find ,no sort - exception
  342. try
  343. {
  344. FindResult = dv.Find("3");
  345. Assert.Fail("DV45: Find Failed to throw ArgumentException");
  346. }
  347. catch (ArgumentException) {}
  348. catch (AssertionException exc) {throw exc;}
  349. catch (Exception exc)
  350. {
  351. Assert.Fail("DV46: Find. Wrong exception type. Got:" + exc);
  352. }
  353. dv.Sort = "String1";
  354. // Find = wrong sort, can not find
  355. FindResult = dv.Find("3");
  356. Assert.AreEqual(-1, FindResult , "DV47");
  357. dv.Sort = "ParentId";
  358. // Find
  359. FindResult = dv.Find("3");
  360. Assert.AreEqual(ExpectedResult, FindResult , "DV48");
  361. }
  362. [Test] public void Find_ByArray()
  363. {
  364. int FindResult,ExpectedResult=-1;
  365. //create the source datatable
  366. DataTable dt = DataProvider.CreateParentDataTable();
  367. //create the dataview for the table
  368. DataView dv = new DataView(dt);
  369. for (int i=0; i<dt.Rows.Count ; i++)
  370. {
  371. if ((int)dt.Rows[i]["ParentId"] == 3 && dt.Rows[i]["String1"].ToString() == "3-String1")
  372. {
  373. ExpectedResult = i;
  374. break;
  375. }
  376. }
  377. // Find ,no sort - exception
  378. try
  379. {
  380. FindResult = dv.Find(new object[] {"3","3-String1"});
  381. Assert.Fail("DV49: Find Failed to throw ArgumentException");
  382. }
  383. catch (ArgumentException) {}
  384. catch (AssertionException exc) {throw exc;}
  385. catch (Exception exc)
  386. {
  387. Assert.Fail("DV50: Find. Wrong exception type. Got:" + exc);
  388. }
  389. dv.Sort = "String1,ParentId";
  390. // Find = wrong sort, can not find
  391. FindResult = dv.Find(new object[] {"3","3-String1"});
  392. Assert.AreEqual(-1, FindResult , "DV51");
  393. dv.Sort = "ParentId,String1";
  394. // Find
  395. FindResult = dv.Find(new object[] {"3","3-String1"});
  396. Assert.AreEqual(ExpectedResult, FindResult , "DV52");
  397. }
  398. //Activate This Construntor to log All To Standard output
  399. //public TestClass():base(true){}
  400. //Activate this constructor to log Failures to a log file
  401. //public TestClass(System.IO.TextWriter tw):base(tw, false){}
  402. //Activate this constructor to log All to a log file
  403. //public TestClass(System.IO.TextWriter tw):base(tw, true){}
  404. //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
  405. [Test] public void GetEnumerator()
  406. {
  407. //create the source datatable
  408. DataTable dt = DataProvider.CreateChildDataTable();
  409. //create the dataview for the table
  410. DataView dv = new DataView(dt);
  411. System.Collections.IEnumerator ienm = null;
  412. // GetEnumerator != null
  413. ienm = dv.GetEnumerator();
  414. Assert.AreEqual(true, ienm != null, "DV53");
  415. int i=0;
  416. while (ienm.MoveNext() )
  417. {
  418. // Check item i
  419. Assert.AreEqual(dv[i], (DataRowView)ienm.Current , "DV54");
  420. i++;
  421. }
  422. }
  423. [Test] public void Item()
  424. {
  425. //create the source datatable
  426. DataTable dt = DataProvider.CreateParentDataTable();
  427. //create the dataview for the table
  428. DataView dv = new DataView(dt);
  429. // DataView Item 0
  430. Assert.AreEqual(dv[0].Row, dt.Rows[0] , "DV55");
  431. // DataView Item 4
  432. Assert.AreEqual(dv[4].Row, dt.Rows[4] , "DV56");
  433. dv.RowFilter="ParentId in (1,3,6)";
  434. // DataView Item 0,DataTable with filter
  435. Assert.AreEqual(dv[1].Row, dt.Rows[2] , "DV57");
  436. }
  437. [Test] public void ListChanged()
  438. {
  439. DataTable dt = DataProvider.CreateParentDataTable();
  440. DataView dv = new DataView(dt);
  441. //add event handler
  442. dv.ListChanged +=new System.ComponentModel.ListChangedEventHandler(dv_ListChanged);
  443. // ----- Change Value ---------
  444. evProp = null;
  445. // change value - Event raised
  446. dv[1]["String1"] = "something";
  447. Assert.AreEqual(true , evProp!=null , "DV58");
  448. // change value - ListChangedType
  449. Assert.AreEqual(System.ComponentModel.ListChangedType.ItemChanged, evProp.lstType , "DV59");
  450. // change value - NewIndex
  451. Assert.AreEqual(1, evProp.NewIndex, "DV60");
  452. // change value - OldIndex
  453. Assert.AreEqual(-1, evProp.OldIndex , "DV61");
  454. // ----- Add New ---------
  455. evProp = null;
  456. // Add New - Event raised
  457. dv.AddNew();
  458. Assert.AreEqual(true , evProp!=null , "DV62");
  459. // Add New - ListChangedType
  460. Assert.AreEqual(System.ComponentModel.ListChangedType.ItemAdded , evProp.lstType , "DV63");
  461. // Add New - NewIndex
  462. Assert.AreEqual(6, evProp.NewIndex, "DV64");
  463. // Add New - OldIndex
  464. Assert.AreEqual(-1, evProp.OldIndex , "DV65");
  465. // ----- Sort ---------
  466. evProp = null;
  467. // sort - Event raised
  468. dv.Sort = "ParentId Desc";
  469. Assert.AreEqual(true , evProp!=null , "DV66");
  470. // sort - ListChangedType
  471. Assert.AreEqual(System.ComponentModel.ListChangedType.Reset , evProp.lstType , "DV67");
  472. // sort - NewIndex
  473. Assert.AreEqual(-1, evProp.NewIndex, "DV68");
  474. // sort - OldIndex
  475. Assert.AreEqual(-1, evProp.OldIndex , "DV69");
  476. //ListChangedType - this was not checked
  477. //Move
  478. //PropertyDescriptorAdded - A PropertyDescriptor was added, which changed the schema.
  479. //PropertyDescriptorChanged - A PropertyDescriptor was changed, which changed the schema.
  480. //PropertyDescriptorDeleted
  481. }
  482. [Test]
  483. public void AcceptChanges ()
  484. {
  485. evProp = null;
  486. DataTable dt = new DataTable ();
  487. IBindingList list = dt.DefaultView;
  488. list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
  489. dt.Columns.Add ("test", typeof (int));
  490. dt.Rows.Add (new object[] { 10 });
  491. dt.Rows.Add (new object[] { 20 });
  492. // ListChangedType.Reset
  493. dt.AcceptChanges ();
  494. Assert.AreEqual(true , evProp != null , "DV166");
  495. // AcceptChanges - should emit ListChangedType.Reset
  496. Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV167");
  497. }
  498. [Test]
  499. public void ClearTable ()
  500. {
  501. evProp = null;
  502. DataTable dt = new DataTable ();
  503. IBindingList list = dt.DefaultView;
  504. list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
  505. dt.Columns.Add ("test", typeof (int));
  506. dt.Rows.Add (new object[] { 10 });
  507. dt.Rows.Add (new object[] { 20 });
  508. // Clears DataTable
  509. dt.Clear ();
  510. Assert.AreEqual(true , evProp != null , "DV168");
  511. // Clear DataTable - should emit ListChangedType.Reset
  512. Assert.AreEqual(System.ComponentModel.ListChangedType.Reset , evProp.lstType , "DV169");
  513. // Clear DataTable - should clear view count
  514. Assert.AreEqual(0, dt.DefaultView.Count , "DV169");
  515. }
  516. private void dv_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
  517. {
  518. evProp = new EventProperties();
  519. evProp.lstType = e.ListChangedType;
  520. evProp.NewIndex = e.NewIndex;
  521. evProp.OldIndex = e.OldIndex;
  522. }
  523. [Test] public void RowFilter()
  524. {
  525. //note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
  526. // this test also check DataView.Count property
  527. DataRowView[] drvResult = null;
  528. System.Collections.ArrayList al = new System.Collections.ArrayList();
  529. //create the source datatable
  530. DataTable dt = DataProvider.CreateChildDataTable();
  531. //create the dataview for the table
  532. DataView dv = new DataView(dt);
  533. //-------------------------------------------------------------
  534. //Get excpected result
  535. al.Clear();
  536. foreach (DataRow dr in dt.Rows )
  537. {
  538. if ((int)dr["ChildId"] == 1)
  539. {
  540. al.Add(dr);
  541. }
  542. }
  543. // RowFilter = 'ChildId=1', check count
  544. dv.RowFilter = "ChildId=1";
  545. Assert.AreEqual(al.Count , dv.Count , "DV70");
  546. // RowFilter = 'ChildId=1', check rows
  547. drvResult = new DataRowView[dv.Count];
  548. dv.CopyTo(drvResult,0);
  549. //check that the filterd rows exists
  550. bool Succeed = true;
  551. for (int i=0; i<drvResult.Length ; i++)
  552. {
  553. Succeed = al.Contains(drvResult[i].Row);
  554. if (!Succeed) break;
  555. }
  556. Assert.AreEqual(true, Succeed , "DV71");
  557. //-------------------------------------------------------------
  558. //-------------------------------------------------------------
  559. //Get excpected result
  560. al.Clear();
  561. foreach (DataRow dr in dt.Rows )
  562. if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" )
  563. al.Add(dr);
  564. // RowFilter - ChildId=1 and String1='1-String1'
  565. dv.RowFilter = "ChildId=1 and String1='1-String1'";
  566. Assert.AreEqual(al.Count , dv.Count , "DV72");
  567. // RowFilter = ChildId=1 and String1='1-String1', check rows
  568. drvResult = new DataRowView[dv.Count];
  569. dv.CopyTo(drvResult,0);
  570. //check that the filterd rows exists
  571. Succeed = true;
  572. for (int i=0; i<drvResult.Length ; i++)
  573. {
  574. Succeed = al.Contains(drvResult[i].Row);
  575. if (!Succeed) break;
  576. }
  577. Assert.AreEqual(true, Succeed , "DV73");
  578. //-------------------------------------------------------------
  579. //EvaluateException
  580. // RowFilter - check EvaluateException
  581. try
  582. {
  583. dv.RowFilter = "Col=1";
  584. Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
  585. }
  586. catch (EvaluateException) {}
  587. catch (AssertionException exc) {throw exc;}
  588. catch (Exception exc)
  589. {
  590. Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
  591. }
  592. //SyntaxErrorException 1
  593. // RowFilter - check SyntaxErrorException 1
  594. try
  595. {
  596. dv.RowFilter = "sum('something')";
  597. Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
  598. }
  599. catch (SyntaxErrorException) {}
  600. catch (AssertionException exc) {throw exc;}
  601. catch (Exception exc)
  602. {
  603. Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
  604. }
  605. //SyntaxErrorException 2
  606. // RowFilter - check SyntaxErrorException 2
  607. try
  608. {
  609. dv.RowFilter = "HH**!";
  610. Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
  611. }
  612. catch (SyntaxErrorException) {}
  613. catch (AssertionException exc) {throw exc;}
  614. catch (Exception exc)
  615. {
  616. Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
  617. }
  618. }
  619. [Test] public void RowStateFilter()
  620. {
  621. /*
  622. Added A new row. 4
  623. CurrentRows Current rows including unchanged, new, and modified rows. 22
  624. Deleted A deleted row. 8
  625. ModifiedCurrent A current version, which is a modified version of original data (see ModifiedOriginal). 16
  626. ModifiedOriginal The original version (although it has since been modified and is available as ModifiedCurrent). 32
  627. None None. 0
  628. OriginalRows Original rows including unchanged and deleted rows. 42
  629. Unchanged An unchanged row. 2
  630. */
  631. //DataRowView[] drvResult = null;
  632. System.Collections.ArrayList al = new System.Collections.ArrayList();
  633. DataTable dt = DataProvider.CreateParentDataTable();
  634. //create the dataview for the table
  635. DataView dv = new DataView(dt);
  636. DataRow[] drResult;
  637. dt.Rows[0].Delete();
  638. dt.Rows[1]["ParentId"] = 1;
  639. dt.Rows[2]["ParentId"] = 1;
  640. dt.Rows[3].Delete();
  641. dt.Rows.Add(new object[] {1,"A","B"});
  642. dt.Rows.Add(new object[] {1,"C","D"});
  643. dt.Rows.Add(new object[] {1,"E","F"});
  644. //---------- Added --------
  645. dv.RowStateFilter = DataViewRowState.Added ;
  646. drResult = GetResultRows(dt,DataRowState.Added);
  647. // Added
  648. Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV80");
  649. //---------- CurrentRows --------
  650. dv.RowStateFilter = DataViewRowState.CurrentRows ;
  651. drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified );
  652. // CurrentRows
  653. Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV81");
  654. //---------- ModifiedCurrent --------
  655. dv.RowStateFilter = DataViewRowState.ModifiedCurrent ;
  656. drResult = GetResultRows(dt,DataRowState.Modified );
  657. // ModifiedCurrent
  658. Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV82");
  659. //---------- ModifiedOriginal --------
  660. dv.RowStateFilter = DataViewRowState.ModifiedOriginal ;
  661. drResult = GetResultRows(dt,DataRowState.Modified );
  662. // ModifiedOriginal
  663. Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV83");
  664. //---------- Deleted --------
  665. dv.RowStateFilter = DataViewRowState.Deleted ;
  666. drResult = GetResultRows(dt,DataRowState.Deleted );
  667. // Deleted
  668. Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV84");
  669. /*
  670. //---------- OriginalRows --------
  671. dv.RowStateFilter = DataViewRowState.OriginalRows ;
  672. drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Deleted );
  673. // OriginalRows
  674. Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV85");
  675. */
  676. }
  677. private DataRow[] GetResultRows(DataTable dt,DataRowState State)
  678. {
  679. //get expected rows
  680. System.Collections.ArrayList al = new System.Collections.ArrayList();
  681. DataRowVersion drVer = DataRowVersion.Current;
  682. //From MSDN - The row the default version for the current DataRowState.
  683. // For a DataRowState value of Added, Modified or Current,
  684. // the default version is Current.
  685. // For a DataRowState of Deleted, the version is Original.
  686. // For a DataRowState value of Detached, the version is Proposed.
  687. if ( ((State & DataRowState.Added) > 0)
  688. | ((State & DataRowState.Modified) > 0)
  689. | ((State & DataRowState.Unchanged) > 0) )
  690. drVer = DataRowVersion.Current;
  691. if ( (State & DataRowState.Deleted) > 0
  692. | (State & DataRowState.Detached) > 0 )
  693. drVer = DataRowVersion.Original;
  694. foreach (DataRow dr in dt.Rows )
  695. {
  696. if ( dr.HasVersion(drVer)
  697. //&& ((int)dr["ParentId", drVer] == 1)
  698. && ((dr.RowState & State) > 0 )
  699. )
  700. al.Add(dr);
  701. }
  702. DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
  703. return result;
  704. }
  705. private bool CompareSortedRowsByParentId(DataView dv, DataRow[] drTable)
  706. {
  707. if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
  708. //comparing the rows by using columns ParentId and ChildId
  709. if ((dv.RowStateFilter & DataViewRowState.Deleted) > 0)
  710. {
  711. for (int i=0; i<dv.Count ; i++)
  712. {
  713. if (dv[i].Row["ParentId",DataRowVersion.Original ].ToString() != drTable[i]["ParentId",DataRowVersion.Original].ToString())
  714. return false;
  715. }
  716. }
  717. else
  718. {
  719. for (int i=0; i<dv.Count ; i++)
  720. {
  721. if (dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString())
  722. return false;
  723. }
  724. }
  725. return true;
  726. }
  727. [Test] public void Sort()
  728. {
  729. DataRow[] drArrTable;
  730. //create the source datatable
  731. DataTable dt = DataProvider.CreateChildDataTable();
  732. //create the dataview for the table
  733. DataView dv = new DataView(dt);
  734. dv.Sort = "ParentId";
  735. drArrTable = dt.Select("","ParentId");
  736. // sort = ParentId
  737. Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV86");
  738. dv.Sort = "ChildId";
  739. drArrTable = dt.Select("","ChildId");
  740. // sort = ChildId
  741. Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV87");
  742. dv.Sort = "ParentId Desc, ChildId";
  743. drArrTable = dt.Select("","ParentId Desc, ChildId");
  744. // sort = ParentId Desc, ChildId
  745. Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV88");
  746. dv.Sort = "ChildId Asc, ParentId";
  747. drArrTable = dt.Select("","ChildId Asc, ParentId");
  748. // sort = ChildId Asc, ParentId
  749. Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV89");
  750. dv.Sort = "ChildId Asc, ChildId Desc";
  751. drArrTable = dt.Select("","ChildId Asc, ChildId Desc");
  752. // sort = ChildId Asc, ChildId Desc
  753. Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV90");
  754. // IndexOutOfRangeException - 1
  755. try
  756. {
  757. dv.Sort = "something";
  758. Assert.Fail("DV91: Sort Failed to throw IndexOutOfRangeException");
  759. }
  760. catch (IndexOutOfRangeException) {}
  761. catch (AssertionException exc) {throw exc;}
  762. catch (Exception exc)
  763. {
  764. Assert.Fail("DV92: Sort. Wrong exception type. Got:" + exc);
  765. }
  766. // IndexOutOfRangeException - 2
  767. try
  768. {
  769. dv.Sort = "ColumnId Desc Asc";
  770. Assert.Fail("DV93: Sort Failed to throw IndexOutOfRangeException");
  771. }
  772. catch (IndexOutOfRangeException) {}
  773. catch (AssertionException exc) {throw exc;}
  774. catch (Exception exc)
  775. {
  776. Assert.Fail("DV94: Sort. Wrong exception type. Got:" + exc);
  777. }
  778. // IndexOutOfRangeException - 3
  779. try
  780. {
  781. dv.Sort = "ColumnId blabla";
  782. Assert.Fail("DV95: Sort Failed to throw IndexOutOfRangeException");
  783. }
  784. catch (IndexOutOfRangeException) {}
  785. catch (AssertionException exc) {throw exc;}
  786. catch (Exception exc)
  787. {
  788. Assert.Fail("DV96: Sort. Wrong exception type. Got:" + exc);
  789. }
  790. }
  791. private bool CompareSortedRowsByParentAndChildId(DataView dv, DataRow[] drTable)
  792. {
  793. if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
  794. //comparing the rows by using columns ParentId and ChildId
  795. for (int i=0; i<dv.Count ; i++)
  796. {
  797. if ( dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString()
  798. &&
  799. dv[i].Row["ChildId"].ToString() != drTable[i]["ChildId"].ToString())
  800. return false;
  801. }
  802. return true;
  803. }
  804. [Test] public void Table()
  805. {
  806. DataTable dt = new DataTable();
  807. DataView dv = new DataView();
  808. // DataTable=null
  809. Assert.AreEqual(null , dv.Table , "DV97");
  810. // DataException - bind to table with no name
  811. try
  812. {
  813. dv.Table = dt;
  814. Assert.Fail("DV98: Table Failed to throw DataException");
  815. }
  816. catch (DataException) {}
  817. catch (AssertionException exc) {throw exc;}
  818. catch (Exception exc)
  819. {
  820. Assert.Fail("DV99: Table. Wrong exception type. Got:" + exc);
  821. }
  822. dt.TableName = "myTable";
  823. // DataTable!=null
  824. dv.Table = dt;
  825. Assert.AreEqual(dt, dv.Table , "DV100");
  826. // assign null to DataTable
  827. dv.Table = null;
  828. Assert.AreEqual(null, dv.Table , "DV101");
  829. }
  830. [Test] public void ctor_Empty()
  831. {
  832. DataView dv;
  833. dv = new DataView();
  834. // ctor
  835. Assert.AreEqual(false, dv == null, "DV102");
  836. }
  837. [Test] public void ctor_DataTable()
  838. {
  839. DataView dv = null;
  840. DataTable dt = new DataTable("myTable");
  841. // ctor
  842. dv = new DataView(dt);
  843. Assert.AreEqual(false, dv == null, "DV103");
  844. // ctor - table
  845. Assert.AreEqual(dt , dv.Table , "DV104");
  846. }
  847. [Test] public void ctor_ExpectedExceptions()
  848. {
  849. DataView dv = null;
  850. DataTable dt = new DataTable("myTable");
  851. // ctor - missing column CutomerID Exception
  852. try
  853. {
  854. //exception: System.Data.EvaluateException: Cannot find column [CustomerId]
  855. dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
  856. Assert.Fail("DV105: DataView ctor Failed to throw EvaluateException or IndexOutOfRangeException");
  857. }
  858. catch (EvaluateException) {}
  859. catch (IndexOutOfRangeException) {}
  860. catch (AssertionException exc) {throw exc;}
  861. catch (Exception exc)
  862. {
  863. Assert.Fail("DV106: DataView ctor. Wrong exception type. Got:" + exc);
  864. }
  865. dt.Columns.Add(new DataColumn("CustomerId"));
  866. // ctor - missing column Age Exception
  867. try
  868. {
  869. //exception: System.Data.EvaluateException: Cannot find column [Age]
  870. dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
  871. Assert.Fail("DV107: DataView ctor Failed to throw IndexOutOfRangeException");
  872. }
  873. catch (IndexOutOfRangeException) {}
  874. catch (AssertionException exc) {throw exc;}
  875. catch (Exception exc)
  876. {
  877. Assert.Fail("DV108: DataView ctor. Wrong exception type. Got:" + exc);
  878. }
  879. }
  880. [Test] public void ctor_Complex()
  881. {
  882. DataView dv = null;
  883. DataTable dt = new DataTable("myTable");
  884. dt.Columns.Add(new DataColumn("CustomerId"));
  885. dt.Columns.Add(new DataColumn("Age"));
  886. // ctor
  887. dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
  888. Assert.AreEqual(false , dv == null , "DV109");
  889. // ctor - table
  890. Assert.AreEqual(dt , dv.Table , "DV110");
  891. // ctor - RowFilter
  892. Assert.AreEqual("CustomerId > 100" , dv.RowFilter , "DV111");
  893. // ctor - Sort
  894. Assert.AreEqual("Age" , dv.Sort, "DV112");
  895. // ctor - RowStateFilter
  896. Assert.AreEqual(DataViewRowState.Added , dv.RowStateFilter , "DV113");
  897. }
  898. [Test]
  899. public void DataViewManager()
  900. {
  901. DataView dv = null;
  902. DataViewManager dvm = null;
  903. DataSet ds = new DataSet();
  904. DataTable dt = new DataTable("myTable");
  905. ds.Tables.Add(dt);
  906. dv = dt.DefaultView;
  907. // public DataViewManager DataViewManager {get;} - The DataViewManager that created this view.
  908. // If this is the default DataView for a DataTable, the DataViewManager property returns the default DataViewManager for the DataSet.
  909. // Otherwise, if the DataView was created without a DataViewManager, this property is a null reference (Nothing in Visual Basic).
  910. dvm = dv.DataViewManager;
  911. Assert.AreEqual(ds.DefaultViewManager,dvm, "DV114");
  912. dv = new DataView(dt);
  913. dvm = dv.DataViewManager;
  914. Assert.AreEqual(null,dvm,"DV115");
  915. dv = ds.DefaultViewManager.CreateDataView(dt);
  916. Assert.AreEqual(ds.DefaultViewManager,dv.DataViewManager , "DV116");
  917. }
  918. [Test]
  919. public void DataView_ListChangedEventTest ()
  920. {
  921. // Test DataView generates events, when datatable is directly modified
  922. DataTable table = new DataTable ("test");
  923. table.Columns.Add ("col1", typeof(int));
  924. DataView view = new DataView (table);
  925. view.ListChanged += new ListChangedEventHandler (dv_ListChanged);
  926. evProp = null;
  927. table.Rows.Add (new object[] {1});
  928. Assert.AreEqual (0, evProp.NewIndex, "#1");
  929. Assert.AreEqual (-1, evProp.OldIndex, "#2");
  930. Assert.AreEqual (ListChangedType.ItemAdded, evProp.lstType, "#3");
  931. evProp = null;
  932. table.Rows[0][0] = 5;
  933. Assert.AreEqual (0, evProp.NewIndex, "#4");
  934. Assert.AreEqual (-1, evProp.OldIndex, "#5");
  935. Assert.AreEqual (ListChangedType.ItemChanged, evProp.lstType, "#6");
  936. evProp = null;
  937. table.Rows.RemoveAt (0);
  938. Assert.AreEqual (0, evProp.NewIndex, "#7");
  939. Assert.AreEqual (-1, evProp.OldIndex, "#8");
  940. Assert.AreEqual (ListChangedType.ItemDeleted, evProp.lstType, "#9");
  941. table.Rows.Clear();
  942. Assert.AreEqual (-1, evProp.NewIndex, "#10");
  943. Assert.AreEqual (-1, evProp.OldIndex, "#11");
  944. Assert.AreEqual (ListChangedType.Reset, evProp.lstType, "#12");
  945. }
  946. [Test]
  947. public void TestDefaultValues()
  948. {
  949. DataView view = new DataView();
  950. Assert.IsFalse(view.ApplyDefaultSort, "#1");
  951. Assert.AreEqual ("", view.Sort, "#2");
  952. Assert.AreEqual("", view.RowFilter, "#3");
  953. Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
  954. Assert.IsTrue(view.AllowDelete, "#5");
  955. Assert.IsTrue(view.AllowEdit, "#6");
  956. Assert.IsTrue(view.AllowNew, "#7");
  957. }
  958. [Test]
  959. public void TestTableProperty()
  960. {
  961. DataTable table = new DataTable("table");
  962. DataView view = new DataView();
  963. view.Table = table;
  964. Assert.AreEqual("", view.Sort, "#1");
  965. Assert.AreEqual("", view.RowFilter, "#2");
  966. Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
  967. }
  968. #if NET_2_0
  969. [Test]
  970. public void TestEquals_SameTableDiffViewProp()
  971. {
  972. DataTable table = new DataTable("table");
  973. table.Columns.Add("col1", typeof(int));
  974. table.Columns.Add("col2", typeof(int));
  975. for (int i = 0; i < 5; ++i)
  976. table.Rows.Add(new object[] { i, 100 + i });
  977. DataView view1 = new DataView(table);
  978. DataView view2 = new DataView(table);
  979. object obj2 = (object)view2;
  980. Assert.IsFalse(view1.Equals(obj2), "#1");
  981. Assert.IsTrue(view1.Equals(view1), "#2");
  982. Assert.IsTrue(view2.Equals(view1), "#3");
  983. view1.Sort = "col1 ASC";
  984. Assert.IsFalse(view1.Equals(view2), "#4");
  985. view2.Sort = "col1 ASC";
  986. Assert.IsTrue(view1.Equals(view2), "#5");
  987. view1.RowFilter = "col1 > 100";
  988. Assert.IsFalse(view1.Equals(view2), "#6");
  989. view1.RowFilter = "";
  990. Assert.IsTrue(view1.Equals(view2), "#7");
  991. view1.RowStateFilter = DataViewRowState.Added;
  992. Assert.IsFalse(view1.Equals(view2), "#8");
  993. view1.RowStateFilter = DataViewRowState.CurrentRows;
  994. Assert.IsTrue(view1.Equals(view2), "#9");
  995. view1.AllowDelete = !view2.AllowDelete;
  996. Assert.IsFalse(view1.Equals(view2), "#10");
  997. view1.AllowDelete = view2.AllowDelete;
  998. Assert.IsTrue(view1.Equals(view2), "#11");
  999. view1.AllowEdit = !view2.AllowEdit;
  1000. Assert.IsFalse(view1.Equals(view2), "#12");
  1001. view1.AllowEdit = view2.AllowEdit;
  1002. Assert.IsTrue(view1.Equals(view2), "#13");
  1003. view1.AllowNew = !view2.AllowNew;
  1004. Assert.IsFalse(view1.Equals(view2), "#14");
  1005. view1.AllowNew = view2.AllowNew;
  1006. Assert.IsTrue(view1.Equals(view2), "#15");
  1007. //ApplyDefaultSort doesnet affect the comparision
  1008. view1.ApplyDefaultSort = !view2.ApplyDefaultSort;
  1009. Assert.IsTrue(view1.Equals(view2), "#16");
  1010. DataTable table2 = table.Copy();
  1011. view1.Table = table2;
  1012. Assert.IsFalse(view1.Equals(view2), "#17");
  1013. view1.Table = table;
  1014. //well.. sort is set to null when Table is assigned..
  1015. view1.Sort = view2.Sort;
  1016. Assert.IsTrue(view1.Equals(view2), "#18");
  1017. }
  1018. [Test]
  1019. public void ToTable_SimpleTest()
  1020. {
  1021. DataSet ds = new DataSet();
  1022. ds.Tables.Add("table");
  1023. ds.Tables[0].Columns.Add("col1", typeof(int));
  1024. ds.Tables[0].Columns.Add("col2", typeof(int), "sum(col1)");
  1025. ds.Tables[0].Columns.Add("col3", typeof(int));
  1026. ds.Tables[0].Columns[2].AutoIncrement = true;
  1027. ds.Tables[0].Rows.Add(new object[] { 1 });
  1028. ds.Tables[0].Rows.Add(new object[] { 2 });
  1029. ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns[0] };
  1030. DataView view = new DataView(ds.Tables[0]);
  1031. DataTable table = view.ToTable();
  1032. // The rule seems to be : Copy any col property that doesent
  1033. // involve/depend on other columns..
  1034. // Constraints and PrimaryKey info not copied over
  1035. Assert.AreEqual(0, table.PrimaryKey.Length, "#1");
  1036. Assert.AreEqual(0, table.Constraints.Count, "#2");
  1037. // AllowDBNull state is maintained by ms.net
  1038. Assert.IsFalse(table.Columns[0].AllowDBNull, "#3");
  1039. Assert.IsTrue(table.Columns[2].AllowDBNull, "#4");
  1040. // Expression is not copied over by ms.net
  1041. Assert.AreEqual("", table.Columns[1].Expression, "#5");
  1042. // AutoIncrement state is maintained by ms.net
  1043. Assert.IsTrue(table.Columns[2].AutoIncrement, "#6");
  1044. Assert.IsFalse (ds.Tables[0] == table, "#7");
  1045. Assert.AreEqual(ds.Tables [0].TableName, table.TableName, "#8");
  1046. Assert.AreEqual(ds.Tables [0].Columns.Count, table.Columns.Count, "#9 Col Count");
  1047. Assert.AreEqual(ds.Tables [0].Rows.Count, table.Rows.Count, "#10");
  1048. for (int i = 0; i < table.Columns.Count; ++i) {
  1049. Assert.AreEqual(ds.Tables [0].Columns[i].ColumnName, table.Columns[i].ColumnName, "10_"+i);
  1050. Assert.AreEqual(ds.Tables [0].Columns[i].DataType, table.Columns[i].DataType, "11_"+i);
  1051. for (int j = 0; j < table.Rows.Count; ++j)
  1052. Assert.AreEqual(ds.Tables [0].Rows[j][i], table.Rows[j][i], "#12_"+i+"_"+j);
  1053. }
  1054. DataTable table1 = view.ToTable("newtable");
  1055. Assert.AreEqual("newtable", table1.TableName, "#13");
  1056. }
  1057. [Test]
  1058. public void ToTableTest_DataValidity ()
  1059. {
  1060. DataTable table = new DataTable();
  1061. table.Columns.Add("col1", typeof(int));
  1062. table.Columns.Add("col2", typeof(int));
  1063. table.Columns.Add("col3", typeof(int));
  1064. for (int i = 0; i < 5; ++i) {
  1065. table.Rows.Add(new object[] { i, i + 1, i + 2 });
  1066. table.Rows.Add(new object[] { i, i + 1, i + 2 });
  1067. }
  1068. table.AcceptChanges();
  1069. DataView view = new DataView(table);
  1070. try {
  1071. DataTable newTable = view.ToTable (false, null);
  1072. } catch (ArgumentNullException e) {
  1073. Assert.AreEqual ("'columnNames' argument cannot be null." + Environment.NewLine +
  1074. "Parameter name: columnNames", e.Message, "#1");
  1075. }
  1076. DataTable newTable1 = view.ToTable(false, new string[] { });
  1077. Assert.AreEqual(10, newTable1.Rows.Count, "#3");
  1078. newTable1 = view.ToTable(true, new string[] {});
  1079. Assert.AreEqual(3, newTable1.Columns.Count, "#4");
  1080. Assert.AreEqual(5, newTable1.Rows.Count, "#5");
  1081. table.Rows.Add(new object[] { 1, 100, 100 });
  1082. newTable1 = view.ToTable(true, new string[] {});
  1083. Assert.AreEqual(3, newTable1.Columns.Count, "#6");
  1084. Assert.AreEqual(6, newTable1.Rows.Count, "#7");
  1085. newTable1 = view.ToTable(true, new string[] {"col1"});
  1086. Assert.AreEqual(1, newTable1.Columns.Count, "#8");
  1087. Assert.AreEqual(5, newTable1.Rows.Count, "#9");
  1088. newTable1 = view.ToTable(true, new string[] { "col2", "col3"});
  1089. Assert.AreEqual(2, newTable1.Columns.Count, "#10");
  1090. Assert.AreEqual(6, newTable1.Rows.Count, "#11");
  1091. for (int i = 0; i < newTable1.Rows.Count; ++i)
  1092. Assert.AreEqual(DataRowState.Added, newTable1.Rows[i].RowState, "#11");
  1093. view = new DataView (table, "col1>1", "col1 asc, col2 desc", DataViewRowState.Added);
  1094. Assert.AreEqual (0, view.Count, "#12");
  1095. newTable1 = view.ToTable (false, new string[] {"col1", "col3"});
  1096. Assert.AreEqual (0, newTable1.Rows.Count, "#13");
  1097. table.Rows.Add (new object[] {10, 1, 1});
  1098. table.Rows.Add (new object[] {10, 1, 3});
  1099. table.Rows.Add (new object[] {10, 1, 2});
  1100. Assert.AreEqual (3, view.Count, "#14");
  1101. view.Sort = "col1 asc, col2 asc, col3 desc";
  1102. newTable1 = view.ToTable (true, new string[] {"col1", "col3"});
  1103. Assert.AreEqual (3, newTable1.Rows.Count, "#14");
  1104. Assert.AreEqual (3, newTable1.Rows [0][1], "#15");
  1105. Assert.AreEqual (2, newTable1.Rows [1][1], "#16");
  1106. Assert.AreEqual (1, newTable1.Rows [2][1], "#17");
  1107. }
  1108. #endif
  1109. }
  1110. }