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