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