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
374 dvArr = dv.FindRows(new object[] {"3","3-String1"});
375 Assert.Fail("DV42: FindRows Failed to throw ArgumentException");
377 catch (FormatException) {}
378 catch (AssertionException exc) {throw exc;}
379 catch (Exception exc)
381 Assert.Fail("DV42-2: FindRows. Wrong exception type. Got:" + exc);
384 dv.Sort = "ChildId,String1";
386 //get expected results
387 DataRow[] drExpected = dt.Select("ChildId=3 and String1='3-String1'");
389 // FindRows - check count
390 dvArr = dv.FindRows(new object[] {"3","3-String1"});
391 Assert.AreEqual(drExpected.Length , dvArr.Length, "DV43");
393 // FindRows - check data
395 //check that result is ok
397 for (int i=0; i<dvArr.Length ; i++)
399 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
402 Assert.AreEqual(true, Succeed , "DV44");
405 //Activate This Construntor to log All To Standard output
406 //public TestClass():base(true){}
408 //Activate this constructor to log Failures to a log file
409 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
411 //Activate this constructor to log All to a log file
412 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
414 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
416 [Test] public void Find_ByObject()
418 int FindResult,ExpectedResult=-1;
420 //create the source datatable
421 DataTable dt = DataProvider.CreateParentDataTable();
423 //create the dataview for the table
424 DataView dv = new DataView(dt);
426 for (int i=0; i<dt.Rows.Count ; i++)
428 if ((int)dt.Rows[i]["ParentId"] == 3)
435 // Find ,no sort - exception
438 FindResult = dv.Find("3");
439 Assert.Fail("DV45: Find Failed to throw ArgumentException");
441 catch (ArgumentException) {}
442 catch (AssertionException exc) {throw exc;}
443 catch (Exception exc)
445 Assert.Fail("DV46: Find. Wrong exception type. Got:" + exc);
449 // Find = wrong sort, can not find
450 FindResult = dv.Find("3");
451 Assert.AreEqual(-1, FindResult , "DV47");
453 dv.Sort = "ParentId";
455 FindResult = dv.Find("3");
456 Assert.AreEqual(ExpectedResult, FindResult , "DV48");
459 [Test] public void Find_ByArray()
461 int FindResult,ExpectedResult=-1;
463 //create the source datatable
464 DataTable dt = DataProvider.CreateParentDataTable();
466 //create the dataview for the table
467 DataView dv = new DataView(dt);
469 for (int i=0; i<dt.Rows.Count ; i++)
471 if ((int)dt.Rows[i]["ParentId"] == 3 && dt.Rows[i]["String1"].ToString() == "3-String1")
478 // Find ,no sort - exception
481 FindResult = dv.Find(new object[] {"3","3-String1"});
482 Assert.Fail("DV49: Find Failed to throw ArgumentException");
484 catch (ArgumentException) {}
485 catch (AssertionException exc) {throw exc;}
486 catch (Exception exc)
488 Assert.Fail("DV50: Find. Wrong exception type. Got:" + exc);
491 dv.Sort = "String1,ParentId";
492 // Find = wrong sort, can not find
495 FindResult = dv.Find(new object[] {"3","3-String1"});
496 Assert.Fail("DV51: Find Failed to throw ArgumentException");
498 catch (FormatException) {}
499 catch (AssertionException exc) {throw exc;}
500 catch (Exception exc)
502 Assert.Fail("DV51-2: Find. Wrong exception type. Got:" + exc);
505 dv.Sort = "ParentId,String1";
507 FindResult = dv.Find(new object[] {"3","3-String1"});
508 Assert.AreEqual(ExpectedResult, FindResult , "DV52");
511 //Activate This Construntor to log All To Standard output
512 //public TestClass():base(true){}
514 //Activate this constructor to log Failures to a log file
515 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
517 //Activate this constructor to log All to a log file
518 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
520 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
522 [Test] public void GetEnumerator()
524 //create the source datatable
525 DataTable dt = DataProvider.CreateChildDataTable();
527 //create the dataview for the table
528 DataView dv = new DataView(dt);
530 IEnumerator ienm = null;
532 // GetEnumerator != null
533 ienm = dv.GetEnumerator();
534 Assert.AreEqual(true, ienm != null, "DV53");
537 while (ienm.MoveNext() )
540 Assert.AreEqual(dv[i], (DataRowView)ienm.Current , "DV54");
545 [Test] public void Item()
547 //create the source datatable
548 DataTable dt = DataProvider.CreateParentDataTable();
550 //create the dataview for the table
551 DataView dv = new DataView(dt);
554 Assert.AreEqual(dv[0].Row, dt.Rows[0] , "DV55");
557 Assert.AreEqual(dv[4].Row, dt.Rows[4] , "DV56");
559 dv.RowFilter="ParentId in (1,3,6)";
561 // DataView Item 0,DataTable with filter
562 Assert.AreEqual(dv[1].Row, dt.Rows[2] , "DV57");
565 [Test] public void ListChanged()
567 DataTable dt = DataProvider.CreateParentDataTable();
568 DataView dv = new DataView(dt);
571 dv.ListChanged +=new ListChangedEventHandler(dv_ListChanged);
573 // ----- Change Value ---------
575 // change value - Event raised
576 dv[1]["String1"] = "something";
577 Assert.AreEqual(true , evProp!=null , "DV58");
578 // change value - ListChangedType
579 Assert.AreEqual(ListChangedType.ItemChanged, evProp.lstType , "DV59");
580 // change value - NewIndex
581 Assert.AreEqual(1, evProp.NewIndex, "DV60");
582 // change value - OldIndex
583 Assert.AreEqual(1, evProp.OldIndex , "DV61");
585 // ----- Add New ---------
587 // Add New - Event raised
589 Assert.AreEqual(true , evProp!=null , "DV62");
590 // Add New - ListChangedType
591 Assert.AreEqual(ListChangedType.ItemAdded , evProp.lstType , "DV63");
592 // Add New - NewIndex
593 Assert.AreEqual(6, evProp.NewIndex, "DV64");
594 // Add New - OldIndex
595 Assert.AreEqual(-1, evProp.OldIndex , "DV65");
597 // ----- Sort ---------
599 // sort - Event raised
600 dv.Sort = "ParentId Desc";
601 Assert.AreEqual(true , evProp!=null , "DV66");
602 // sort - ListChangedType
603 Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV67");
605 Assert.AreEqual(-1, evProp.NewIndex, "DV68");
607 Assert.AreEqual(-1, evProp.OldIndex , "DV69");
609 //ListChangedType - this was not checked
611 //PropertyDescriptorAdded - A PropertyDescriptor was added, which changed the schema.
612 //PropertyDescriptorChanged - A PropertyDescriptor was changed, which changed the schema.
613 //PropertyDescriptorDeleted
617 public void AcceptChanges ()
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 });
626 // ListChangedType.Reset
629 Assert.AreEqual(true , evProp != null , "DV166");
630 // AcceptChanges - should emit ListChangedType.Reset
631 Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV167");
635 public void ClearTable ()
638 DataTable dt = new DataTable ();
639 IBindingList list = dt.DefaultView;
640 list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
641 dt.Columns.Add ("test", typeof (int));
642 dt.Rows.Add (new object[] { 10 });
643 dt.Rows.Add (new object[] { 20 });
647 Assert.AreEqual(true , evProp != null , "DV168");
648 // Clear DataTable - should emit ListChangedType.Reset
649 Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV169");
650 // Clear DataTable - should clear view count
651 Assert.AreEqual(0, dt.DefaultView.Count , "DV169");
654 private void dv_ListChanged(object sender, ListChangedEventArgs e)
656 evProp = new EventProperties();
657 evProp.lstType = e.ListChangedType;
658 evProp.NewIndex = e.NewIndex;
659 evProp.OldIndex = e.OldIndex;
662 [Test] public void RowFilter()
664 //note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
665 // this test also check DataView.Count property
667 DataRowView[] drvResult = null;
668 ArrayList al = new ArrayList();
670 //create the source datatable
671 DataTable dt = DataProvider.CreateChildDataTable();
673 //create the dataview for the table
674 DataView dv = new DataView(dt);
676 //-------------------------------------------------------------
677 //Get excpected result
679 foreach (DataRow dr in dt.Rows )
681 if ((int)dr["ChildId"] == 1)
687 // RowFilter = 'ChildId=1', check count
688 dv.RowFilter = "ChildId=1";
689 Assert.AreEqual(al.Count , dv.Count , "DV70");
691 // RowFilter = 'ChildId=1', check rows
692 drvResult = new DataRowView[dv.Count];
693 dv.CopyTo(drvResult,0);
694 //check that the filterd rows exists
696 for (int i=0; i<drvResult.Length ; i++)
698 Succeed = al.Contains(drvResult[i].Row);
701 Assert.AreEqual(true, Succeed , "DV71");
702 //-------------------------------------------------------------
704 //-------------------------------------------------------------
705 //Get excpected result
707 foreach (DataRow dr in dt.Rows )
708 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" )
711 // RowFilter - ChildId=1 and String1='1-String1'
712 dv.RowFilter = "ChildId=1 and String1='1-String1'";
713 Assert.AreEqual(al.Count , dv.Count , "DV72");
715 // RowFilter = ChildId=1 and String1='1-String1', check rows
716 drvResult = new DataRowView[dv.Count];
717 dv.CopyTo(drvResult,0);
718 //check that the filterd rows exists
720 for (int i=0; i<drvResult.Length ; i++)
722 Succeed = al.Contains(drvResult[i].Row);
725 Assert.AreEqual(true, Succeed , "DV73");
726 //-------------------------------------------------------------
729 // RowFilter - check EvaluateException
732 dv.RowFilter = "Col=1";
733 Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
735 catch (EvaluateException) {}
736 catch (AssertionException exc) {throw exc;}
737 catch (Exception exc)
739 Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
742 //SyntaxErrorException 1
743 // RowFilter - check SyntaxErrorException 1
746 dv.RowFilter = "sum('something')";
747 Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
749 catch (SyntaxErrorException) {}
750 catch (AssertionException exc) {throw exc;}
751 catch (Exception exc)
753 Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
756 //SyntaxErrorException 2
757 // RowFilter - check SyntaxErrorException 2
760 dv.RowFilter = "HH**!";
761 Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
763 catch (SyntaxErrorException) {}
764 catch (AssertionException exc) {throw exc;}
765 catch (Exception exc)
767 Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
771 [Test] public void RowStateFilter()
775 CurrentRows Current rows including unchanged, new, and modified rows. 22
776 Deleted A deleted row. 8
777 ModifiedCurrent A current version, which is a modified version of original data (see ModifiedOriginal). 16
778 ModifiedOriginal The original version (although it has since been modified and is available as ModifiedCurrent). 32
780 OriginalRows Original rows including unchanged and deleted rows. 42
781 Unchanged An unchanged row. 2
784 //DataRowView[] drvResult = null;
785 ArrayList al = new ArrayList();
787 DataTable dt = DataProvider.CreateParentDataTable();
789 //create the dataview for the table
790 DataView dv = new DataView(dt);
795 dt.Rows[1]["ParentId"] = 1;
796 dt.Rows[2]["ParentId"] = 1;
798 dt.Rows.Add(new object[] {1,"A","B"});
799 dt.Rows.Add(new object[] {1,"C","D"});
800 dt.Rows.Add(new object[] {1,"E","F"});
802 //---------- Added --------
803 dv.RowStateFilter = DataViewRowState.Added ;
804 drResult = GetResultRows(dt,DataRowState.Added);
806 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV80");
808 //---------- CurrentRows --------
809 dv.RowStateFilter = DataViewRowState.CurrentRows ;
810 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified );
812 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV81");
814 //---------- ModifiedCurrent --------
815 dv.RowStateFilter = DataViewRowState.ModifiedCurrent ;
816 drResult = GetResultRows(dt,DataRowState.Modified );
818 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV82");
820 //---------- ModifiedOriginal --------
821 dv.RowStateFilter = DataViewRowState.ModifiedOriginal ;
822 drResult = GetResultRows(dt,DataRowState.Modified );
824 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV83");
826 //---------- Deleted --------
827 dv.RowStateFilter = DataViewRowState.Deleted ;
828 drResult = GetResultRows(dt,DataRowState.Deleted );
830 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV84");
832 //---------- OriginalRows --------
833 dv.RowStateFilter = DataViewRowState.OriginalRows ;
834 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Deleted );
836 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV85");
840 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
843 ArrayList al = new ArrayList();
844 DataRowVersion drVer = DataRowVersion.Current;
846 //From MSDN - The row the default version for the current DataRowState.
847 // For a DataRowState value of Added, Modified or Current,
848 // the default version is Current.
849 // For a DataRowState of Deleted, the version is Original.
850 // For a DataRowState value of Detached, the version is Proposed.
852 if ( ((State & DataRowState.Added) > 0)
853 | ((State & DataRowState.Modified) > 0)
854 | ((State & DataRowState.Unchanged) > 0) )
855 drVer = DataRowVersion.Current;
856 if ( (State & DataRowState.Deleted) > 0
857 | (State & DataRowState.Detached) > 0 )
858 drVer = DataRowVersion.Original;
860 foreach (DataRow dr in dt.Rows )
862 if ( dr.HasVersion(drVer)
863 //&& ((int)dr["ParentId", drVer] == 1)
864 && ((dr.RowState & State) > 0 )
868 DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
872 private bool CompareSortedRowsByParentId(DataView dv, DataRow[] drTable)
874 if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
876 //comparing the rows by using columns ParentId and ChildId
877 if ((dv.RowStateFilter & DataViewRowState.Deleted) > 0)
879 for (int i=0; i<dv.Count ; i++)
881 if (dv[i].Row["ParentId",DataRowVersion.Original ].ToString() != drTable[i]["ParentId",DataRowVersion.Original].ToString())
887 for (int i=0; i<dv.Count ; i++)
889 if (dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString())
896 [Test] public void Sort()
898 DataRow[] drArrTable;
900 //create the source datatable
901 DataTable dt = DataProvider.CreateChildDataTable();
903 //create the dataview for the table
904 DataView dv = new DataView(dt);
906 dv.Sort = "ParentId";
907 drArrTable = dt.Select("","ParentId");
909 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV86");
912 drArrTable = dt.Select("","ChildId");
914 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV87");
916 dv.Sort = "ParentId Desc, ChildId";
917 drArrTable = dt.Select("","ParentId Desc, ChildId");
918 // sort = ParentId Desc, ChildId
919 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV88");
921 dv.Sort = "ChildId Asc, ParentId";
922 drArrTable = dt.Select("","ChildId Asc, ParentId");
923 // sort = ChildId Asc, ParentId
924 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV89");
926 dv.Sort = "ChildId Asc, ChildId Desc";
927 drArrTable = dt.Select("","ChildId Asc, ChildId Desc");
928 // sort = ChildId Asc, ChildId Desc
929 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV90");
931 // IndexOutOfRangeException - 1
934 dv.Sort = "something";
935 Assert.Fail("DV91: Sort Failed to throw IndexOutOfRangeException");
937 catch (IndexOutOfRangeException) {}
938 catch (AssertionException exc) {throw exc;}
939 catch (Exception exc)
941 Assert.Fail("DV92: Sort. Wrong exception type. Got:" + exc);
944 // IndexOutOfRangeException - 2
947 dv.Sort = "ColumnId Desc Asc";
948 Assert.Fail("DV93: Sort Failed to throw IndexOutOfRangeException");
950 catch (IndexOutOfRangeException) {}
951 catch (AssertionException exc) {throw exc;}
952 catch (Exception exc)
954 Assert.Fail("DV94: Sort. Wrong exception type. Got:" + exc);
957 // IndexOutOfRangeException - 3
960 dv.Sort = "ColumnId blabla";
961 Assert.Fail("DV95: Sort Failed to throw IndexOutOfRangeException");
963 catch (IndexOutOfRangeException) {}
964 catch (AssertionException exc) {throw exc;}
965 catch (Exception exc)
967 Assert.Fail("DV96: Sort. Wrong exception type. Got:" + exc);
971 private bool CompareSortedRowsByParentAndChildId(DataView dv, DataRow[] drTable)
973 if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
975 //comparing the rows by using columns ParentId and ChildId
976 for (int i=0; i<dv.Count ; i++)
978 if ( dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString()
980 dv[i].Row["ChildId"].ToString() != drTable[i]["ChildId"].ToString())
986 [Test] public void Table()
988 DataTable dt = new DataTable();
989 DataView dv = new DataView();
992 Assert.AreEqual(null , dv.Table , "DV97");
994 // DataException - bind to table with no name
998 Assert.Fail("DV98: Table Failed to throw DataException");
1000 catch (DataException) {}
1001 catch (AssertionException exc) {throw exc;}
1002 catch (Exception exc)
1004 Assert.Fail("DV99: Table. Wrong exception type. Got:" + exc);
1007 dt.TableName = "myTable";
1010 Assert.AreEqual(dt, dv.Table , "DV100");
1012 // assign null to DataTable
1014 Assert.AreEqual(null, dv.Table , "DV101");
1017 [Test] public void ctor_Empty()
1020 dv = new DataView();
1023 Assert.AreEqual(false, dv == null, "DV102");
1026 [Test] public void ctor_DataTable()
1029 DataTable dt = new DataTable("myTable");
1032 dv = new DataView(dt);
1033 Assert.AreEqual(false, dv == null, "DV103");
1036 Assert.AreEqual(dt , dv.Table , "DV104");
1039 [Test] public void ctor_ExpectedExceptions()
1042 DataTable dt = new DataTable("myTable");
1044 // ctor - missing column CutomerID Exception
1047 //exception: System.Data.EvaluateException: Cannot find column [CustomerId]
1048 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1049 Assert.Fail("DV105: DataView ctor Failed to throw EvaluateException or IndexOutOfRangeException");
1051 catch (EvaluateException) {}
1052 catch (IndexOutOfRangeException) {}
1053 catch (AssertionException exc) {throw exc;}
1054 catch (Exception exc)
1056 Assert.Fail("DV106: DataView ctor. Wrong exception type. Got:" + exc);
1059 dt.Columns.Add(new DataColumn("CustomerId"));
1061 // ctor - missing column Age Exception
1064 //exception: System.Data.EvaluateException: Cannot find column [Age]
1065 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1066 Assert.Fail("DV107: DataView ctor Failed to throw IndexOutOfRangeException");
1068 catch (IndexOutOfRangeException) {}
1069 catch (AssertionException exc) {throw exc;}
1070 catch (Exception exc)
1072 Assert.Fail("DV108: DataView ctor. Wrong exception type. Got:" + exc);
1076 [Test] public void ctor_Complex()
1079 DataTable dt = new DataTable("myTable");
1081 dt.Columns.Add(new DataColumn("CustomerId"));
1082 dt.Columns.Add(new DataColumn("Age"));
1085 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1086 Assert.AreEqual(false , dv == null , "DV109");
1089 Assert.AreEqual(dt , dv.Table , "DV110");
1092 Assert.AreEqual("CustomerId > 100" , dv.RowFilter , "DV111");
1095 Assert.AreEqual("Age" , dv.Sort, "DV112");
1097 // ctor - RowStateFilter
1098 Assert.AreEqual(DataViewRowState.Added , dv.RowStateFilter , "DV113");
1102 public void DataViewManager()
1105 DataViewManager dvm = null;
1106 DataSet ds = new DataSet();
1107 DataTable dt = new DataTable("myTable");
1110 dv = dt.DefaultView;
1112 // public DataViewManager DataViewManager {get;} - The DataViewManager that created this view.
1113 // If this is the default DataView for a DataTable, the DataViewManager property returns the default DataViewManager for the DataSet.
1114 // Otherwise, if the DataView was created without a DataViewManager, this property is a null reference (Nothing in Visual Basic).
1116 dvm = dv.DataViewManager;
1117 Assert.AreSame (ds.DefaultViewManager, dvm, "DV114");
1119 dv = new DataView(dt);
1120 dvm = dv.DataViewManager;
1121 Assert.IsNull (dvm, "DV115");
1123 dv = ds.DefaultViewManager.CreateDataView(dt);
1124 Assert.AreSame (ds.DefaultViewManager, dv.DataViewManager , "DV116");
1128 public void DataView_ListChangedEventTest ()
1130 // Test DataView generates events, when datatable is directly modified
1132 DataTable table = new DataTable ("test");
1133 table.Columns.Add ("col1", typeof(int));
1135 DataView view = new DataView (table);
1137 view.ListChanged += new ListChangedEventHandler (dv_ListChanged);
1140 table.Rows.Add (new object[] {1});
1141 Assert.AreEqual (0, evProp.NewIndex, "#1");
1142 Assert.AreEqual (-1, evProp.OldIndex, "#2");
1143 Assert.AreEqual (ListChangedType.ItemAdded, evProp.lstType, "#3");
1146 table.Rows[0][0] = 5;
1147 Assert.AreEqual (0, evProp.NewIndex, "#4");
1148 Assert.AreEqual (0, evProp.OldIndex, "#5");
1149 Assert.AreEqual (ListChangedType.ItemChanged, evProp.lstType, "#6");
1152 table.Rows.RemoveAt (0);
1153 Assert.AreEqual (0, evProp.NewIndex, "#7");
1154 Assert.AreEqual (-1, evProp.OldIndex, "#8");
1155 Assert.AreEqual (ListChangedType.ItemDeleted, evProp.lstType, "#9");
1158 Assert.AreEqual (-1, evProp.NewIndex, "#10");
1159 Assert.AreEqual (-1, evProp.OldIndex, "#11");
1160 Assert.AreEqual (ListChangedType.Reset, evProp.lstType, "#12");
1162 // Keep the view alive otherwise we might miss events
1163 GC.KeepAlive (view);
1167 public void TestDefaultValues()
1169 DataView view = new DataView();
1170 Assert.IsFalse(view.ApplyDefaultSort, "#1");
1171 Assert.AreEqual ("", view.Sort, "#2");
1172 Assert.AreEqual("", view.RowFilter, "#3");
1173 Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1174 Assert.IsTrue(view.AllowDelete, "#5");
1175 Assert.IsTrue(view.AllowEdit, "#6");
1176 Assert.IsTrue(view.AllowNew, "#7");
1180 public void TestTableProperty()
1182 DataTable table = new DataTable("table");
1183 DataView view = new DataView();
1185 Assert.AreEqual("", view.Sort, "#1");
1186 Assert.AreEqual("", view.RowFilter, "#2");
1187 Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1191 public void TestEquals_SameTableDiffViewProp()
1194 DataTable table = new DataTable("table");
1195 table.Columns.Add("col1", typeof(int));
1196 table.Columns.Add("col2", typeof(int));
1197 for (int i = 0; i < 5; ++i)
1198 table.Rows.Add(new object[] { i, 100 + i });
1200 DataView view1 = new DataView(table);
1201 DataView view2 = new DataView(table);
1203 object obj2 = (object)view2;
1204 Assert.IsFalse(view1.Equals(obj2), "#1");
1206 Assert.IsTrue(view1.Equals(view1), "#2");
1207 Assert.IsTrue(view2.Equals(view1), "#3");
1209 view1.Sort = "col1 ASC";
1210 Assert.IsFalse(view1.Equals(view2), "#4");
1211 view2.Sort = "col1 ASC";
1212 Assert.IsTrue(view1.Equals(view2), "#5");
1214 view1.RowFilter = "col1 > 100";
1215 Assert.IsFalse(view1.Equals(view2), "#6");
1216 view1.RowFilter = "";
1217 Assert.IsTrue(view1.Equals(view2), "#7");
1219 view1.RowStateFilter = DataViewRowState.Added;
1220 Assert.IsFalse(view1.Equals(view2), "#8");
1221 view1.RowStateFilter = DataViewRowState.CurrentRows;
1222 Assert.IsTrue(view1.Equals(view2), "#9");
1224 view1.AllowDelete = !view2.AllowDelete;
1225 Assert.IsFalse(view1.Equals(view2), "#10");
1226 view1.AllowDelete = view2.AllowDelete;
1227 Assert.IsTrue(view1.Equals(view2), "#11");
1229 view1.AllowEdit = !view2.AllowEdit;
1230 Assert.IsFalse(view1.Equals(view2), "#12");
1231 view1.AllowEdit = view2.AllowEdit;
1232 Assert.IsTrue(view1.Equals(view2), "#13");
1234 view1.AllowNew = !view2.AllowNew;
1235 Assert.IsFalse(view1.Equals(view2), "#14");
1236 view1.AllowNew = view2.AllowNew;
1237 Assert.IsTrue(view1.Equals(view2), "#15");
1239 //ApplyDefaultSort doesnet affect the comparision
1240 view1.ApplyDefaultSort = !view2.ApplyDefaultSort;
1241 Assert.IsTrue(view1.Equals(view2), "#16");
1243 DataTable table2 = table.Copy();
1244 view1.Table = table2;
1245 Assert.IsFalse(view1.Equals(view2), "#17");
1247 view1.Table = table;
1248 //well.. sort is set to null when Table is assigned..
1249 view1.Sort = view2.Sort;
1250 Assert.IsTrue(view1.Equals(view2), "#18");
1254 public void ToTable_SimpleTest()
1256 DataSet ds = new DataSet();
1257 ds.Tables.Add("table");
1258 ds.Tables[0].Columns.Add("col1", typeof(int));
1259 ds.Tables[0].Columns.Add("col2", typeof(int), "sum(col1)");
1260 ds.Tables[0].Columns.Add("col3", typeof(int));
1261 ds.Tables[0].Columns[2].AutoIncrement = true;
1263 ds.Tables[0].Rows.Add(new object[] { 1 });
1264 ds.Tables[0].Rows.Add(new object[] { 2 });
1266 ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns[0] };
1268 DataView view = new DataView(ds.Tables[0]);
1269 DataTable table = view.ToTable();
1271 // The rule seems to be : Copy any col property that doesent
1272 // involve/depend on other columns..
1273 // Constraints and PrimaryKey info not copied over
1274 Assert.AreEqual(0, table.PrimaryKey.Length, "#1");
1275 Assert.AreEqual(0, table.Constraints.Count, "#2");
1276 // AllowDBNull state is maintained by ms.net
1277 Assert.IsFalse(table.Columns[0].AllowDBNull, "#3");
1278 Assert.IsTrue(table.Columns[2].AllowDBNull, "#4");
1279 // Expression is not copied over by ms.net
1280 Assert.AreEqual("", table.Columns[1].Expression, "#5");
1281 // AutoIncrement state is maintained by ms.net
1282 Assert.IsTrue(table.Columns[2].AutoIncrement, "#6");
1284 Assert.IsFalse (ds.Tables[0] == table, "#7");
1286 Assert.AreEqual(ds.Tables [0].TableName, table.TableName, "#8");
1287 Assert.AreEqual(ds.Tables [0].Columns.Count, table.Columns.Count, "#9 Col Count");
1288 Assert.AreEqual(ds.Tables [0].Rows.Count, table.Rows.Count, "#10");
1290 for (int i = 0; i < table.Columns.Count; ++i) {
1291 Assert.AreEqual(ds.Tables [0].Columns[i].ColumnName, table.Columns[i].ColumnName, "10_"+i);
1292 Assert.AreEqual(ds.Tables [0].Columns[i].DataType, table.Columns[i].DataType, "11_"+i);
1293 for (int j = 0; j < table.Rows.Count; ++j)
1294 Assert.AreEqual(ds.Tables [0].Rows[j][i], table.Rows[j][i], "#12_"+i+"_"+j);
1297 DataTable table1 = view.ToTable("newtable");
1298 Assert.AreEqual("newtable", table1.TableName, "#13");
1302 public void ToTableTest_DataValidity ()
1304 DataTable table = new DataTable();
1305 table.Columns.Add("col1", typeof(int));
1306 table.Columns.Add("col2", typeof(int));
1307 table.Columns.Add("col3", typeof(int));
1309 for (int i = 0; i < 5; ++i) {
1310 table.Rows.Add(new object[] { i, i + 1, i + 2 });
1311 table.Rows.Add(new object[] { i, i + 1, i + 2 });
1314 table.AcceptChanges();
1315 DataView view = new DataView(table);
1317 DataTable newTable = view.ToTable (false, null);
1318 } catch (ArgumentNullException e) {
1319 // Never premise English.
1320 //Assert.AreEqual ("'columnNames' argument cannot be null." + Environment.NewLine +
1321 // "Parameter name: columnNames", e.Message, "#1");
1323 DataTable newTable1 = view.ToTable(false, new string[] { });
1324 Assert.AreEqual(10, newTable1.Rows.Count, "#3");
1326 newTable1 = view.ToTable(true, new string[] {});
1327 Assert.AreEqual(3, newTable1.Columns.Count, "#4");
1328 Assert.AreEqual(5, newTable1.Rows.Count, "#5");
1330 table.Rows.Add(new object[] { 1, 100, 100 });
1332 newTable1 = view.ToTable(true, new string[] {});
1333 Assert.AreEqual(3, newTable1.Columns.Count, "#6");
1334 Assert.AreEqual(6, newTable1.Rows.Count, "#7");
1336 newTable1 = view.ToTable(true, new string[] {"col1"});
1337 Assert.AreEqual(1, newTable1.Columns.Count, "#8");
1338 Assert.AreEqual(5, newTable1.Rows.Count, "#9");
1340 newTable1 = view.ToTable(true, new string[] { "col2", "col3"});
1341 Assert.AreEqual(2, newTable1.Columns.Count, "#10");
1342 Assert.AreEqual(6, newTable1.Rows.Count, "#11");
1344 for (int i = 0; i < newTable1.Rows.Count; ++i)
1345 Assert.AreEqual(DataRowState.Added, newTable1.Rows[i].RowState, "#11");
1347 view = new DataView (table, "col1>1", "col1 asc, col2 desc", DataViewRowState.Added);
1348 Assert.AreEqual (0, view.Count, "#12");
1350 newTable1 = view.ToTable (false, new string[] {"col1", "col3"});
1351 Assert.AreEqual (0, newTable1.Rows.Count, "#13");
1353 table.Rows.Add (new object[] {10, 1, 1});
1354 table.Rows.Add (new object[] {10, 1, 3});
1355 table.Rows.Add (new object[] {10, 1, 2});
1357 Assert.AreEqual (3, view.Count, "#14");
1358 view.Sort = "col1 asc, col2 asc, col3 desc";
1359 newTable1 = view.ToTable (true, new string[] {"col1", "col3"});
1360 Assert.AreEqual (3, newTable1.Rows.Count, "#14");
1361 Assert.AreEqual (3, newTable1.Rows [0][1], "#15");
1362 Assert.AreEqual (2, newTable1.Rows [1][1], "#16");
1363 Assert.AreEqual (1, newTable1.Rows [2][1], "#17");