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;
32 using System.ComponentModel;
34 using MonoTests.System.Data.Utils;
36 namespace MonoTests_System.Data
38 [TestFixture] public class DataViewTest2
40 private EventProperties evProp = null;
42 class EventProperties //hold the event properties to be checked
44 public System.ComponentModel.ListChangedType lstType ;
49 [Test] public void AddNew()
51 //create the source datatable
52 DataTable dt = DataProvider.CreateChildDataTable();
54 //create the dataview for the table
55 DataView dv = new DataView(dt);
57 int CountView = dv.Count ;
58 int CountTable= dt.Rows.Count ;
60 DataRowView drv = null;
62 // AddNew - DataView Row Count
64 Assert.AreEqual(dv.Count , CountView+1, "DV1");
66 // AddNew - Table Row Count
67 Assert.AreEqual(dt.Rows.Count , CountTable, "DV2");
69 // AddNew - new row in DataTable
71 Assert.AreEqual(dt.Rows.Count , CountTable+1, "DV3");
73 // AddNew - new row != null
74 Assert.AreEqual(true, drv!=null, "DV4");
76 // AddNew - check table
77 Assert.AreEqual(dt, drv.Row.Table, "DV5");
80 [Test] public void AllowDelete()
82 DataTable dt = DataProvider.CreateParentDataTable();
83 DataView dv = new DataView(dt);
85 // AllowDelete - default value
86 Assert.AreEqual(true , dv.AllowDelete , "DV6");
89 dv.AllowDelete = true;
90 Assert.AreEqual(true, dv.AllowDelete , "DV7");
92 // AllowDelete - false
93 dv.AllowDelete = false;
94 Assert.AreEqual(false, dv.AllowDelete , "DV8");
96 dv.AllowDelete = false;
97 // AllowDelete false- Exception
101 Assert.Fail("DV9: Delete Failed to throw DataException");
103 catch (DataException) {}
104 catch (AssertionException exc) {throw exc;}
105 catch (Exception exc)
107 Assert.Fail("DV10: Delete. Wrong exception type. Got:" + exc);
110 dv.AllowDelete = true;
111 int RowsCount = dv.Count ;
112 // AllowDelete true- Exception
114 Assert.AreEqual(RowsCount-1, dv.Count , "DV11");
117 [Test] public void AllowEdit()
119 DataTable dt = DataProvider.CreateParentDataTable();
120 DataView dv = new DataView(dt);
122 // AllowEdit - default value
123 Assert.AreEqual(true , dv.AllowEdit , "DV12");
127 Assert.AreEqual(true, dv.AllowEdit , "DV13");
130 dv.AllowEdit = false;
131 Assert.AreEqual(false, dv.AllowEdit , "DV14");
135 // AllowEdit false - exception
139 Assert.Fail("DV15: Indexer Failed to throw DataException");
141 catch (DataException) {}
142 catch (AssertionException exc) {throw exc;}
143 catch (Exception exc)
145 Assert.Fail("DV16: Indexer. Wrong exception type. Got:" + exc);
150 // AllowEdit true- exception
152 Assert.AreEqual("aaa", dv[0][2] , "DV17");
155 [Test] public void AllowNew()
157 DataTable dt = DataProvider.CreateParentDataTable();
158 DataView dv = new DataView(dt);
160 // AllowNew - default value
161 Assert.AreEqual(true , dv.AllowNew , "DV18");
165 Assert.AreEqual(true, dv.AllowNew , "DV19");
169 Assert.AreEqual(false, dv.AllowNew , "DV20");
171 // AllowNew - exception
175 Assert.Fail("DV21: AddNew Failed to throw DataException");
177 catch (DataException) {}
178 catch (AssertionException exc) {throw exc;}
179 catch (Exception exc)
181 Assert.Fail("DV22: AddNew. Wrong exception type. Got:" + exc);
185 int RowsCount = dv.Count ;
187 // AllowNew - exception
189 Assert.AreEqual(RowsCount+1, dv.Count , "DV23");
192 [Test] public void ApplyDefaultSort()
194 DataTable dt = DataProvider.CreateParentDataTable();
195 DataView dv = new DataView(dt);
197 // ApplyDefaultSort - default value
198 Assert.AreEqual(false , dv.ApplyDefaultSort , "DV24");
200 // ApplyDefaultSort - true
201 dv.ApplyDefaultSort = true;
202 Assert.AreEqual(true, dv.ApplyDefaultSort , "DV25");
204 // ApplyDefaultSort - false
205 dv.ApplyDefaultSort = false;
206 Assert.AreEqual(false, dv.ApplyDefaultSort , "DV26");
209 [Test] public void CopyTo()
211 //create the source datatable
212 DataTable dt = DataProvider.CreateChildDataTable();
214 //create the dataview for the table
215 DataView dv = new DataView(dt);
217 DataRowView[] drvExpected = null;
218 DataRowView[] drvResult = null;
220 // ------- Copy from Index=0
221 drvExpected = new DataRowView[dv.Count];
222 for (int i=0; i < dv.Count ;i++)
224 drvExpected[i] = dv[i];
227 drvResult = new DataRowView[dv.Count];
228 // CopyTo from index 0
229 dv.CopyTo(drvResult,0);
230 Assert.AreEqual(drvResult, drvExpected , "DV27");
232 // ------- Copy from Index=3
233 drvExpected = new DataRowView[dv.Count+3];
234 for (int i=0; i < dv.Count ;i++)
236 drvExpected[i+3] = dv[i];
239 drvResult = new DataRowView[dv.Count+3];
240 // CopyTo from index 3
241 dv.CopyTo(drvResult,3);
242 Assert.AreEqual(drvResult , drvExpected , "DV28");
244 // ------- Copy from Index=3,larger array
245 drvExpected = new DataRowView[dv.Count+9];
246 for (int i=0; i < dv.Count ;i++)
248 drvExpected[i+3] = dv[i];
251 drvResult = new DataRowView[dv.Count+9];
252 // CopyTo from index 3,larger array
253 dv.CopyTo(drvResult,3);
254 Assert.AreEqual(drvResult, drvExpected , "DV29");
256 // ------- CopyTo smaller array, check exception
257 drvResult = new DataRowView[dv.Count-1];
259 // CopyTo smaller array, check exception
262 dv.CopyTo(drvResult,0);
263 Assert.Fail("DV30: CopyTo Failed to throw IndexOutOfRangeException");
265 catch (IndexOutOfRangeException) {}
266 catch (AssertionException exc) {throw exc;}
267 catch (Exception exc)
269 Assert.Fail("DV31: CopyTo. Wrong exception type. Got:" + exc);
273 [Test] public void Delete()
275 //create the source datatable
276 DataTable dt = DataProvider.CreateChildDataTable();
278 //create the dataview for the table
279 DataView dv = new DataView(dt);
281 int CountView = dv.Count ;
282 int CountTable= dt.Rows.Count ;
284 DataRowView drv = dv[0];
286 // Delete - DataView Row Count
288 Assert.AreEqual(dv.Count , CountView-1, "DV32");
290 // Delete - Table Row Count
291 Assert.AreEqual(dt.Rows.Count , CountTable, "DV33");
293 // Delete - check table
294 Assert.AreEqual(dt, drv.Row.Table, "DV34");
297 [Test] public void FindRows_ByKey()
299 DataRowView[] dvArr = null;
301 //create the source datatable
302 DataTable dt = DataProvider.CreateChildDataTable();
304 //create the dataview for the table
305 DataView dv = new DataView(dt);
307 // FindRows ,no sort - exception
310 dvArr = dv.FindRows(3);
311 Assert.Fail("DV35: FindRows Failed to throw ArgumentException");
313 catch (ArgumentException) {}
314 catch (AssertionException exc) {throw exc;}
315 catch (Exception exc)
317 Assert.Fail("DV36: FindRows. Wrong exception type. Got:" + exc);
321 // Find = wrong sort, can not find
322 dvArr = dv.FindRows(3);
323 Assert.AreEqual(0, dvArr.Length , "DV37");
327 //get expected results
328 DataRow[] drExpected = dt.Select("ChildId=3");
330 // FindRows - check count
331 dvArr = dv.FindRows(3);
332 Assert.AreEqual(drExpected.Length , dvArr.Length, "DV38");
334 // FindRows - check data
336 //check that result is ok
338 for (int i=0; i<dvArr.Length ; i++)
340 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
343 Assert.AreEqual(true, Succeed , "DV39");
346 [Test] public void FindRows_ByKeys()
348 DataRowView[] dvArr = null;
350 //create the source datatable
351 DataTable dt = DataProvider.CreateChildDataTable();
353 //create the dataview for the table
354 DataView dv = new DataView(dt);
356 // FindRows ,no sort - exception
359 dvArr = dv.FindRows(new object[] {"3","3-String1"});
360 Assert.Fail("DV40: FindRows Failed to throw ArgumentException");
362 catch (ArgumentException) {}
363 catch (AssertionException exc) {throw exc;}
364 catch (Exception exc)
366 Assert.Fail("DV41: FindRows. Wrong exception type. Got:" + exc);
369 dv.Sort = "String1,ChildId";
370 // Find = wrong sort, can not find
371 dvArr = dv.FindRows(new object[] {"3","3-String1"});
372 Assert.AreEqual(0, dvArr.Length , "DV42");
374 dv.Sort = "ChildId,String1";
376 //get expected results
377 DataRow[] drExpected = dt.Select("ChildId=3 and String1='3-String1'");
379 // FindRows - check count
380 dvArr = dv.FindRows(new object[] {"3","3-String1"});
381 Assert.AreEqual(drExpected.Length , dvArr.Length, "DV43");
383 // FindRows - check data
385 //check that result is ok
387 for (int i=0; i<dvArr.Length ; i++)
389 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
392 Assert.AreEqual(true, Succeed , "DV44");
395 //Activate This Construntor to log All To Standard output
396 //public TestClass():base(true){}
398 //Activate this constructor to log Failures to a log file
399 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
401 //Activate this constructor to log All to a log file
402 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
404 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
406 [Test] public void Find_ByObject()
408 int FindResult,ExpectedResult=-1;
410 //create the source datatable
411 DataTable dt = DataProvider.CreateParentDataTable();
413 //create the dataview for the table
414 DataView dv = new DataView(dt);
416 for (int i=0; i<dt.Rows.Count ; i++)
418 if ((int)dt.Rows[i]["ParentId"] == 3)
425 // Find ,no sort - exception
428 FindResult = dv.Find("3");
429 Assert.Fail("DV45: Find Failed to throw ArgumentException");
431 catch (ArgumentException) {}
432 catch (AssertionException exc) {throw exc;}
433 catch (Exception exc)
435 Assert.Fail("DV46: Find. Wrong exception type. Got:" + exc);
439 // Find = wrong sort, can not find
440 FindResult = dv.Find("3");
441 Assert.AreEqual(-1, FindResult , "DV47");
443 dv.Sort = "ParentId";
445 FindResult = dv.Find("3");
446 Assert.AreEqual(ExpectedResult, FindResult , "DV48");
449 [Test] public void Find_ByArray()
451 int FindResult,ExpectedResult=-1;
453 //create the source datatable
454 DataTable dt = DataProvider.CreateParentDataTable();
456 //create the dataview for the table
457 DataView dv = new DataView(dt);
459 for (int i=0; i<dt.Rows.Count ; i++)
461 if ((int)dt.Rows[i]["ParentId"] == 3 && dt.Rows[i]["String1"].ToString() == "3-String1")
468 // Find ,no sort - exception
471 FindResult = dv.Find(new object[] {"3","3-String1"});
472 Assert.Fail("DV49: Find Failed to throw ArgumentException");
474 catch (ArgumentException) {}
475 catch (AssertionException exc) {throw exc;}
476 catch (Exception exc)
478 Assert.Fail("DV50: Find. Wrong exception type. Got:" + exc);
481 dv.Sort = "String1,ParentId";
482 // Find = wrong sort, can not find
483 FindResult = dv.Find(new object[] {"3","3-String1"});
484 Assert.AreEqual(-1, FindResult , "DV51");
486 dv.Sort = "ParentId,String1";
488 FindResult = dv.Find(new object[] {"3","3-String1"});
489 Assert.AreEqual(ExpectedResult, FindResult , "DV52");
492 //Activate This Construntor to log All To Standard output
493 //public TestClass():base(true){}
495 //Activate this constructor to log Failures to a log file
496 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
498 //Activate this constructor to log All to a log file
499 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
501 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
503 [Test] public void GetEnumerator()
505 //create the source datatable
506 DataTable dt = DataProvider.CreateChildDataTable();
508 //create the dataview for the table
509 DataView dv = new DataView(dt);
511 System.Collections.IEnumerator ienm = null;
513 // GetEnumerator != null
514 ienm = dv.GetEnumerator();
515 Assert.AreEqual(true, ienm != null, "DV53");
518 while (ienm.MoveNext() )
521 Assert.AreEqual(dv[i], (DataRowView)ienm.Current , "DV54");
526 [Test] public void Item()
528 //create the source datatable
529 DataTable dt = DataProvider.CreateParentDataTable();
531 //create the dataview for the table
532 DataView dv = new DataView(dt);
535 Assert.AreEqual(dv[0].Row, dt.Rows[0] , "DV55");
538 Assert.AreEqual(dv[4].Row, dt.Rows[4] , "DV56");
540 dv.RowFilter="ParentId in (1,3,6)";
542 // DataView Item 0,DataTable with filter
543 Assert.AreEqual(dv[1].Row, dt.Rows[2] , "DV57");
546 [Test] public void ListChanged()
548 DataTable dt = DataProvider.CreateParentDataTable();
549 DataView dv = new DataView(dt);
552 dv.ListChanged +=new System.ComponentModel.ListChangedEventHandler(dv_ListChanged);
554 // ----- Change Value ---------
556 // change value - Event raised
557 dv[1]["String1"] = "something";
558 Assert.AreEqual(true , evProp!=null , "DV58");
559 // change value - ListChangedType
560 Assert.AreEqual(System.ComponentModel.ListChangedType.ItemChanged, evProp.lstType , "DV59");
561 // change value - NewIndex
562 Assert.AreEqual(1, evProp.NewIndex, "DV60");
563 // change value - OldIndex
564 Assert.AreEqual(-1, evProp.OldIndex , "DV61");
566 // ----- Add New ---------
568 // Add New - Event raised
570 Assert.AreEqual(true , evProp!=null , "DV62");
571 // Add New - ListChangedType
572 Assert.AreEqual(System.ComponentModel.ListChangedType.ItemAdded , evProp.lstType , "DV63");
573 // Add New - NewIndex
574 Assert.AreEqual(6, evProp.NewIndex, "DV64");
575 // Add New - OldIndex
576 Assert.AreEqual(-1, evProp.OldIndex , "DV65");
578 // ----- Sort ---------
580 // sort - Event raised
581 dv.Sort = "ParentId Desc";
582 Assert.AreEqual(true , evProp!=null , "DV66");
583 // sort - ListChangedType
584 Assert.AreEqual(System.ComponentModel.ListChangedType.Reset , evProp.lstType , "DV67");
586 Assert.AreEqual(-1, evProp.NewIndex, "DV68");
588 Assert.AreEqual(-1, evProp.OldIndex , "DV69");
590 //ListChangedType - this was not checked
592 //PropertyDescriptorAdded - A PropertyDescriptor was added, which changed the schema.
593 //PropertyDescriptorChanged - A PropertyDescriptor was changed, which changed the schema.
594 //PropertyDescriptorDeleted
598 public void AcceptChanges ()
601 DataTable dt = new DataTable ();
602 IBindingList list = dt.DefaultView;
603 list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
604 dt.Columns.Add ("test", typeof (int));
605 dt.Rows.Add (new object[] { 10 });
606 dt.Rows.Add (new object[] { 20 });
607 // ListChangedType.Reset
610 Assert.AreEqual(true , evProp != null , "DV166");
611 // AcceptChanges - should emit ListChangedType.Reset
612 Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV167");
616 public void ClearTable ()
619 DataTable dt = new DataTable ();
620 IBindingList list = dt.DefaultView;
621 list.ListChanged += new ListChangedEventHandler (dv_ListChanged);
622 dt.Columns.Add ("test", typeof (int));
623 dt.Rows.Add (new object[] { 10 });
624 dt.Rows.Add (new object[] { 20 });
628 Assert.AreEqual(true , evProp != null , "DV168");
629 // Clear DataTable - should emit ListChangedType.Reset
630 Assert.AreEqual(System.ComponentModel.ListChangedType.Reset , evProp.lstType , "DV169");
631 // Clear DataTable - should clear view count
632 Assert.AreEqual(0, dt.DefaultView.Count , "DV169");
635 private void dv_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
637 evProp = new EventProperties();
638 evProp.lstType = e.ListChangedType;
639 evProp.NewIndex = e.NewIndex;
640 evProp.OldIndex = e.OldIndex;
643 [Test] public void RowFilter()
645 //note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
646 // this test also check DataView.Count property
648 DataRowView[] drvResult = null;
649 System.Collections.ArrayList al = new System.Collections.ArrayList();
651 //create the source datatable
652 DataTable dt = DataProvider.CreateChildDataTable();
654 //create the dataview for the table
655 DataView dv = new DataView(dt);
657 //-------------------------------------------------------------
658 //Get excpected result
660 foreach (DataRow dr in dt.Rows )
662 if ((int)dr["ChildId"] == 1)
668 // RowFilter = 'ChildId=1', check count
669 dv.RowFilter = "ChildId=1";
670 Assert.AreEqual(al.Count , dv.Count , "DV70");
672 // RowFilter = 'ChildId=1', check rows
673 drvResult = new DataRowView[dv.Count];
674 dv.CopyTo(drvResult,0);
675 //check that the filterd rows exists
677 for (int i=0; i<drvResult.Length ; i++)
679 Succeed = al.Contains(drvResult[i].Row);
682 Assert.AreEqual(true, Succeed , "DV71");
683 //-------------------------------------------------------------
685 //-------------------------------------------------------------
686 //Get excpected result
688 foreach (DataRow dr in dt.Rows )
689 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" )
692 // RowFilter - ChildId=1 and String1='1-String1'
693 dv.RowFilter = "ChildId=1 and String1='1-String1'";
694 Assert.AreEqual(al.Count , dv.Count , "DV72");
696 // RowFilter = ChildId=1 and String1='1-String1', check rows
697 drvResult = new DataRowView[dv.Count];
698 dv.CopyTo(drvResult,0);
699 //check that the filterd rows exists
701 for (int i=0; i<drvResult.Length ; i++)
703 Succeed = al.Contains(drvResult[i].Row);
706 Assert.AreEqual(true, Succeed , "DV73");
707 //-------------------------------------------------------------
710 // RowFilter - check EvaluateException
713 dv.RowFilter = "Col=1";
714 Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
716 catch (EvaluateException) {}
717 catch (AssertionException exc) {throw exc;}
718 catch (Exception exc)
720 Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
723 //SyntaxErrorException 1
724 // RowFilter - check SyntaxErrorException 1
727 dv.RowFilter = "sum('something')";
728 Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
730 catch (SyntaxErrorException) {}
731 catch (AssertionException exc) {throw exc;}
732 catch (Exception exc)
734 Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
737 //SyntaxErrorException 2
738 // RowFilter - check SyntaxErrorException 2
741 dv.RowFilter = "HH**!";
742 Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
744 catch (SyntaxErrorException) {}
745 catch (AssertionException exc) {throw exc;}
746 catch (Exception exc)
748 Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
752 [Test] public void RowStateFilter()
756 CurrentRows Current rows including unchanged, new, and modified rows. 22
757 Deleted A deleted row. 8
758 ModifiedCurrent A current version, which is a modified version of original data (see ModifiedOriginal). 16
759 ModifiedOriginal The original version (although it has since been modified and is available as ModifiedCurrent). 32
761 OriginalRows Original rows including unchanged and deleted rows. 42
762 Unchanged An unchanged row. 2
765 //DataRowView[] drvResult = null;
766 System.Collections.ArrayList al = new System.Collections.ArrayList();
768 DataTable dt = DataProvider.CreateParentDataTable();
770 //create the dataview for the table
771 DataView dv = new DataView(dt);
776 dt.Rows[1]["ParentId"] = 1;
777 dt.Rows[2]["ParentId"] = 1;
779 dt.Rows.Add(new object[] {1,"A","B"});
780 dt.Rows.Add(new object[] {1,"C","D"});
781 dt.Rows.Add(new object[] {1,"E","F"});
783 //---------- Added --------
784 dv.RowStateFilter = DataViewRowState.Added ;
785 drResult = GetResultRows(dt,DataRowState.Added);
787 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV80");
789 //---------- CurrentRows --------
790 dv.RowStateFilter = DataViewRowState.CurrentRows ;
791 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified );
793 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV81");
795 //---------- ModifiedCurrent --------
796 dv.RowStateFilter = DataViewRowState.ModifiedCurrent ;
797 drResult = GetResultRows(dt,DataRowState.Modified );
799 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV82");
801 //---------- ModifiedOriginal --------
802 dv.RowStateFilter = DataViewRowState.ModifiedOriginal ;
803 drResult = GetResultRows(dt,DataRowState.Modified );
805 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV83");
807 //---------- Deleted --------
808 dv.RowStateFilter = DataViewRowState.Deleted ;
809 drResult = GetResultRows(dt,DataRowState.Deleted );
811 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV84");
813 //---------- OriginalRows --------
814 dv.RowStateFilter = DataViewRowState.OriginalRows ;
815 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Deleted );
817 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV85");
821 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
824 System.Collections.ArrayList al = new System.Collections.ArrayList();
825 DataRowVersion drVer = DataRowVersion.Current;
827 //From MSDN - The row the default version for the current DataRowState.
828 // For a DataRowState value of Added, Modified or Current,
829 // the default version is Current.
830 // For a DataRowState of Deleted, the version is Original.
831 // For a DataRowState value of Detached, the version is Proposed.
833 if ( ((State & DataRowState.Added) > 0)
834 | ((State & DataRowState.Modified) > 0)
835 | ((State & DataRowState.Unchanged) > 0) )
836 drVer = DataRowVersion.Current;
837 if ( (State & DataRowState.Deleted) > 0
838 | (State & DataRowState.Detached) > 0 )
839 drVer = DataRowVersion.Original;
841 foreach (DataRow dr in dt.Rows )
843 if ( dr.HasVersion(drVer)
844 //&& ((int)dr["ParentId", drVer] == 1)
845 && ((dr.RowState & State) > 0 )
849 DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
853 private bool CompareSortedRowsByParentId(DataView dv, DataRow[] drTable)
855 if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
857 //comparing the rows by using columns ParentId and ChildId
858 if ((dv.RowStateFilter & DataViewRowState.Deleted) > 0)
860 for (int i=0; i<dv.Count ; i++)
862 if (dv[i].Row["ParentId",DataRowVersion.Original ].ToString() != drTable[i]["ParentId",DataRowVersion.Original].ToString())
868 for (int i=0; i<dv.Count ; i++)
870 if (dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString())
877 [Test] public void Sort()
879 DataRow[] drArrTable;
881 //create the source datatable
882 DataTable dt = DataProvider.CreateChildDataTable();
884 //create the dataview for the table
885 DataView dv = new DataView(dt);
887 dv.Sort = "ParentId";
888 drArrTable = dt.Select("","ParentId");
890 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV86");
893 drArrTable = dt.Select("","ChildId");
895 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV87");
897 dv.Sort = "ParentId Desc, ChildId";
898 drArrTable = dt.Select("","ParentId Desc, ChildId");
899 // sort = ParentId Desc, ChildId
900 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV88");
902 dv.Sort = "ChildId Asc, ParentId";
903 drArrTable = dt.Select("","ChildId Asc, ParentId");
904 // sort = ChildId Asc, ParentId
905 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV89");
907 dv.Sort = "ChildId Asc, ChildId Desc";
908 drArrTable = dt.Select("","ChildId Asc, ChildId Desc");
909 // sort = ChildId Asc, ChildId Desc
910 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV90");
912 // IndexOutOfRangeException - 1
915 dv.Sort = "something";
916 Assert.Fail("DV91: Sort Failed to throw IndexOutOfRangeException");
918 catch (IndexOutOfRangeException) {}
919 catch (AssertionException exc) {throw exc;}
920 catch (Exception exc)
922 Assert.Fail("DV92: Sort. Wrong exception type. Got:" + exc);
925 // IndexOutOfRangeException - 2
928 dv.Sort = "ColumnId Desc Asc";
929 Assert.Fail("DV93: Sort Failed to throw IndexOutOfRangeException");
931 catch (IndexOutOfRangeException) {}
932 catch (AssertionException exc) {throw exc;}
933 catch (Exception exc)
935 Assert.Fail("DV94: Sort. Wrong exception type. Got:" + exc);
938 // IndexOutOfRangeException - 3
941 dv.Sort = "ColumnId blabla";
942 Assert.Fail("DV95: Sort Failed to throw IndexOutOfRangeException");
944 catch (IndexOutOfRangeException) {}
945 catch (AssertionException exc) {throw exc;}
946 catch (Exception exc)
948 Assert.Fail("DV96: Sort. Wrong exception type. Got:" + exc);
952 private bool CompareSortedRowsByParentAndChildId(DataView dv, DataRow[] drTable)
954 if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
956 //comparing the rows by using columns ParentId and ChildId
957 for (int i=0; i<dv.Count ; i++)
959 if ( dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString()
961 dv[i].Row["ChildId"].ToString() != drTable[i]["ChildId"].ToString())
967 [Test] public void Table()
969 DataTable dt = new DataTable();
970 DataView dv = new DataView();
973 Assert.AreEqual(null , dv.Table , "DV97");
975 // DataException - bind to table with no name
979 Assert.Fail("DV98: Table Failed to throw DataException");
981 catch (DataException) {}
982 catch (AssertionException exc) {throw exc;}
983 catch (Exception exc)
985 Assert.Fail("DV99: Table. Wrong exception type. Got:" + exc);
988 dt.TableName = "myTable";
991 Assert.AreEqual(dt, dv.Table , "DV100");
993 // assign null to DataTable
995 Assert.AreEqual(null, dv.Table , "DV101");
998 [Test] public void ctor_Empty()
1001 dv = new DataView();
1004 Assert.AreEqual(false, dv == null, "DV102");
1007 [Test] public void ctor_DataTable()
1010 DataTable dt = new DataTable("myTable");
1013 dv = new DataView(dt);
1014 Assert.AreEqual(false, dv == null, "DV103");
1017 Assert.AreEqual(dt , dv.Table , "DV104");
1020 [Test] public void ctor_ExpectedExceptions()
1023 DataTable dt = new DataTable("myTable");
1025 // ctor - missing column CutomerID Exception
1028 //exception: System.Data.EvaluateException: Cannot find column [CustomerId]
1029 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1030 Assert.Fail("DV105: DataView ctor Failed to throw EvaluateException or IndexOutOfRangeException");
1032 catch (EvaluateException) {}
1033 catch (IndexOutOfRangeException) {}
1034 catch (AssertionException exc) {throw exc;}
1035 catch (Exception exc)
1037 Assert.Fail("DV106: DataView ctor. Wrong exception type. Got:" + exc);
1040 dt.Columns.Add(new DataColumn("CustomerId"));
1042 // ctor - missing column Age Exception
1045 //exception: System.Data.EvaluateException: Cannot find column [Age]
1046 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1047 Assert.Fail("DV107: DataView ctor Failed to throw IndexOutOfRangeException");
1049 catch (IndexOutOfRangeException) {}
1050 catch (AssertionException exc) {throw exc;}
1051 catch (Exception exc)
1053 Assert.Fail("DV108: DataView ctor. Wrong exception type. Got:" + exc);
1057 [Test] public void ctor_Complex()
1060 DataTable dt = new DataTable("myTable");
1062 dt.Columns.Add(new DataColumn("CustomerId"));
1063 dt.Columns.Add(new DataColumn("Age"));
1066 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1067 Assert.AreEqual(false , dv == null , "DV109");
1070 Assert.AreEqual(dt , dv.Table , "DV110");
1073 Assert.AreEqual("CustomerId > 100" , dv.RowFilter , "DV111");
1076 Assert.AreEqual("Age" , dv.Sort, "DV112");
1078 // ctor - RowStateFilter
1079 Assert.AreEqual(DataViewRowState.Added , dv.RowStateFilter , "DV113");
1083 public void DataViewManager()
\r
1085 DataView dv = null;
\r
1086 DataViewManager dvm = null;
\r
1087 DataSet ds = new DataSet();
\r
1088 DataTable dt = new DataTable("myTable");
\r
1089 ds.Tables.Add(dt);
\r
1091 dv = dt.DefaultView;
\r
1093 // public DataViewManager DataViewManager {get;} - The DataViewManager that created this view.
\r
1094 // If this is the default DataView for a DataTable, the DataViewManager property returns the default DataViewManager for the DataSet.
\r
1095 // Otherwise, if the DataView was created without a DataViewManager, this property is a null reference (Nothing in Visual Basic).
\r
1097 dvm = dv.DataViewManager;
\r
1098 Assert.AreEqual(ds.DefaultViewManager,dvm, "DV114");
\r
1100 dv = new DataView(dt);
\r
1101 dvm = dv.DataViewManager;
\r
1102 Assert.AreEqual(null,dvm,"DV115");
\r
1104 dv = ds.DefaultViewManager.CreateDataView(dt);
\r
1105 Assert.AreEqual(ds.DefaultViewManager,dv.DataViewManager , "DV116");
\r
1109 public void DataView_ListChangedEventTest ()
1111 // Test DataView generates events, when datatable is directly modified
1113 DataTable table = new DataTable ("test");
1114 table.Columns.Add ("col1", typeof(int));
1116 DataView view = new DataView (table);
1118 view.ListChanged += new ListChangedEventHandler (dv_ListChanged);
1121 table.Rows.Add (new object[] {1});
1122 Assert.AreEqual (0, evProp.NewIndex, "#1");
1123 Assert.AreEqual (-1, evProp.OldIndex, "#2");
1124 Assert.AreEqual (ListChangedType.ItemAdded, evProp.lstType, "#3");
1127 table.Rows[0][0] = 5;
1128 Assert.AreEqual (0, evProp.NewIndex, "#4");
1129 Assert.AreEqual (-1, evProp.OldIndex, "#5");
1130 Assert.AreEqual (ListChangedType.ItemChanged, evProp.lstType, "#6");
1133 table.Rows.RemoveAt (0);
1134 Assert.AreEqual (0, evProp.NewIndex, "#7");
1135 Assert.AreEqual (-1, evProp.OldIndex, "#8");
1136 Assert.AreEqual (ListChangedType.ItemDeleted, evProp.lstType, "#9");
1139 Assert.AreEqual (-1, evProp.NewIndex, "#10");
1140 Assert.AreEqual (-1, evProp.OldIndex, "#11");
1141 Assert.AreEqual (ListChangedType.Reset, evProp.lstType, "#12");
1145 public void TestDefaultValues()
1147 DataView view = new DataView();
1148 Assert.IsFalse(view.ApplyDefaultSort, "#1");
1149 Assert.AreEqual ("", view.Sort, "#2");
1150 Assert.AreEqual("", view.RowFilter, "#3");
1151 Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1152 Assert.IsTrue(view.AllowDelete, "#5");
1153 Assert.IsTrue(view.AllowEdit, "#6");
1154 Assert.IsTrue(view.AllowNew, "#7");
1158 public void TestTableProperty()
1160 DataTable table = new DataTable("table");
1161 DataView view = new DataView();
1163 Assert.AreEqual("", view.Sort, "#1");
1164 Assert.AreEqual("", view.RowFilter, "#2");
1165 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 Assert.AreEqual ("'columnNames' argument cannot be null." + Environment.NewLine +
1299 "Parameter name: columnNames", e.Message, "#1");
1301 DataTable newTable1 = view.ToTable(false, new string[] { });
1302 Assert.AreEqual(10, newTable1.Rows.Count, "#3");
1304 newTable1 = view.ToTable(true, new string[] {});
1305 Assert.AreEqual(3, newTable1.Columns.Count, "#4");
1306 Assert.AreEqual(5, newTable1.Rows.Count, "#5");
1308 table.Rows.Add(new object[] { 1, 100, 100 });
1310 newTable1 = view.ToTable(true, new string[] {});
1311 Assert.AreEqual(3, newTable1.Columns.Count, "#6");
1312 Assert.AreEqual(6, newTable1.Rows.Count, "#7");
1314 newTable1 = view.ToTable(true, new string[] {"col1"});
1315 Assert.AreEqual(1, newTable1.Columns.Count, "#8");
1316 Assert.AreEqual(5, newTable1.Rows.Count, "#9");
1318 newTable1 = view.ToTable(true, new string[] { "col2", "col3"});
1319 Assert.AreEqual(2, newTable1.Columns.Count, "#10");
1320 Assert.AreEqual(6, newTable1.Rows.Count, "#11");
1322 for (int i = 0; i < newTable1.Rows.Count; ++i)
1323 Assert.AreEqual(DataRowState.Added, newTable1.Rows[i].RowState, "#11");
1325 view = new DataView (table, "col1>1", "col1 asc, col2 desc", DataViewRowState.Added);
1326 Assert.AreEqual (0, view.Count, "#12");
1328 newTable1 = view.ToTable (false, new string[] {"col1", "col3"});
1329 Assert.AreEqual (0, newTable1.Rows.Count, "#13");
1331 table.Rows.Add (new object[] {10, 1, 1});
1332 table.Rows.Add (new object[] {10, 1, 3});
1333 table.Rows.Add (new object[] {10, 1, 2});
1335 Assert.AreEqual (3, view.Count, "#14");
1336 view.Sort = "col1 asc, col2 asc, col3 desc";
1337 newTable1 = view.ToTable (true, new string[] {"col1", "col3"});
1338 Assert.AreEqual (3, newTable1.Rows.Count, "#14");
1339 Assert.AreEqual (3, newTable1.Rows [0][1], "#15");
1340 Assert.AreEqual (2, newTable1.Rows [1][1], "#16");
1341 Assert.AreEqual (1, newTable1.Rows [2][1], "#17");