Merge pull request #1624 from esdrubal/getprocesstimes
[mono.git] / mcs / class / System.Data / Test / System.Data / DataViewTest2.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using NUnit.Framework;
30 using System;
31 using System.Collections;
32 using System.ComponentModel;
33 using System.IO;
34 using System.Data;
35 using MonoTests.System.Data.Utils;
36
37 namespace MonoTests.System.Data
38 {
39         [TestFixture] public class DataViewTest2
40         {
41                 private EventProperties evProp = null;
42
43                 class EventProperties  //hold the event properties to be checked
44                 {
45                         public ListChangedType lstType ;
46                         public int NewIndex;
47                         public int OldIndex;
48                 }
49
50                 [Test] public void AddNew()
51                 {
52                         //create the source datatable
53                         DataTable dt = DataProvider.CreateChildDataTable();
54
55                         //create the dataview for the table
56                         DataView dv = new DataView(dt);
57
58                         int CountView = dv.Count ;
59                         int CountTable= dt.Rows.Count ;
60
61                         DataRowView drv = null;
62
63                         // AddNew - DataView Row Count
64                         drv = dv.AddNew();
65                         Assert.AreEqual(dv.Count , CountView+1, "DV1");
66
67                         // AddNew - Table Row Count 
68                         Assert.AreEqual(dt.Rows.Count , CountTable, "DV2");
69
70                         // AddNew - new row in DataTable
71                         drv.EndEdit();
72                         Assert.AreEqual(dt.Rows.Count , CountTable+1, "DV3");
73
74                         // AddNew - new row != null
75                         Assert.AreEqual(true, drv!=null, "DV4");
76
77                         // AddNew - check table
78                         Assert.AreEqual(dt, drv.Row.Table, "DV5");
79                 }
80
81                 [Test] public void AllowDelete()
82                 {
83                         DataTable dt = DataProvider.CreateParentDataTable();
84                         DataView dv = new DataView(dt);
85
86                         // AllowDelete - default value
87                         Assert.AreEqual(true , dv.AllowDelete , "DV6");
88
89                         // AllowDelete - true
90                         dv.AllowDelete = true;
91                         Assert.AreEqual(true, dv.AllowDelete , "DV7");
92
93                         // AllowDelete - false
94                         dv.AllowDelete = false;
95                         Assert.AreEqual(false, dv.AllowDelete , "DV8");
96
97                         dv.AllowDelete = false;
98                         // AllowDelete false- Exception
99                         try 
100                         {
101                                 dv.Delete(0);
102                                 Assert.Fail("DV9: Delete Failed to throw DataException");
103                         }
104                         catch (DataException) {}
105                         catch (AssertionException exc) {throw  exc;}
106                         catch (Exception exc)
107                         {
108                                 Assert.Fail("DV10: Delete. Wrong exception type. Got:" + exc);
109                         }
110
111                         dv.AllowDelete = true;
112                         int RowsCount = dv.Count ;
113                         // AllowDelete true- Exception
114                         dv.Delete(0);
115                         Assert.AreEqual(RowsCount-1, dv.Count , "DV11");
116                 }
117
118                 [Test] public void AllowEdit()
119                 {
120                         DataTable dt = DataProvider.CreateParentDataTable();
121                         DataView dv = new DataView(dt);
122
123                         // AllowEdit - default value
124                         Assert.AreEqual(true , dv.AllowEdit , "DV12");
125
126                         // AllowEdit - true
127                         dv.AllowEdit = true;
128                         Assert.AreEqual(true, dv.AllowEdit , "DV13");
129
130                         // AllowEdit - false
131                         dv.AllowEdit = false;
132                         Assert.AreEqual(false, dv.AllowEdit , "DV14");
133
134                         dv.AllowEdit=false;
135
136                         // AllowEdit false - exception
137                         try 
138                         {
139                                 dv[0][2] = "aaa";
140                                 Assert.Fail("DV15: Indexer Failed to throw DataException");
141                         }
142                         catch (DataException) {}
143                         catch (AssertionException exc) {throw  exc;}
144                         catch (Exception exc)
145                         {
146                                 Assert.Fail("DV16: Indexer. Wrong exception type. Got:" + exc);
147                         }
148
149                         dv.AllowEdit=true;
150
151                         // AllowEdit true- exception
152                         dv[0][2] = "aaa";
153                         Assert.AreEqual("aaa", dv[0][2] , "DV17");
154                 }
155
156                 [Test] public void AllowNew()
157                 {
158                         DataTable dt = DataProvider.CreateParentDataTable();
159                         DataView dv = new DataView(dt);
160
161                         // AllowNew - default value
162                         Assert.AreEqual(true , dv.AllowNew , "DV18");
163
164                         // AllowNew - true
165                         dv.AllowNew = true;
166                         Assert.AreEqual(true, dv.AllowNew , "DV19");
167
168                         // AllowNew - false
169                         dv.AllowNew = false;
170                         Assert.AreEqual(false, dv.AllowNew , "DV20");
171
172                         // AllowNew - exception
173                         try 
174                         {
175                                 dv.AddNew();
176                                 Assert.Fail("DV21: AddNew Failed to throw DataException");
177                         }
178                         catch (DataException) {}
179                         catch (AssertionException exc) {throw  exc;}
180                         catch (Exception exc)
181                         {
182                                 Assert.Fail("DV22: AddNew. Wrong exception type. Got:" + exc);
183                         }
184
185                         dv.AllowNew=true;
186                         int RowsCount = dv.Count ;
187
188                         // AllowNew - exception
189                         dv.AddNew();
190                         Assert.AreEqual(RowsCount+1, dv.Count , "DV23");
191                 }
192
193                 [Test] public void ApplyDefaultSort()
194                 {
195                         DataTable dt = DataProvider.CreateParentDataTable();
196                         DataView dv = new DataView(dt);
197
198                         // ApplyDefaultSort - default value
199                         Assert.AreEqual(false , dv.ApplyDefaultSort , "DV24");
200
201                         // ApplyDefaultSort - true
202                         dv.ApplyDefaultSort = true;
203                         Assert.AreEqual(true, dv.ApplyDefaultSort , "DV25");
204
205                         // ApplyDefaultSort - false
206                         dv.ApplyDefaultSort = false;
207                         Assert.AreEqual(false, dv.ApplyDefaultSort , "DV26");
208                 }
209
210                 [Test] public void CopyTo()
211                 {
212                         //create the source datatable
213                         DataTable dt = DataProvider.CreateChildDataTable();
214
215                         //create the dataview for the table
216                         DataView dv = new DataView(dt);
217
218                         DataRowView[] drvExpected = null;
219                         DataRowView[] drvResult = null;
220
221                         // ------- Copy from Index=0
222                         drvExpected = new DataRowView[dv.Count];
223                         for (int i=0; i < dv.Count ;i++)
224                         {
225                                 drvExpected[i] = dv[i];
226                         }
227
228                         drvResult = new DataRowView[dv.Count];
229                         // CopyTo from index 0
230                         dv.CopyTo(drvResult,0);
231                         Assert.AreEqual(drvResult, drvExpected , "DV27");
232
233                         // ------- Copy from Index=3
234                         drvExpected = new DataRowView[dv.Count+3];
235                         for (int i=0; i < dv.Count ;i++)
236                         {
237                                 drvExpected[i+3] = dv[i];
238                         }
239
240                         drvResult = new DataRowView[dv.Count+3];
241                         // CopyTo from index 3
242                         dv.CopyTo(drvResult,3);
243                         Assert.AreEqual(drvResult , drvExpected , "DV28");
244
245                         // ------- Copy from Index=3,larger array
246                         drvExpected = new DataRowView[dv.Count+9];
247                         for (int i=0; i < dv.Count ;i++)
248                         {
249                                 drvExpected[i+3] = dv[i];
250                         }
251
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");
256
257                         // ------- CopyTo smaller array, check exception
258                         drvResult = new DataRowView[dv.Count-1];
259
260                         // CopyTo smaller array, check exception
261                         try 
262                         {
263                                 dv.CopyTo(drvResult,0);
264                                 Assert.Fail("DV30: CopyTo Failed to throw IndexOutOfRangeException");
265                         }
266                         catch (IndexOutOfRangeException) {}
267                         catch (AssertionException exc) {throw  exc;}
268                         catch (Exception exc)
269                         {
270                                 Assert.Fail("DV31: CopyTo. Wrong exception type. Got:" + exc);
271                         }
272                 }
273
274                 [Test] public void Delete()
275                 {
276                         //create the source datatable
277                         DataTable dt = DataProvider.CreateChildDataTable();
278
279                         //create the dataview for the table
280                         DataView dv = new DataView(dt);
281
282                         int CountView = dv.Count ;
283                         int CountTable= dt.Rows.Count ;
284
285                         DataRowView drv = dv[0];
286
287                         // Delete - DataView Row Count
288                         dv.Delete(0);
289                         Assert.AreEqual(dv.Count , CountView-1, "DV32");
290
291                         // Delete - Table Row Count 
292                         Assert.AreEqual(dt.Rows.Count , CountTable, "DV33");
293
294                         // Delete - check table
295                         Assert.AreEqual(dt, drv.Row.Table, "DV34");
296                 }
297
298                 [Test] public void FindRows_ByKey()
299                 {
300                         DataRowView[] dvArr = null;
301
302                         //create the source datatable
303                         DataTable dt = DataProvider.CreateChildDataTable();
304
305                         //create the dataview for the table
306                         DataView dv = new DataView(dt);
307
308                         // FindRows ,no sort - exception
309                         try 
310                         {
311                                 dvArr = dv.FindRows(3);
312                                 Assert.Fail("DV35: FindRows Failed to throw ArgumentException");
313                         }
314                         catch (ArgumentException) {}
315                         catch (AssertionException exc) {throw  exc;}
316                         catch (Exception exc)
317                         {
318                                 Assert.Fail("DV36: FindRows. Wrong exception type. Got:" + exc);
319                         }
320
321                         dv.Sort = "String1";
322                         // Find = wrong sort, can not find
323                         dvArr = dv.FindRows(3);
324                         Assert.AreEqual(0, dvArr.Length  , "DV37");
325
326                         dv.Sort = "ChildId";
327
328                         //get expected results
329                         DataRow[] drExpected = dt.Select("ChildId=3");
330
331                         // FindRows - check count
332                         dvArr = dv.FindRows(3);
333                         Assert.AreEqual(drExpected.Length , dvArr.Length, "DV38");
334
335                         // FindRows - check data
336
337                         //check that result is ok
338                         bool Succeed = true;
339                         for (int i=0; i<dvArr.Length ; i++)
340                         {
341                                 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
342                                 if (!Succeed) break;
343                         }
344                         Assert.AreEqual(true, Succeed , "DV39");
345                 }
346
347                 [Test] public void FindRows_ByKeys()
348                 {
349                         DataRowView[] dvArr = null;
350
351                         //create the source datatable
352                         DataTable dt = DataProvider.CreateChildDataTable();
353
354                         //create the dataview for the table
355                         DataView dv = new DataView(dt);
356
357                         // FindRows ,no sort - exception
358                         try 
359                         {
360                                 dvArr = dv.FindRows(new object[] {"3","3-String1"});
361                                 Assert.Fail("DV40: FindRows Failed to throw ArgumentException");
362                         }
363                         catch (ArgumentException) {}
364                         catch (AssertionException exc) {throw  exc;}
365                         catch (Exception exc)
366                         {
367                                 Assert.Fail("DV41: FindRows. Wrong exception type. Got:" + exc);
368                         }
369
370                         dv.Sort = "String1,ChildId";
371                         // Find = wrong sort, can not find
372                         try 
373                         {
374                                 dvArr = dv.FindRows(new object[] {"3","3-String1"});
375                                 Assert.Fail("DV42: FindRows Failed to throw ArgumentException");
376                         }
377                         catch (FormatException) {}
378                         catch (AssertionException exc) {throw  exc;}
379                         catch (Exception exc)
380                         {
381                                 Assert.Fail("DV42-2: FindRows. Wrong exception type. Got:" + exc);
382                         }
383
384                         dv.Sort = "ChildId,String1";
385
386                         //get expected results
387                         DataRow[] drExpected = dt.Select("ChildId=3 and String1='3-String1'");
388
389                         // FindRows - check count
390                         dvArr = dv.FindRows(new object[] {"3","3-String1"});
391                         Assert.AreEqual(drExpected.Length , dvArr.Length, "DV43");
392
393                         // FindRows - check data
394
395                         //check that result is ok
396                         bool Succeed = true;
397                         for (int i=0; i<dvArr.Length ; i++)
398                         {
399                                 Succeed = (int)dvArr[i]["ChildId"] == (int)drExpected [i]["ChildId"];
400                                 if (!Succeed) break;
401                         }
402                         Assert.AreEqual(true, Succeed , "DV44");
403                 }
404
405                 //Activate This Construntor to log All To Standard output
406                 //public TestClass():base(true){}
407
408                 //Activate this constructor to log Failures to a log file
409                 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
410
411                 //Activate this constructor to log All to a log file
412                 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
413
414                 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
415
416                 [Test] public void Find_ByObject()
417                 {
418                         int FindResult,ExpectedResult=-1;
419
420                         //create the source datatable
421                         DataTable dt = DataProvider.CreateParentDataTable();
422
423                         //create the dataview for the table
424                         DataView dv = new DataView(dt);
425
426                         for (int i=0; i<dt.Rows.Count ; i++)
427                         {
428                                 if ((int)dt.Rows[i]["ParentId"] == 3)
429                                 {
430                                         ExpectedResult = i;
431                                         break;
432                                 }
433                         }
434
435                         // Find ,no sort - exception
436                         try 
437                         {
438                                 FindResult = dv.Find("3");
439                                 Assert.Fail("DV45: Find Failed to throw ArgumentException");
440                         }
441                         catch (ArgumentException) {}
442                         catch (AssertionException exc) {throw  exc;}
443                         catch (Exception exc)
444                         {
445                                 Assert.Fail("DV46: Find. Wrong exception type. Got:" + exc);
446                         }
447
448                         dv.Sort = "String1";
449                         // Find = wrong sort, can not find
450                         FindResult = dv.Find("3");
451                         Assert.AreEqual(-1, FindResult , "DV47");
452
453                         dv.Sort = "ParentId";
454                         // Find 
455                         FindResult = dv.Find("3");
456                         Assert.AreEqual(ExpectedResult, FindResult , "DV48");
457                 }
458
459                 [Test] public void Find_ByArray()
460                 {
461                         int FindResult,ExpectedResult=-1;
462
463                         //create the source datatable
464                         DataTable dt = DataProvider.CreateParentDataTable();
465
466                         //create the dataview for the table
467                         DataView dv = new DataView(dt);
468
469                         for (int i=0; i<dt.Rows.Count ; i++)
470                         {
471                                 if ((int)dt.Rows[i]["ParentId"] == 3 && dt.Rows[i]["String1"].ToString() == "3-String1")
472                                 {
473                                         ExpectedResult = i;
474                                         break;
475                                 }
476                         }
477
478                         // Find ,no sort - exception
479                         try 
480                         {
481                                 FindResult = dv.Find(new object[] {"3","3-String1"});
482                                 Assert.Fail("DV49: Find Failed to throw ArgumentException");
483                         }
484                         catch (ArgumentException) {}
485                         catch (AssertionException exc) {throw  exc;}
486                         catch (Exception exc)
487                         {
488                                 Assert.Fail("DV50: Find. Wrong exception type. Got:" + exc);
489                         }
490
491                         dv.Sort = "String1,ParentId";
492                         // Find = wrong sort, can not find
493                         try 
494                         {
495                                 FindResult = dv.Find(new object[] {"3","3-String1"});
496                                 Assert.Fail("DV51: Find Failed to throw ArgumentException");
497                         }
498                         catch (FormatException) {}
499                         catch (AssertionException exc) {throw  exc;}
500                         catch (Exception exc)
501                         {
502                                 Assert.Fail("DV51-2: Find. Wrong exception type. Got:" + exc);
503                         }
504
505                         dv.Sort = "ParentId,String1";
506                         // Find 
507                         FindResult = dv.Find(new object[] {"3","3-String1"});
508                         Assert.AreEqual(ExpectedResult, FindResult , "DV52");
509                 }
510
511                 //Activate This Construntor to log All To Standard output
512                 //public TestClass():base(true){}
513
514                 //Activate this constructor to log Failures to a log file
515                 //public TestClass(System.IO.TextWriter tw):base(tw, false){}
516
517                 //Activate this constructor to log All to a log file
518                 //public TestClass(System.IO.TextWriter tw):base(tw, true){}
519
520                 //BY DEFAULT LOGGING IS DONE TO THE STANDARD OUTPUT ONLY FOR FAILURES
521
522                 [Test] public void GetEnumerator()
523                 {
524                         //create the source datatable
525                         DataTable dt = DataProvider.CreateChildDataTable();
526
527                         //create the dataview for the table
528                         DataView dv = new DataView(dt);
529
530                         IEnumerator ienm = null;
531
532                         // GetEnumerator != null
533                         ienm = dv.GetEnumerator();
534                         Assert.AreEqual(true, ienm != null, "DV53");
535
536                         int i=0;
537                         while (ienm.MoveNext() )
538                         {
539                                 // Check item i
540                                 Assert.AreEqual(dv[i], (DataRowView)ienm.Current , "DV54");
541                                 i++;
542                         }
543                 }
544
545                 [Test] public void Item()
546                 {
547                         //create the source datatable
548                         DataTable dt = DataProvider.CreateParentDataTable();
549
550                         //create the dataview for the table
551                         DataView dv = new DataView(dt);
552
553                         // DataView Item 0
554                         Assert.AreEqual(dv[0].Row, dt.Rows[0] , "DV55");
555
556                         // DataView Item 4
557                         Assert.AreEqual(dv[4].Row, dt.Rows[4] , "DV56");
558
559                         dv.RowFilter="ParentId in (1,3,6)";
560
561                         // DataView Item 0,DataTable with filter
562                         Assert.AreEqual(dv[1].Row, dt.Rows[2] , "DV57");
563                 }
564
565                 [Test] public void ListChanged()
566                 {
567                         DataTable dt = DataProvider.CreateParentDataTable();
568                         DataView dv = new DataView(dt);
569
570                         //add event handler
571                         dv.ListChanged +=new ListChangedEventHandler(dv_ListChanged);
572
573                         // ----- Change Value ---------
574                         evProp = null;
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");
584
585                         // ----- Add New ---------
586                         evProp = null;
587                         // Add New  - Event raised
588                         dv.AddNew();
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");
596
597                         // ----- Sort ---------
598                         evProp = null;
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");
604                         // sort - NewIndex
605                         Assert.AreEqual(-1, evProp.NewIndex, "DV68");
606                         // sort - OldIndex
607                         Assert.AreEqual(-1, evProp.OldIndex , "DV69");
608
609                         //ListChangedType - this was not checked
610                         //Move
611                         //PropertyDescriptorAdded - A PropertyDescriptor was added, which changed the schema. 
612                         //PropertyDescriptorChanged - A PropertyDescriptor was changed, which changed the schema. 
613                         //PropertyDescriptorDeleted 
614                 }
615
616                 [Test]
617                 public void AcceptChanges ()
618                 {
619                         evProp = null;
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
627                         dt.AcceptChanges ();
628
629                         Assert.AreEqual(true , evProp != null , "DV166");
630                         // AcceptChanges - should emit ListChangedType.Reset
631                         Assert.AreEqual(ListChangedType.Reset , evProp.lstType , "DV167");
632                 }
633
634                 [Test]
635                 public void ClearTable ()
636                 {
637                         evProp = null;
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 });
644                         // Clears DataTable
645                         dt.Clear ();
646
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");
652                 }
653
654                 private void dv_ListChanged(object sender, ListChangedEventArgs e)
655                 {
656                         evProp = new EventProperties(); 
657                         evProp.lstType = e.ListChangedType;
658                         evProp.NewIndex = e.NewIndex;
659                         evProp.OldIndex = e.OldIndex; 
660                 }
661
662                 [Test] public void RowFilter()
663                 {
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
666
667                         DataRowView[] drvResult = null;
668                         ArrayList al = new ArrayList();
669
670                         //create the source datatable
671                         DataTable dt = DataProvider.CreateChildDataTable();
672
673                         //create the dataview for the table
674                         DataView dv = new DataView(dt);
675
676                         //-------------------------------------------------------------
677                         //Get excpected result 
678                         al.Clear();
679                         foreach (DataRow dr in dt.Rows ) 
680                         {
681                                 if ((int)dr["ChildId"] == 1)
682                                 {
683                                         al.Add(dr);
684                                 }
685                         }
686
687                         // RowFilter = 'ChildId=1', check count
688                         dv.RowFilter = "ChildId=1";
689                         Assert.AreEqual(al.Count , dv.Count , "DV70");
690
691                         // RowFilter = 'ChildId=1', check rows
692                         drvResult = new DataRowView[dv.Count];
693                         dv.CopyTo(drvResult,0);
694                         //check that the filterd rows exists
695                         bool Succeed = true;
696                         for (int i=0; i<drvResult.Length ; i++)
697                         {
698                                 Succeed = al.Contains(drvResult[i].Row);
699                                 if (!Succeed) break;
700                         }
701                         Assert.AreEqual(true, Succeed , "DV71");
702                         //-------------------------------------------------------------
703
704                         //-------------------------------------------------------------
705                         //Get excpected result 
706                         al.Clear();
707                         foreach (DataRow dr in dt.Rows ) 
708                                 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" ) 
709                                         al.Add(dr);
710
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");
714
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
719                         Succeed = true;
720                         for (int i=0; i<drvResult.Length ; i++)
721                         {
722                                 Succeed = al.Contains(drvResult[i].Row);
723                                 if (!Succeed) break;
724                         }
725                         Assert.AreEqual(true, Succeed , "DV73");
726                         //-------------------------------------------------------------
727
728                         //EvaluateException
729                         // RowFilter - check EvaluateException
730                         try 
731                         {
732                                 dv.RowFilter = "Col=1";
733                                 Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
734                         }
735                         catch (EvaluateException) {}
736                         catch (AssertionException exc) {throw  exc;}
737                         catch (Exception exc)
738                         {
739                                 Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
740                         }
741
742                         //SyntaxErrorException 1
743                         // RowFilter - check SyntaxErrorException 1
744                         try 
745                         {
746                                 dv.RowFilter = "sum('something')";
747                                 Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
748                         }
749                         catch (SyntaxErrorException) {}
750                         catch (AssertionException exc) {throw  exc;}
751                         catch (Exception exc)
752                         {
753                                 Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
754                         }
755
756                         //SyntaxErrorException 2
757                         // RowFilter - check SyntaxErrorException 2
758                         try 
759                         {
760                                 dv.RowFilter = "HH**!";
761                                 Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
762                         }
763                         catch (SyntaxErrorException) {}
764                         catch (AssertionException exc) {throw  exc;}
765                         catch (Exception exc)
766                         {
767                                 Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
768                         }
769                 }
770
771                 [Test] public void RowStateFilter()
772                 {
773                         /*
774                                 Added                   A new row. 4 
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 
779                                 None                    None. 0 
780                                 OriginalRows    Original rows including unchanged and deleted rows. 42 
781                                 Unchanged               An unchanged row. 2 
782                          */
783
784                         //DataRowView[] drvResult = null;
785                         ArrayList al = new ArrayList();
786
787                         DataTable dt = DataProvider.CreateParentDataTable();
788
789                         //create the dataview for the table
790                         DataView dv = new DataView(dt);
791
792                         DataRow[]  drResult;
793
794                         dt.Rows[0].Delete();
795                         dt.Rows[1]["ParentId"] = 1;
796                         dt.Rows[2]["ParentId"] = 1;
797                         dt.Rows[3].Delete();
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"});
801
802                         //---------- Added -------- 
803                         dv.RowStateFilter = DataViewRowState.Added ;
804                         drResult = GetResultRows(dt,DataRowState.Added);
805                         // Added
806                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV80");
807
808                         //---------- CurrentRows -------- 
809                         dv.RowStateFilter = DataViewRowState.CurrentRows ;
810                         drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added  | DataRowState.Modified );
811                         // CurrentRows
812                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV81");
813
814                         //---------- ModifiedCurrent -------- 
815                         dv.RowStateFilter = DataViewRowState.ModifiedCurrent  ;
816                         drResult = GetResultRows(dt,DataRowState.Modified );
817                         // ModifiedCurrent
818                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV82");
819
820                         //---------- ModifiedOriginal -------- 
821                         dv.RowStateFilter = DataViewRowState.ModifiedOriginal   ;
822                         drResult = GetResultRows(dt,DataRowState.Modified );
823                         // ModifiedOriginal
824                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV83");
825
826                         //---------- Deleted -------- 
827                         dv.RowStateFilter = DataViewRowState.Deleted ;
828                         drResult = GetResultRows(dt,DataRowState.Deleted );
829                         // Deleted
830                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV84");
831                         /*
832                                         //---------- OriginalRows -------- 
833                                         dv.RowStateFilter = DataViewRowState.OriginalRows ;
834                                         drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Deleted );
835                                                 // OriginalRows
836                                                 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV85");
837                         */
838                 }
839
840                 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
841                 {
842                         //get expected rows
843                         ArrayList al = new ArrayList();
844                         DataRowVersion drVer = DataRowVersion.Current;
845
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.
851
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; 
859
860                         foreach (DataRow dr in dt.Rows )
861                         {
862                                 if ( dr.HasVersion(drVer) 
863                                         //&& ((int)dr["ParentId", drVer] == 1) 
864                                         && ((dr.RowState & State) > 0 ) 
865                                         )
866                                         al.Add(dr);
867                         }
868                         DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
869                         return result; 
870                 }
871
872                 private bool CompareSortedRowsByParentId(DataView dv, DataRow[] drTable)
873                 {
874                         if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
875
876                         //comparing the rows by using columns ParentId and ChildId
877                         if ((dv.RowStateFilter & DataViewRowState.Deleted) > 0)
878                         {
879                                 for (int i=0; i<dv.Count ; i++)
880                                 {
881                                         if (dv[i].Row["ParentId",DataRowVersion.Original ].ToString() != drTable[i]["ParentId",DataRowVersion.Original].ToString()) 
882                                                 return false;
883                                 }
884                         }
885                         else
886                         {
887                                 for (int i=0; i<dv.Count ; i++)
888                                 {
889                                         if (dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString()) 
890                                                 return false;
891                                 }
892                         }
893                         return true;
894                 }
895
896                 [Test] public void Sort()
897                 {
898                         DataRow[] drArrTable;
899
900                         //create the source datatable
901                         DataTable dt = DataProvider.CreateChildDataTable();
902
903                         //create the dataview for the table
904                         DataView dv = new DataView(dt);
905
906                         dv.Sort = "ParentId";
907                         drArrTable = dt.Select("","ParentId");
908                         // sort = ParentId
909                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV86");
910
911                         dv.Sort = "ChildId";
912                         drArrTable = dt.Select("","ChildId");
913                         // sort = ChildId
914                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV87");
915
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");
920
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");
925
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");
930
931                         // IndexOutOfRangeException - 1
932                         try 
933                         {
934                                 dv.Sort = "something";
935                                 Assert.Fail("DV91: Sort Failed to throw IndexOutOfRangeException");
936                         }
937                         catch (IndexOutOfRangeException) {}
938                         catch (AssertionException exc) {throw  exc;}
939                         catch (Exception exc)
940                         {
941                                 Assert.Fail("DV92: Sort. Wrong exception type. Got:" + exc);
942                         }
943
944                         // IndexOutOfRangeException - 2
945                         try 
946                         {
947                                 dv.Sort = "ColumnId Desc Asc";
948                                 Assert.Fail("DV93: Sort Failed to throw IndexOutOfRangeException");
949                         }
950                         catch (IndexOutOfRangeException) {}
951                         catch (AssertionException exc) {throw  exc;}
952                         catch (Exception exc)
953                         {
954                                 Assert.Fail("DV94: Sort. Wrong exception type. Got:" + exc);
955                         }
956
957                         // IndexOutOfRangeException - 3
958                         try 
959                         {
960                                 dv.Sort = "ColumnId blabla";
961                                 Assert.Fail("DV95: Sort Failed to throw IndexOutOfRangeException");
962                         }
963                         catch (IndexOutOfRangeException) {}
964                         catch (AssertionException exc) {throw  exc;}
965                         catch (Exception exc)
966                         {
967                                 Assert.Fail("DV96: Sort. Wrong exception type. Got:" + exc);
968                         }
969                 }
970
971                 private bool CompareSortedRowsByParentAndChildId(DataView dv, DataRow[] drTable)
972                 {
973                         if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
974
975                         //comparing the rows by using columns ParentId and ChildId
976                         for (int i=0; i<dv.Count ; i++)
977                         {
978                                 if (    dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString() 
979                                         && 
980                                         dv[i].Row["ChildId"].ToString() != drTable[i]["ChildId"].ToString())
981                                         return false;
982                         }
983                         return true;
984                 }
985
986                 [Test] public void Table()
987                 {
988                         DataTable dt = new DataTable();
989                         DataView dv = new DataView();
990
991                         // DataTable=null
992                         Assert.AreEqual(null , dv.Table , "DV97");
993
994                         // DataException - bind to table with no name
995                         try 
996                         {
997                                 dv.Table = dt;
998                                 Assert.Fail("DV98: Table Failed to throw DataException");
999                         }
1000                         catch (DataException) {}
1001                         catch (AssertionException exc) {throw  exc;}
1002                         catch (Exception exc)
1003                         {
1004                                 Assert.Fail("DV99: Table. Wrong exception type. Got:" + exc);
1005                         }
1006
1007                         dt.TableName = "myTable";
1008                         // DataTable!=null
1009                         dv.Table = dt;
1010                         Assert.AreEqual(dt, dv.Table , "DV100");
1011
1012                         // assign null to DataTable
1013                         dv.Table = null; 
1014                         Assert.AreEqual(null, dv.Table , "DV101");
1015                 }
1016
1017                 [Test] public void ctor_Empty()
1018                 {
1019                         DataView dv; 
1020                         dv = new DataView();
1021
1022                         // ctor
1023                         Assert.AreEqual(false, dv == null, "DV102");
1024                 }
1025
1026                 [Test] public void ctor_DataTable()
1027                 {
1028                         DataView dv = null; 
1029                         DataTable dt = new DataTable("myTable");
1030
1031                         // ctor
1032                         dv = new DataView(dt);
1033                         Assert.AreEqual(false, dv == null, "DV103");
1034
1035                         // ctor - table
1036                         Assert.AreEqual(dt , dv.Table  , "DV104");
1037                 }
1038
1039                 [Test] public void ctor_ExpectedExceptions()
1040                 {
1041                         DataView dv = null; 
1042                         DataTable dt = new DataTable("myTable");
1043
1044                         // ctor - missing column CutomerID Exception
1045                         try 
1046                         {
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");
1050                         }
1051                         catch (EvaluateException) {}
1052                         catch (IndexOutOfRangeException) {}
1053                         catch (AssertionException exc) {throw  exc;}
1054                         catch (Exception exc)
1055                         {
1056                                 Assert.Fail("DV106: DataView ctor. Wrong exception type. Got:" + exc);
1057                         }
1058
1059                         dt.Columns.Add(new DataColumn("CustomerId"));
1060
1061                         // ctor - missing column Age Exception
1062                         try 
1063                         {
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");
1067                         }
1068                         catch (IndexOutOfRangeException) {}
1069                         catch (AssertionException exc) {throw  exc;}
1070                         catch (Exception exc)
1071                         {
1072                                 Assert.Fail("DV108: DataView ctor. Wrong exception type. Got:" + exc);
1073                         }
1074                 }
1075
1076                 [Test] public void ctor_Complex()
1077                 {
1078                         DataView dv = null; 
1079                         DataTable dt = new DataTable("myTable");
1080
1081                         dt.Columns.Add(new DataColumn("CustomerId"));
1082                         dt.Columns.Add(new DataColumn("Age"));
1083
1084                         // ctor
1085                         dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1086                         Assert.AreEqual(false , dv == null  , "DV109");
1087
1088                         // ctor - table
1089                         Assert.AreEqual(dt , dv.Table  , "DV110");
1090
1091                         // ctor - RowFilter
1092                         Assert.AreEqual("CustomerId > 100" , dv.RowFilter , "DV111");
1093
1094                         // ctor - Sort
1095                         Assert.AreEqual("Age" , dv.Sort, "DV112");
1096
1097                         // ctor - RowStateFilter 
1098                         Assert.AreEqual(DataViewRowState.Added , dv.RowStateFilter , "DV113");
1099                 }
1100
1101                 [Test]
1102                 public void DataViewManager()
1103                 {
1104                         DataView dv = null; 
1105                         DataViewManager dvm = null;
1106                         DataSet ds = new DataSet();
1107                         DataTable dt = new DataTable("myTable");
1108                         ds.Tables.Add(dt);
1109
1110                         dv = dt.DefaultView;
1111
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).
1115
1116                         dvm = dv.DataViewManager;
1117                         Assert.AreSame (ds.DefaultViewManager, dvm, "DV114");
1118
1119                         dv = new DataView(dt);
1120                         dvm = dv.DataViewManager;
1121                         Assert.IsNull (dvm, "DV115");
1122                         
1123                         dv = ds.DefaultViewManager.CreateDataView(dt);
1124                         Assert.AreSame (ds.DefaultViewManager, dv.DataViewManager , "DV116");
1125                 }
1126
1127                 [Test]
1128                 public void DataView_ListChangedEventTest ()
1129                 {
1130                         // Test DataView generates events, when datatable is directly modified
1131
1132                         DataTable table = new DataTable ("test");
1133                         table.Columns.Add ("col1", typeof(int));
1134                         
1135                         DataView view = new DataView (table);
1136                         
1137                         view.ListChanged += new ListChangedEventHandler (dv_ListChanged);
1138                         
1139                         evProp = null;
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");
1144
1145                         evProp = null;
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");
1150
1151                         evProp = null;
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");
1156
1157                         table.Rows.Clear();
1158                         Assert.AreEqual (-1, evProp.NewIndex, "#10");
1159                         Assert.AreEqual (-1, evProp.OldIndex, "#11");
1160                         Assert.AreEqual (ListChangedType.Reset, evProp.lstType, "#12");
1161                 }
1162
1163                 [Test]
1164                 public void TestDefaultValues()
1165                 {
1166                         DataView view = new DataView();
1167                         Assert.IsFalse(view.ApplyDefaultSort, "#1");
1168                         Assert.AreEqual ("", view.Sort, "#2");
1169                         Assert.AreEqual("", view.RowFilter, "#3");
1170                         Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1171                         Assert.IsTrue(view.AllowDelete, "#5");
1172                         Assert.IsTrue(view.AllowEdit, "#6");
1173                         Assert.IsTrue(view.AllowNew, "#7");
1174                 }
1175                 
1176                 [Test]
1177                 public void TestTableProperty()
1178                 {
1179                         DataTable table = new DataTable("table");
1180                         DataView view = new DataView();
1181                         view.Table = table;
1182                         Assert.AreEqual("", view.Sort, "#1");
1183                         Assert.AreEqual("", view.RowFilter, "#2");
1184                         Assert.AreEqual(DataViewRowState.CurrentRows, view.RowStateFilter, "#4");
1185                 }
1186
1187                 [Test]
1188                 public void TestEquals_SameTableDiffViewProp()
1189                 {
1190
1191                         DataTable table = new DataTable("table");
1192                         table.Columns.Add("col1", typeof(int));
1193                         table.Columns.Add("col2", typeof(int));
1194                         for (int i = 0; i < 5; ++i)
1195                                 table.Rows.Add(new object[] { i, 100 + i });
1196
1197                         DataView view1 = new DataView(table);
1198                         DataView view2 = new DataView(table);
1199
1200                         object obj2 = (object)view2;
1201                         Assert.IsFalse(view1.Equals(obj2), "#1");
1202
1203                         Assert.IsTrue(view1.Equals(view1), "#2");
1204                         Assert.IsTrue(view2.Equals(view1), "#3");
1205
1206                         view1.Sort = "col1 ASC";
1207                         Assert.IsFalse(view1.Equals(view2), "#4");
1208                         view2.Sort = "col1 ASC";
1209                         Assert.IsTrue(view1.Equals(view2), "#5");
1210
1211                         view1.RowFilter = "col1 > 100";
1212                         Assert.IsFalse(view1.Equals(view2), "#6");
1213                         view1.RowFilter = "";
1214                         Assert.IsTrue(view1.Equals(view2), "#7");
1215
1216                         view1.RowStateFilter = DataViewRowState.Added;
1217                         Assert.IsFalse(view1.Equals(view2), "#8");
1218                         view1.RowStateFilter = DataViewRowState.CurrentRows;
1219                         Assert.IsTrue(view1.Equals(view2), "#9");
1220
1221                         view1.AllowDelete = !view2.AllowDelete;
1222                         Assert.IsFalse(view1.Equals(view2), "#10");
1223                         view1.AllowDelete = view2.AllowDelete;
1224                         Assert.IsTrue(view1.Equals(view2), "#11");
1225
1226                         view1.AllowEdit = !view2.AllowEdit;
1227                         Assert.IsFalse(view1.Equals(view2), "#12");
1228                         view1.AllowEdit = view2.AllowEdit;
1229                         Assert.IsTrue(view1.Equals(view2), "#13");
1230
1231                         view1.AllowNew = !view2.AllowNew;
1232                         Assert.IsFalse(view1.Equals(view2), "#14");
1233                         view1.AllowNew = view2.AllowNew;
1234                         Assert.IsTrue(view1.Equals(view2), "#15");
1235
1236                         //ApplyDefaultSort doesnet affect the comparision
1237                         view1.ApplyDefaultSort = !view2.ApplyDefaultSort;
1238                         Assert.IsTrue(view1.Equals(view2), "#16");
1239
1240                         DataTable table2 = table.Copy();
1241                         view1.Table = table2;
1242                         Assert.IsFalse(view1.Equals(view2), "#17");
1243
1244                         view1.Table = table;
1245                         //well.. sort is set to null when Table is assigned..
1246                         view1.Sort = view2.Sort;          
1247                         Assert.IsTrue(view1.Equals(view2), "#18"); 
1248                 }
1249
1250                 [Test]
1251                 public void ToTable_SimpleTest()
1252                 {
1253                         DataSet ds = new DataSet();
1254                         ds.Tables.Add("table");
1255                         ds.Tables[0].Columns.Add("col1", typeof(int));
1256                         ds.Tables[0].Columns.Add("col2", typeof(int), "sum(col1)");
1257                         ds.Tables[0].Columns.Add("col3", typeof(int));
1258                         ds.Tables[0].Columns[2].AutoIncrement = true;
1259
1260                         ds.Tables[0].Rows.Add(new object[] { 1 });
1261                         ds.Tables[0].Rows.Add(new object[] { 2 });
1262
1263                         ds.Tables[0].PrimaryKey = new DataColumn[] { ds.Tables[0].Columns[0] };
1264                         
1265                         DataView view = new DataView(ds.Tables[0]);
1266                         DataTable table = view.ToTable();
1267                         
1268                         // The rule seems to be : Copy any col property that doesent
1269                         // involve/depend on other columns..
1270                         // Constraints and PrimaryKey info not copied over
1271                         Assert.AreEqual(0, table.PrimaryKey.Length, "#1");
1272                         Assert.AreEqual(0, table.Constraints.Count, "#2");
1273                         // AllowDBNull state is maintained by ms.net
1274                         Assert.IsFalse(table.Columns[0].AllowDBNull, "#3");
1275                         Assert.IsTrue(table.Columns[2].AllowDBNull, "#4");
1276                         // Expression is not copied over by ms.net
1277                         Assert.AreEqual("", table.Columns[1].Expression, "#5");
1278                         // AutoIncrement state is maintained by ms.net
1279                         Assert.IsTrue(table.Columns[2].AutoIncrement, "#6");
1280                         
1281                         Assert.IsFalse (ds.Tables[0] == table, "#7");
1282
1283                         Assert.AreEqual(ds.Tables [0].TableName, table.TableName, "#8");
1284                         Assert.AreEqual(ds.Tables [0].Columns.Count, table.Columns.Count, "#9 Col Count");
1285                         Assert.AreEqual(ds.Tables [0].Rows.Count, table.Rows.Count, "#10");
1286                         
1287                         for (int i = 0; i < table.Columns.Count; ++i) {
1288                                 Assert.AreEqual(ds.Tables [0].Columns[i].ColumnName, table.Columns[i].ColumnName, "10_"+i);
1289                                 Assert.AreEqual(ds.Tables [0].Columns[i].DataType, table.Columns[i].DataType, "11_"+i);
1290                                 for (int j = 0; j < table.Rows.Count; ++j)
1291                                         Assert.AreEqual(ds.Tables [0].Rows[j][i], table.Rows[j][i], "#12_"+i+"_"+j);
1292                         }
1293                         
1294                         DataTable table1 = view.ToTable("newtable");
1295                         Assert.AreEqual("newtable", table1.TableName, "#13");
1296                 }
1297                 
1298                 [Test]
1299                 public void ToTableTest_DataValidity ()
1300                 {              
1301                         DataTable table = new DataTable();
1302                         table.Columns.Add("col1", typeof(int));
1303                         table.Columns.Add("col2", typeof(int));
1304                         table.Columns.Add("col3", typeof(int));
1305                         
1306                         for (int i = 0; i < 5; ++i) {
1307                                 table.Rows.Add(new object[] { i, i + 1, i + 2 });
1308                                 table.Rows.Add(new object[] { i, i + 1, i + 2 });
1309                         }
1310                         
1311                         table.AcceptChanges();
1312                         DataView view = new DataView(table);
1313                         try {
1314                                 DataTable newTable = view.ToTable (false, null);
1315                         } catch (ArgumentNullException e) {
1316                                 // Never premise English.
1317                                 //Assert.AreEqual ("'columnNames' argument cannot be null." + Environment.NewLine + 
1318                                 //              "Parameter name: columnNames", e.Message, "#1");
1319                         }
1320                         DataTable newTable1 = view.ToTable(false, new string[] { });
1321                         Assert.AreEqual(10, newTable1.Rows.Count, "#3");
1322
1323                         newTable1 = view.ToTable(true, new string[] {});
1324                         Assert.AreEqual(3, newTable1.Columns.Count, "#4");
1325                         Assert.AreEqual(5, newTable1.Rows.Count, "#5");
1326                         
1327                         table.Rows.Add(new object[] { 1, 100, 100 });
1328
1329                         newTable1 = view.ToTable(true, new string[] {});
1330                         Assert.AreEqual(3, newTable1.Columns.Count, "#6");
1331                         Assert.AreEqual(6, newTable1.Rows.Count, "#7");
1332                         
1333                         newTable1 = view.ToTable(true, new string[] {"col1"});
1334                         Assert.AreEqual(1, newTable1.Columns.Count, "#8");
1335                         Assert.AreEqual(5, newTable1.Rows.Count, "#9");
1336                         
1337                         newTable1 = view.ToTable(true, new string[] { "col2", "col3"});
1338                         Assert.AreEqual(2, newTable1.Columns.Count, "#10");
1339                         Assert.AreEqual(6, newTable1.Rows.Count, "#11");
1340                         
1341                         for (int i = 0; i < newTable1.Rows.Count; ++i)
1342                                 Assert.AreEqual(DataRowState.Added, newTable1.Rows[i].RowState, "#11");
1343
1344                         view = new DataView (table, "col1>1", "col1 asc, col2 desc", DataViewRowState.Added);
1345                         Assert.AreEqual (0, view.Count, "#12");
1346
1347                         newTable1 = view.ToTable (false, new string[] {"col1", "col3"});
1348                         Assert.AreEqual (0, newTable1.Rows.Count, "#13");
1349                         
1350                         table.Rows.Add (new object[] {10, 1, 1});
1351                         table.Rows.Add (new object[] {10, 1, 3});
1352                         table.Rows.Add (new object[] {10, 1, 2});
1353
1354                         Assert.AreEqual (3, view.Count, "#14");
1355                         view.Sort = "col1 asc, col2 asc, col3 desc";
1356                         newTable1 =  view.ToTable (true, new string[] {"col1", "col3"});
1357                         Assert.AreEqual (3, newTable1.Rows.Count, "#14");
1358                         Assert.AreEqual (3, newTable1.Rows [0][1], "#15");
1359                         Assert.AreEqual (2, newTable1.Rows [1][1], "#16");
1360                         Assert.AreEqual (1, newTable1.Rows [2][1], "#17");
1361                 }
1362         }
1363 }