* DataViewTest.cs: Add some tests for new methods.
[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 using NUnit.Framework;
12 using System;
13 using System.Data;
14
15 namespace MonoTests.System.Data
16 {
17         [TestFixture]
18         public class DataTableTest : Assertion
19         {
20                 [Test]
21                 public void Ctor()
22                 {
23                         DataTable dt = new DataTable();
24
25                         AssertEquals("CaseSensitive must be false." ,false,dt.CaseSensitive);
26                         Assert("Col",dt.Columns != null);
27                         //Assert(dt.ChildRelations != null);
28                         Assert("Const", dt.Constraints != null);
29                         Assert("ds", dt.DataSet == null); 
30                         Assert("dv", dt.DefaultView != null);
31                         Assert("de", dt.DisplayExpression == "");
32                         Assert("ep", dt.ExtendedProperties != null);
33                         Assert("he", dt.HasErrors == false);
34                         Assert("lc", dt.Locale != null);
35                         Assert("mc", dt.MinimumCapacity == 50); //LAMESPEC:
36                         Assert("ns", dt.Namespace == "");
37                         //Assert(dt.ParentRelations != null);
38                         Assert("pf", dt.Prefix == "");
39                         Assert("pk", dt.PrimaryKey != null);
40                         Assert("rows", dt.Rows != null);
41                         Assert("Site", dt.Site == null);
42                         Assert("tname", dt.TableName == "");
43                         
44                 }
45
46                 [Test]
47                 public void Select ()
48                 {
49                         DataSet Set = new DataSet ();
50                         DataTable Mom = new DataTable ("Mom");
51                         DataTable Child = new DataTable ("Child");                      
52                         Set.Tables.Add (Mom);
53                         Set.Tables.Add (Child);
54                         
55                         DataColumn Col = new DataColumn ("Name");
56                         DataColumn Col2 = new DataColumn ("ChildName");
57                         Mom.Columns.Add (Col);
58                         Mom.Columns.Add (Col2);
59                         
60                         DataColumn Col3 = new DataColumn ("Name");
61                         DataColumn Col4 = new DataColumn ("Age");
62                         Col4.DataType = Type.GetType ("System.Int16");
63                         Child.Columns.Add (Col3);
64                         Child.Columns.Add (Col4);
65                         
66                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
67                         Set.Relations.Add (Relation);
68
69                         DataRow Row = Mom.NewRow ();
70                         Row [0] = "Laura";
71                         Row [1] = "Nick";
72                         Mom.Rows.Add (Row);
73                         
74                         Row = Mom.NewRow ();
75                         Row [0] = "Laura";
76                         Row [1] = "Dick";
77                         Mom.Rows.Add (Row);
78                         
79                         Row = Mom.NewRow ();
80                         Row [0] = "Laura";
81                         Row [1] = "Mick";
82                         Mom.Rows.Add (Row);
83
84                         Row = Mom.NewRow ();
85                         Row [0] = "Teresa";
86                         Row [1] = "Jack";
87                         Mom.Rows.Add (Row);
88                         
89                         Row = Mom.NewRow ();
90                         Row [0] = "Teresa";
91                         Row [1] = "Mack";
92                         Mom.Rows.Add (Row);
93                         
94                         Row = Child.NewRow ();
95                         Row [0] = "Nick";
96                         Row [1] = 15;
97                         Child.Rows.Add (Row);
98                         
99                         Row = Child.NewRow ();
100                         Row [0] = "Dick";
101                         Row [1] = 25;
102                         Child.Rows.Add (Row);
103                         
104                         Row = Child.NewRow ();
105                         Row [0] = "Mick";
106                         Row [1] = 35;
107                         Child.Rows.Add (Row);
108                         
109                         Row = Child.NewRow ();
110                         Row [0] = "Jack";
111                         Row [1] = 10;
112                         Child.Rows.Add (Row);
113                         
114                         Row = Child.NewRow ();
115                         Row [0] = "Mack";
116                         Row [1] = 19;
117                         Child.Rows.Add (Row);
118                 
119                         Row = Child.NewRow ();
120                         Row [0] = "Mack";
121                         Row [1] = 99;
122                         Child.Rows.Add (Row);
123                         
124                         DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
125                         AssertEquals ("test#01", 2, Rows.Length);
126                         
127                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
128                         AssertEquals ("test#02", 0, Rows.Length);
129
130                         Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Jack'");
131                         AssertEquals ("test#03", 1, Rows.Length);
132
133                         Rows = Mom.Select ("Name = 'Teresa' and ChildName <> 'Jack'");
134                         AssertEquals ("test#04", "Mack", Rows [0] [1]);
135                         
136                         Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
137                         AssertEquals ("test#05", 5, Rows.Length);
138                         
139                         Rows = Child.Select ("age = 20 - 1");
140                         AssertEquals ("test#06", 1, Rows.Length);
141                         
142                         Rows = Child.Select ("age <= 20");
143                         AssertEquals ("test#07", 3, Rows.Length);
144                         
145                         Rows = Child.Select ("age >= 20");
146                         AssertEquals ("test#08", 3, Rows.Length);
147                         
148                         Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
149                         AssertEquals ("test#09", 2, Rows.Length);
150
151                         Rows = Child.Select ("age >= 20 and (name = 'Mack' or name = 'Nick')");
152                         AssertEquals ("test#10", 1, Rows.Length);
153                         AssertEquals ("test#11", "Mack", Rows [0] [0]);
154                 }
155                 
156                 [Test]
157                 public void Select2 ()
158                 {
159                         DataSet Set = new DataSet ();
160                         DataTable Child = new DataTable ("Child");
161
162                         Set.Tables.Add (Child);
163                                                 
164                         DataColumn Col3 = new DataColumn ("Name");
165                         DataColumn Col4 = new DataColumn ("Age");
166                         Col4.DataType = Type.GetType ("System.Int16");
167                         Child.Columns.Add (Col3);
168                         Child.Columns.Add (Col4);
169                         
170                         DataRow Row = Child.NewRow ();
171                         Row [0] = "Nick";
172                         Row [1] = 15;
173                         Child.Rows.Add (Row);
174                         
175                         Row = Child.NewRow ();
176                         Row [0] = "Dick";
177                         Row [1] = 25;
178                         Child.Rows.Add (Row);
179                         
180                         Row = Child.NewRow ();
181                         Row [0] = "Mick";
182                         Row [1] = 35;
183                         Child.Rows.Add (Row);
184                         
185                         Row = Child.NewRow ();
186                         Row [0] = "Jack";
187                         Row [1] = 10;
188                         Child.Rows.Add (Row);
189                         
190                         Row = Child.NewRow ();
191                         Row [0] = "Mack";
192                         Row [1] = 19;
193                         Child.Rows.Add (Row);
194                 
195                         Row = Child.NewRow ();
196                         Row [0] = "Mack";
197                         Row [1] = 99;
198                         Child.Rows.Add (Row);
199
200                         DataRow [] Rows = Child.Select ("age >= 20", "age DESC");
201                         AssertEquals ("test#01", 3, Rows.Length);
202                         AssertEquals ("test#02", "Mack", Rows [0] [0]);
203                         AssertEquals ("test#03", "Mick", Rows [1] [0]);                 
204                         AssertEquals ("test#04", "Dick", Rows [2] [0]);                 
205                         
206                         Rows = Child.Select ("age >= 20", "age asc");
207                         AssertEquals ("test#05", 3, Rows.Length);
208                         AssertEquals ("test#06", "Dick", Rows [0] [0]);
209                         AssertEquals ("test#07", "Mick", Rows [1] [0]);                 
210                         AssertEquals ("test#08", "Mack", Rows [2] [0]);                 
211                 
212                         Rows = Child.Select ("age >= 20", "name asc");
213                         AssertEquals ("test#09", 3, Rows.Length);
214                         AssertEquals ("test#10", "Dick", Rows [0] [0]);
215                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
216                         AssertEquals ("test#12", "Mick", Rows [2] [0]);                 
217
218                         Rows = Child.Select ("age >= 20", "name desc");
219                         AssertEquals ("test#09", 3, Rows.Length);
220                         AssertEquals ("test#10", "Mick", Rows [0] [0]);
221                         AssertEquals ("test#11", "Mack", Rows [1] [0]);                 
222                         AssertEquals ("test#12", "Dick", Rows [2] [0]);                 
223
224                 }
225
226                 [Test]
227                 public void SelectParsing ()
228                 {
229                         DataTable T = new DataTable ("test");
230                         DataColumn C = new DataColumn ("name");
231                         T.Columns.Add (C);
232                         C = new DataColumn ("age");
233                         C.DataType = typeof (int);
234                         T.Columns.Add (C);
235                         C = new DataColumn ("id");
236                         T.Columns.Add (C);
237                         
238                         DataSet Set = new DataSet ("TestSet");
239                         Set.Tables.Add (T);
240                         
241                         DataRow Row = null;
242                         for (int i = 0; i < 100; i++) {
243                                 Row = T.NewRow ();
244                                 Row [0] = "human" + i;
245                                 Row [1] = i;
246                                 Row [2] = i;
247                                 T.Rows.Add (Row);
248                         }
249                         
250                         Row = T.NewRow ();
251                         Row [0] = "h*an";
252                         Row [1] = 1;
253                         Row [2] = 1;
254                         T.Rows.Add (Row);
255
256                         AssertEquals ("test#01", 12, T.Select ("age<=10").Length);
257                         
258                         AssertEquals ("test#02", 12, T.Select ("age\n\t<\n\t=\t\n10").Length);
259
260                         try {
261                                 T.Select ("name = 1human ");
262                                 Fail ("test#03");
263                         } catch (Exception e) {
264                                 
265                                 // missing operand after 'human' operand 
266                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());                          
267                         }
268                         
269                         try {                   
270                                 T.Select ("name = 1");
271                                 Fail ("test#05");
272                         } catch (Exception e) {
273                                 
274                                 // Cannot perform '=' operation between string and Int32
275                                 AssertEquals ("test#06", typeof (EvaluateException), e.GetType ());
276                         }
277                         
278                         AssertEquals ("test#07", 1, T.Select ("age = '13'").Length);
279
280                 }
281
282                 [Test]
283                 public void SelectOperators ()
284                 {
285                         DataTable T = new DataTable ("test");
286                         DataColumn C = new DataColumn ("name");
287                         T.Columns.Add (C);
288                         C = new DataColumn ("age");
289                         C.DataType = typeof (int);
290                         T.Columns.Add (C);
291                         C = new DataColumn ("id");
292                         T.Columns.Add (C);
293                         
294                         DataSet Set = new DataSet ("TestSet");
295                         Set.Tables.Add (T);
296                         
297                         DataRow Row = null;
298                         for (int i = 0; i < 100; i++) {
299                                 Row = T.NewRow ();
300                                 Row [0] = "human" + i;
301                                 Row [1] = i;
302                                 Row [2] = i;
303                                 T.Rows.Add (Row);
304                         }
305                         
306                         Row = T.NewRow ();
307                         Row [0] = "h*an";
308                         Row [1] = 1;
309                         Row [2] = 1;
310                         T.Rows.Add (Row);
311                         
312                         AssertEquals ("test#01", 11, T.Select ("age < 10").Length);
313                         AssertEquals ("test#02", 12, T.Select ("age <= 10").Length);                    
314                         AssertEquals ("test#03", 12, T.Select ("age< =10").Length);                     
315                         AssertEquals ("test#04", 89, T.Select ("age > 10").Length);
316                         AssertEquals ("test#05", 90, T.Select ("age >= 10").Length);                    
317                         AssertEquals ("test#06", 100, T.Select ("age <> 10").Length);
318                         AssertEquals ("test#07", 3, T.Select ("name < 'human10'").Length);
319                         AssertEquals ("test#08", 3, T.Select ("id < '10'").Length);
320                         // FIXME: Somebody explain how this can be possible.
321                         // it seems that it is no matter between 10 - 30. The
322                         // result is allways 25 :-P
323                         AssertEquals ("test#09", 25, T.Select ("id < 10").Length);
324                         
325                 }
326
327                 [Test]
328                 public void SelectExceptions ()
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                         for (int i = 0; i < 100; i++) {
340                                 DataRow Row = T.NewRow ();
341                                 Row [0] = "human" + i;
342                                 Row [1] = i;
343                                 Row [2] = i;
344                                 T.Rows.Add (Row);
345                         }
346                         
347                         try {
348                                 T.Select ("name = human1");
349                                 Fail ("test#01");
350                         } catch (Exception e) {
351                                 
352                                 // column name human not found
353                                 AssertEquals ("test#02", typeof (EvaluateException), e.GetType ());
354                         }
355                         
356                         AssertEquals ("test#04", 1, T.Select ("id = '12'").Length);
357                         AssertEquals ("test#05", 1, T.Select ("id = 12").Length);
358                         
359                         try {
360                                 T.Select ("id = 1k3");
361                                 Fail ("test#06");
362                         } catch (Exception e) {
363                                 
364                                 // no operands after k3 operator
365                                 AssertEquals ("test#07", typeof (SyntaxErrorException), e.GetType ());
366                         }                                               
367                 }
368                 
369                 [Test]
370                 public void SelectStringOperators ()
371                 {
372                         DataTable T = new DataTable ("test");
373                         DataColumn C = new DataColumn ("name");
374                         T.Columns.Add (C);
375                         C = new DataColumn ("age");
376                         C.DataType = typeof (int);
377                         T.Columns.Add (C);
378                         C = new DataColumn ("id");
379                         T.Columns.Add (C);
380                         
381                         DataSet Set = new DataSet ("TestSet");
382                         Set.Tables.Add (T);
383                         
384                         DataRow Row = null;
385                         for (int i = 0; i < 100; i++) {
386                                 Row = T.NewRow ();
387                                 Row [0] = "human" + i;
388                                 Row [1] = i;
389                                 Row [2] = i;
390                                 T.Rows.Add (Row);
391                         }
392                         Row = T.NewRow ();
393                         Row [0] = "h*an";
394                         Row [1] = 1;
395                         Row [2] = 1;
396                         T.Rows.Add (Row);
397                                         
398                         AssertEquals ("test#01", 1, T.Select ("name = 'human' + 1").Length);
399                         
400                         AssertEquals ("test#02", "human1", T.Select ("name = 'human' + 1") [0] ["name"]);                       
401                         AssertEquals ("test#03", 1, T.Select ("name = 'human' + '1'").Length);
402                         AssertEquals ("test#04", "human1", T.Select ("name = 'human' + '1'") [0] ["name"]);                     
403                         AssertEquals ("test#05", 1, T.Select ("name = 'human' + 1 + 2").Length);
404                         AssertEquals ("test#06", "human12", T.Select ("name = 'human' + '1' + '2'") [0] ["name"]);
405                         
406                         AssertEquals ("test#07", 1, T.Select ("name = 'huMAn' + 1").Length);
407                         
408                         Set.CaseSensitive = true;
409                         AssertEquals ("test#08", 0, T.Select ("name = 'huMAn' + 1").Length);
410                         
411                         T.CaseSensitive = false;
412                         AssertEquals ("test#09", 1, T.Select ("name = 'huMAn' + 1").Length);
413                         
414                         T.CaseSensitive = true;
415                         AssertEquals ("test#10", 0, T.Select ("name = 'huMAn' + 1").Length);
416                         
417                         Set.CaseSensitive = false;
418                         AssertEquals ("test#11", 0, T.Select ("name = 'huMAn' + 1").Length);
419                         
420                         T.CaseSensitive = false;
421                         AssertEquals ("test#12", 1, T.Select ("name = 'huMAn' + 1").Length);
422                         
423                         AssertEquals ("test#13", 0, T.Select ("name = 'human1*'").Length);
424                         AssertEquals ("test#14", 11, T.Select ("name like 'human1*'").Length);
425                         AssertEquals ("test#15", 11, T.Select ("name like 'human1%'").Length);
426                         
427                         try {
428                                 AssertEquals ("test#16", 11, T.Select ("name like 'h*an1'").Length);
429                                 Fail ("test#16");
430                         } catch (Exception e) {
431                                 
432                                 // 'h*an1' is invalid
433                                 AssertEquals ("test#17", typeof (EvaluateException), e.GetType ());
434                         }
435                         
436                         try {
437                                 AssertEquals ("test#18", 11, T.Select ("name like 'h%an1'").Length);
438                                 Fail ("test#19");
439                         } catch (Exception e) {
440                                 
441                                 // 'h%an1' is invalid
442                                 AssertEquals ("test#20", typeof (EvaluateException), e.GetType ());
443                         }
444                         
445                         AssertEquals ("test#21", 0, T.Select ("name like 'h[%]an'").Length);
446                         AssertEquals ("test#22", 1, T.Select ("name like 'h[*]an'").Length);
447                         
448                 }
449
450                 [Test]
451                 public void SelectAggregates ()
452                 {
453                         DataTable T = new DataTable ("test");
454                         DataColumn C = new DataColumn ("name");
455                         T.Columns.Add (C);
456                         C = new DataColumn ("age");
457                         C.DataType = typeof (int);
458                         T.Columns.Add (C);
459                         C = new DataColumn ("id");
460                         T.Columns.Add (C);
461                         DataRow Row = null;
462                         
463                         for (int i = 0; i < 1000; i++) {
464                                 Row = T.NewRow ();
465                                 Row [0] = "human" + i;
466                                 Row [1] = i;
467                                 Row [2] = i;
468                                 T.Rows.Add (Row);
469                         }
470                         
471                         AssertEquals ("test#01", 1000, T.Select ("Sum(age) > 10").Length);
472                         AssertEquals ("test#02", 1000, T.Select ("avg(age) = 499").Length);
473                         AssertEquals ("test#03", 1000, T.Select ("min(age) = 0").Length);
474                         AssertEquals ("test#04", 1000, T.Select ("max(age) = 999").Length);
475                         AssertEquals ("test#05", 1000, T.Select ("count(age) = 1000").Length);
476                         AssertEquals ("test#06", 1000, T.Select ("stdev(age) > 287 and stdev(age) < 289").Length);
477                         AssertEquals ("test#07", 1000, T.Select ("var(age) < 83417 and var(age) > 83416").Length);
478                 }
479                 
480                 [Test]
481                 public void SelectFunctions ()
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                         Row = T.NewRow ();
502                         Row [0] = "human" + "test";
503                         Row [1] = DBNull.Value;
504                         Row [2] = DBNull.Value;
505                         T.Rows.Add (Row);
506
507                         //TODO: How to test Convert-function
508                         AssertEquals ("test#01", 25, T.Select ("age = 5*5") [0]["age"]);                        
509                         AssertEquals ("test#02", 901, T.Select ("len(name) > 7").Length);
510                         AssertEquals ("test#03", 125, T.Select ("age = 5*5*5 AND len(name)>7") [0]["age"]);
511                         AssertEquals ("test#04", 1, T.Select ("isnull(id, 'test') = 'test'").Length);                   
512                         AssertEquals ("test#05", 1000, T.Select ("iif(id = '56', 'test', 'false') = 'false'").Length);                  
513                         AssertEquals ("test#06", 1, T.Select ("iif(id = '56', 'test', 'false') = 'test'").Length);
514                         AssertEquals ("test#07", 9, T.Select ("substring(id, 2, 3) = '23'").Length);
515                         AssertEquals ("test#08", "123", T.Select ("substring(id, 2, 3) = '23'") [0] ["id"]);
516                         AssertEquals ("test#09", "423", T.Select ("substring(id, 2, 3) = '23'") [3] ["id"]);
517                         AssertEquals ("test#10", "923", T.Select ("substring(id, 2, 3) = '23'") [8] ["id"]);
518                         
519                 }
520
521                 [Test]
522                 public void SelectRelations ()
523                 {
524                         DataSet Set = new DataSet ();
525                         DataTable Mom = new DataTable ("Mom");
526                         DataTable Child = new DataTable ("Child");
527
528                         Set.Tables.Add (Mom);
529                         Set.Tables.Add (Child);
530                         
531                         DataColumn Col = new DataColumn ("Name");
532                         DataColumn Col2 = new DataColumn ("ChildName");
533                         Mom.Columns.Add (Col);
534                         Mom.Columns.Add (Col2);
535                         
536                         DataColumn Col3 = new DataColumn ("Name");
537                         DataColumn Col4 = new DataColumn ("Age");
538                         Col4.DataType = Type.GetType ("System.Int16");
539                         Child.Columns.Add (Col3);
540                         Child.Columns.Add (Col4);
541                         
542                         DataRelation Relation = new DataRelation ("Rel", Mom.Columns [1], Child.Columns [0]);
543                         Set.Relations.Add (Relation);
544
545                         DataRow Row = Mom.NewRow ();
546                         Row [0] = "Laura";
547                         Row [1] = "Nick";
548                         Mom.Rows.Add (Row);
549                         
550                         Row = Mom.NewRow ();
551                         Row [0] = "Laura";
552                         Row [1] = "Dick";
553                         Mom.Rows.Add (Row);
554                         
555                         Row = Mom.NewRow ();
556                         Row [0] = "Laura";
557                         Row [1] = "Mick";
558                         Mom.Rows.Add (Row);
559
560                         Row = Mom.NewRow ();
561                         Row [0] = "Teresa";
562                         Row [1] = "Jack";
563                         Mom.Rows.Add (Row);
564                         
565                         Row = Mom.NewRow ();
566                         Row [0] = "Teresa";
567                         Row [1] = "Mack";
568                         Mom.Rows.Add (Row);
569                         
570                         Row = Child.NewRow ();
571                         Row [0] = "Nick";
572                         Row [1] = 15;
573                         Child.Rows.Add (Row);
574                         
575                         Row = Child.NewRow ();
576                         Row [0] = "Dick";
577                         Row [1] = 25;
578                         Child.Rows.Add (Row);
579                         
580                         Row = Child.NewRow ();
581                         Row [0] = "Mick";
582                         Row [1] = 35;
583                         Child.Rows.Add (Row);
584                         
585                         Row = Child.NewRow ();
586                         Row [0] = "Jack";
587                         Row [1] = 10;
588                         Child.Rows.Add (Row);
589                         
590                         Row = Child.NewRow ();
591                         Row [0] = "Mack";
592                         Row [1] = 19;
593                         Child.Rows.Add (Row);
594                 
595                         Row = Child.NewRow ();
596                         Row [0] = "Mack";
597                         Row [1] = 99;
598                         Child.Rows.Add (Row);
599                         
600                         DataRow [] Rows = Child.Select ("name = Parent.Childname");
601                         AssertEquals ("test#01", 6, Rows.Length);
602                         Rows = Child.Select ("Parent.childname = 'Jack'");
603                         AssertEquals ("test#02", 1, Rows.Length);
604                         
605                         try {
606                                 // FIXME: LAMESPEC: Why the exception is thrown why... why... 
607                                 Mom.Select ("Child.Name = 'Jack'");
608                                 Fail ("test#03");
609                         } catch (Exception e) {
610                                 AssertEquals ("test#04", typeof (SyntaxErrorException), e.GetType ());
611                                 AssertEquals ("test#05", "Cannot interpret token 'Child' at position 1.", e.Message);
612                         }
613                         
614                         Rows = Child.Select ("Parent.name = 'Laura'");
615                         AssertEquals ("test#06", 3, Rows.Length);
616                         
617                         DataTable Parent2 = new DataTable ("Parent2");
618                         Col = new DataColumn ("Name");
619                         Col2 = new DataColumn ("ChildName");
620
621                         Parent2.Columns.Add (Col);
622                         Parent2.Columns.Add (Col2);
623                         Set.Tables.Add (Parent2);
624                         
625                         Row = Parent2.NewRow ();
626                         Row [0] = "Laura";
627                         Row [1] = "Nick";
628                         Parent2.Rows.Add (Row);
629                         
630                         Row = Parent2.NewRow ();
631                         Row [0] = "Laura";
632                         Row [1] = "Dick";
633                         Parent2.Rows.Add (Row);
634                         
635                         Row = Parent2.NewRow ();
636                         Row [0] = "Laura";
637                         Row [1] = "Mick";
638                         Parent2.Rows.Add (Row);
639
640                         Row = Parent2.NewRow ();
641                         Row [0] = "Teresa";
642                         Row [1] = "Jack";
643                         Parent2.Rows.Add (Row);
644                         
645                         Row = Parent2.NewRow ();
646                         Row [0] = "Teresa";
647                         Row [1] = "Mack";
648                         Parent2.Rows.Add (Row);
649
650                         Relation = new DataRelation ("Rel2", Parent2.Columns [1], Child.Columns [0]);
651                         Set.Relations.Add (Relation);
652                         
653                         try {
654                                 Rows = Child.Select ("Parent.ChildName = 'Jack'");
655                                 Fail ("test#07");
656                         } catch (Exception e) {
657                                 AssertEquals ("test#08", typeof (EvaluateException), e.GetType ());
658                                 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);
659                         }
660                         
661                         Rows = Child.Select ("Parent(rel).ChildName = 'Jack'");
662                         AssertEquals ("test#10", 1, Rows.Length);
663
664                         Rows = Child.Select ("Parent(Rel2).ChildName = 'Jack'");
665                         AssertEquals ("test#10", 1, Rows.Length);
666                         
667                         try {
668                                 Mom.Select ("Parent.name  = 'John'");
669                         } catch (Exception e) {
670                                 AssertEquals ("test#11", typeof (IndexOutOfRangeException), e.GetType ());
671                                 AssertEquals ("test#12", "Cannot find relation 0.", e.Message);
672                         }
673                         
674                 }
675
676                 [Test]
677                 public void ToStringTest()
678                 {
679                         DataTable dt = new DataTable();
680                         dt.Columns.Add("Col1",typeof(int));
681                         
682                         dt.TableName = "Mytable";
683                         dt.DisplayExpression = "Col1";
684                         
685                         
686                         string cmpr = dt.TableName + " + " + dt.DisplayExpression;
687                         AssertEquals(cmpr,dt.ToString());
688                 }
689
690                 [Test]
691                 public void PrimaryKey ()
692                 {
693                         DataTable dt = new DataTable ();
694                         DataColumn Col = new DataColumn ();
695                         Col.AllowDBNull = false;
696                         Col.DataType = typeof (int);
697                         dt.Columns.Add (Col);
698                         dt.Columns.Add ();
699                         dt.Columns.Add ();
700                         dt.Columns.Add ();
701                         
702                         AssertEquals ("test#01", 0, dt.PrimaryKey.Length);
703                         
704                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0]};
705                         AssertEquals ("test#02", 1, dt.PrimaryKey.Length);
706                         AssertEquals ("test#03", "Column1", dt.PrimaryKey [0].ColumnName);
707                         
708                         dt.PrimaryKey = null;
709                         AssertEquals ("test#04", 0, dt.PrimaryKey.Length);
710                         
711                         Col = new DataColumn ("failed");
712                         
713                         try {
714                                 dt.PrimaryKey = new DataColumn [] {Col};
715                                 Fail ("test#05");                                       
716                         } catch (Exception e) {
717                                 AssertEquals ("test#06", typeof (ArgumentException), e.GetType ());
718                                 AssertEquals ("test#07", "Column must belong to a table.", e.Message);
719                         }
720                         
721                         DataTable dt2 = new DataTable ();
722                         dt2.Columns.Add ();
723                         
724                         try {
725                                 dt.PrimaryKey = new DataColumn [] {dt2.Columns [0]};
726                                 Fail ("test#08");
727                         } catch (Exception e) {
728                                 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
729                                 AssertEquals ("test#10", "PrimaryKey columns do not belong to this table.", e.Message);
730                         }
731                         
732                         
733                         AssertEquals ("test#11", 0, dt.Constraints.Count);
734                         
735                         dt.PrimaryKey = new DataColumn [] {dt.Columns [0], dt.Columns [1]};
736                         AssertEquals ("test#12", 2, dt.PrimaryKey.Length);
737                         AssertEquals ("test#13", 1, dt.Constraints.Count);
738                         AssertEquals ("test#14", true, dt.Constraints [0] is UniqueConstraint);
739                         AssertEquals ("test#15", "Column1", dt.PrimaryKey [0].ColumnName);
740                         AssertEquals ("test#16", "Column2", dt.PrimaryKey [1].ColumnName);
741                         
742                 }
743
744         }
745 }