2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using NUnit.Framework;
31 using System.Collections;
32 using System.ComponentModel;
35 using MonoTests.System.Data.Utils;
37 namespace MonoTests.System.Data
39 [TestFixture] public class DataViewTest2
41 private EventProperties evProp = null;
43 class EventProperties //hold the event properties to be checked
45 public ListChangedType lstType ;
50 [Test] public void AddNew()
52 //create the source datatable
53 DataTable dt = DataProvider.CreateChildDataTable();
55 //create the dataview for the table
56 DataView dv = new DataView(dt);
58 int CountView = dv.Count ;
59 int CountTable= dt.Rows.Count ;
61 DataRowView drv = null;
63 // AddNew - DataView Row Count
65 Assert.AreEqual(dv.Count , CountView+1, "DV1");
67 // AddNew - Table Row Count
68 Assert.AreEqual(dt.Rows.Count , CountTable, "DV2");
70 // AddNew - new row in DataTable
72 Assert.AreEqual(dt.Rows.Count , CountTable+1, "DV3");
74 // AddNew - new row != null
75 Assert.AreEqual(true, drv!=null, "DV4");
77 // AddNew - check table
78 Assert.AreEqual(dt, drv.Row.Table, "DV5");
81 [Test] public void AllowDelete()
83 DataTable dt = DataProvider.CreateParentDataTable();
84 DataView dv = new DataView(dt);
86 // AllowDelete - default value
87 Assert.AreEqual(true , dv.AllowDelete , "DV6");
90 dv.AllowDelete = true;
91 Assert.AreEqual(true, dv.AllowDelete , "DV7");
93 // AllowDelete - false
94 dv.AllowDelete = false;
95 Assert.AreEqual(false, dv.AllowDelete , "DV8");
97 dv.AllowDelete = false;
98 // AllowDelete false- Exception
102 Assert.Fail("DV9: Delete Failed to throw DataException");
104 catch (DataException) {}
105 catch (AssertionException exc) {throw exc;}
106 catch (Exception exc)
108 Assert.Fail("DV10: Delete. Wrong exception type. Got:" + exc);
111 dv.AllowDelete = true;
112 int RowsCount = dv.Count ;
113 // AllowDelete true- Exception
115 Assert.AreEqual(RowsCount-1, dv.Count , "DV11");
118 [Test] public void AllowEdit()
120 DataTable dt = DataProvider.CreateParentDataTable();
121 DataView dv = new DataView(dt);
123 // AllowEdit - default value
124 Assert.AreEqual(true , dv.AllowEdit , "DV12");
128 Assert.AreEqual(true, dv.AllowEdit , "DV13");
131 dv.AllowEdit = false;
132 Assert.AreEqual(false, dv.AllowEdit , "DV14");
136 // AllowEdit false - exception
140 Assert.Fail("DV15: Indexer Failed to throw DataException");
142 catch (DataException) {}
143 catch (AssertionException exc) {throw exc;}
144 catch (Exception exc)
146 Assert.Fail("DV16: Indexer. Wrong exception type. Got:" + exc);
151 // AllowEdit true- exception
153 Assert.AreEqual("aaa", dv[0][2] , "DV17");
156 [Test] public void AllowNew()
158 DataTable dt = DataProvider.CreateParentDataTable();
159 DataView dv = new DataView(dt);
161 // AllowNew - default value
162 Assert.AreEqual(true , dv.AllowNew , "DV18");
166 Assert.AreEqual(true, dv.AllowNew , "DV19");
170 Assert.AreEqual(false, dv.AllowNew , "DV20");
172 // AllowNew - exception
176 Assert.Fail("DV21: AddNew Failed to throw DataException");
178 catch (DataException) {}
179 catch (AssertionException exc) {throw exc;}
180 catch (Exception exc)
182 Assert.Fail("DV22: AddNew. Wrong exception type. Got:" + exc);
186 int RowsCount = dv.Count ;
188 // AllowNew - exception
190 Assert.AreEqual(RowsCount+1, dv.Count , "DV23");
193 [Test] public void ApplyDefaultSort()
195 DataTable dt = DataProvider.CreateParentDataTable();
196 DataView dv = new DataView(dt);
198 // ApplyDefaultSort - default value
199 Assert.AreEqual(false , dv.ApplyDefaultSort , "DV24");
201 // ApplyDefaultSort - true
202 dv.ApplyDefaultSort = true;
203 Assert.AreEqual(true, dv.ApplyDefaultSort , "DV25");
205 // ApplyDefaultSort - false
206 dv.ApplyDefaultSort = false;
207 Assert.AreEqual(false, dv.ApplyDefaultSort , "DV26");
210 [Test] public void CopyTo()
212 //create the source datatable
213 DataTable dt = DataProvider.CreateChildDataTable();
215 //create the dataview for the table
216 DataView dv = new DataView(dt);
218 DataRowView[] drvExpected = null;
219 DataRowView[] drvResult = null;
221 // ------- Copy from Index=0
222 drvExpected = new DataRowView[dv.Count];
223 for (int i=0; i < dv.Count ;i++)
225 drvExpected[i] = dv[i];
228 drvResult = new DataRowView[dv.Count];
229 // CopyTo from index 0
230 dv.CopyTo(drvResult,0);
231 Assert.AreEqual(drvResult, drvExpected , "DV27");
233 // ------- Copy from Index=3
234 drvExpected = new DataRowView[dv.Count+3];
235 for (int i=0; i < dv.Count ;i++)
237 drvExpected[i+3] = dv[i];
240 drvResult = new DataRowView[dv.Count+3];
241 // CopyTo from index 3
242 dv.CopyTo(drvResult,3);
243 Assert.AreEqual(drvResult , drvExpected , "DV28");
245 // ------- Copy from Index=3,larger array
246 drvExpected = new DataRowView[dv.Count+9];
247 for (int i=0; i < dv.Count ;i++)
249 drvExpected[i+3] = dv[i];
252 drvResult = new DataRowView[dv.Count+9];
253 // CopyTo from index 3,larger array
254 dv.CopyTo(drvResult,3);
255 Assert.AreEqual(drvResult, drvExpected , "DV29");
257 // ------- CopyTo smaller array, check exception
258 drvResult = new DataRowView[dv.Count-1];
260 // CopyTo smaller array, check exception
263 dv.CopyTo(drvResult,0);
264 Assert.Fail("DV30: CopyTo Failed to throw IndexOutOfRangeException");
266 catch (IndexOutOfRangeException) {}
267 catch (AssertionException exc) {throw exc;}
268 catch (Exception exc)
270 Assert.Fail("DV31: CopyTo. Wrong exception type. Got:" + exc);
274 [Test] public void Delete()
276 //create the source datatable
277 DataTable dt = DataProvider.CreateChildDataTable();
279 //create the dataview for the table
280 DataView dv = new DataView(dt);
282 int CountView = dv.Count ;
283 int CountTable= dt.Rows.Count ;
285 DataRowView drv = dv[0];
287 // Delete - DataView Row Count
289 Assert.AreEqual(dv.Count , CountView-1, "DV32");
291 // Delete - Table Row Count
292 Assert.AreEqual(dt.Rows.Count , CountTable, "DV33");
294 // Delete - check table
295 Assert.AreEqual(dt, drv.Row.Table, "DV34");
298 [Test] public void FindRows_ByKey()
300 DataRowView[] dvArr = null;
302 //create the source datatable
303 DataTable dt = DataProvider.CreateChildDataTable();
305 //create the dataview for the table
306 DataView dv = new DataView(dt);
308 // FindRows ,no sort - exception
311 dvArr = dv.FindRows(3);
312 Assert.Fail("DV35: FindRows Failed to throw ArgumentException");
314 catch (ArgumentException) {}
315 catch (AssertionException exc) {throw exc;}
316 catch (Exception exc)
318 Assert.Fail("DV36: FindRows. Wrong exception type. Got:" + exc);
322 // Find = wrong sort, can not find
323 dvArr = dv.FindRows(3);
324 Assert.AreEqual(0, dvArr.Length , "DV37");
328 //get expected results
329 DataRow[] drExpected = dt.Select("ChildId=3");
331 // FindRows - check count
332 dvArr = dv.FindRows(3);
333 Assert.AreEqual(drExpected.Length , dvArr.Length, "DV38");
335 // FindRows - check data
337 //check that result is ok
339 for (int i=0; i<dvArr.Length ; i++)
341 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
344 Assert.AreEqual(true, Succeed , "DV39");
347 [Test] public void FindRows_ByKeys()
349 DataRowView[] dvArr = null;
351 //create the source datatable
352 DataTable dt = DataProvider.CreateChildDataTable();
354 //create the dataview for the table
355 DataView dv = new DataView(dt);
357 // FindRows ,no sort - exception
360 dvArr = dv.FindRows(new object[] {"3","3-String1"});
361 Assert.Fail("DV40: FindRows Failed to throw ArgumentException");
363 catch (ArgumentException) {}
364 catch (AssertionException exc) {throw exc;}
365 catch (Exception exc)
367 Assert.Fail("DV41: FindRows. Wrong exception type. Got:" + exc);
370 dv.Sort = "String1,ChildId";
371 // Find = wrong sort, can not find
372 dvArr = dv.FindRows(new object[] {"3","3-String1"});
373 Assert.AreEqual(0, dvArr.Length , "DV42");
375 dv.Sort = "ChildId,String1";
377 //get expected results
378 DataRow[] drExpected = dt.Select("ChildId=3 and String1='3-String1'");
380 // FindRows - check count
381 dvArr = dv.FindRows(new object[] {"3","3-String1"});
382 Assert.AreEqual(drExpected.Length , dvArr.Length, "DV43");
384 // FindRows - check data
386 //check that result is ok
388 for (int i=0; i<dvArr.Length ; i++)
390 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
393 Assert.AreEqual(true, Succeed , "DV44");
396 //Activate This Construntor to log All To Standard output
397 //public TestClass():base(true){}
399 //Activate this constructor to log Failures to a log file
400 //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){}
405 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
407 [Test] public void Find_ByObject()
409 int FindResult,ExpectedResult=-1;
411 //create the source datatable
412 DataTable dt = DataProvider.CreateParentDataTable();
414 //create the dataview for the table
415 DataView dv = new DataView(dt);
417 for (int i=0; i<dt.Rows.Count ; i++)
419 if ((int)dt.Rows[i]["ParentId"] == 3)
426 // Find ,no sort - exception
429 FindResult = dv.Find("3");
430 Assert.Fail("DV45: Find Failed to throw ArgumentException");
432 catch (ArgumentException) {}
433 catch (AssertionException exc) {throw exc;}
434 catch (Exception exc)
436 Assert.Fail("DV46: Find. Wrong exception type. Got:" + exc);
440 // Find = wrong sort, can not find
441 FindResult = dv.Find("3");
442 Assert.AreEqual(-1, FindResult , "DV47");
444 dv.Sort = "ParentId";
446 FindResult = dv.Find("3");
447 Assert.AreEqual(ExpectedResult, FindResult , "DV48");
450 [Test] public void Find_ByArray()
452 int FindResult,ExpectedResult=-1;
454 //create the source datatable
455 DataTable dt = DataProvider.CreateParentDataTable();
457 //create the dataview for the table
458 DataView dv = new DataView(dt);
460 for (int i=0; i<dt.Rows.Count ; i++)
462 if ((int)dt.Rows[i]["ParentId"] == 3 && dt.Rows[i]["String1"].ToString() == "3-String1")
469 // Find ,no sort - exception
472 FindResult = dv.Find(new object[] {"3","3-String1"});
473 Assert.Fail("DV49: Find Failed to throw ArgumentException");
475 catch (ArgumentException) {}
476 catch (AssertionException exc) {throw exc;}
477 catch (Exception exc)
479 Assert.Fail("DV50: Find. Wrong exception type. Got:" + exc);
482 dv.Sort = "String1,ParentId";
483 // Find = wrong sort, can not find
484 FindResult = dv.Find(new object[] {"3","3-String1"});
485 Assert.AreEqual(-1, FindResult , "DV51");
487 dv.Sort = "ParentId,String1";
489 FindResult = dv.Find(new object[] {"3","3-String1"});
490 Assert.AreEqual(ExpectedResult, FindResult , "DV52");
493 //Activate This Construntor to log All To Standard output
494 //public TestClass():base(true){}
496 //Activate this constructor to log Failures to a log file
497 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
499 //Activate this constructor to log All to a log file
500 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
502 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
504 [Test] public void GetEnumerator()
506 //create the source datatable
507 DataTable dt = DataProvider.CreateChildDataTable();
509 //create the dataview for the table
510 DataView dv = new DataView(dt);
512 IEnumerator ienm = null;
514 // GetEnumerator != null
515 ienm = dv.GetEnumerator();
516 Assert.AreEqual(true, ienm != null, "DV53");
519 while (ienm.MoveNext() )
522 Assert.AreEqual(dv[i], (DataRowView)ienm.Current , "DV54");
527 [Test] public void Item()
529 //create the source datatable
530 DataTable dt = DataProvider.CreateParentDataTable();
532 //create the dataview for the table
533 DataView dv = new DataView(dt);
536 Assert.AreEqual(dv[0].Row, dt.Rows[0] , "DV55");
539 Assert.AreEqual(dv[4].Row, dt.Rows[4] , "DV56");
541 dv.RowFilter="ParentId in (1,3,6)";
543 // DataView Item 0,DataTable with filter
544 Assert.AreEqual(dv[1].Row, dt.Rows[2] , "DV57");
547 [Test] public void ListChanged()
549 DataTable dt = DataProvider.CreateParentDataTable();
550 DataView dv = new DataView(dt);
553 dv.ListChanged +=new ListChangedEventHandler(dv_ListChanged);
555 // ----- Change Value ---------
557 // change value - Event raised
558 dv[1]["String1"] = "something";
559 Assert.AreEqual(true , evProp!=null , "DV58");
560 // change value - ListChangedType
561 Assert.AreEqual(ListChangedType.ItemChanged, evProp.lstType , "DV59");
562 // change value - NewIndex
563 Assert.AreEqual(1, evProp.NewIndex, "DV60");
564 // change value - OldIndex
565 Assert.AreEqual(-1, evProp.OldIndex , "DV61");
567 // ----- Add New ---------
569 // Add New - Event raised
571 Assert.AreEqual(true , evProp!=null , "DV62");
572 // Add New - ListChangedType
573 Assert.AreEqual(ListChangedType.ItemAdded , evProp.lstType , "DV63");
574 // Add New - NewIndex
575 Assert.AreEqual(6, evProp.NewIndex, "DV64");
576 // Add New - OldIndex
577 Assert.AreEqual(-1, evProp.OldIndex , "DV65");
579 // ----- Sort ---------
581 // sort - Event raised
582 dv.Sort = "ParentId Desc";
583 Assert.AreEqual(true , evProp!=null , "DV66");
584 // sort - ListChangedType
585 Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV67");
587 Assert.AreEqual(-1, evProp.NewIndex, "DV68");
589 Assert.AreEqual(-1, evProp.OldIndex , "DV69");
591 //ListChangedType - this was not checked
593 //PropertyDescriptorAdded - A PropertyDescriptor was added, which changed the schema.
594 //PropertyDescriptorChanged - A PropertyDescriptor was changed, which changed the schema.
595 //PropertyDescriptorDeleted
599 public void AcceptChanges ()
602 DataTable dt = new DataTable ();
603 IBindingList list = dt.DefaultView;
604 list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
605 dt.Columns.Add ("test", typeof (int));
606 dt.Rows.Add (new object[] { 10 });
607 dt.Rows.Add (new object[] { 20 });
608 // ListChangedType.Reset
611 Assert.AreEqual(true , evProp != null , "DV166");
612 // AcceptChanges - should emit ListChangedType.Reset
613 Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV167");
617 public void ClearTable ()
620 DataTable dt = new DataTable ();
621 IBindingList list = dt.DefaultView;
622 list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
623 dt.Columns.Add ("test", typeof (int));
624 dt.Rows.Add (new object[] { 10 });
625 dt.Rows.Add (new object[] { 20 });
629 Assert.AreEqual(true , evProp != null , "DV168");
630 // Clear DataTable - should emit ListChangedType.Reset
631 Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV169");
632 // Clear DataTable - should clear view count
633 Assert.AreEqual(0, dt.DefaultView.Count , "DV169");
636 private void dv_ListChanged(object sender, ListChangedEventArgs e)
638 evProp = new EventProperties();
639 evProp.lstType = e.ListChangedType;
640 evProp.NewIndex = e.NewIndex;
641 evProp.OldIndex = e.OldIndex;
644 [Test] public void RowFilter()
646 //note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
647 // this test also check DataView.Count property
649 DataRowView[] drvResult = null;
650 ArrayList al = new ArrayList();
652 //create the source datatable
653 DataTable dt = DataProvider.CreateChildDataTable();
655 //create the dataview for the table
656 DataView dv = new DataView(dt);
658 //-------------------------------------------------------------
659 //Get excpected result
661 foreach (DataRow dr in dt.Rows )
663 if ((int)dr["ChildId"] == 1)
669 // RowFilter = 'ChildId=1', check count
670 dv.RowFilter = "ChildId=1";
671 Assert.AreEqual(al.Count , dv.Count , "DV70");
673 // RowFilter = 'ChildId=1', check rows
674 drvResult = new DataRowView[dv.Count];
675 dv.CopyTo(drvResult,0);
676 //check that the filterd rows exists
678 for (int i=0; i<drvResult.Length ; i++)
680 Succeed = al.Contains(drvResult[i].Row);
683 Assert.AreEqual(true, Succeed , "DV71");
684 //-------------------------------------------------------------
686 //-------------------------------------------------------------
687 //Get excpected result
689 foreach (DataRow dr in dt.Rows )
690 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" )
693 // RowFilter - ChildId=1 and String1='1-String1'
694 dv.RowFilter = "ChildId=1 and String1='1-String1'";
695 Assert.AreEqual(al.Count , dv.Count , "DV72");
697 // RowFilter = ChildId=1 and String1='1-String1', check rows
698 drvResult = new DataRowView[dv.Count];
699 dv.CopyTo(drvResult,0);
700 //check that the filterd rows exists
702 for (int i=0; i<drvResult.Length ; i++)
704 Succeed = al.Contains(drvResult[i].Row);
707 Assert.AreEqual(true, Succeed , "DV73");
708 //-------------------------------------------------------------
711 // RowFilter - check EvaluateException
714 dv.RowFilter = "Col=1";
715 Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
717 catch (EvaluateException) {}
718 catch (AssertionException exc) {throw exc;}
719 catch (Exception exc)
721 Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
724 //SyntaxErrorException 1
725 // RowFilter - check SyntaxErrorException 1
728 dv.RowFilter = "sum('something')";
729 Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
731 catch (SyntaxErrorException) {}
732 catch (AssertionException exc) {throw exc;}
733 catch (Exception exc)
735 Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
738 //SyntaxErrorException 2
739 // RowFilter - check SyntaxErrorException 2
742 dv.RowFilter = "HH**!";
743 Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
745 catch (SyntaxErrorException) {}
746 catch (AssertionException exc) {throw exc;}
747 catch (Exception exc)
749 Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
753 [Test] public void RowStateFilter()
757 CurrentRows Current rows including unchanged, new, and modified rows. 22
758 Deleted A deleted row. 8
759 ModifiedCurrent A current version, which is a modified version of original data (see ModifiedOriginal). 16
760 ModifiedOriginal The original version (although it has since been modified and is available as ModifiedCurrent). 32
762 OriginalRows Original rows including unchanged and deleted rows. 42
763 Unchanged An unchanged row. 2
766 //DataRowView[] drvResult = null;
767 ArrayList al = new ArrayList();
769 DataTable dt = DataProvider.CreateParentDataTable();
771 //create the dataview for the table
772 DataView dv = new DataView(dt);
777 dt.Rows[1]["ParentId"] = 1;
778 dt.Rows[2]["ParentId"] = 1;
780 dt.Rows.Add(new object[] {1,"A","B"});
781 dt.Rows.Add(new object[] {1,"C","D"});
782 dt.Rows.Add(new object[] {1,"E","F"});
784 //---------- Added --------
785 dv.RowStateFilter = DataViewRowState.Added ;
786 drResult = GetResultRows(dt,DataRowState.Added);
788 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV80");
790 //---------- CurrentRows --------
791 dv.RowStateFilter = DataViewRowState.CurrentRows ;
792 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified );
794 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV81");
796 //---------- ModifiedCurrent --------
797 dv.RowStateFilter = DataViewRowState.ModifiedCurrent ;
798 drResult = GetResultRows(dt,DataRowState.Modified );
800 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV82");
802 //---------- ModifiedOriginal --------
803 dv.RowStateFilter = DataViewRowState.ModifiedOriginal ;
804 drResult = GetResultRows(dt,DataRowState.Modified );
806 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV83");
808 //---------- Deleted --------
809 dv.RowStateFilter = DataViewRowState.Deleted ;
810 drResult = GetResultRows(dt,DataRowState.Deleted );
812 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV84");
814 //---------- OriginalRows --------
815 dv.RowStateFilter = DataViewRowState.OriginalRows ;
816 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Deleted );
818 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV85");
822 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
825 ArrayList al = new ArrayList();
826 DataRowVersion drVer = DataRowVersion.Current;
828 //From MSDN - The row the default version for the current DataRowState.
829 // For a DataRowState value of Added, Modified or Current,
830 // the default version is Current.
831 // For a DataRowState of Deleted, the version is Original.
832 // For a DataRowState value of Detached, the version is Proposed.
834 if ( ((State & DataRowState.Added) > 0)
835 | ((State & DataRowState.Modified) > 0)
836 | ((State & DataRowState.Unchanged) > 0) )
837 drVer = DataRowVersion.Current;
838 if ( (State & DataRowState.Deleted) > 0
839 | (State & DataRowState.Detached) > 0 )
840 drVer = DataRowVersion.Original;
842 foreach (DataRow dr in dt.Rows )
844 if ( dr.HasVersion(drVer)
845 //&& ((int)dr["ParentId", drVer] == 1)
846 && ((dr.RowState & State) > 0 )
850 DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
854 private bool CompareSortedRowsByParentId(DataView dv, DataRow[] drTable)
856 if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
858 //comparing the rows by using columns ParentId and ChildId
859 if ((dv.RowStateFilter & DataViewRowState.Deleted) > 0)
861 for (int i=0; i<dv.Count ; i++)
863 if (dv[i].Row["ParentId",DataRowVersion.Original ].ToString() != drTable[i]["ParentId",DataRowVersion.Original].ToString())
869 for (int i=0; i<dv.Count ; i++)
871 if (dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString())
878 [Test] public void Sort()
880 DataRow[] drArrTable;
882 //create the source datatable
883 DataTable dt = DataProvider.CreateChildDataTable();
885 //create the dataview for the table
886 DataView dv = new DataView(dt);
888 dv.Sort = "ParentId";
889 drArrTable = dt.Select("","ParentId");
891 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV86");
894 drArrTable = dt.Select("","ChildId");
896 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV87");
898 dv.Sort = "ParentId Desc, ChildId";
899 drArrTable = dt.Select("","ParentId Desc, ChildId");
900 // sort = ParentId Desc, ChildId
901 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV88");
903 dv.Sort = "ChildId Asc, ParentId";
904 drArrTable = dt.Select("","ChildId Asc, ParentId");
905 // sort = ChildId Asc, ParentId
906 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV89");
908 dv.Sort = "ChildId Asc, ChildId Desc";
909 drArrTable = dt.Select("","ChildId Asc, ChildId Desc");
910 // sort = ChildId Asc, ChildId Desc
911 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV90");
913 // IndexOutOfRangeException - 1
916 dv.Sort = "something";
917 Assert.Fail("DV91: Sort Failed to throw IndexOutOfRangeException");
919 catch (IndexOutOfRangeException) {}
920 catch (AssertionException exc) {throw exc;}
921 catch (Exception exc)
923 Assert.Fail("DV92: Sort. Wrong exception type. Got:" + exc);
926 // IndexOutOfRangeException - 2
929 dv.Sort = "ColumnId Desc Asc";
930 Assert.Fail("DV93: Sort Failed to throw IndexOutOfRangeException");
932 catch (IndexOutOfRangeException) {}
933 catch (AssertionException exc) {throw exc;}
934 catch (Exception exc)
936 Assert.Fail("DV94: Sort. Wrong exception type. Got:" + exc);
939 // IndexOutOfRangeException - 3
942 dv.Sort = "ColumnId blabla";
943 Assert.Fail("DV95: Sort Failed to throw IndexOutOfRangeException");
945 catch (IndexOutOfRangeException) {}
946 catch (AssertionException exc) {throw exc;}
947 catch (Exception exc)
949 Assert.Fail("DV96: Sort. Wrong exception type. Got:" + exc);
953 private bool CompareSortedRowsByParentAndChildId(DataView dv, DataRow[] drTable)
955 if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
957 //comparing the rows by using columns ParentId and ChildId
958 for (int i=0; i<dv.Count ; i++)
960 if ( dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString()
962 dv[i].Row["ChildId"].ToString() != drTable[i]["ChildId"].ToString())
968 [Test] public void Table()
970 DataTable dt = new DataTable();
971 DataView dv = new DataView();
974 Assert.AreEqual(null , dv.Table , "DV97");
976 // DataException - bind to table with no name
980 Assert.Fail("DV98: Table Failed to throw DataException");
982 catch (DataException) {}
983 catch (AssertionException exc) {throw exc;}
984 catch (Exception exc)
986 Assert.Fail("DV99: Table. Wrong exception type. Got:" + exc);
989 dt.TableName = "myTable";
992 Assert.AreEqual(dt, dv.Table , "DV100");
994 // assign null to DataTable
996 Assert.AreEqual(null, dv.Table , "DV101");
999 [Test] public void ctor_Empty()
1002 dv = new DataView();
1005 Assert.AreEqual(false, dv == null, "DV102");
1008 [Test] public void ctor_DataTable()
1011 DataTable dt = new DataTable("myTable");
1014 dv = new DataView(dt);
1015 Assert.AreEqual(false, dv == null, "DV103");
1018 Assert.AreEqual(dt , dv.Table , "DV104");
1021 [Test] public void ctor_ExpectedExceptions()
1024 DataTable dt = new DataTable("myTable");
1026 // ctor - missing column CutomerID Exception
1029 //exception: System.Data.EvaluateException: Cannot find column [CustomerId]
1030 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1031 Assert.Fail("DV105: DataView ctor Failed to throw EvaluateException or IndexOutOfRangeException");
1033 catch (EvaluateException) {}
1034 catch (IndexOutOfRangeException) {}
1035 catch (AssertionException exc) {throw exc;}
1036 catch (Exception exc)
1038 Assert.Fail("DV106: DataView ctor. Wrong exception type. Got:" + exc);
1041 dt.Columns.Add(new DataColumn("CustomerId"));
1043 // ctor - missing column Age Exception
1046 //exception: System.Data.EvaluateException: Cannot find column [Age]
1047 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1048 Assert.Fail("DV107: DataView ctor Failed to throw IndexOutOfRangeException");
1050 catch (IndexOutOfRangeException) {}
1051 catch (AssertionException exc) {throw exc;}
1052 catch (Exception exc)
1054 Assert.Fail("DV108: DataView ctor. Wrong exception type. Got:" + exc);
1058 [Test] public void ctor_Complex()
1061 DataTable dt = new DataTable("myTable");
1063 dt.Columns.Add(new DataColumn("CustomerId"));
1064 dt.Columns.Add(new DataColumn("Age"));
1067 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1068 Assert.AreEqual(false , dv == null , "DV109");
1071 Assert.AreEqual(dt , dv.Table , "DV110");
1074 Assert.AreEqual("CustomerId > 100" , dv.RowFilter , "DV111");
1077 Assert.AreEqual("Age" , dv.Sort, "DV112");
1079 // ctor - RowStateFilter
1080 Assert.AreEqual(DataViewRowState.Added , dv.RowStateFilter , "DV113");
1084 public void DataViewManager()
1087 DataViewManager dvm = null;
1088 DataSet ds = new DataSet();
1089 DataTable dt = new DataTable("myTable");
1092 dv = dt.DefaultView;
1094 // public DataViewManager DataViewManager {get;} - The DataViewManager that created this view.
1095 // If this is the default DataView for a DataTable, the DataViewManager property returns the default DataViewManager for the DataSet.
1096 // Otherwise, if the DataView was created without a DataViewManager, this property is a null reference (Nothing in Visual Basic).
1098 dvm = dv.DataViewManager;
1099 Assert.AreSame (ds.DefaultViewManager, dvm, "DV114");
1101 dv = new DataView(dt);
1102 dvm = dv.DataViewManager;
1103 Assert.IsNull (dvm, "DV115");
1105 dv = ds.DefaultViewManager.CreateDataView(dt);
1106 Assert.AreSame (ds.DefaultViewManager, dv.DataViewManager , "DV116");
1110 public void DataView_ListChangedEventTest ()
1112 // Test DataView generates events, when datatable is directly modified
1114 DataTable table = new DataTable ("test");
1115 table.Columns.Add ("col1", typeof(int));
1117 DataView view = new DataView (table);
1119 view.ListChanged += new ListChangedEventHandler (dv_ListChanged);
1122 table.Rows.Add (new object[] {1});
1123 Assert.AreEqual (0, evProp.NewIndex, "#1");
1124 Assert.AreEqual (-1, evProp.OldIndex, "#2");
1125 Assert.AreEqual (ListChangedType.ItemAdded, evProp.lstType, "#3");
1128 table.Rows[0][0] = 5;
1129 Assert.AreEqual (0, evProp.NewIndex, "#4");
1130 Assert.AreEqual (-1, evProp.OldIndex, "#5");
1131 Assert.AreEqual (ListChangedType.ItemChanged, evProp.lstType, "#6");
1134 table.Rows.RemoveAt (0);
1135 Assert.AreEqual (0, evProp.NewIndex, "#7");
1136 Assert.AreEqual (-1, evProp.OldIndex, "#8");
1137 Assert.AreEqual (ListChangedType.ItemDeleted, evProp.lstType, "#9");
1140 Assert.AreEqual (-1, evProp.NewIndex, "#10");
1141 Assert.AreEqual (-1, evProp.OldIndex, "#11");
1142 Assert.AreEqual (ListChangedType.Reset, evProp.lstType, "#12");
1146 public void TestDefaultValues()
1148 DataView view = new DataView();
1149 Assert.IsFalse(view.ApplyDefaultSort, "#1");
1150 Assert.AreEqual ("", view.Sort, "#2");
1151 Assert.AreEqual("", view.RowFilter, "#3");
1152 Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1153 Assert.IsTrue(view.AllowDelete, "#5");
1154 Assert.IsTrue(view.AllowEdit, "#6");
1155 Assert.IsTrue(view.AllowNew, "#7");
1159 public void TestTableProperty()
1161 DataTable table = new DataTable("table");
1162 DataView view = new DataView();
1164 Assert.AreEqual("", view.Sort, "#1");
1165 Assert.AreEqual("", view.RowFilter, "#2");
1166 Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1170 public void TestEquals_SameTableDiffViewProp()
1173 DataTable table = new DataTable("table");
1174 table.Columns.Add("col1", typeof(int));
1175 table.Columns.Add("col2", typeof(int));
1176 for (int i = 0; i < 5; ++i)
1177 table.Rows.Add(new object[] { i, 100 + i });
1179 DataView view1 = new DataView(table);
1180 DataView view2 = new DataView(table);
1182 object obj2 = (object)view2;
1183 Assert.IsFalse(view1.Equals(obj2), "#1");
1185 Assert.IsTrue(view1.Equals(view1), "#2");
1186 Assert.IsTrue(view2.Equals(view1), "#3");
1188 view1.Sort = "col1 ASC";
1189 Assert.IsFalse(view1.Equals(view2), "#4");
1190 view2.Sort = "col1 ASC";
1191 Assert.IsTrue(view1.Equals(view2), "#5");
1193 view1.RowFilter = "col1 > 100";
1194 Assert.IsFalse(view1.Equals(view2), "#6");
1195 view1.RowFilter = "";
1196 Assert.IsTrue(view1.Equals(view2), "#7");
1198 view1.RowStateFilter = DataViewRowState.Added;
1199 Assert.IsFalse(view1.Equals(view2), "#8");
1200 view1.RowStateFilter = DataViewRowState.CurrentRows;
1201 Assert.IsTrue(view1.Equals(view2), "#9");
1203 view1.AllowDelete = !view2.AllowDelete;
1204 Assert.IsFalse(view1.Equals(view2), "#10");
1205 view1.AllowDelete = view2.AllowDelete;
1206 Assert.IsTrue(view1.Equals(view2), "#11");
1208 view1.AllowEdit = !view2.AllowEdit;
1209 Assert.IsFalse(view1.Equals(view2), "#12");
1210 view1.AllowEdit = view2.AllowEdit;
1211 Assert.IsTrue(view1.Equals(view2), "#13");
1213 view1.AllowNew = !view2.AllowNew;
1214 Assert.IsFalse(view1.Equals(view2), "#14");
1215 view1.AllowNew = view2.AllowNew;
1216 Assert.IsTrue(view1.Equals(view2), "#15");
1218 //ApplyDefaultSort doesnet affect the comparision
1219 view1.ApplyDefaultSort = !view2.ApplyDefaultSort;
1220 Assert.IsTrue(view1.Equals(view2), "#16");
1222 DataTable table2 = table.Copy();
1223 view1.Table = table2;
1224 Assert.IsFalse(view1.Equals(view2), "#17");
1226 view1.Table = table;
1227 //well.. sort is set to null when Table is assigned..
1228 view1.Sort = view2.Sort;
1229 Assert.IsTrue(view1.Equals(view2), "#18");
1233 public void ToTable_SimpleTest()
1235 DataSet ds = new DataSet();
1236 ds.Tables.Add("table");
1237 ds.Tables[0].Columns.Add("col1", typeof(int));
1238 ds.Tables[0].Columns.Add("col2", typeof(int), "sum(col1)");
1239 ds.Tables[0].Columns.Add("col3", typeof(int));
1240 ds.Tables[0].Columns[2].AutoIncrement = true;
1242 ds.Tables[0].Rows.Add(new object[] { 1 });
1243 ds.Tables[0].Rows.Add(new object[] { 2 });
1245 ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns[0] };
1247 DataView view = new DataView(ds.Tables[0]);
1248 DataTable table = view.ToTable();
1250 // The rule seems to be : Copy any col property that doesent
1251 // involve/depend on other columns..
1252 // Constraints and PrimaryKey info not copied over
1253 Assert.AreEqual(0, table.PrimaryKey.Length, "#1");
1254 Assert.AreEqual(0, table.Constraints.Count, "#2");
1255 // AllowDBNull state is maintained by ms.net
1256 Assert.IsFalse(table.Columns[0].AllowDBNull, "#3");
1257 Assert.IsTrue(table.Columns[2].AllowDBNull, "#4");
1258 // Expression is not copied over by ms.net
1259 Assert.AreEqual("", table.Columns[1].Expression, "#5");
1260 // AutoIncrement state is maintained by ms.net
1261 Assert.IsTrue(table.Columns[2].AutoIncrement, "#6");
1263 Assert.IsFalse (ds.Tables[0] == table, "#7");
1265 Assert.AreEqual(ds.Tables [0].TableName, table.TableName, "#8");
1266 Assert.AreEqual(ds.Tables [0].Columns.Count, table.Columns.Count, "#9 Col Count");
1267 Assert.AreEqual(ds.Tables [0].Rows.Count, table.Rows.Count, "#10");
1269 for (int i = 0; i < table.Columns.Count; ++i) {
1270 Assert.AreEqual(ds.Tables [0].Columns[i].ColumnName, table.Columns[i].ColumnName, "10_"+i);
1271 Assert.AreEqual(ds.Tables [0].Columns[i].DataType, table.Columns[i].DataType, "11_"+i);
1272 for (int j = 0; j < table.Rows.Count; ++j)
1273 Assert.AreEqual(ds.Tables [0].Rows[j][i], table.Rows[j][i], "#12_"+i+"_"+j);
1276 DataTable table1 = view.ToTable("newtable");
1277 Assert.AreEqual("newtable", table1.TableName, "#13");
1281 public void ToTableTest_DataValidity ()
1283 DataTable table = new DataTable();
1284 table.Columns.Add("col1", typeof(int));
1285 table.Columns.Add("col2", typeof(int));
1286 table.Columns.Add("col3", typeof(int));
1288 for (int i = 0; i < 5; ++i) {
1289 table.Rows.Add(new object[] { i, i + 1, i + 2 });
1290 table.Rows.Add(new object[] { i, i + 1, i + 2 });
1293 table.AcceptChanges();
1294 DataView view = new DataView(table);
1296 DataTable newTable = view.ToTable (false, null);
1297 } catch (ArgumentNullException e) {
1298 // Never premise English.
1299 //Assert.AreEqual ("'columnNames' argument cannot be null." + Environment.NewLine +
1300 // "Parameter name: columnNames", e.Message, "#1");
1302 DataTable newTable1 = view.ToTable(false, new string[] { });
1303 Assert.AreEqual(10, newTable1.Rows.Count, "#3");
1305 newTable1 = view.ToTable(true, new string[] {});
1306 Assert.AreEqual(3, newTable1.Columns.Count, "#4");
1307 Assert.AreEqual(5, newTable1.Rows.Count, "#5");
1309 table.Rows.Add(new object[] { 1, 100, 100 });
1311 newTable1 = view.ToTable(true, new string[] {});
1312 Assert.AreEqual(3, newTable1.Columns.Count, "#6");
1313 Assert.AreEqual(6, newTable1.Rows.Count, "#7");
1315 newTable1 = view.ToTable(true, new string[] {"col1"});
1316 Assert.AreEqual(1, newTable1.Columns.Count, "#8");
1317 Assert.AreEqual(5, newTable1.Rows.Count, "#9");
1319 newTable1 = view.ToTable(true, new string[] { "col2", "col3"});
1320 Assert.AreEqual(2, newTable1.Columns.Count, "#10");
1321 Assert.AreEqual(6, newTable1.Rows.Count, "#11");
1323 for (int i = 0; i < newTable1.Rows.Count; ++i)
1324 Assert.AreEqual(DataRowState.Added, newTable1.Rows[i].RowState, "#11");
1326 view = new DataView (table, "col1>1", "col1 asc, col2 desc", DataViewRowState.Added);
1327 Assert.AreEqual (0, view.Count, "#12");
1329 newTable1 = view.ToTable (false, new string[] {"col1", "col3"});
1330 Assert.AreEqual (0, newTable1.Rows.Count, "#13");
1332 table.Rows.Add (new object[] {10, 1, 1});
1333 table.Rows.Add (new object[] {10, 1, 3});
1334 table.Rows.Add (new object[] {10, 1, 2});
1336 Assert.AreEqual (3, view.Count, "#14");
1337 view.Sort = "col1 asc, col2 asc, col3 desc";
1338 newTable1 = view.ToTable (true, new string[] {"col1", "col3"});
1339 Assert.AreEqual (3, newTable1.Rows.Count, "#14");
1340 Assert.AreEqual (3, newTable1.Rows [0][1], "#15");
1341 Assert.AreEqual (2, newTable1.Rows [1][1], "#16");
1342 Assert.AreEqual (1, newTable1.Rows [2][1], "#17");