2005-05-04 Sureshkumar T <tsureshkumar@novell.com>
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableTest.cs
1 // DataTableTest.cs - NUnit Test Cases for testing the DataTable 
2 //
3 // Authors:
4 //   Franklin Wise (gracenote@earthlink.net)
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 // 
7 // (C) Franklin Wise
8 // (C) 2003 Martin Willemoes Hansen
9 // 
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 using NUnit.Framework;
35 using System;
36 using System.Data;
37 using System.Globalization;
38 using System.IO;
39 using System.Runtime.Serialization.Formatters.Binary;
40 using System.Xml;
41
42 namespace MonoTests.System.Data
43 {
44         [TestFixture]
45         public class DataTableTest : Assertion
46         {
47                 [Test]
48                 public void Ctor()
49                 {
50                         DataTable dt = new DataTable();
51
52                         AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
53                         Assert("Col",dt.Columns != null);
54                         //Assert(dt.ChildRelations != null);
55                         Assert("Const", dt.Constraints != null);
56                         Assert("ds", dt.DataSet == null); 
57                         Assert("dv", dt.DefaultView != null);
58                         Assert("de", dt.DisplayExpression == "");
59                         Assert("ep", dt.ExtendedProperties != null);
60                         Assert("he", dt.HasErrors == false);
61                         Assert("lc", dt.Locale != null);
62                         Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
63                         Assert("ns", dt.Namespace == "");
64                         //Assert(dt.ParentRelations != null);
65                         Assert("pf", dt.Prefix == "");
66                         Assert("pk", dt.PrimaryKey != null);
67                         Assert("rows", dt.Rows != null);
68                         Assert("Site", dt.Site == null);
69                         Assert("tname", dt.TableName == "");
70                         
71                 }
72
73                 [Test]
74                 public void Select ()
75                 {
76                         DataSet Set = new DataSet ();
77                         DataTable Mom = new DataTable ("Mom");
78                         DataTable Child = new DataTable ("Child");                      
79                         Set.Tables.Add (Mom);
80                         Set.Tables.Add (Child);
81                         
82                         DataColumn Col = new DataColumn ("Name");
83                         DataColumn Col2 = new DataColumn ("ChildName");
84                         Mom.Columns.Add (Col);
85                         Mom.Columns.Add (Col2);
86                         
87                         DataColumn Col3 = new DataColumn ("Name");
88                         DataColumn Col4 = new DataColumn ("Age");
89                         Col4.DataType = Type.GetType ("System.Int16");
90                         Child.Columns.Add (Col3);
91                         Child.Columns.Add (Col4);
92                         
93                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
94                         Set.Relations.Add (Relation);
95
96                         DataRow Row = Mom.NewRow ();
97                         Row [0] = "Laura";
98                         Row [1] = "Nick";
99                         Mom.Rows.Add (Row);
100                         
101                         Row = Mom.NewRow ();
102                         Row [0] = "Laura";
103                         Row [1] = "Dick";
104                         Mom.Rows.Add (Row);
105                         
106                         Row = Mom.NewRow ();
107                         Row [0] = "Laura";
108                         Row [1] = "Mick";
109                         Mom.Rows.Add (Row);
110
111                         Row = Mom.NewRow ();
112                         Row [0] = "Teresa";
113                         Row [1] = "Jack";
114                         Mom.Rows.Add (Row);
115                         
116                         Row = Mom.NewRow ();
117                         Row [0] = "Teresa";
118                         Row [1] = "Mack";
119                         Mom.Rows.Add (Row);
120                         
121                         Row = Child.NewRow ();
122                         Row [0] = "Nick";
123                         Row [1] = 15;
124                         Child.Rows.Add (Row);
125                         
126                         Row = Child.NewRow ();
127                         Row [0] = "Dick";
128                         Row [1] = 25;
129                         Child.Rows.Add (Row);
130                         
131                         Row = Child.NewRow ();
132                         Row [0] = "Mick";
133                         Row [1] = 35;
134                         Child.Rows.Add (Row);
135                         
136                         Row = Child.NewRow ();
137                         Row [0] = "Jack";
138                         Row [1] = 10;
139                         Child.Rows.Add (Row);
140                         
141                         Row = Child.NewRow ();
142                         Row [0] = "Mack";
143                         Row [1] = 19;
144                         Child.Rows.Add (Row);
145                 
146                         Row = Child.NewRow ();
147                         Row [0] = "Mack";
148                         Row [1] = 99;
149                         Child.Rows.Add (Row);
150                         
151                         DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
152                         AssertEquals ("test#01", 2, Rows.Length);
153                         
154                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
155                         AssertEquals ("test#02", 0, Rows.Length);
156
157                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
158                         AssertEquals ("test#03", 1, Rows.Length);
159
160                         Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
161                         AssertEquals ("test#04", "Mack", Rows [0] [1]);
162                         
163                         Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
164                         AssertEquals ("test#05", 5, Rows.Length);
165                         
166                         Rows = Child.Select ("age = 20 - 1");
167                         AssertEquals ("test#06", 1, Rows.Length);
168                         
169                         Rows = Child.Select ("age <= 20");
170                         AssertEquals ("test#07", 3, Rows.Length);
171                         
172                         Rows = Child.Select ("age >= 20");
173                         AssertEquals ("test#08", 3, Rows.Length);
174                         
175                         Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
176                         AssertEquals ("test#09", 2, Rows.Length);
177
178                         Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
179                         AssertEquals ("test#10", 1, Rows.Length);
180                         AssertEquals ("test#11", "Mack", Rows [0] [0]);
181                         
182                         Rows = Child.Select ("not (Name = 'Jack')");
183                         AssertEquals ("test#12", 5, Rows.Length);
184                 }
185                 
186                 [Test]
187                 public void Select2 ()
188                 {
189                         DataSet Set = new DataSet ();
190                         DataTable Child = new DataTable ("Child");
191
192                         Set.Tables.Add (Child);
193                                                 
194                         DataColumn Col3 = new DataColumn ("Name");
195                         DataColumn Col4 = new DataColumn ("Age");
196                         Col4.DataType = Type.GetType ("System.Int16");
197                         Child.Columns.Add (Col3);
198                         Child.Columns.Add (Col4);
199                         
200                         DataRow Row = Child.NewRow ();
201                         Row [0] = "Nick";
202                         Row [1] = 15;
203                         Child.Rows.Add (Row);
204                         
205                         Row = Child.NewRow ();
206                         Row [0] = "Dick";
207                         Row [1] = 25;
208                         Child.Rows.Add (Row);
209                         
210                         Row = Child.NewRow ();
211                         Row [0] = "Mick";
212                         Row [1] = 35;
213                         Child.Rows.Add (Row);
214                         
215                         Row = Child.NewRow ();
216                         Row [0] = "Jack";
217                         Row [1] = 10;
218                         Child.Rows.Add (Row);
219                         
220                         Row = Child.NewRow ();
221                         Row [0] = "Mack";
222                         Row [1] = 19;
223                         Child.Rows.Add (Row);
224                 
225                         Row = Child.NewRow ();
226                         Row [0] = "Mack";
227                         Row [1] = 99;
228                         Child.Rows.Add (Row);
229
230                         DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
231                         AssertEquals ("test#01", 3, Rows.Length);
232                         AssertEquals ("test#02", "Mack", Rows [0] [0]);
233                         AssertEquals ("test#03", "Mick", Rows [1] [0]);                 
234                         AssertEquals ("test#04", "Dick", Rows [2] [0]);                 
235                         
236                         Rows = Child.Select ("age >= 20", "age asc");
237                         AssertEquals ("test#05", 3, Rows.Length);
238                         AssertEquals ("test#06", "Dick", Rows [0] [0]);
239                         AssertEquals ("test#07", "Mick", Rows [1] [0]);                 
240                         AssertEquals ("test#08", "Mack", Rows [2] [0]);                 
241                 
242                         Rows = Child.Select ("age >= 20", "name asc");
243                         AssertEquals ("test#09", 3, Rows.Length);
244                         AssertEquals ("test#10", "Dick", Rows [0] [0]);
245                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
246                         AssertEquals ("test#12", "Mick", Rows [2] [0]);                 
247
248                         Rows = Child.Select ("age >= 20", "name desc");
249                         AssertEquals ("test#09", 3, Rows.Length);
250                         AssertEquals ("test#10", "Mick", Rows [0] [0]);
251                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
252                         AssertEquals ("test#12", "Dick", Rows [2] [0]);                 
253
254                 }
255
256                 [Test]
257                 public void SelectParsing ()
258                 {
259                         DataTable T = new DataTable ("test");
260                         DataColumn C = new DataColumn ("name");
261                         T.Columns.Add (C);
262                         C = new DataColumn ("age");
263                         C.DataType = typeof (int);
264                         T.Columns.Add (C);
265                         C = new DataColumn ("id");
266                         T.Columns.Add (C);
267                         
268                         DataSet Set = new DataSet ("TestSet");
269                         Set.Tables.Add (T);
270                         
271                         DataRow Row = null;
272                         for (int i = 0; i < 100; i++) {
273                                 Row = T.NewRow ();
274                                 Row [0] = "human" + i;
275                                 Row [1] = i;
276                                 Row [2] = i;
277                                 T.Rows.Add (Row);
278                         }
279                         
280                         Row = T.NewRow ();
281                         Row [0] = "h*an";
282                         Row [1] = 1;
283                         Row [2] = 1;
284                         T.Rows.Add (Row);
285
286                         AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
287                         
288                         AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
289
290                         try {
291                                 T.Select ("name = 1human ");
292                                 Fail ("test#03");
293                         } catch (Exception e) {
294                                 
295                                 // missing operand after 'human' operand 
296                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());                          
297                         }
298                         
299                         try {                   
300                                 T.Select ("name = 1");
301                                 Fail ("test#05");
302                         } catch (Exception e) {
303                                 
304                                 // Cannot perform '=' operation between string and Int32
305                                 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
306                         }
307                         
308                         AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
309
310                 }
311
312                 [Test]
313                 public void SelectOperators ()
314                 {
315                         DataTable T = new DataTable ("test");
316                         DataColumn C = new DataColumn ("name");
317                         T.Columns.Add (C);
318                         C = new DataColumn ("age");
319                         C.DataType = typeof (int);
320                         T.Columns.Add (C);
321                         C = new DataColumn ("id");
322                         T.Columns.Add (C);
323                         
324                         DataSet Set = new DataSet ("TestSet");
325                         Set.Tables.Add (T);
326                         
327                         DataRow Row = null;
328                         for (int i = 0; i < 100; i++) {
329                                 Row = T.NewRow ();
330                                 Row [0] = "human" + i;
331                                 Row [1] = i;
332                                 Row [2] = i;
333                                 T.Rows.Add (Row);
334                         }
335                         
336                         Row = T.NewRow ();
337                         Row [0] = "h*an";
338                         Row [1] = 1;
339                         Row [2] = 1;
340                         T.Rows.Add (Row);
341                         
342                         AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
343                         AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);                    
344                         AssertEquals ("test#03", 12, T.Select ("age< =10").Length);                     
345                         AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
346                         AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);                    
347                         AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
348                         AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
349                         AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
350                         // FIXME: Somebody explain how this can be possible.
351                         // it seems that it is no matter between 10 - 30. The
352                         // result is allways 25 :-P
353                         //AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
354                         
355                 }
356
357                 [Test]
358                 public void SelectExceptions ()
359                 {
360                         DataTable T = new DataTable ("test");
361                         DataColumn C = new DataColumn ("name");
362                         T.Columns.Add (C);
363                         C = new DataColumn ("age");
364                         C.DataType = typeof (int);
365                         T.Columns.Add (C);
366                         C = new DataColumn ("id");
367                         T.Columns.Add (C);
368                         
369                         for (int i = 0; i < 100; i++) {
370                                 DataRow Row = T.NewRow ();
371                                 Row [0] = "human" + i;
372                                 Row [1] = i;
373                                 Row [2] = i;
374                                 T.Rows.Add (Row);
375                         }
376                         
377                         try {
378                                 T.Select ("name = human1");
379                                 Fail ("test#01");
380                         } catch (Exception e) {
381                                 
382                                 // column name human not found
383                                 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
384                         }
385                         
386                         AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
387                         AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
388                         
389                         try {
390                                 T.Select ("id = 1k3");
391                                 Fail ("test#06");
392                         } catch (Exception e) {
393                                 
394                                 // no operands after k3 operator
395                                 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
396                         }                                               
397                 }
398                 
399                 [Test]
400                 public void SelectStringOperators ()
401                 {
402                         DataTable T = new DataTable ("test");
403                         DataColumn C = new DataColumn ("name");
404                         T.Columns.Add (C);
405                         C = new DataColumn ("age");
406                         C.DataType = typeof (int);
407                         T.Columns.Add (C);
408                         C = new DataColumn ("id");
409                         T.Columns.Add (C);
410                         
411                         DataSet Set = new DataSet ("TestSet");
412                         Set.Tables.Add (T);
413                         
414                         DataRow Row = null;
415                         for (int i = 0; i < 100; i++) {
416                                 Row = T.NewRow ();
417                                 Row [0] = "human" + i;
418                                 Row [1] = i;
419                                 Row [2] = i;
420                                 T.Rows.Add (Row);
421                         }
422                         Row = T.NewRow ();
423                         Row [0] = "h*an";
424                         Row [1] = 1;
425                         Row [2] = 1;
426                         T.Rows.Add (Row);
427                                         
428                         AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
429                         
430                         AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);                       
431                         AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
432                         AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);                     
433                         AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
434                         AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
435                         
436                         AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
437                         
438                         Set.CaseSensitive = true;
439                         AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
440                         
441                         T.CaseSensitive = false;
442                         AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
443                         
444                         T.CaseSensitive = true;
445                         AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
446                         
447                         Set.CaseSensitive = false;
448                         AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
449                         
450                         T.CaseSensitive = false;
451                         AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
452                         
453                         AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
454                         AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
455                         AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
456                         
457                         try {
458                                 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
459                                 Fail ("test#16");
460                         } catch (Exception e) {
461                                 
462                                 // 'h*an1' is invalid
463                                 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
464                         }
465                         
466                         try {
467                                 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
468                                 Fail ("test#19");
469                         } catch (Exception e) {
470                                 
471                                 // 'h%an1' is invalid
472                                 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
473                         }
474                         
475                         AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
476                         AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
477                         
478                 }
479
480                 [Test]
481                 public void SelectAggregates ()
482                 {
483                         DataTable T = new DataTable ("test");
484                         DataColumn C = new DataColumn ("name");
485                         T.Columns.Add (C);
486                         C = new DataColumn ("age");
487                         C.DataType = typeof (int);
488                         T.Columns.Add (C);
489                         C = new DataColumn ("id");
490                         T.Columns.Add (C);
491                         DataRow Row = null;
492                         
493                         for (int i = 0; i < 1000; i++) {
494                                 Row = T.NewRow ();
495                                 Row [0] = "human" + i;
496                                 Row [1] = i;
497                                 Row [2] = i;
498                                 T.Rows.Add (Row);
499                         }
500                         
501                         AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
502                         AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
503                         AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
504                         AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
505                         AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
506                         AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
507                         AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
508                 }
509                 
510                 [Test]
511                 public void SelectFunctions ()
512                 {
513                         DataTable T = new DataTable ("test");
514                         DataColumn C = new DataColumn ("name");
515                         T.Columns.Add (C);
516                         C = new DataColumn ("age");
517                         C.DataType = typeof (int);
518                         T.Columns.Add (C);
519                         C = new DataColumn ("id");
520                         T.Columns.Add (C);
521                         DataRow Row = null;
522                         
523                         for (int i = 0; i < 1000; i++) {
524                                 Row = T.NewRow ();
525                                 Row [0] = "human" + i;
526                                 Row [1] = i;
527                                 Row [2] = i;
528                                 T.Rows.Add (Row);
529                         }
530                         
531                         Row = T.NewRow ();
532                         Row [0] = "human" + "test";
533                         Row [1] = DBNull.Value;
534                         Row [2] = DBNull.Value;
535                         T.Rows.Add (Row);
536
537                         //TODO: How to test Convert-function
538                         AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);                        
539                         AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
540                         AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
541                         AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);                   
542                         AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);                  
543                         AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
544                         AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
545                         AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
546                         AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
547                         AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
548                         
549                 }
550
551                 [Test]
552                 public void SelectRelations ()
553                 {
554                         DataSet Set = new DataSet ();
555                         DataTable Mom = new DataTable ("Mom");
556                         DataTable Child = new DataTable ("Child");
557
558                         Set.Tables.Add (Mom);
559                         Set.Tables.Add (Child);
560                         
561                         DataColumn Col = new DataColumn ("Name");
562                         DataColumn Col2 = new DataColumn ("ChildName");
563                         Mom.Columns.Add (Col);
564                         Mom.Columns.Add (Col2);
565                         
566                         DataColumn Col3 = new DataColumn ("Name");
567                         DataColumn Col4 = new DataColumn ("Age");
568                         Col4.DataType = Type.GetType ("System.Int16");
569                         Child.Columns.Add (Col3);
570                         Child.Columns.Add (Col4);
571                         
572                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
573                         Set.Relations.Add (Relation);
574
575                         DataRow Row = Mom.NewRow ();
576                         Row [0] = "Laura";
577                         Row [1] = "Nick";
578                         Mom.Rows.Add (Row);
579                         
580                         Row = Mom.NewRow ();
581                         Row [0] = "Laura";
582                         Row [1] = "Dick";
583                         Mom.Rows.Add (Row);
584                         
585                         Row = Mom.NewRow ();
586                         Row [0] = "Laura";
587                         Row [1] = "Mick";
588                         Mom.Rows.Add (Row);
589
590                         Row = Mom.NewRow ();
591                         Row [0] = "Teresa";
592                         Row [1] = "Jack";
593                         Mom.Rows.Add (Row);
594                         
595                         Row = Mom.NewRow ();
596                         Row [0] = "Teresa";
597                         Row [1] = "Mack";
598                         Mom.Rows.Add (Row);
599                         
600                         Row = Child.NewRow ();
601                         Row [0] = "Nick";
602                         Row [1] = 15;
603                         Child.Rows.Add (Row);
604                         
605                         Row = Child.NewRow ();
606                         Row [0] = "Dick";
607                         Row [1] = 25;
608                         Child.Rows.Add (Row);
609                         
610                         Row = Child.NewRow ();
611                         Row [0] = "Mick";
612                         Row [1] = 35;
613                         Child.Rows.Add (Row);
614                         
615                         Row = Child.NewRow ();
616                         Row [0] = "Jack";
617                         Row [1] = 10;
618                         Child.Rows.Add (Row);
619                         
620                         Row = Child.NewRow ();
621                         Row [0] = "Mack";
622                         Row [1] = 19;
623                         Child.Rows.Add (Row);
624                 
625                         Row = Child.NewRow ();
626                         Row [0] = "Mack";
627                         Row [1] = 99;
628                         Child.Rows.Add (Row);
629                         
630                         DataRow [] Rows = Child.Select ("name = Parent.Childname");
631                         AssertEquals ("test#01", 6, Rows.Length);
632                         Rows = Child.Select ("Parent.childname = 'Jack'");
633                         AssertEquals ("test#02", 1, Rows.Length);
634                         
635                         /*
636                         try {
637                                 // FIXME: LAMESPEC: Why the exception is thrown why... why... 
638                                 Mom.Select ("Child.Name = 'Jack'");
639                                 Fail ("test#03");
640                         } catch (Exception e) {
641                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
642                                 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
643                         }
644                         */
645                         
646                         Rows = Child.Select ("Parent.name = 'Laura'");
647                         AssertEquals ("test#06", 3, Rows.Length);
648                         
649                         DataTable Parent2 = new DataTable ("Parent2");
650                         Col = new DataColumn ("Name");
651                         Col2 = new DataColumn ("ChildName");
652
653                         Parent2.Columns.Add (Col);
654                         Parent2.Columns.Add (Col2);
655                         Set.Tables.Add (Parent2);
656                         
657                         Row = Parent2.NewRow ();
658                         Row [0] = "Laura";
659                         Row [1] = "Nick";
660                         Parent2.Rows.Add (Row);
661                         
662                         Row = Parent2.NewRow ();
663                         Row [0] = "Laura";
664                         Row [1] = "Dick";
665                         Parent2.Rows.Add (Row);
666                         
667                         Row = Parent2.NewRow ();
668                         Row [0] = "Laura";
669                         Row [1] = "Mick";
670                         Parent2.Rows.Add (Row);
671
672                         Row = Parent2.NewRow ();
673                         Row [0] = "Teresa";
674                         Row [1] = "Jack";
675                         Parent2.Rows.Add (Row);
676                         
677                         Row = Parent2.NewRow ();
678                         Row [0] = "Teresa";
679                         Row [1] = "Mack";
680                         Parent2.Rows.Add (Row);
681
682                         Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
683                         Set.Relations.Add (Relation);
684                         
685                         try {
686                                 Rows = Child.Select ("Parent.ChildName = 'Jack'");
687                                 Fail ("test#07");
688                         } catch (Exception e) {
689                                 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
690                                 //AssertEquals ("test#09", "The table [Child] involved in more than one relation. You must explicitly mention a relation name in the expression 'parent.[ChildName]'.", e.Message);
691                         }
692                         
693                         Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
694                         AssertEquals ("test#10", 1, Rows.Length);
695
696                         Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
697                         AssertEquals ("test#10", 1, Rows.Length);
698                         
699                         try {
700                                 Mom.Select ("Parent.name  = 'John'");
701                         } catch (Exception e) {
702                                 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
703                                 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
704                         }
705                         
706                 }
707
708                 [Test]
709                 public void ToStringTest()
710                 {
711                         DataTable dt = new DataTable();
712                         dt.Columns.Add("Col1",typeof(int));
713                         
714                         dt.TableName = "Mytable";
715                         dt.DisplayExpression = "Col1";
716                         
717                         
718                         string cmpr = dt.TableName + " + " + dt.DisplayExpression;
719                         AssertEquals(cmpr,dt.ToString());
720                 }
721
722                 [Test]
723                 public void PrimaryKey ()
724                 {
725                         DataTable dt = new DataTable ();
726                         DataColumn Col = new DataColumn ();
727                         Col.AllowDBNull = false;
728                         Col.DataType = typeof (int);
729                         dt.Columns.Add (Col);
730                         dt.Columns.Add ();
731                         dt.Columns.Add ();
732                         dt.Columns.Add ();
733                         
734                         AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
735                         
736                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
737                         AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
738                         AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
739                         
740                         dt.PrimaryKey = null;
741                         AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
742                         
743                         Col = new DataColumn ("failed");
744                         
745                         try {
746                                 dt.PrimaryKey = new DataColumn [] {Col};
747                                 Fail ("test#05");                                       
748                         } catch (Exception e) {
749                                 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
750                                 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
751                         }
752                         
753                         DataTable dt2 = new DataTable ();
754                         dt2.Columns.Add ();
755                         
756                         try {
757                                 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
758                                 Fail ("test#08");
759                         } catch (Exception e) {
760                                 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
761                                 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
762                         }
763                         
764                         
765                         AssertEquals ("test#11", 0, dt.Constraints.Count);
766                         
767                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
768                         AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
769                         AssertEquals ("test#13", 1, dt.Constraints.Count);
770                         AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
771                         AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
772                         AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
773                         
774                 }
775                 
776                 [Test]
777                 public void PropertyExceptions ()
778                 {
779                         DataSet set = new DataSet ();
780                         DataTable table = new DataTable ();
781                         DataTable table1 =  new DataTable ();
782                         set.Tables.Add (table);
783                         set.Tables.Add (table1);
784
785                         DataColumn col = new DataColumn ();
786                         col.ColumnName = "Id";
787                         col.DataType = Type.GetType ("System.Int32");
788                         table.Columns.Add (col);
789                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
790                         table.Constraints.Add (uc);
791                         table.CaseSensitive = false;
792                                                                                                                            
793                         col = new DataColumn ();
794                         col.ColumnName = "Name";
795                         col.DataType = Type.GetType ("System.String");
796                         table.Columns.Add (col);
797                         
798                         col = new DataColumn ();
799                         col.ColumnName = "Id";
800                         col.DataType = Type.GetType ("System.Int32");
801                         table1.Columns.Add (col);
802                         col = new DataColumn ();
803                         col.ColumnName = "Name";
804                         col.DataType = Type.GetType ("System.String");
805                         table1.Columns.Add (col);
806
807                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
808                         set.Relations.Add (dr);
809
810                         try {
811                                 table.CaseSensitive = true;
812                                 table1.CaseSensitive = true;
813                                 Fail ("#A01");
814                         }
815                         catch (Exception e) {
816                                 if (e.GetType () != typeof (AssertionException))
817                                         AssertEquals ("#A02", "Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.",e.Message);
818                                 else
819                                         Console.WriteLine (e);
820                         }
821                         try {
822                                 CultureInfo cultureInfo = new CultureInfo ("en-gb");
823                                 table.Locale = cultureInfo;
824                                 table1.Locale = cultureInfo;
825                                 Fail ("#A03");
826                         }
827                         catch (Exception e) {
828                                  if (e.GetType () != typeof (AssertionException))
829                                         AssertEquals ("#A04", "Cannot change CaseSensitive or Locale property. This change would lead to at least one DataRelation or Constraint to have different Locale or CaseSensitive settings between its related tables.",e.Message);
830                                 else
831                                         Console.WriteLine (e);
832                         }
833                         try {
834                                 table.Prefix = "Prefix#1";
835                                 Fail ("#A05");
836                         }
837                         catch (Exception e){
838                                 if (e.GetType () != typeof (AssertionException))
839                                         AssertEquals ("#A06", "Prefix 'Prefix#1' is not valid, because it contains special characters.",e.Message);
840                                 else
841                                         Console.WriteLine (e);
842
843                         }
844                 }
845
846                 [Test]
847                 public void GetErrors ()
848                 {
849                         DataTable table = new DataTable ();
850
851                         DataColumn col = new DataColumn ();
852                         col.ColumnName = "Id";
853                         col.DataType = Type.GetType ("System.Int32");
854                         table.Columns.Add (col);
855                                                                                                                              
856                         col = new DataColumn ();
857                         col.ColumnName = "Name";
858                         col.DataType = Type.GetType ("System.String");
859                         table.Columns.Add (col);
860                         
861                         DataRow row = table.NewRow ();
862                         row ["Id"] = 147;
863                         row ["name"] = "Abc";
864                         row.RowError = "Error#1";
865                         table.Rows.Add (row);
866
867                         AssertEquals ("#A01", 1, table.GetErrors ().Length);
868                         AssertEquals ("#A02", "Error#1", (table.GetErrors ())[0].RowError);
869                 }
870                 [Test]
871                 public void CloneCopyTest ()
872                 {
873                         DataTable table = new DataTable ();
874                         table.TableName = "Table#1";
875                         DataTable table1 = new DataTable ();
876                         table1.TableName = "Table#2";
877                 
878                         table.AcceptChanges ();
879                         
880                         DataSet set = new DataSet ("Data Set#1");
881                         set.DataSetName = "Dataset#1";
882                         set.Tables.Add (table);
883                         set.Tables.Add (table1);
884
885                         DataColumn col = new DataColumn ();
886                         col.ColumnName = "Id";
887                         col.DataType = Type.GetType ("System.Int32");
888                         table.Columns.Add (col);
889                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
890                         table.Constraints.Add (uc);
891                 
892                         col = new DataColumn ();
893                         col.ColumnName = "Id";
894                         col.DataType = Type.GetType ("System.Int32");
895                         table1.Columns.Add (col);
896                                                                                                                              
897                         col = new DataColumn ();
898                         col.ColumnName = "Name";
899                         col.DataType = Type.GetType ("System.String");
900                         table.Columns.Add (col);
901                         
902                         col = new DataColumn ();
903                         col.ColumnName = "Name";
904                         col.DataType = Type.GetType ("System.String");
905                         table1.Columns.Add (col);
906                         DataRow row = table.NewRow ();
907                         row ["Id"] = 147;
908                         row ["name"] = "Abc";
909                         row.RowError = "Error#1";
910                         table.Rows.Add (row);
911                                                                                                                              
912                         row = table.NewRow ();
913                         row ["Id"] = 47;
914                         row ["name"] = "Efg";
915                         table.Rows.Add (row);
916                         table.AcceptChanges ();
917                                                                                                                              
918                         table.CaseSensitive = true;
919                         table1.CaseSensitive = true;
920                         table.MinimumCapacity = 100;
921                         table.Prefix = "PrefixNo:1";
922                         table.Namespace = "Namespace#1";
923                         table.DisplayExpression = "Id / Name + (Id * Id)";
924                         DataColumn[] colArray = {table.Columns[0]};
925                         table.PrimaryKey = colArray;
926                         table.ExtendedProperties.Add ("TimeStamp", DateTime.Now);
927 #if NET_1_1 // This prevents further tests after .NET 1.1.
928 #else
929                         CultureInfo cultureInfo = new CultureInfo ("en-gb");
930                         table.Locale = cultureInfo;
931 #endif
932
933                         row = table1.NewRow ();
934                         row ["Name"] = "Abc";
935                         row ["Id"] = 147;
936                         table1.Rows.Add (row);
937
938                         row = table1.NewRow ();
939                         row ["Id"] = 47;
940                         row ["Name"] = "Efg";
941                         table1.Rows.Add (row);
942                 
943                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
944                         set.Relations.Add (dr);
945
946                         //Testing properties of clone
947                         DataTable cloneTable = table.Clone ();
948                         AssertEquals ("#A01",true ,cloneTable.CaseSensitive);
949                         AssertEquals ("#A02", 0 , cloneTable.ChildRelations.Count);
950                         AssertEquals ("#A03", 0 , cloneTable.ParentRelations.Count);
951                         AssertEquals ("#A04", 2,  cloneTable.Columns.Count);
952                         AssertEquals ("#A05", 1, cloneTable.Constraints.Count);
953                         AssertEquals ("#A06", "Id / Name + (Id * Id)", cloneTable.DisplayExpression);
954                         AssertEquals ("#A07", 1 ,cloneTable.ExtendedProperties.Count);
955                         AssertEquals ("#A08", false ,cloneTable.HasErrors);
956 #if NET_1_1
957 #else
958                         AssertEquals ("#A09", 2057, cloneTable.Locale.LCID);
959 #endif
960                         AssertEquals ("#A10", 100, cloneTable.MinimumCapacity);
961                         AssertEquals ("#A11","Namespace#1", cloneTable.Namespace);
962                         AssertEquals ("#A12", "PrefixNo:1",cloneTable.Prefix);
963                         AssertEquals ("#A13", "Id",  cloneTable.PrimaryKey[0].ColumnName);
964                         AssertEquals ("#A14",0 , cloneTable.Rows.Count );
965                         AssertEquals ("#A15", "Table#1", cloneTable.TableName);
966
967                         //Testing properties of copy
968                         DataTable copyTable = table.Copy ();
969                         AssertEquals ("#A16",true ,copyTable.CaseSensitive);
970                         AssertEquals ("#A17", 0 , copyTable.ChildRelations.Count);
971                         AssertEquals ("#A18", 0 , copyTable.ParentRelations.Count);
972                         AssertEquals ("#A19", 2,  copyTable.Columns.Count);
973                         AssertEquals ("#A20", 1, copyTable.Constraints.Count);
974                         AssertEquals ("#A21", "Id / Name + (Id * Id)", copyTable.DisplayExpression);
975                         AssertEquals ("#A22", 1 ,copyTable.ExtendedProperties.Count);
976                         AssertEquals ("#A23", true ,copyTable.HasErrors);
977 #if NET_1_1
978 #else
979                         AssertEquals ("#A24", 2057, copyTable.Locale.LCID);
980 #endif
981                         AssertEquals ("#A25", 100, copyTable.MinimumCapacity);
982                         AssertEquals ("#A26","Namespace#1", copyTable.Namespace);
983                         AssertEquals ("#A27", "PrefixNo:1",copyTable.Prefix);
984                         AssertEquals ("#A28", "Id",  copyTable.PrimaryKey[0].ColumnName);
985                         AssertEquals ("#A29", 2 , copyTable.Rows.Count );
986                         AssertEquals ("#A30", "Table#1", copyTable.TableName);
987                 }
988
989                 [Test]
990                 public void LoadDataException ()
991                 {
992                         DataTable table = new DataTable ();
993                         DataColumn col = new DataColumn ();
994                         col.ColumnName = "Id";
995                         col.DataType = Type.GetType ("System.Int32");
996                         col.DefaultValue = 47;
997                         table.Columns.Add (col);
998                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
999                         table.Constraints.Add (uc);
1000                 
1001                         col = new DataColumn ();
1002                         col.ColumnName = "Name";
1003                         col.DataType = Type.GetType ("System.String");
1004                         col.DefaultValue = "Hello";
1005                         table.Columns.Add (col);
1006                 
1007                         table.BeginLoadData();
1008                         object[] row = {147, "Abc"};
1009                         DataRow newRow = table.LoadDataRow (row, true);
1010                 
1011                         object[] row1 = {147, "Efg"};
1012                         DataRow newRow1 = table.LoadDataRow (row1, true);
1013                                                                                                                              
1014                         object[] row2 = {143, "Hij"};
1015                         DataRow newRow2 = table.LoadDataRow (row2, true);
1016                                                                                                                              
1017                         try {
1018                                 table.EndLoadData ();
1019                                 Fail ("#A01");
1020                         }
1021                         catch (ConstraintException) {
1022                         }
1023                 }
1024                 [Test]
1025                 public void Changes () //To test GetChanges and RejectChanges
1026                 {
1027                         DataTable table = new DataTable ();
1028
1029                         DataColumn col = new DataColumn ();
1030                         col.ColumnName = "Id";
1031                         col.DataType = Type.GetType ("System.Int32");
1032                         table.Columns.Add (col);
1033                         UniqueConstraint uc = new UniqueConstraint ("UK1", table.Columns[0] );
1034                         table.Constraints.Add (uc);
1035                                                                                                                              
1036                         col = new DataColumn ();
1037                         col.ColumnName = "Name";
1038                         col.DataType = Type.GetType ("System.String");
1039                         table.Columns.Add (col);                        
1040
1041                         DataRow row = table.NewRow ();
1042                         row ["Id"] = 147;
1043                         row ["name"] = "Abc";
1044                         table.Rows.Add (row);
1045                         table.AcceptChanges ();
1046                         
1047                         row = table.NewRow ();
1048                         row ["Id"] = 47;
1049                         row ["name"] = "Efg";
1050                         table.Rows.Add (row);
1051
1052                         //Testing GetChanges
1053                         DataTable changesTable = table.GetChanges ();
1054                         AssertEquals ("#A01", 1 ,changesTable.Rows.Count);
1055                         AssertEquals ("#A02","Efg" ,changesTable.Rows[0]["Name"]);                      
1056                         table.AcceptChanges ();
1057                         changesTable = table.GetChanges ();
1058                         try {
1059                                 int cnt = changesTable.Rows.Count;
1060                         }
1061                         catch(Exception e) {
1062                                 if (e.GetType () != typeof (AssertionException))
1063                                         AssertEquals ("#A03",typeof(NullReferenceException) ,e.GetType ());
1064                                 else
1065                                         Console.WriteLine (e);
1066                         }
1067                         
1068                         //Testing RejectChanges
1069                         row = table.NewRow ();
1070                         row ["Id"] = 247;
1071                         row ["name"] = "Hij";
1072                         table.Rows.Add (row);
1073
1074                         (table.Rows [0])["Name"] = "AaBbCc";
1075                         table.RejectChanges ();
1076                         AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
1077                         AssertEquals ("#A04", 2, table.Rows.Count);
1078                 }
1079                 [Test]
1080                 public void ImportRow ()
1081                 {
1082                         DataTable table = new DataTable ();
1083                         DataColumn col = new DataColumn ();
1084                         col.ColumnName = "Id";
1085                         col.DataType = Type.GetType ("System.Int32");
1086                         table.Columns.Add (col);
1087
1088                         col = new DataColumn ();
1089                         col.ColumnName = "Name";
1090                         col.DataType = Type.GetType ("System.String");
1091                         table.Columns.Add (col);
1092                         
1093                         DataRow row = table.NewRow ();
1094                         row ["Id"] = 147;
1095                         row ["name"] = "Abc";
1096                         table.Rows.Add (row);
1097                         table.AcceptChanges ();
1098                                                                                                      
1099                         row = table.NewRow ();
1100                         row ["Id"] = 47;
1101                         row ["name"] = "Efg";
1102                         table.Rows.Add (row);
1103
1104                         (table.Rows [0]) ["Name"] = "AaBbCc";
1105                 
1106                         table.ImportRow (table.Rows [0]);
1107                         table.ImportRow (table.Rows [1]);
1108
1109                         AssertEquals ("#A01", 147, table.Rows [2]["Id"]);
1110                         AssertEquals ("#A02", 47, table.Rows [3]["Id"]);
1111                         AssertEquals ("#A03", DataRowState.Modified, table.Rows [2].RowState);
1112                         AssertEquals ("#A04", DataRowState.Added, table.Rows [3].RowState);
1113                 }
1114
1115                 [Test]
1116                 public void ImportRowDetachedTest ()
1117                 {
1118                         DataTable table = new DataTable ();
1119                         DataColumn col = new DataColumn ();
1120                         col.ColumnName = "Id";
1121                         col.DataType = Type.GetType ("System.Int32");
1122                         table.Columns.Add (col);
1123
1124                         table.PrimaryKey = new DataColumn [] {col};
1125
1126                         col = new DataColumn ();
1127                         col.ColumnName = "Name";
1128                         col.DataType = Type.GetType ("System.String");
1129                         table.Columns.Add (col);
1130                         
1131                         DataRow row = table.NewRow ();
1132                         row ["Id"] = 147;
1133                         row ["name"] = "Abc";
1134
1135                         // keep silent as ms.net ;-), though this is not useful.
1136                         table.ImportRow (row); 
1137                 }
1138
1139                 [Test]
1140                 public void ClearReset () //To test Clear and Reset methods
1141                 {
1142                         DataTable table = new DataTable ("table");
1143                         DataTable table1 = new DataTable ("table1");
1144                 
1145                         DataSet set = new DataSet ();
1146                         set.Tables.Add (table);
1147                         set.Tables.Add (table1);
1148
1149                         table.Columns.Add ("Id", typeof (int));
1150                         table.Columns.Add ("Name", typeof (string));
1151                         table.Constraints.Add (new UniqueConstraint ("UK1", table.Columns [0]));
1152                         table.CaseSensitive = false;
1153                         
1154                         table1.Columns.Add ("Id", typeof (int));
1155                         table1.Columns.Add ("Name", typeof (string));
1156
1157                         DataRelation dr = new DataRelation ("DR", table.Columns[0], table1.Columns[0]);
1158                         set.Relations.Add (dr);
1159                 
1160                         DataRow row = table.NewRow ();
1161                         row ["Id"] = 147;
1162                         row ["name"] = "Roopa";
1163                         table.Rows.Add (row);
1164                 
1165                         row = table.NewRow ();
1166                         row ["Id"] = 47;
1167                         row ["Name"] = "roopa";
1168                         table.Rows.Add (row);
1169                 
1170                         AssertEquals (2, table.Rows.Count);
1171                         AssertEquals (1, table.ChildRelations.Count);
1172                         try {
1173                                 table.Reset ();
1174                                 Fail ("#A01, should have thrown ArgumentException");
1175                         }
1176                         catch (ArgumentException) {
1177                         }
1178                         AssertEquals ("#CT01", 0, table.Rows.Count);
1179                         AssertEquals ("#CT02", 0, table.ChildRelations.Count);
1180                         AssertEquals ("#CT03", 0, table.ParentRelations.Count);
1181                         AssertEquals ("#CT04", 0, table.Constraints.Count);
1182
1183                         table1.Reset ();
1184                         AssertEquals ("#A05", 0, table1.Rows.Count);
1185                         AssertEquals ("#A06", 0, table1.Constraints.Count);
1186                         AssertEquals ("#A07", 0, table1.ParentRelations.Count);
1187                 
1188                         // clear test
1189                         table.Clear ();
1190                         AssertEquals ("#A08", 0, table.Rows.Count);
1191 #if NET_1_1
1192                         AssertEquals ("#A09", 0, table.Constraints.Count);
1193 #else
1194                         AssertEquals ("#A09", 1, table.Constraints.Count);
1195 #endif
1196                         AssertEquals ("#A10", 0, table.ChildRelations.Count);
1197
1198                 }
1199
1200                 [Test]
1201                 public void ClearTest ()
1202                 {
1203                         DataTable table = new DataTable ("test");
1204                         table.Columns.Add ("id", typeof (int));
1205                         table.Columns.Add ("name", typeof (string));
1206                         
1207                         table.PrimaryKey = new DataColumn [] { table.Columns [0] } ;
1208                         
1209                         table.Rows.Add (new object [] { 1, "mono 1" });
1210                         table.Rows.Add (new object [] { 2, "mono 2" });
1211                         table.Rows.Add (new object [] { 3, "mono 3" });
1212                         table.Rows.Add (new object [] { 4, "mono 4" });
1213
1214                         table.AcceptChanges ();
1215 #if NET_2_0
1216                         _tableClearedEventFired = false;
1217                         table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
1218 #endif // NET_2_0
1219                         
1220                         table.Clear ();
1221 #if NET_2_0
1222                         AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
1223 #endif // NET_2_0
1224                         
1225                         DataRow r = table.Rows.Find (1);
1226                         AssertEquals ("#1 should have cleared", true, r == null);
1227
1228                         // try adding new row. indexes should have cleared
1229                         table.Rows.Add (new object [] { 2, "mono 2" });
1230                         AssertEquals ("#2 should add row", 1, table.Rows.Count);
1231                 }
1232 #if NET_2_0
1233                 private bool _tableClearedEventFired = false;
1234                 private void OnTableCleared (object src, DataTableClearEventArgs args)
1235                 {
1236                         _tableClearedEventFired = true;
1237                 }
1238 #endif // NET_2_0
1239                 
1240
1241                 [Test]
1242                 public void Serialize ()
1243                 {
1244                         MemoryStream fs = new MemoryStream ();
1245                         
1246                         // Construct a BinaryFormatter and use it 
1247                         // to serialize the data to the stream.
1248                         BinaryFormatter formatter = new BinaryFormatter();
1249                 
1250                         // Create an array with multiple elements refering to 
1251                         // the one Singleton object.
1252                         DataTable dt = new DataTable();
1253                 
1254                 
1255                         dt.Columns.Add(new DataColumn("Id", typeof(string)));
1256                         dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
1257                         dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
1258                         dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
1259                         dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
1260                 
1261                         DataRow loRowToAdd;
1262                         loRowToAdd = dt.NewRow();
1263                         loRowToAdd[0] = "a";
1264                         loRowToAdd[1] = "b";
1265                         loRowToAdd[2] = "c";
1266                         loRowToAdd[3] = "d";
1267                         loRowToAdd[4] = "e";
1268                                                 
1269                         dt.Rows.Add(loRowToAdd);
1270                 
1271                         DataTable[] dtarr = new DataTable[] {dt}; 
1272                 
1273                         // Serialize the array elements.
1274                         formatter.Serialize(fs, dtarr);
1275                 
1276                         // Deserialize the array elements.
1277                         fs.Position = 0;
1278                         DataTable[] a2 = (DataTable[]) formatter.Deserialize(fs);
1279                 
1280                         DataSet ds = new DataSet();
1281                         ds.Tables.Add(a2[0]);
1282                 
1283                         StringWriter sw = new StringWriter ();
1284                         ds.WriteXml(sw);
1285                         XmlDocument doc = new XmlDocument ();
1286                         doc.LoadXml (sw.ToString ());
1287                         AssertEquals (5, doc.DocumentElement.FirstChild.ChildNodes.Count);
1288                 }
1289
1290                 [Test]
1291                 [ExpectedException (typeof (DataException))]
1292                 [NUnit.Framework.Category ("NotWorking")]
1293                 public void SetPrimaryKeyAssertsNonNull ()
1294                 {
1295                         DataTable dt = new DataTable ("table");
1296                         dt.Columns.Add ("col1");
1297                         dt.Columns.Add ("col2");
1298                         dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1299                         dt.Rows.Add (new object [] {1, 3});
1300                         dt.Rows.Add (new object [] {DBNull.Value, 3});
1301
1302                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1303                 }
1304
1305                 [Test]
1306                 [ExpectedException (typeof (NoNullAllowedException))]
1307                 public void PrimaryKeyColumnChecksNonNull ()
1308                 {
1309                         DataTable dt = new DataTable ("table");
1310                         dt.Columns.Add ("col1");
1311                         dt.Columns.Add ("col2");
1312                         dt.Constraints.Add (new UniqueConstraint (dt.Columns [0]));
1313                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
1314                         dt.Rows.Add (new object [] {1, 3});
1315                         dt.Rows.Add (new object [] {DBNull.Value, 3});
1316                 }
1317
1318                 void RowChanging (object o, DataRowChangeEventArgs e)
1319                 {
1320                         AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
1321                         rowChangingRowChanging = true;
1322                 }
1323
1324                 void RowChanged (object o, DataRowChangeEventArgs e)
1325                 {
1326                         AssertEquals ("changed.Action", rowChangingExpectedAction, e.Action);
1327                         rowChangingRowChanged = true;
1328                 }
1329
1330                 bool rowChangingRowChanging, rowChangingRowChanged;
1331                 DataRowAction rowChangingExpectedAction;
1332
1333                 [Test]
1334                 public void RowChanging ()
1335                 {
1336                         DataTable dt = new DataTable ("table");
1337                         dt.Columns.Add ("col1");
1338                         dt.Columns.Add ("col2");
1339                         dt.RowChanging += new DataRowChangeEventHandler (RowChanging);
1340                         dt.RowChanged += new DataRowChangeEventHandler (RowChanged);
1341                         rowChangingExpectedAction = DataRowAction.Add;
1342                         dt.Rows.Add (new object [] {1, 2});
1343                         Assert ("changing,Added", rowChangingRowChanging);
1344                         Assert ("changed,Added", rowChangingRowChanged);
1345                         rowChangingExpectedAction = DataRowAction.Change;
1346                         dt.Rows [0] [0] = 2;
1347                         Assert ("changing,Changed", rowChangingRowChanging);
1348                         Assert ("changed,Changed", rowChangingRowChanged);
1349                 }
1350
1351                  [Test]
1352                 public void CloneSubClassTest()
1353                 {
1354                         MyDataTable dt1 = new MyDataTable();
1355                         MyDataTable dt = (MyDataTable)(dt1.Clone());
1356                         AssertEquals("A#01",2,MyDataTable.count);
1357                 }
1358
1359                 DataRowAction rowActionChanging = DataRowAction.Nothing;
1360                 DataRowAction rowActionChanged  = DataRowAction.Nothing;
1361                 [Test]
1362                 public void AcceptChangesTest ()
1363                 {
1364                         DataTable dt = new DataTable ("test");
1365                         dt.Columns.Add ("id", typeof (int));
1366                         dt.Columns.Add ("name", typeof (string));
1367                         
1368                         dt.Rows.Add (new object [] { 1, "mono 1" });
1369
1370                         dt.RowChanged  += new DataRowChangeEventHandler (OnRowChanged);
1371                         dt.RowChanging += new DataRowChangeEventHandler (OnRowChanging);
1372
1373                         try {
1374                                 rowActionChanged = rowActionChanging = DataRowAction.Nothing;
1375                                 dt.AcceptChanges ();
1376
1377                                 AssertEquals ("#1 should have fired event and set action to commit",
1378                                               DataRowAction.Commit, rowActionChanging);
1379                                 AssertEquals ("#2 should have fired event and set action to commit",
1380                                               DataRowAction.Commit, rowActionChanged);
1381
1382                         } finally {
1383                                 dt.RowChanged  -= new DataRowChangeEventHandler (OnRowChanged);
1384                                 dt.RowChanging -= new DataRowChangeEventHandler (OnRowChanging);
1385
1386                         }
1387                 }
1388
1389                 public void OnRowChanging (object src, DataRowChangeEventArgs args)
1390                 {
1391                         rowActionChanging = args.Action;
1392                 }
1393                 
1394                 public void OnRowChanged (object src, DataRowChangeEventArgs args)
1395                 {
1396                         rowActionChanged = args.Action;
1397                 }
1398                 
1399                                                                                                     
1400         }
1401                                                                                                     
1402                                                                                                     
1403          public  class MyDataTable:DataTable {
1404                                                                                                     
1405              public static int count = 0;
1406                                                                                                     
1407              public MyDataTable() {
1408                                                                                                     
1409                     count++;
1410              }
1411                                                                                                     
1412          }
1413
1414         
1415 }