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.Test.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
597 private void dv_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
599 evProp = new EventProperties();
600 evProp.lstType = e.ListChangedType;
601 evProp.NewIndex = e.NewIndex;
602 evProp.OldIndex = e.OldIndex;
605 [Test] public void RowFilter()
607 //note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
608 // this test also check DataView.Count property
610 DataRowView[] drvResult = null;
611 System.Collections.ArrayList al = new System.Collections.ArrayList();
613 //create the source datatable
614 DataTable dt = DataProvider.CreateChildDataTable();
616 //create the dataview for the table
617 DataView dv = new DataView(dt);
619 //-------------------------------------------------------------
620 //Get excpected result
622 foreach (DataRow dr in dt.Rows )
624 if ((int)dr["ChildId"] == 1)
630 // RowFilter = 'ChildId=1', check count
631 dv.RowFilter = "ChildId=1";
632 Assert.AreEqual(al.Count , dv.Count , "DV70");
634 // RowFilter = 'ChildId=1', check rows
635 drvResult = new DataRowView[dv.Count];
636 dv.CopyTo(drvResult,0);
637 //check that the filterd rows exists
639 for (int i=0; i<drvResult.Length ; i++)
641 Succeed = al.Contains(drvResult[i].Row);
644 Assert.AreEqual(true, Succeed , "DV71");
645 //-------------------------------------------------------------
647 //-------------------------------------------------------------
648 //Get excpected result
650 foreach (DataRow dr in dt.Rows )
651 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" )
654 // RowFilter - ChildId=1 and String1='1-String1'
655 dv.RowFilter = "ChildId=1 and String1='1-String1'";
656 Assert.AreEqual(al.Count , dv.Count , "DV72");
658 // RowFilter = ChildId=1 and String1='1-String1', check rows
659 drvResult = new DataRowView[dv.Count];
660 dv.CopyTo(drvResult,0);
661 //check that the filterd rows exists
663 for (int i=0; i<drvResult.Length ; i++)
665 Succeed = al.Contains(drvResult[i].Row);
668 Assert.AreEqual(true, Succeed , "DV73");
669 //-------------------------------------------------------------
672 // RowFilter - check EvaluateException
675 dv.RowFilter = "Col=1";
676 Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
678 catch (EvaluateException) {}
679 catch (AssertionException exc) {throw exc;}
680 catch (Exception exc)
682 Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
685 //SyntaxErrorException 1
686 // RowFilter - check SyntaxErrorException 1
689 dv.RowFilter = "sum('something')";
690 Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
692 catch (SyntaxErrorException) {}
693 catch (AssertionException exc) {throw exc;}
694 catch (Exception exc)
696 Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
699 //SyntaxErrorException 2
700 // RowFilter - check SyntaxErrorException 2
703 dv.RowFilter = "HH**!";
704 Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
706 catch (SyntaxErrorException) {}
707 catch (AssertionException exc) {throw exc;}
708 catch (Exception exc)
710 Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
714 [Test] public void RowStateFilter()
718 CurrentRows Current rows including unchanged, new, and modified rows. 22
719 Deleted A deleted row. 8
720 ModifiedCurrent A current version, which is a modified version of original data (see ModifiedOriginal). 16
721 ModifiedOriginal The original version (although it has since been modified and is available as ModifiedCurrent). 32
723 OriginalRows Original rows including unchanged and deleted rows. 42
724 Unchanged An unchanged row. 2
727 //DataRowView[] drvResult = null;
728 System.Collections.ArrayList al = new System.Collections.ArrayList();
730 DataTable dt = DataProvider.CreateParentDataTable();
732 //create the dataview for the table
733 DataView dv = new DataView(dt);
738 dt.Rows[1]["ParentId"] = 1;
739 dt.Rows[2]["ParentId"] = 1;
741 dt.Rows.Add(new object[] {1,"A","B"});
742 dt.Rows.Add(new object[] {1,"C","D"});
743 dt.Rows.Add(new object[] {1,"E","F"});
745 //---------- Added --------
746 dv.RowStateFilter = DataViewRowState.Added ;
747 drResult = GetResultRows(dt,DataRowState.Added);
749 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV80");
751 //---------- CurrentRows --------
752 dv.RowStateFilter = DataViewRowState.CurrentRows ;
753 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified );
755 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV81");
757 //---------- ModifiedCurrent --------
758 dv.RowStateFilter = DataViewRowState.ModifiedCurrent ;
759 drResult = GetResultRows(dt,DataRowState.Modified );
761 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV82");
763 //---------- ModifiedOriginal --------
764 dv.RowStateFilter = DataViewRowState.ModifiedOriginal ;
765 drResult = GetResultRows(dt,DataRowState.Modified );
767 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV83");
769 //---------- Deleted --------
770 dv.RowStateFilter = DataViewRowState.Deleted ;
771 drResult = GetResultRows(dt,DataRowState.Deleted );
773 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV84");
775 //---------- OriginalRows --------
776 dv.RowStateFilter = DataViewRowState.OriginalRows ;
777 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Deleted );
779 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV85");
783 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
786 System.Collections.ArrayList al = new System.Collections.ArrayList();
787 DataRowVersion drVer = DataRowVersion.Current;
789 //From MSDN - The row the default version for the current DataRowState.
790 // For a DataRowState value of Added, Modified or Current,
791 // the default version is Current.
792 // For a DataRowState of Deleted, the version is Original.
793 // For a DataRowState value of Detached, the version is Proposed.
795 if ( ((State & DataRowState.Added) > 0)
796 | ((State & DataRowState.Modified) > 0)
797 | ((State & DataRowState.Unchanged) > 0) )
798 drVer = DataRowVersion.Current;
799 if ( (State & DataRowState.Deleted) > 0
800 | (State & DataRowState.Detached) > 0 )
801 drVer = DataRowVersion.Original;
803 foreach (DataRow dr in dt.Rows )
805 if ( dr.HasVersion(drVer)
806 //&& ((int)dr["ParentId", drVer] == 1)
807 && ((dr.RowState & State) > 0 )
811 DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
815 private bool CompareSortedRowsByParentId(DataView dv, DataRow[] drTable)
817 if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
819 //comparing the rows by using columns ParentId and ChildId
820 if ((dv.RowStateFilter & DataViewRowState.Deleted) > 0)
822 for (int i=0; i<dv.Count ; i++)
824 if (dv[i].Row["ParentId",DataRowVersion.Original ].ToString() != drTable[i]["ParentId",DataRowVersion.Original].ToString())
830 for (int i=0; i<dv.Count ; i++)
832 if (dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString())
839 [Test] public void Sort()
841 DataRow[] drArrTable;
843 //create the source datatable
844 DataTable dt = DataProvider.CreateChildDataTable();
846 //create the dataview for the table
847 DataView dv = new DataView(dt);
849 dv.Sort = "ParentId";
850 drArrTable = dt.Select("","ParentId");
852 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV86");
855 drArrTable = dt.Select("","ChildId");
857 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV87");
859 dv.Sort = "ParentId Desc, ChildId";
860 drArrTable = dt.Select("","ParentId Desc, ChildId");
861 // sort = ParentId Desc, ChildId
862 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV88");
864 dv.Sort = "ChildId Asc, ParentId";
865 drArrTable = dt.Select("","ChildId Asc, ParentId");
866 // sort = ChildId Asc, ParentId
867 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV89");
869 dv.Sort = "ChildId Asc, ChildId Desc";
870 drArrTable = dt.Select("","ChildId Asc, ChildId Desc");
871 // sort = ChildId Asc, ChildId Desc
872 Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV90");
874 // IndexOutOfRangeException - 1
877 dv.Sort = "something";
878 Assert.Fail("DV91: Sort Failed to throw IndexOutOfRangeException");
880 catch (IndexOutOfRangeException) {}
881 catch (AssertionException exc) {throw exc;}
882 catch (Exception exc)
884 Assert.Fail("DV92: Sort. Wrong exception type. Got:" + exc);
887 // IndexOutOfRangeException - 2
890 dv.Sort = "ColumnId Desc Asc";
891 Assert.Fail("DV93: Sort Failed to throw IndexOutOfRangeException");
893 catch (IndexOutOfRangeException) {}
894 catch (AssertionException exc) {throw exc;}
895 catch (Exception exc)
897 Assert.Fail("DV94: Sort. Wrong exception type. Got:" + exc);
900 // IndexOutOfRangeException - 3
903 dv.Sort = "ColumnId blabla";
904 Assert.Fail("DV95: Sort Failed to throw IndexOutOfRangeException");
906 catch (IndexOutOfRangeException) {}
907 catch (AssertionException exc) {throw exc;}
908 catch (Exception exc)
910 Assert.Fail("DV96: Sort. Wrong exception type. Got:" + exc);
914 private bool CompareSortedRowsByParentAndChildId(DataView dv, DataRow[] drTable)
916 if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
918 //comparing the rows by using columns ParentId and ChildId
919 for (int i=0; i<dv.Count ; i++)
921 if ( dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString()
923 dv[i].Row["ChildId"].ToString() != drTable[i]["ChildId"].ToString())
929 [Test] public void Table()
931 DataTable dt = new DataTable();
932 DataView dv = new DataView();
935 Assert.AreEqual(null , dv.Table , "DV97");
937 // DataException - bind to table with no name
941 Assert.Fail("DV98: Table Failed to throw DataException");
943 catch (DataException) {}
944 catch (AssertionException exc) {throw exc;}
945 catch (Exception exc)
947 Assert.Fail("DV99: Table. Wrong exception type. Got:" + exc);
950 dt.TableName = "myTable";
953 Assert.AreEqual(dt, dv.Table , "DV100");
955 // assign null to DataTable
957 Assert.AreEqual(null, dv.Table , "DV101");
960 [Test] public void ctor_Empty()
966 Assert.AreEqual(false, dv == null, "DV102");
969 [Test] public void ctor_DataTable()
972 DataTable dt = new DataTable("myTable");
975 dv = new DataView(dt);
976 Assert.AreEqual(false, dv == null, "DV103");
979 Assert.AreEqual(dt , dv.Table , "DV104");
982 [Test] public void ctor_ExpectedExceptions()
985 DataTable dt = new DataTable("myTable");
987 // ctor - missing column CutomerID Exception
990 //exception: System.Data.EvaluateException: Cannot find column [CustomerId]
991 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
992 Assert.Fail("DV105: DataView ctor Failed to throw EvaluateException or IndexOutOfRangeException");
994 catch (EvaluateException) {}
995 catch (IndexOutOfRangeException) {}
996 catch (AssertionException exc) {throw exc;}
997 catch (Exception exc)
999 Assert.Fail("DV106: DataView ctor. Wrong exception type. Got:" + exc);
1002 dt.Columns.Add(new DataColumn("CustomerId"));
1004 // ctor - missing column Age Exception
1007 //exception: System.Data.EvaluateException: Cannot find column [Age]
1008 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1009 Assert.Fail("DV107: DataView ctor Failed to throw IndexOutOfRangeException");
1011 catch (IndexOutOfRangeException) {}
1012 catch (AssertionException exc) {throw exc;}
1013 catch (Exception exc)
1015 Assert.Fail("DV108: DataView ctor. Wrong exception type. Got:" + exc);
1019 [Test] public void ctor_Complex()
1022 DataTable dt = new DataTable("myTable");
1024 dt.Columns.Add(new DataColumn("CustomerId"));
1025 dt.Columns.Add(new DataColumn("Age"));
1028 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1029 Assert.AreEqual(false , dv == null , "DV109");
1032 Assert.AreEqual(dt , dv.Table , "DV110");
1035 Assert.AreEqual("CustomerId > 100" , dv.RowFilter , "DV111");
1038 Assert.AreEqual("Age" , dv.Sort, "DV112");
1040 // ctor - RowStateFilter
1041 Assert.AreEqual(DataViewRowState.Added , dv.RowStateFilter , "DV113");