Standardized Mainsoft System.Data tests except exception tests.
[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.Test.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                 private void dv_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
598                 {
599                         evProp = new EventProperties(); 
600                         evProp.lstType = e.ListChangedType;
601                         evProp.NewIndex = e.NewIndex;
602                         evProp.OldIndex = e.OldIndex; 
603                 }
604
605                 [Test] public void RowFilter()
606                 {
607                         //note: this test does not check all the possible row filter expression. this is done in DataTable.Select method.
608                         // this test also check DataView.Count property
609
610                         DataRowView[] drvResult = null;
611                         System.Collections.ArrayList al = new System.Collections.ArrayList();
612
613                         //create the source datatable
614                         DataTable dt = DataProvider.CreateChildDataTable();
615
616                         //create the dataview for the table
617                         DataView dv = new DataView(dt);
618
619                         //-------------------------------------------------------------
620                         //Get excpected result 
621                         al.Clear();
622                         foreach (DataRow dr in dt.Rows ) 
623                         {
624                                 if ((int)dr["ChildId"] == 1)
625                                 {
626                                         al.Add(dr);
627                                 }
628                         }
629
630                         // RowFilter = 'ChildId=1', check count
631                         dv.RowFilter = "ChildId=1";
632                         Assert.AreEqual(al.Count , dv.Count , "DV70");
633
634                         // RowFilter = 'ChildId=1', check rows
635                         drvResult = new DataRowView[dv.Count];
636                         dv.CopyTo(drvResult,0);
637                         //check that the filterd rows exists
638                         bool Succeed = true;
639                         for (int i=0; i<drvResult.Length ; i++)
640                         {
641                                 Succeed = al.Contains(drvResult[i].Row);
642                                 if (!Succeed) break;
643                         }
644                         Assert.AreEqual(true, Succeed , "DV71");
645                         //-------------------------------------------------------------
646
647                         //-------------------------------------------------------------
648                         //Get excpected result 
649                         al.Clear();
650                         foreach (DataRow dr in dt.Rows ) 
651                                 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" ) 
652                                         al.Add(dr);
653
654                         // RowFilter - ChildId=1 and String1='1-String1'
655                         dv.RowFilter = "ChildId=1 and String1='1-String1'";
656                         Assert.AreEqual(al.Count , dv.Count , "DV72");
657
658                         // RowFilter = ChildId=1 and String1='1-String1', check rows
659                         drvResult = new DataRowView[dv.Count];
660                         dv.CopyTo(drvResult,0);
661                         //check that the filterd rows exists
662                         Succeed = true;
663                         for (int i=0; i<drvResult.Length ; i++)
664                         {
665                                 Succeed = al.Contains(drvResult[i].Row);
666                                 if (!Succeed) break;
667                         }
668                         Assert.AreEqual(true, Succeed , "DV73");
669                         //-------------------------------------------------------------
670
671                         //EvaluateException
672                         // RowFilter - check EvaluateException
673                         try 
674                         {
675                                 dv.RowFilter = "Col=1";
676                                 Assert.Fail("DV74: RowFilter Failed to throw EvaluateException");
677                         }
678                         catch (EvaluateException) {}
679                         catch (AssertionException exc) {throw  exc;}
680                         catch (Exception exc)
681                         {
682                                 Assert.Fail("DV75: RowFilter. Wrong exception type. Got:" + exc);
683                         }
684
685                         //SyntaxErrorException 1
686                         // RowFilter - check SyntaxErrorException 1
687                         try 
688                         {
689                                 dv.RowFilter = "sum('something')";
690                                 Assert.Fail("DV76: RowFilter Failed to throw SyntaxErrorException");
691                         }
692                         catch (SyntaxErrorException) {}
693                         catch (AssertionException exc) {throw  exc;}
694                         catch (Exception exc)
695                         {
696                                 Assert.Fail("DV77: RowFilter. Wrong exception type. Got:" + exc);
697                         }
698
699                         //SyntaxErrorException 2
700                         // RowFilter - check SyntaxErrorException 2
701                         try 
702                         {
703                                 dv.RowFilter = "HH**!";
704                                 Assert.Fail("DV78: RowFilter Failed to throw SyntaxErrorException");
705                         }
706                         catch (SyntaxErrorException) {}
707                         catch (AssertionException exc) {throw  exc;}
708                         catch (Exception exc)
709                         {
710                                 Assert.Fail("DV79: RowFilter. Wrong exception type. Got:" + exc);
711                         }
712                 }
713
714                 [Test] public void RowStateFilter()
715                 {
716                         /*
717                                 Added                   A new row. 4 
718                                 CurrentRows             Current rows including unchanged, new, and modified rows. 22 
719                                 Deleted                 A deleted row. 8 
720                                 ModifiedCurrent A current version, which is a modified version of original data (see ModifiedOriginal). 16 
721                                 ModifiedOriginal The original version (although it has since been modified and is available as ModifiedCurrent). 32 
722                                 None                    None. 0 
723                                 OriginalRows    Original rows including unchanged and deleted rows. 42 
724                                 Unchanged               An unchanged row. 2 
725                          */
726
727                         //DataRowView[] drvResult = null;
728                         System.Collections.ArrayList al = new System.Collections.ArrayList();
729
730                         DataTable dt = DataProvider.CreateParentDataTable();
731
732                         //create the dataview for the table
733                         DataView dv = new DataView(dt);
734
735                         DataRow[]  drResult;
736
737                         dt.Rows[0].Delete();
738                         dt.Rows[1]["ParentId"] = 1;
739                         dt.Rows[2]["ParentId"] = 1;
740                         dt.Rows[3].Delete();
741                         dt.Rows.Add(new object[] {1,"A","B"});
742                         dt.Rows.Add(new object[] {1,"C","D"});
743                         dt.Rows.Add(new object[] {1,"E","F"});
744
745                         //---------- Added -------- 
746                         dv.RowStateFilter = DataViewRowState.Added ;
747                         drResult = GetResultRows(dt,DataRowState.Added);
748                         // Added
749                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV80");
750
751                         //---------- CurrentRows -------- 
752                         dv.RowStateFilter = DataViewRowState.CurrentRows ;
753                         drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added  | DataRowState.Modified );
754                         // CurrentRows
755                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV81");
756
757                         //---------- ModifiedCurrent -------- 
758                         dv.RowStateFilter = DataViewRowState.ModifiedCurrent  ;
759                         drResult = GetResultRows(dt,DataRowState.Modified );
760                         // ModifiedCurrent
761                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV82");
762
763                         //---------- ModifiedOriginal -------- 
764                         dv.RowStateFilter = DataViewRowState.ModifiedOriginal   ;
765                         drResult = GetResultRows(dt,DataRowState.Modified );
766                         // ModifiedOriginal
767                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult) , "DV83");
768
769                         //---------- Deleted -------- 
770                         dv.RowStateFilter = DataViewRowState.Deleted ;
771                         drResult = GetResultRows(dt,DataRowState.Deleted );
772                         // Deleted
773                         Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV84");
774                         /*
775                                         //---------- OriginalRows -------- 
776                                         dv.RowStateFilter = DataViewRowState.OriginalRows ;
777                                         drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Deleted );
778                                                 // OriginalRows
779                                                 Assert.AreEqual(true , CompareSortedRowsByParentId(dv,drResult), "DV85");
780                         */
781                 }
782
783                 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
784                 {
785                         //get expected rows
786                         System.Collections.ArrayList al = new System.Collections.ArrayList();
787                         DataRowVersion drVer = DataRowVersion.Current;
788
789                         //From MSDN -   The row the default version for the current DataRowState.
790                         //                              For a DataRowState value of Added, Modified or Current, 
791                         //                              the default version is Current. 
792                         //                              For a DataRowState of Deleted, the version is Original.
793                         //                              For a DataRowState value of Detached, the version is Proposed.
794
795                         if (    ((State & DataRowState.Added)           > 0)  
796                                 | ((State & DataRowState.Modified)      > 0)  
797                                 | ((State & DataRowState.Unchanged)     > 0) ) 
798                                 drVer = DataRowVersion.Current;
799                         if ( (State & DataRowState.Deleted)             > 0
800                                 | (State & DataRowState.Detached)       > 0 )  
801                                 drVer = DataRowVersion.Original; 
802
803                         foreach (DataRow dr in dt.Rows )
804                         {
805                                 if ( dr.HasVersion(drVer) 
806                                         //&& ((int)dr["ParentId", drVer] == 1) 
807                                         && ((dr.RowState & State) > 0 ) 
808                                         )
809                                         al.Add(dr);
810                         }
811                         DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
812                         return result; 
813                 }
814
815                 private bool CompareSortedRowsByParentId(DataView dv, DataRow[] drTable)
816                 {
817                         if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
818
819                         //comparing the rows by using columns ParentId and ChildId
820                         if ((dv.RowStateFilter & DataViewRowState.Deleted) > 0)
821                         {
822                                 for (int i=0; i<dv.Count ; i++)
823                                 {
824                                         if (dv[i].Row["ParentId",DataRowVersion.Original ].ToString() != drTable[i]["ParentId",DataRowVersion.Original].ToString()) 
825                                                 return false;
826                                 }
827                         }
828                         else
829                         {
830                                 for (int i=0; i<dv.Count ; i++)
831                                 {
832                                         if (dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString()) 
833                                                 return false;
834                                 }
835                         }
836                         return true;
837                 }
838
839                 [Test] public void Sort()
840                 {
841                         DataRow[] drArrTable;
842
843                         //create the source datatable
844                         DataTable dt = DataProvider.CreateChildDataTable();
845
846                         //create the dataview for the table
847                         DataView dv = new DataView(dt);
848
849                         dv.Sort = "ParentId";
850                         drArrTable = dt.Select("","ParentId");
851                         // sort = ParentId
852                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV86");
853
854                         dv.Sort = "ChildId";
855                         drArrTable = dt.Select("","ChildId");
856                         // sort = ChildId
857                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV87");
858
859                         dv.Sort = "ParentId Desc, ChildId";
860                         drArrTable = dt.Select("","ParentId Desc, ChildId");
861                         // sort = ParentId Desc, ChildId
862                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV88");
863
864                         dv.Sort = "ChildId Asc, ParentId";
865                         drArrTable = dt.Select("","ChildId Asc, ParentId");
866                         // sort = ChildId Asc, ParentId
867                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV89");
868
869                         dv.Sort = "ChildId Asc, ChildId Desc";
870                         drArrTable = dt.Select("","ChildId Asc, ChildId Desc");
871                         // sort = ChildId Asc, ChildId Desc
872                         Assert.AreEqual(true, CompareSortedRowsByParentAndChildId(dv,drArrTable), "DV90");
873
874                         // IndexOutOfRangeException - 1
875                         try 
876                         {
877                                 dv.Sort = "something";
878                                 Assert.Fail("DV91: Sort Failed to throw IndexOutOfRangeException");
879                         }
880                         catch (IndexOutOfRangeException) {}
881                         catch (AssertionException exc) {throw  exc;}
882                         catch (Exception exc)
883                         {
884                                 Assert.Fail("DV92: Sort. Wrong exception type. Got:" + exc);
885                         }
886
887                         // IndexOutOfRangeException - 2
888                         try 
889                         {
890                                 dv.Sort = "ColumnId Desc Asc";
891                                 Assert.Fail("DV93: Sort Failed to throw IndexOutOfRangeException");
892                         }
893                         catch (IndexOutOfRangeException) {}
894                         catch (AssertionException exc) {throw  exc;}
895                         catch (Exception exc)
896                         {
897                                 Assert.Fail("DV94: Sort. Wrong exception type. Got:" + exc);
898                         }
899
900                         // IndexOutOfRangeException - 3
901                         try 
902                         {
903                                 dv.Sort = "ColumnId blabla";
904                                 Assert.Fail("DV95: Sort Failed to throw IndexOutOfRangeException");
905                         }
906                         catch (IndexOutOfRangeException) {}
907                         catch (AssertionException exc) {throw  exc;}
908                         catch (Exception exc)
909                         {
910                                 Assert.Fail("DV96: Sort. Wrong exception type. Got:" + exc);
911                         }
912                 }
913
914                 private bool CompareSortedRowsByParentAndChildId(DataView dv, DataRow[] drTable)
915                 {
916                         if (dv.Count != drTable.Length) throw new Exception("DataRows[] length are different");
917
918                         //comparing the rows by using columns ParentId and ChildId
919                         for (int i=0; i<dv.Count ; i++)
920                         {
921                                 if (    dv[i].Row["ParentId"].ToString() != drTable[i]["ParentId"].ToString() 
922                                         && 
923                                         dv[i].Row["ChildId"].ToString() != drTable[i]["ChildId"].ToString())
924                                         return false;
925                         }
926                         return true;
927                 }
928
929                 [Test] public void Table()
930                 {
931                         DataTable dt = new DataTable();
932                         DataView dv = new DataView();
933
934                         // DataTable=null
935                         Assert.AreEqual(null , dv.Table , "DV97");
936
937                         // DataException - bind to table with no name
938                         try 
939                         {
940                                 dv.Table = dt;
941                                 Assert.Fail("DV98: Table Failed to throw DataException");
942                         }
943                         catch (DataException) {}
944                         catch (AssertionException exc) {throw  exc;}
945                         catch (Exception exc)
946                         {
947                                 Assert.Fail("DV99: Table. Wrong exception type. Got:" + exc);
948                         }
949
950                         dt.TableName = "myTable";
951                         // DataTable!=null
952                         dv.Table = dt;
953                         Assert.AreEqual(dt, dv.Table , "DV100");
954
955                         // assign null to DataTable
956                         dv.Table = null; 
957                         Assert.AreEqual(null, dv.Table , "DV101");
958                 }
959
960                 [Test] public void ctor_Empty()
961                 {
962                         DataView dv; 
963                         dv = new DataView();
964
965                         // ctor
966                         Assert.AreEqual(false, dv == null, "DV102");
967                 }
968
969                 [Test] public void ctor_DataTable()
970                 {
971                         DataView dv = null; 
972                         DataTable dt = new DataTable("myTable");
973
974                         // ctor
975                         dv = new DataView(dt);
976                         Assert.AreEqual(false, dv == null, "DV103");
977
978                         // ctor - table
979                         Assert.AreEqual(dt , dv.Table  , "DV104");
980                 }
981
982                 [Test] public void ctor_ExpectedExceptions()
983                 {
984                         DataView dv = null; 
985                         DataTable dt = new DataTable("myTable");
986
987                         // ctor - missing column CutomerID Exception
988                         try 
989                         {
990                                 //exception: System.Data.EvaluateException: Cannot find column [CustomerId]
991                                 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
992                                 Assert.Fail("DV105: DataView ctor Failed to throw EvaluateException or IndexOutOfRangeException");
993                         }
994                         catch (EvaluateException) {}
995                         catch (IndexOutOfRangeException) {}
996                         catch (AssertionException exc) {throw  exc;}
997                         catch (Exception exc)
998                         {
999                                 Assert.Fail("DV106: DataView ctor. Wrong exception type. Got:" + exc);
1000                         }
1001
1002                         dt.Columns.Add(new DataColumn("CustomerId"));
1003
1004                         // ctor - missing column Age Exception
1005                         try 
1006                         {
1007                                 //exception: System.Data.EvaluateException: Cannot find column [Age]
1008                                 dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1009                                 Assert.Fail("DV107: DataView ctor Failed to throw IndexOutOfRangeException");
1010                         }
1011                         catch (IndexOutOfRangeException) {}
1012                         catch (AssertionException exc) {throw  exc;}
1013                         catch (Exception exc)
1014                         {
1015                                 Assert.Fail("DV108: DataView ctor. Wrong exception type. Got:" + exc);
1016                         }
1017                 }
1018
1019                 [Test] public void ctor_Complex()
1020                 {
1021                         DataView dv = null; 
1022                         DataTable dt = new DataTable("myTable");
1023
1024                         dt.Columns.Add(new DataColumn("CustomerId"));
1025                         dt.Columns.Add(new DataColumn("Age"));
1026
1027                         // ctor
1028                         dv = new DataView(dt,"CustomerId > 100","Age",DataViewRowState.Added );
1029                         Assert.AreEqual(false , dv == null  , "DV109");
1030
1031                         // ctor - table
1032                         Assert.AreEqual(dt , dv.Table  , "DV110");
1033
1034                         // ctor - RowFilter
1035                         Assert.AreEqual("CustomerId > 100" , dv.RowFilter , "DV111");
1036
1037                         // ctor - Sort
1038                         Assert.AreEqual("Age" , dv.Sort, "DV112");
1039
1040                         // ctor - RowStateFilter 
1041                         Assert.AreEqual(DataViewRowState.Added , dv.RowStateFilter , "DV113");
1042                 }
1043         }
1044 }