Merge pull request #3091 from alexanderkyte/mobile_static_fix_mcs_tests
[mono.git] / mcs / class / System.Data / Test / System.Data / DataColumnTest2.cs
1 // Authors:
2 //   Rafael Mizrahi   <rafim@mainsoft.com>
3 //   Erez Lotan       <erezl@mainsoft.com>
4 //   Oren Gurfinkel   <oreng@mainsoft.com>
5 //   Ofer Borstein
6 // 
7 // Copyright (c) 2004 Mainsoft Co.
8 // 
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.ComponentModel;
31 using System.Data;
32 using System.Globalization;
33 using System.Threading;
34
35 using MonoTests.System.Data.Utils;
36
37 using NUnit.Framework;
38
39 namespace MonoTests.System.Data
40 {
41         [TestFixture]
42         public class DataColumnTest2
43         {
44                 private CultureInfo originalCulture;
45
46                 [SetUp]
47                 public void SetUp ()
48                 {
49                         originalCulture = Thread.CurrentThread.CurrentCulture;
50                         Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
51                 }
52
53                 [TearDown]
54                 public void TearDown ()
55                 {
56                         Thread.CurrentThread.CurrentCulture = originalCulture;
57                 }
58
59                 [Test] public void AllowDBNull()
60                 {
61                         DataTable dt = new DataTable();
62                         DataColumn dc;
63                         dc = new DataColumn("ColName",typeof(int));
64                         dc.DefaultValue = DBNull.Value;
65                         dt.Columns.Add(dc);
66                         dc.AutoIncrement=false;
67
68                         // Checking default value (True)
69                         Assert.AreEqual(true ,  dc.AllowDBNull, "DC1");
70
71                         // AllowDBNull=true - adding new row with null value
72                         dt.Rows.Add(dt.NewRow());
73                         Assert.AreEqual(DBNull.Value , dt.Rows[0][0], "DC2");
74
75                         // set AllowDBNull=false 
76                         try
77                         {
78                                 dc.AllowDBNull=false; //the exisiting row contains null value
79                                 Assert.Fail("DC3: AllowDbNull Failed to throw DataException");
80                         }
81                         catch (DataException) {}
82                         catch (AssertionException exc) {throw  exc;}
83                         catch (Exception exc)
84                         {
85                                 Assert.Fail("DC4: AllowDbNull. Wrong exception type. Got:" + exc);
86                         }
87
88                         dt.Rows.Clear();
89                         dc.AllowDBNull=false;
90                         // AllowDBNull=false - adding new row with null value
91                         try
92                         {
93                                 dt.Rows.Add(dt.NewRow());
94                                 Assert.Fail("DC5: RowAdd Failed to throw NoNullAllowedException");
95                         }
96                         catch (NoNullAllowedException) {}
97                         catch (AssertionException exc) {throw  exc;}
98                         catch (Exception exc)
99                         {
100                                 Assert.Fail("DC6: RowAdd. Wrong exception type. Got:" + exc);
101                         }
102
103                         dc.AutoIncrement=true;
104                         int iRowCount = dt.Rows.Count;
105                         // AllowDBNull=false,AutoIncrement=true - adding new row with null value
106                         dt.Rows.Add(dt.NewRow());
107                         Assert.AreEqual(dt.Rows.Count, iRowCount+1, "DC7");
108                 }
109
110                 [Test] public void AutoIncrement()
111                 {
112                         DataColumn dc;
113                         dc = new DataColumn("ColName",typeof(string));
114
115                         // Checking default value (False)
116                         Assert.AreEqual(false , dc.AutoIncrement, "DC8");
117
118                         //Cheking Set
119                         dc.AutoIncrement=true;
120                         // Checking Get
121                         Assert.AreEqual(true , dc.AutoIncrement, "DC9");
122                 }
123
124                 [Test] public void AutoIncrementSeed()
125                 {
126                         DataColumn dc;
127                         dc = new DataColumn("ColName",typeof(string));
128
129                         // Checking default value 0
130                         Assert.AreEqual((long)0, dc.AutoIncrementSeed , "DC10");
131
132                         //Cheking Set
133                         dc.AutoIncrementSeed = long.MaxValue;
134                         // Checking Get MaxValue
135                         Assert.AreEqual(long.MaxValue, dc.AutoIncrementSeed , "DC11");
136
137                         //Cheking Set
138                         dc.AutoIncrementSeed = long.MinValue ;
139                         // Checking Get MinValue
140                         Assert.AreEqual(long.MinValue , dc.AutoIncrementSeed, "DC12");
141                 }
142
143                 [Test] public void AutoIncrementStep()
144                 {
145                         DataColumn dc;
146                         dc = new DataColumn("ColName",typeof(string));
147                         // Checking default value 1
148                         Assert.AreEqual((long)1,  dc.AutoIncrementStep , "DC13");
149
150                         //Cheking Set
151                         dc.AutoIncrementStep = long.MaxValue;
152                         // Checking Get MaxValue
153                         Assert.AreEqual(long.MaxValue, dc.AutoIncrementStep , "DC14");
154
155                         //Cheking Set
156                         dc.AutoIncrementStep = long.MinValue ;
157                         // Checking Get MinValue
158                         Assert.AreEqual(long.MinValue ,  dc.AutoIncrementStep , "DC15");
159                 }
160
161                 [Test] public void Caption()
162                 {
163                         DataColumn dc;
164                         string sCaption = "NewCaption";
165                         dc = new DataColumn("ColName",typeof(string));
166
167                         //Checking default value ( ColumnName )
168                         // Checking default value ( ColumnName )
169                         Assert.AreEqual(dc.ColumnName , dc.Caption, "DC16");
170
171                         //Cheking Set
172                         dc.Caption = sCaption;
173                         // Checking Get
174                         Assert.AreEqual(sCaption , dc.Caption , "DC17");
175                 }
176
177                 [Test] public void ColumnName()
178                 {
179                         DataColumn dc;
180                         string sName = "NewName";
181
182                         dc = new DataColumn();
183                         //Checking default value ("")
184                         // ColumnName default value
185                         Assert.AreEqual(string.Empty ,  dc.ColumnName, "DC18");
186
187                         //Cheking Set
188                         dc.ColumnName = sName;
189                         //Checking Get
190                         // ColumnName Get/Set
191                         Assert.AreEqual(sName , dc.ColumnName , "DC19");
192
193                         //Special chars (valid chars)
194                         sName = "~()#\\/=><+-*%&|^'\"[]";
195                         // ColumnName Special chars
196                         dc.ColumnName = sName ; 
197                         Assert.AreEqual(sName , dc.ColumnName , "DC20");
198                 }
199
200                 [Test] public void DataType()
201                 {
202                         DataColumn dc;
203                         dc = new DataColumn();
204                         string[] sTypeArr = { "System.Boolean", "System.Byte", "System.Char", "System.DateTime",
205                                 "System.Decimal", "System.Double", "System.Int16", "System.Int32",
206                                 "System.Int64", "System.SByte", "System.Single", "System.String", 
207                                 "System.TimeSpan", "System.UInt16", "System.UInt32", "System.UInt64" };
208
209                         //Checking default value (string)
210                         // GetType - Default
211                         Assert.AreEqual(Type.GetType("System.String") ,  dc.DataType, "DC21");
212
213                         foreach (string sType in sTypeArr) 
214                         {
215                                 //Cheking Set
216                                 dc.DataType = Type.GetType(sType);
217                                 // Checking GetType " + sType);
218                                 Assert.AreEqual(Type.GetType(sType) , dc.DataType , "DC22");
219                         }
220                 }
221
222                 [Test] public void Equals()
223                 {
224                         DataColumn dc1,dc2;
225                         dc1 = new DataColumn();
226                         dc2 = new DataColumn();
227                         // #1
228                         // Equals 1
229                         Assert.AreEqual(false , dc1.Equals(dc2) , "DC23");
230
231                         dc1 = dc2;
232                         // #2
233                         // Equals 2
234                         Assert.AreEqual(dc2 , dc1 , "DC24");
235                 }
236
237                 [Test] public void ExtendedProperties()
238                 {
239                         DataColumn dc;
240                         PropertyCollection pc;
241                         dc = new DataColumn();
242
243                         pc = dc.ExtendedProperties ;
244                         // Checking ExtendedProperties default 
245                         Assert.AreEqual(true, pc != null, "DC25");
246
247                         // Checking ExtendedProperties count 
248                         Assert.AreEqual(0, pc.Count , "DC26");
249                 }
250
251                 [Test] public void TestGetHashCode()
252                 {
253                         DataColumn dc1;
254                         int iHashCode1;
255                         dc1 = new DataColumn();
256
257                         iHashCode1 = dc1.GetHashCode();
258                         for (int i=0; i<10; i++)
259                         {       // must return the same value each time
260                                 // GetHashCode #" + i.ToString());
261                                 Assert.AreEqual(dc1.GetHashCode(), iHashCode1 , "DC27");
262                         }
263                 }
264
265                 [Test] public void TestGetType()
266                 {
267                         DataColumn dc;
268                         Type myType;
269                         dc = new DataColumn();
270                         myType = dc.GetType();
271
272                         // GetType
273                         Assert.AreEqual(typeof(DataColumn), myType, "DC28");
274                 }
275
276                 [Test] public void MaxLength()
277                 {
278                         DataColumn dc;
279                         dc = new DataColumn("ColName",typeof(string));
280
281                         //Checking default value (-1)
282                         // MaxLength default
283                         Assert.AreEqual((int)-1, dc.MaxLength , "DC29");
284
285                         //Cheking Set MaxValue
286                         dc.MaxLength = int.MaxValue ;
287                         //Checking Get MaxValue
288                         // MaxLength MaxValue
289                         Assert.AreEqual(int.MaxValue , dc.MaxLength , "DC30");
290
291                         //Cheking Set MinValue
292                         dc.MaxLength = int.MinValue  ;
293                         //Checking Get MinValue
294                         // MaxLength MinValue
295                         Assert.AreEqual(-1, dc.MaxLength , "DC31");
296
297                         DataTable dt = new DataTable();
298                         dt.Columns.Add(new DataColumn("col",typeof(string)));
299                         dt.Columns[0].MaxLength = 5;
300                         dt.Rows.Add(new object[] {"a"});
301
302                         //MaxLength = 5
303                         try
304                         {
305                                 // MaxLength = 5
306                                 dt.Rows[0][0] = "123456";
307                                 Assert.Fail("DC32: Indexer failed to throw ArgumentException");
308                         }
309                         catch(ArgumentException) {}
310                         catch (AssertionException exc) {throw  exc;}
311                         catch (Exception exc)
312                         {
313                                 Assert.Fail("DC33: Indexer. Wrong exception type. Got:" + exc);
314                         }
315                 }
316
317                 [Test] public void Namespace()
318                 {
319                         DataColumn dc;
320                         string sName = "NewName";
321
322                         dc = new DataColumn();
323                         //Checking default value ("")
324                                 // Namespace default
325                                 Assert.AreEqual(string.Empty , dc.Namespace , "DC34");
326
327                         //Cheking Set
328                         dc.Namespace  = sName;
329                         //Checking Get
330                                 // Namespace Get/Set
331                                 Assert.AreEqual(sName, dc.Namespace , "DC35");
332                 }
333
334                 [Test] public void Prefix()
335                 {
336                         DataColumn dc;
337                         string sPrefix = "Prefix";
338                         dc = new DataColumn("ColName",typeof(string));
339
340                                 // Prefix Checking default value (string.Empty)
341                                 Assert.AreEqual(string.Empty , dc.Prefix , "DC36");
342
343                         //Cheking Set
344                         dc.Prefix = sPrefix;
345                         //Checking Get
346                                 // Prefix Checking Get
347                                 Assert.AreEqual(sPrefix , dc.Prefix , "DC37");
348                 }
349
350                 [Test] public void ReadOnly()
351                 {
352                         DataColumn dc;
353                         dc = new DataColumn();
354
355                         //Checking default value (false)
356                                 // ReadOnly default
357                                 Assert.AreEqual(false , dc.ReadOnly , "DC38");
358
359                         //Cheking Set
360                         dc.ReadOnly=true;
361                         //Checking Get
362                                 // ReadOnly Get/Set
363                                 Assert.AreEqual(true, dc.ReadOnly , "DC39");
364                 }
365
366                 [Test] public void Table()
367                 {
368                         DataColumn dc;
369                         dc = new DataColumn();
370
371                         //Checking First Get
372                                 // Table test1
373                                 Assert.AreEqual(null,  dc.Table, "DC40");
374
375                         DataTable dt = new DataTable();
376                         dt.Columns.Add(dc);
377
378                         //Checking Second Get
379                                 // Table test2
380                                 Assert.AreEqual(dt, dc.Table , "DC41");
381                 }
382
383                 [Test] public void TestToString()
384                 {
385                         DataColumn dc;
386                         string sColName,sExp;
387                         dc = new DataColumn();
388
389                         //ToString = ""
390                         //Console.WriteLine(dc.ToString());
391
392                         //ToString = ColumnName                         
393                         sColName = "Test1";
394                         dc.ColumnName = sColName;
395                                 // ToString - ColumnName
396                                 Assert.AreEqual(sColName , dc.ToString() , "DC42");
397
398                         //TosTring = ColumnName + " + " + Expression
399                         sExp = "Tax * 1.234";
400                         dc.Expression = sExp;
401                                 // TosTring=ColumnName + Expression
402                                 Assert.AreEqual(sColName + " + " + sExp , dc.ToString() , "DC43");
403                 }
404
405                 [Test] public void Unique()
406                 {
407                         DataColumn dc;
408                         dc = new DataColumn();
409                         //Checking default value (false)
410
411                                 // Unique default
412                                 Assert.AreEqual(false , dc.Unique , "DC44");
413
414                         //Cheking Set
415                         dc.Unique=true;
416
417                         //Checking Get
418                         // Unique Get/Set
419                         Assert.AreEqual(true,  dc.Unique, "DC45");
420                 }
421
422                 [Test] public void Unique_PrimaryKey()
423                 {
424                         DataTable table = new DataTable ("Table1");
425                         DataColumn col = table.Columns.Add ("col1");
426                         table.PrimaryKey = new DataColumn [] {col};
427                 
428                         Assert.IsTrue (col.Unique, "#1");
429
430                         try {
431                                 col.Unique = false;
432                                 Assert.Fail ("#2 cannot remove uniqueness of a primarykey");
433                         } catch (ArgumentException e) {
434                         }
435
436                         Assert.IsTrue (col.Unique, "#3");
437                 }
438
439                 [Test] public void ctor()
440                 {
441                         DataColumn dc;
442                         dc = new DataColumn();
443
444                         // ctor
445                         Assert.AreEqual(false, dc == null, "DC46");
446                 }
447
448                 [Test] public void ctor_ByColumnName()
449                 {
450                         DataColumn dc;
451                         string sName = "ColName";
452                         dc = new DataColumn(sName);
453
454                         // ctor - object
455                         Assert.AreEqual(false , dc==null , "DC47");
456
457                         // ctor - ColName
458                         Assert.AreEqual(sName, dc.ColumnName , "DC48");
459                 }
460
461                 [Test] public void ctor_ByColumnNameType()
462                 {
463                         Type typTest;
464                         DataColumn dc = null;
465                         string[] sTypeArr = { "System.Boolean", "System.Byte", "System.Char", "System.DateTime",
466                                 "System.Decimal", "System.Double", "System.Int16", "System.Int32",
467                                 "System.Int64", "System.SByte", "System.Single", "System.String", 
468                                 "System.TimeSpan", "System.UInt16", "System.UInt32", "System.UInt64" };
469
470                         foreach (string sType in sTypeArr) 
471                         {
472                                 typTest = Type.GetType(sType);
473                                 dc = new DataColumn("ColName",typTest);
474                                 // ctor - object
475                                 Assert.AreEqual(false , dc==null, "DC49");
476
477                                 // ctor - ColName
478                                 Assert.AreEqual(typTest ,  dc.DataType , "DC50");
479                         }
480                 }
481
482                 [Test] public void ctor_ByColumnNameTypeExpression()
483                 {
484                         DataColumn dc;
485                         dc = new DataColumn("ColName",typeof(String),"Price * 1.18");
486
487                         // ctor - object
488                         Assert.AreEqual(false , dc==null, "DC51");
489                 }
490
491                 [Test] public void ctor_ByColumnNameTypeExpressionMappingType()
492                 {
493                         DataColumn dc;
494                         //Cheking constructor for each Enum MappingType
495                         foreach (int i in Enum.GetValues(typeof(MappingType))) 
496                         {
497                                 dc = null;
498                                 dc = new DataColumn("ColName",typeof(string),"Price * 1.18",(MappingType)i );
499                                 // Ctor #" + i.ToString());
500                                 Assert.AreEqual(false , dc==null , "DC52");
501                         }
502                 }
503
504                 [Test] public void ordinal()
505                 {
506                         DataColumn dc;
507                         dc = new DataColumn("ColName",typeof(string));
508
509                         //DEBUG
510                         //Console.WriteLine( "***" + dc.Ordinal.ToString()  + "***
511                         //DEBUG
512
513                         //Checking default value 
514                         // Ordinal default value
515                         Assert.AreEqual((int)-1 ,  dc.Ordinal, "DC53");
516
517                         // needs a DataTable.Columns to test   
518                         DataColumnCollection dcColl ;
519                         DataTable tb = new DataTable();
520                         dcColl = tb.Columns ;
521                         dcColl.Add();   //0
522                         dcColl.Add();   //1
523                         dcColl.Add();   //2
524                         dcColl.Add(dc); //3
525
526                         //Checking Get
527                         // Ordinal Get
528                         Assert.AreEqual((int)3 , dc.Ordinal , "DC54");
529                 }
530
531                 [Test]
532                 public void Expression()
533                 {
534                         DataColumn dc;
535                         string sExpression = "Tax * 0.59";
536                         dc = new DataColumn("ColName",typeof(string));
537
538                         Assert.AreEqual(string.Empty, dc.Expression, "dce#1");
539
540                         dc.Expression = sExpression;
541
542                         Assert.AreEqual(sExpression,dc.Expression, "dce#2");                            
543                 }
544
545                 [Test]
546                 public void Expression_Whitespace ()
547                 {
548                         DataColumn dc = new DataColumn ("ColName", typeof(string));
549
550                         string plainWhitespace = "    ";
551                         string surroundWhitespace = "  'abc'  ";
552
553                         Assert.AreEqual (string.Empty, dc.Expression, "dce#1");
554
555                         dc.Expression = plainWhitespace;
556                         Assert.AreEqual (string.Empty, dc.Expression, "dce#2");
557
558                         dc.Expression = surroundWhitespace;
559                         Assert.AreEqual (surroundWhitespace, dc.Expression, "dce#3");
560                 }
561
562                 [Test]
563                 public void Expression_Exceptions()
564                 {
565                         DataTable dt = DataProvider.CreateParentDataTable();
566                         try
567                         {
568                                 dt.Columns[0].Unique=true;
569                                 dt.Columns[0].Expression = "sum(" + dt.Columns[0].ColumnName + ")";
570                                 Assert.Fail("dccee#1: Expression failed to throw ArgmentException");
571                         }
572                         catch (ArgumentException) {}
573                         catch (AssertionException exc) {throw  exc;}
574                         catch (Exception exc)
575                         {
576                                 Assert.Fail("dccee#2: Expression. Wrong exception type. Got:" + exc);
577                         }       
578
579                         try
580                         {
581                                 DataTable dt1 = DataProvider.CreateParentDataTable();
582                                 dt1.Columns[0].AutoIncrement=true;
583                                 dt1.Columns[0].Expression = "sum(" + dt1.Columns[0].ColumnName + ")";
584                                 Assert.Fail("dccee#3: Expression failed to throw ArgmentException");
585                         }
586                         catch (ArgumentException) {}
587                         catch (AssertionException exc) {throw  exc;}
588                         catch (Exception exc)
589                         {
590                                 Assert.Fail("dccee#4: Expression. Wrong exception type. Got:" + exc);
591                         }
592
593                         try
594                         {
595                                 DataTable dt1 = DataProvider.CreateParentDataTable();
596                                 dt1.Constraints.Add(new UniqueConstraint(dt1.Columns[0],false));
597                                 dt1.Columns[0].Expression = "count(" + dt1.Columns[0].ColumnName + ")";
598                                 Assert.Fail("dccee#5: Expression failed to throw ArgmentException");
599                         }
600                         catch (ArgumentException) {}
601                         catch (AssertionException exc) {throw  exc;}
602                         catch (Exception exc)
603                         {
604                                 Assert.Fail("dccee#6: Expression. Wrong exception type. Got:" + exc);
605                         }
606
607                         try
608                         {
609                                 DataTable dt1 = DataProvider.CreateParentDataTable();
610                         
611                                 dt1.Columns[0].Expression = "CONVERT(" + dt1.Columns[1].ColumnName + ",'System.Int32')";
612                                 Assert.Fail("dccee#7: Expression failed to throw FormatException");
613                         }
614                         catch (FormatException) {}
615                         catch (AssertionException exc) {throw  exc;}
616                         catch (Exception exc)
617                         {
618                                 Assert.Fail("dccee#8: Expression. Wrong exception type. Got:" + exc);
619                         }
620
621                         try
622                         {
623                                 DataTable dt1 = DataProvider.CreateParentDataTable();
624                         
625                                 dt1.Columns[0].Expression = "CONVERT(" + dt1.Columns[0].ColumnName + ",'System.DateTime')";
626                                 Assert.Fail("dccee#9: Expression failed to throw ArgmentException");
627                         }
628                         catch (ArgumentException) {}
629                         catch (AssertionException exc) {throw  exc;}
630                         catch (Exception exc)
631                         {
632                                 Assert.Fail("dccee#10: Expression. Wrong exception type. Got:" + exc);
633                         }
634
635
636                         try
637                         {
638                                 DataTable dt1 = DataProvider.CreateParentDataTable();
639                         
640                                 dt1.Columns[1].Expression = "CONVERT(" + dt1.Columns[0].ColumnName + ",'System.DateTime')";
641                                 Assert.Fail("dccee#11: Expression failed to throw InvalidCastException");
642                         }
643                         catch (InvalidCastException) {}
644                         catch (AssertionException exc) {throw  exc;}
645                         catch (Exception exc)
646                         {
647                                 Assert.Fail("dccee#12: Expression. Wrong exception type. Got:" + exc);
648                         }
649
650                         try
651                         {
652                                 DataTable dt1 = DataProvider.CreateParentDataTable();
653                         
654                                 dt1.Columns[1].Expression = "SUBSTRING(" + dt1.Columns[2].ColumnName + ",60000000000,2)";
655                                 Assert.Fail("dccee#13: Expression failed to throw OverflowException");
656                         }
657                         catch (OverflowException) {}
658                         catch (AssertionException exc) {throw  exc;}
659                         catch (Exception exc)
660                         {
661                                 Assert.Fail("dccee#14: Expression. Wrong exception type. Got:" + exc);
662                         }
663                 }
664
665                 [Test]
666                 public void Expression_Simple()
667                 {
668                         DataTable dt = DataProvider.CreateParentDataTable();
669                         //Simple expression --> not aggregate
670                         DataColumn dc = new DataColumn("expr",Type.GetType("System.Decimal"));
671                         dt.Columns.Add(dc);
672                         dt.Columns["expr"].Expression = dt.Columns[0].ColumnName + "*0.52 +" + dt.Columns[0].ColumnName; 
673
674                         //Check the values
675                         //double temp;
676                         string temp;
677                         string str;
678
679                         foreach(DataRow dr in dt.Rows) {
680                                 str = ( ((int)dr[0])*0.52 + ((int)dr[0])).ToString();
681                                 if (str.Length > 3)
682                                         temp = str.Substring(0,4);
683                                 else
684                                         temp = str;
685                                 //Due to bug in GH 4.56 sometimes looks like : 4.56000000000000005
686
687                                 //temp = Convert.ToDouble(str);
688
689                                 if (dr["expr"].ToString().Length >3)
690                                         str = dr["expr"].ToString().Substring(0,4);
691                                 else
692                                         str = dr["expr"].ToString();
693
694                                 if (str == "7.60")
695                                         str = "7.6";
696
697                                 Assert.AreEqual(temp,str, "dcse#1");
698                                 //Compare(Convert.ToDouble(dr["expr"]), temp);
699                         }
700                 }
701
702                 [Test]
703                 public void Expression_Aggregate()
704                 {
705                         DataTable dt = DataProvider.CreateParentDataTable();
706                         //Simple expression -->  aggregate
707                         DataColumn dc = new DataColumn("expr",Type.GetType("System.Decimal"));
708                         dt.Columns.Add(dc);
709                         dt.Columns["expr"].Expression = "sum(" + dt.Columns[0].ColumnName + ") + count(" + dt.Columns[0].ColumnName + ")" ; 
710                         dt.Columns["expr"].Expression+= " + avg(" + dt.Columns[0].ColumnName + ") + Min(" + dt.Columns[0].ColumnName + ")" ; 
711
712
713                         //Check the values
714                         double temp;
715                         string str;
716
717                         double sum = Convert.ToDouble(dt.Compute("sum(" + dt.Columns[0].ColumnName + ")",string.Empty));
718                         double count = Convert.ToDouble(dt.Compute("count(" + dt.Columns[0].ColumnName + ")",string.Empty));
719                         double avg = Convert.ToDouble(dt.Compute("avg(" + dt.Columns[0].ColumnName + ")",string.Empty));
720                         double min = Convert.ToDouble(dt.Compute("min(" + dt.Columns[0].ColumnName + ")",string.Empty));
721
722                         str = (sum+count+avg+min).ToString();
723                         foreach(DataRow dr in dt.Rows)
724                         {
725                                 if (str.Length > 3)
726                                 {
727                                         temp = Convert.ToDouble(str.Substring(0,4));
728                                 }
729                                 else
730                                 {
731                                         temp = Convert.ToDouble(str);
732                                 }
733                                 
734                                 Assert.AreEqual(temp, Convert.ToDouble(dr["expr"]), "dcea#1");
735                         }
736                 }
737
738                 [Test]
739                 public void Expression_AggregateRelation()
740                 {
741                         DataTable parent = DataProvider.CreateParentDataTable();
742                         DataTable child  = DataProvider.CreateChildDataTable();
743                         DataSet ds = new DataSet();
744                         ds.Tables.Add(parent);
745                         ds.Tables.Add(child);
746                         
747                         ds.Relations.Add("Relation1",parent.Columns[0],child.Columns[0],false);
748
749                         //Create the computed columns 
750
751                         DataColumn dcComputedParent = new DataColumn("computedParent",Type.GetType("System.Double"));
752                         parent.Columns.Add(dcComputedParent);
753                         dcComputedParent.Expression = "sum(child(Relation1)." + child.Columns[1].ColumnName + ")";
754
755                         double preCalculatedExpression;
756
757                         foreach (DataRow dr in parent.Rows)
758                         {
759                                 object o = child.Compute("sum(" + child.Columns[1].ColumnName + ")",
760                                         parent.Columns[0].ColumnName + "=" + dr[0]);
761                                 if (o == DBNull.Value)
762                                 {
763                                         Assert.AreEqual(dr["computedParent"],o,"dcear#1");
764                                 }
765                                 else
766                                 {
767                                         preCalculatedExpression = Convert.ToDouble(o);
768                                         Assert.AreEqual(dr["computedParent"],preCalculatedExpression,"dcear#2");
769                                 }
770                         }
771
772                         DataColumn dcComputedChild = new DataColumn("computedChild",Type.GetType("System.Double"));
773                         child.Columns.Add(dcComputedChild);
774                         dcComputedChild.Expression = "Parent." + parent.Columns[0].ColumnName;
775
776                         int index=0;
777                         double val;
778                         foreach (DataRow dr in child.Rows)
779                         {
780                                 val = Convert.ToDouble(dr.GetParentRow("Relation1")[0]);
781                                 Assert.AreEqual(dr["computedChild"],val,"dcear#3");
782                                 index++;                                
783                         }
784                 }
785
786                 [Test]
787                 public void Expression_IIF()
788                 {
789                         DataTable dt = DataProvider.CreateParentDataTable();
790                         DataColumn dcComputedParent = new DataColumn("computedCol",Type.GetType("System.Double"));
791                         dcComputedParent.DefaultValue=25.5;
792                         dt.Columns.Add(dcComputedParent);
793                         dcComputedParent.Expression = "IIF(" + dt.Columns[0].ColumnName + ">3" + ",1,2)"; 
794
795                         double val;
796                         foreach (DataRow dr in dt.Rows)
797                         {
798                                 val = (int)dr[0] >3 ? 1:2;
799                                 Assert.AreEqual(val,dr["computedCol"],"dceiif#1");                              
800                         }
801                         //Now reset the expression and check that the column got his deafult value
802
803                         dcComputedParent.Expression = null;
804                         foreach (DataRow dr in dt.Rows)
805                         {
806                                 Assert.AreEqual(25.5,dr["computedCol"],"dceiif#2");
807                         }
808                 }
809
810                 // bug #78254
811                 [Test]
812                 public void Expression_ISNULL ()
813                 {
814                         DataSet ds = new DataSet ();
815
816                         DataTable ptable = new DataTable ();
817                         ptable.Columns.Add ("col1", typeof (int));
818
819                         DataTable ctable = new DataTable ();
820                         ctable.Columns.Add ("col1", typeof (int));
821                         ctable.Columns.Add ("col2", typeof (int));
822
823                         ds.Tables.AddRange (new DataTable[] {ptable, ctable});
824                         ds.Relations.Add ("rel1", ptable.Columns [0], ctable.Columns [0]);
825
826                         ptable.Rows.Add (new object[] {1});
827                         ptable.Rows.Add (new object[] {2});
828                         for (int i=0; i < 5; ++i)
829                                 ctable.Rows.Add (new object[] {1, i});
830
831                         // should not throw exception
832                         ptable.Columns.Add ("col2", typeof (int), "IsNull (Sum (Child (rel1).col2), -1)");
833
834                         Assert.AreEqual (10, ptable.Rows [0][1], "#1");
835                         Assert.AreEqual (-1, ptable.Rows [1][1], "#2");
836                 }
837
838                 [Test]
839                 public void DateTimeMode_DataType ()
840                 {
841                         DataColumn col = new DataColumn("col", typeof(int));
842                         Assert.AreEqual (DataSetDateTime.UnspecifiedLocal, col.DateTimeMode, "#1");
843                         try {
844                                 col.DateTimeMode = DataSetDateTime.Local;
845                                 Assert.Fail ("#2");
846                         } catch (InvalidOperationException e) {}
847
848                         col = new DataColumn ("col", typeof (DateTime));
849                         col.DateTimeMode = DataSetDateTime.Utc;
850                         Assert.AreEqual (DataSetDateTime.Utc, col.DateTimeMode, "#3");
851                         col.DataType = typeof (int);
852                         Assert.AreEqual (DataSetDateTime.UnspecifiedLocal, col.DateTimeMode, "#4");
853                 }
854         
855                 [Test]
856                 public void DateTimeMode_InvalidValues ()
857                 {
858                         DataColumn col = new DataColumn("col", typeof(DateTime));
859                         try {
860                                 col.DateTimeMode = (DataSetDateTime)(-1);
861                                 Assert.Fail("#1");
862                         } catch (InvalidEnumArgumentException e) {}
863
864                         try {
865                                 col.DateTimeMode = (DataSetDateTime)5;
866                                 Assert.Fail("#2");
867                         } catch (InvalidEnumArgumentException e) {}
868                 }
869
870                 [Test]
871                 public void DateTimeMode_RowsAdded ()
872                 {
873                         DataTable table = new DataTable();
874                         table.Columns.Add("col", typeof(DateTime));
875                         table.Rows.Add(new object[] {DateTime.Now});
876
877                         Assert.AreEqual(DataSetDateTime.UnspecifiedLocal, table.Columns[0].DateTimeMode, "#1");
878                         // allowed
879                         table.Columns[0].DateTimeMode = DataSetDateTime.Unspecified;
880                         table.Columns[0].DateTimeMode = DataSetDateTime.UnspecifiedLocal;
881
882                         try {
883                                 table.Columns[0].DateTimeMode = DataSetDateTime.Local;
884                                 Assert.Fail("#2");
885                         } catch (InvalidOperationException e) {}
886
887                         try {
888                                 table.Columns[0].DateTimeMode = DataSetDateTime.Utc;
889                                 Assert.Fail("#3");
890                         } catch (InvalidOperationException e) {}
891                 }
892
893                 [Test]
894                 public void SetOrdinalTest()
895                 {
896                         DataColumn col = new DataColumn("col", typeof(int));
897                         try {
898                                 col.SetOrdinal(2);
899                                 Assert.Fail ("#1");
900                         } catch (ArgumentException e) { }
901
902                         DataTable table = new DataTable();
903                         DataColumn col1 = table.Columns.Add ("col1", typeof (int));
904                         DataColumn col2 = table.Columns.Add("col2", typeof(int));
905                         DataColumn col3 = table.Columns.Add("col3", typeof(int));
906
907                         Assert.AreEqual("col1", table.Columns[0].ColumnName, "#2");
908                         Assert.AreEqual("col3", table.Columns[2].ColumnName, "#3");
909
910                         table.Columns[0].SetOrdinal (2);
911                         Assert.AreEqual("col2", table.Columns[0].ColumnName, "#4");
912                         Assert.AreEqual("col1", table.Columns[2].ColumnName, "#5");
913
914                         Assert.AreEqual(0, col2.Ordinal, "#6");
915                         Assert.AreEqual(1, col3.Ordinal, "#7");
916                         Assert.AreEqual(2, col1.Ordinal, "#8");
917
918                         try {
919                                 table.Columns[0].SetOrdinal (-1);
920                                 Assert.Fail ("#9");
921                         } catch (ArgumentOutOfRangeException e) { }
922
923                         try {
924                                 table.Columns[0].SetOrdinal (4);
925                                 Assert.Fail ("#10");
926                         } catch (ArgumentOutOfRangeException e) { }
927                 }
928                 [Test]
929                 public void bug672113_MulpleColConstraint ()
930                 {
931                         DataTable FirstTable = new DataTable ("First Table");
932                         DataColumn col0 = new DataColumn ("empno", typeof (int));
933                         DataColumn col1 = new DataColumn ("name", typeof (string));
934                         DataColumn col2 = new DataColumn ("age", typeof (int));
935                         FirstTable.Columns.Add (col0);
936                         FirstTable.Columns.Add (col1);
937                         FirstTable.Columns.Add (col2);
938                         DataColumn[] primkeys = new DataColumn[2];
939                         primkeys[0] = FirstTable.Columns[0];
940                         primkeys[1] = FirstTable.Columns[1];
941                         FirstTable.Constraints.Add("PRIM1",primkeys,true);
942
943                         DataTable SecondTable = new DataTable ("Second Table");
944                         col0 = new DataColumn ("field1", typeof (int));
945                         col1 = new DataColumn ("field2", typeof (int));
946                         col2 = new DataColumn ("field3", typeof (int));
947                         SecondTable.Columns.Add (col0);
948                         SecondTable.Columns.Add (col1);
949                         SecondTable.Columns.Add (col2);
950
951                         primkeys[0] = SecondTable.Columns[0];
952                         primkeys[1] = SecondTable.Columns[1];
953                         SecondTable.Constraints.Add("PRIM2",primkeys,true);
954
955                         DataRow row1 = FirstTable.NewRow ();
956                         row1["empno"] = 1;
957                         row1["name"] = "Test";
958                         row1["age"] = 32;
959                         FirstTable.Rows.Add (row1);
960                         FirstTable.AcceptChanges ();
961                         Assert.AreEqual (32, FirstTable.Rows[0]["age"], "#1");
962
963                         row1 = SecondTable.NewRow ();
964                         row1["field1"] = 10000;
965                         row1["field2"] = 12000;
966                         row1["field3"] = 1000;
967                         SecondTable.Rows.Add (row1);
968                         SecondTable.AcceptChanges ();
969                         Assert.AreEqual (12000, SecondTable.Rows[0]["field2"], "#2");
970                 }
971         }
972 }