2 // Rafael Mizrahi <rafim@mainsoft.com>
3 // Erez Lotan <erezl@mainsoft.com>
4 // Oren Gurfinkel <oreng@mainsoft.com>
7 // Copyright (c) 2004 Mainsoft Co.
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.ComponentModel;
32 using System.Globalization;
33 using System.Threading;
35 using MonoTests.System.Data.Utils;
37 using NUnit.Framework;
39 namespace MonoTests.System.Data
42 public class DataColumnTest2
44 private CultureInfo originalCulture;
49 originalCulture = Thread.CurrentThread.CurrentCulture;
50 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
54 public void TearDown ()
56 Thread.CurrentThread.CurrentCulture = originalCulture;
59 [Test] public void AllowDBNull()
61 DataTable dt = new DataTable();
63 dc = new DataColumn("ColName",typeof(int));
64 dc.DefaultValue = DBNull.Value;
66 dc.AutoIncrement=false;
68 // Checking default value (True)
69 Assert.AreEqual(true , dc.AllowDBNull, "DC1");
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");
75 // set AllowDBNull=false
78 dc.AllowDBNull=false; //the exisiting row contains null value
79 Assert.Fail("DC3: AllowDbNull Failed to throw DataException");
81 catch (DataException) {}
82 catch (AssertionException exc) {throw exc;}
85 Assert.Fail("DC4: AllowDbNull. Wrong exception type. Got:" + exc);
90 // AllowDBNull=false - adding new row with null value
93 dt.Rows.Add(dt.NewRow());
94 Assert.Fail("DC5: RowAdd Failed to throw NoNullAllowedException");
96 catch (NoNullAllowedException) {}
97 catch (AssertionException exc) {throw exc;}
100 Assert.Fail("DC6: RowAdd. Wrong exception type. Got:" + exc);
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");
110 [Test] public void AutoIncrement()
113 dc = new DataColumn("ColName",typeof(string));
115 // Checking default value (False)
116 Assert.AreEqual(false , dc.AutoIncrement, "DC8");
119 dc.AutoIncrement=true;
121 Assert.AreEqual(true , dc.AutoIncrement, "DC9");
124 [Test] public void AutoIncrementSeed()
127 dc = new DataColumn("ColName",typeof(string));
129 // Checking default value 0
130 Assert.AreEqual((long)0, dc.AutoIncrementSeed , "DC10");
133 dc.AutoIncrementSeed = long.MaxValue;
134 // Checking Get MaxValue
135 Assert.AreEqual(long.MaxValue, dc.AutoIncrementSeed , "DC11");
138 dc.AutoIncrementSeed = long.MinValue ;
139 // Checking Get MinValue
140 Assert.AreEqual(long.MinValue , dc.AutoIncrementSeed, "DC12");
143 [Test] public void AutoIncrementStep()
146 dc = new DataColumn("ColName",typeof(string));
147 // Checking default value 1
148 Assert.AreEqual((long)1, dc.AutoIncrementStep , "DC13");
151 dc.AutoIncrementStep = long.MaxValue;
152 // Checking Get MaxValue
153 Assert.AreEqual(long.MaxValue, dc.AutoIncrementStep , "DC14");
156 dc.AutoIncrementStep = long.MinValue ;
157 // Checking Get MinValue
158 Assert.AreEqual(long.MinValue , dc.AutoIncrementStep , "DC15");
161 [Test] public void Caption()
164 string sCaption = "NewCaption";
165 dc = new DataColumn("ColName",typeof(string));
167 //Checking default value ( ColumnName )
168 // Checking default value ( ColumnName )
169 Assert.AreEqual(dc.ColumnName , dc.Caption, "DC16");
172 dc.Caption = sCaption;
174 Assert.AreEqual(sCaption , dc.Caption , "DC17");
177 [Test] public void ColumnName()
180 string sName = "NewName";
182 dc = new DataColumn();
183 //Checking default value ("")
184 // ColumnName default value
185 Assert.AreEqual(string.Empty , dc.ColumnName, "DC18");
188 dc.ColumnName = sName;
190 // ColumnName Get/Set
191 Assert.AreEqual(sName , dc.ColumnName , "DC19");
193 //Special chars (valid chars)
194 sName = "~()#\\/=><+-*%&|^'\"[]";
195 // ColumnName Special chars
196 dc.ColumnName = sName ;
197 Assert.AreEqual(sName , dc.ColumnName , "DC20");
200 [Test] public void DataType()
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" };
209 //Checking default value (string)
211 Assert.AreEqual(Type.GetType("System.String") , dc.DataType, "DC21");
213 foreach (string sType in sTypeArr)
216 dc.DataType = Type.GetType(sType);
217 // Checking GetType " + sType);
218 Assert.AreEqual(Type.GetType(sType) , dc.DataType , "DC22");
222 [Test] public void Equals()
225 dc1 = new DataColumn();
226 dc2 = new DataColumn();
229 Assert.AreEqual(false , dc1.Equals(dc2) , "DC23");
234 Assert.AreEqual(dc2 , dc1 , "DC24");
237 [Test] public void ExtendedProperties()
240 PropertyCollection pc;
241 dc = new DataColumn();
243 pc = dc.ExtendedProperties ;
244 // Checking ExtendedProperties default
245 Assert.AreEqual(true, pc != null, "DC25");
247 // Checking ExtendedProperties count
248 Assert.AreEqual(0, pc.Count , "DC26");
251 [Test] public void TestGetHashCode()
255 dc1 = new DataColumn();
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");
265 [Test] public void TestGetType()
269 dc = new DataColumn();
270 myType = dc.GetType();
273 Assert.AreEqual(typeof(DataColumn), myType, "DC28");
276 [Test] public void MaxLength()
279 dc = new DataColumn("ColName",typeof(string));
281 //Checking default value (-1)
283 Assert.AreEqual((int)-1, dc.MaxLength , "DC29");
285 //Cheking Set MaxValue
286 dc.MaxLength = int.MaxValue ;
287 //Checking Get MaxValue
288 // MaxLength MaxValue
289 Assert.AreEqual(int.MaxValue , dc.MaxLength , "DC30");
291 //Cheking Set MinValue
292 dc.MaxLength = int.MinValue ;
293 //Checking Get MinValue
294 // MaxLength MinValue
295 Assert.AreEqual(-1, dc.MaxLength , "DC31");
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"});
306 dt.Rows[0][0] = "123456";
307 Assert.Fail("DC32: Indexer failed to throw ArgumentException");
309 catch(ArgumentException) {}
310 catch (AssertionException exc) {throw exc;}
311 catch (Exception exc)
313 Assert.Fail("DC33: Indexer. Wrong exception type. Got:" + exc);
317 [Test] public void Namespace()
320 string sName = "NewName";
322 dc = new DataColumn();
323 //Checking default value ("")
325 Assert.AreEqual(string.Empty , dc.Namespace , "DC34");
328 dc.Namespace = sName;
331 Assert.AreEqual(sName, dc.Namespace , "DC35");
334 [Test] public void Prefix()
337 string sPrefix = "Prefix";
338 dc = new DataColumn("ColName",typeof(string));
340 // Prefix Checking default value (string.Empty)
341 Assert.AreEqual(string.Empty , dc.Prefix , "DC36");
346 // Prefix Checking Get
347 Assert.AreEqual(sPrefix , dc.Prefix , "DC37");
350 [Test] public void ReadOnly()
353 dc = new DataColumn();
355 //Checking default value (false)
357 Assert.AreEqual(false , dc.ReadOnly , "DC38");
363 Assert.AreEqual(true, dc.ReadOnly , "DC39");
366 [Test] public void Table()
369 dc = new DataColumn();
373 Assert.AreEqual(null, dc.Table, "DC40");
375 DataTable dt = new DataTable();
378 //Checking Second Get
380 Assert.AreEqual(dt, dc.Table , "DC41");
383 [Test] public void TestToString()
386 string sColName,sExp;
387 dc = new DataColumn();
390 //Console.WriteLine(dc.ToString());
392 //ToString = ColumnName
394 dc.ColumnName = sColName;
395 // ToString - ColumnName
396 Assert.AreEqual(sColName , dc.ToString() , "DC42");
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");
405 [Test] public void Unique()
408 dc = new DataColumn();
409 //Checking default value (false)
412 Assert.AreEqual(false , dc.Unique , "DC44");
419 Assert.AreEqual(true, dc.Unique, "DC45");
422 [Test] public void Unique_PrimaryKey()
424 DataTable table = new DataTable ("Table1");
425 DataColumn col = table.Columns.Add ("col1");
426 table.PrimaryKey = new DataColumn [] {col};
428 Assert.IsTrue (col.Unique, "#1");
432 Assert.Fail ("#2 cannot remove uniqueness of a primarykey");
433 } catch (ArgumentException e) {
436 Assert.IsTrue (col.Unique, "#3");
439 [Test] public void ctor()
442 dc = new DataColumn();
445 Assert.AreEqual(false, dc == null, "DC46");
448 [Test] public void ctor_ByColumnName()
451 string sName = "ColName";
452 dc = new DataColumn(sName);
455 Assert.AreEqual(false , dc==null , "DC47");
458 Assert.AreEqual(sName, dc.ColumnName , "DC48");
461 [Test] public void ctor_ByColumnNameType()
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" };
470 foreach (string sType in sTypeArr)
472 typTest = Type.GetType(sType);
473 dc = new DataColumn("ColName",typTest);
475 Assert.AreEqual(false , dc==null, "DC49");
478 Assert.AreEqual(typTest , dc.DataType , "DC50");
482 [Test] public void ctor_ByColumnNameTypeExpression()
485 dc = new DataColumn("ColName",typeof(String),"Price * 1.18");
488 Assert.AreEqual(false , dc==null, "DC51");
491 [Test] public void ctor_ByColumnNameTypeExpressionMappingType()
494 //Cheking constructor for each Enum MappingType
495 foreach (int i in Enum.GetValues(typeof(MappingType)))
498 dc = new DataColumn("ColName",typeof(string),"Price * 1.18",(MappingType)i );
499 // Ctor #" + i.ToString());
500 Assert.AreEqual(false , dc==null , "DC52");
504 [Test] public void ordinal()
507 dc = new DataColumn("ColName",typeof(string));
510 //Console.WriteLine( "***" + dc.Ordinal.ToString() + "***
513 //Checking default value
514 // Ordinal default value
515 Assert.AreEqual((int)-1 , dc.Ordinal, "DC53");
517 // needs a DataTable.Columns to test
518 DataColumnCollection dcColl ;
519 DataTable tb = new DataTable();
520 dcColl = tb.Columns ;
528 Assert.AreEqual((int)3 , dc.Ordinal , "DC54");
532 public void Expression()
535 string sExpression = "Tax * 0.59";
536 dc = new DataColumn("ColName",typeof(string));
538 Assert.AreEqual(string.Empty, dc.Expression, "dce#1");
540 dc.Expression = sExpression;
542 Assert.AreEqual(sExpression,dc.Expression, "dce#2");
546 public void Expression_Whitespace ()
548 DataColumn dc = new DataColumn ("ColName", typeof(string));
550 string plainWhitespace = " ";
551 string surroundWhitespace = " 'abc' ";
553 Assert.AreEqual (string.Empty, dc.Expression, "dce#1");
555 dc.Expression = plainWhitespace;
556 Assert.AreEqual (string.Empty, dc.Expression, "dce#2");
558 dc.Expression = surroundWhitespace;
559 Assert.AreEqual (surroundWhitespace, dc.Expression, "dce#3");
563 public void Expression_Exceptions()
565 DataTable dt = DataProvider.CreateParentDataTable();
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");
572 catch (ArgumentException) {}
573 catch (AssertionException exc) {throw exc;}
574 catch (Exception exc)
576 Assert.Fail("dccee#2: Expression. Wrong exception type. Got:" + exc);
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");
586 catch (ArgumentException) {}
587 catch (AssertionException exc) {throw exc;}
588 catch (Exception exc)
590 Assert.Fail("dccee#4: Expression. Wrong exception type. Got:" + exc);
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");
600 catch (ArgumentException) {}
601 catch (AssertionException exc) {throw exc;}
602 catch (Exception exc)
604 Assert.Fail("dccee#6: Expression. Wrong exception type. Got:" + exc);
609 DataTable dt1 = DataProvider.CreateParentDataTable();
611 dt1.Columns[0].Expression = "CONVERT(" + dt1.Columns[1].ColumnName + ",'System.Int32')";
612 Assert.Fail("dccee#7: Expression failed to throw FormatException");
614 catch (FormatException) {}
615 catch (AssertionException exc) {throw exc;}
616 catch (Exception exc)
618 Assert.Fail("dccee#8: Expression. Wrong exception type. Got:" + exc);
623 DataTable dt1 = DataProvider.CreateParentDataTable();
625 dt1.Columns[0].Expression = "CONVERT(" + dt1.Columns[0].ColumnName + ",'System.DateTime')";
626 Assert.Fail("dccee#9: Expression failed to throw ArgmentException");
628 catch (ArgumentException) {}
629 catch (AssertionException exc) {throw exc;}
630 catch (Exception exc)
632 Assert.Fail("dccee#10: Expression. Wrong exception type. Got:" + exc);
638 DataTable dt1 = DataProvider.CreateParentDataTable();
640 dt1.Columns[1].Expression = "CONVERT(" + dt1.Columns[0].ColumnName + ",'System.DateTime')";
641 Assert.Fail("dccee#11: Expression failed to throw InvalidCastException");
643 catch (InvalidCastException) {}
644 catch (AssertionException exc) {throw exc;}
645 catch (Exception exc)
647 Assert.Fail("dccee#12: Expression. Wrong exception type. Got:" + exc);
652 DataTable dt1 = DataProvider.CreateParentDataTable();
654 dt1.Columns[1].Expression = "SUBSTRING(" + dt1.Columns[2].ColumnName + ",60000000000,2)";
655 Assert.Fail("dccee#13: Expression failed to throw OverflowException");
657 catch (OverflowException) {}
658 catch (AssertionException exc) {throw exc;}
659 catch (Exception exc)
661 Assert.Fail("dccee#14: Expression. Wrong exception type. Got:" + exc);
666 public void Expression_Simple()
668 DataTable dt = DataProvider.CreateParentDataTable();
669 //Simple expression --> not aggregate
670 DataColumn dc = new DataColumn("expr",Type.GetType("System.Decimal"));
672 dt.Columns["expr"].Expression = dt.Columns[0].ColumnName + "*0.52 +" + dt.Columns[0].ColumnName;
679 foreach(DataRow dr in dt.Rows) {
680 str = ( ((int)dr[0])*0.52 + ((int)dr[0])).ToString();
682 temp = str.Substring(0,4);
685 //Due to bug in GH 4.56 sometimes looks like : 4.56000000000000005
687 //temp = Convert.ToDouble(str);
689 if (dr["expr"].ToString().Length >3)
690 str = dr["expr"].ToString().Substring(0,4);
692 str = dr["expr"].ToString();
697 Assert.AreEqual(temp,str, "dcse#1");
698 //Compare(Convert.ToDouble(dr["expr"]), temp);
703 public void Expression_Aggregate()
705 DataTable dt = DataProvider.CreateParentDataTable();
706 //Simple expression --> aggregate
707 DataColumn dc = new DataColumn("expr",Type.GetType("System.Decimal"));
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 + ")" ;
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));
722 str = (sum+count+avg+min).ToString();
723 foreach(DataRow dr in dt.Rows)
727 temp = Convert.ToDouble(str.Substring(0,4));
731 temp = Convert.ToDouble(str);
734 Assert.AreEqual(temp, Convert.ToDouble(dr["expr"]), "dcea#1");
739 public void Expression_AggregateRelation()
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);
747 ds.Relations.Add("Relation1",parent.Columns[0],child.Columns[0],false);
749 //Create the computed columns
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 + ")";
755 double preCalculatedExpression;
757 foreach (DataRow dr in parent.Rows)
759 object o = child.Compute("sum(" + child.Columns[1].ColumnName + ")",
760 parent.Columns[0].ColumnName + "=" + dr[0]);
761 if (o == DBNull.Value)
763 Assert.AreEqual(dr["computedParent"],o,"dcear#1");
767 preCalculatedExpression = Convert.ToDouble(o);
768 Assert.AreEqual(dr["computedParent"],preCalculatedExpression,"dcear#2");
772 DataColumn dcComputedChild = new DataColumn("computedChild",Type.GetType("System.Double"));
773 child.Columns.Add(dcComputedChild);
774 dcComputedChild.Expression = "Parent." + parent.Columns[0].ColumnName;
778 foreach (DataRow dr in child.Rows)
780 val = Convert.ToDouble(dr.GetParentRow("Relation1")[0]);
781 Assert.AreEqual(dr["computedChild"],val,"dcear#3");
787 public void Expression_IIF()
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)";
796 foreach (DataRow dr in dt.Rows)
798 val = (int)dr[0] >3 ? 1:2;
799 Assert.AreEqual(val,dr["computedCol"],"dceiif#1");
801 //Now reset the expression and check that the column got his deafult value
803 dcComputedParent.Expression = null;
804 foreach (DataRow dr in dt.Rows)
806 Assert.AreEqual(25.5,dr["computedCol"],"dceiif#2");
812 public void Expression_ISNULL ()
814 DataSet ds = new DataSet ();
816 DataTable ptable = new DataTable ();
817 ptable.Columns.Add ("col1", typeof (int));
819 DataTable ctable = new DataTable ();
820 ctable.Columns.Add ("col1", typeof (int));
821 ctable.Columns.Add ("col2", typeof (int));
823 ds.Tables.AddRange (new DataTable[] {ptable, ctable});
824 ds.Relations.Add ("rel1", ptable.Columns [0], ctable.Columns [0]);
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});
831 // should not throw exception
832 ptable.Columns.Add ("col2", typeof (int), "IsNull (Sum (Child (rel1).col2), -1)");
834 Assert.AreEqual (10, ptable.Rows [0][1], "#1");
835 Assert.AreEqual (-1, ptable.Rows [1][1], "#2");
839 public void DateTimeMode_DataType ()
841 DataColumn col = new DataColumn("col", typeof(int));
842 Assert.AreEqual (DataSetDateTime.UnspecifiedLocal, col.DateTimeMode, "#1");
844 col.DateTimeMode = DataSetDateTime.Local;
846 } catch (InvalidOperationException e) {}
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");
856 public void DateTimeMode_InvalidValues ()
858 DataColumn col = new DataColumn("col", typeof(DateTime));
860 col.DateTimeMode = (DataSetDateTime)(-1);
862 } catch (InvalidEnumArgumentException e) {}
865 col.DateTimeMode = (DataSetDateTime)5;
867 } catch (InvalidEnumArgumentException e) {}
871 public void DateTimeMode_RowsAdded ()
873 DataTable table = new DataTable();
874 table.Columns.Add("col", typeof(DateTime));
875 table.Rows.Add(new object[] {DateTime.Now});
877 Assert.AreEqual(DataSetDateTime.UnspecifiedLocal, table.Columns[0].DateTimeMode, "#1");
879 table.Columns[0].DateTimeMode = DataSetDateTime.Unspecified;
880 table.Columns[0].DateTimeMode = DataSetDateTime.UnspecifiedLocal;
883 table.Columns[0].DateTimeMode = DataSetDateTime.Local;
885 } catch (InvalidOperationException e) {}
888 table.Columns[0].DateTimeMode = DataSetDateTime.Utc;
890 } catch (InvalidOperationException e) {}
894 public void SetOrdinalTest()
896 DataColumn col = new DataColumn("col", typeof(int));
900 } catch (ArgumentException e) { }
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));
907 Assert.AreEqual("col1", table.Columns[0].ColumnName, "#2");
908 Assert.AreEqual("col3", table.Columns[2].ColumnName, "#3");
910 table.Columns[0].SetOrdinal (2);
911 Assert.AreEqual("col2", table.Columns[0].ColumnName, "#4");
912 Assert.AreEqual("col1", table.Columns[2].ColumnName, "#5");
914 Assert.AreEqual(0, col2.Ordinal, "#6");
915 Assert.AreEqual(1, col3.Ordinal, "#7");
916 Assert.AreEqual(2, col1.Ordinal, "#8");
919 table.Columns[0].SetOrdinal (-1);
921 } catch (ArgumentOutOfRangeException e) { }
924 table.Columns[0].SetOrdinal (4);
926 } catch (ArgumentOutOfRangeException e) { }
929 public void bug672113_MulpleColConstraint ()
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);
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);
951 primkeys[0] = SecondTable.Columns[0];
952 primkeys[1] = SecondTable.Columns[1];
953 SecondTable.Constraints.Add("PRIM2",primkeys,true);
955 DataRow row1 = FirstTable.NewRow ();
957 row1["name"] = "Test";
959 FirstTable.Rows.Add (row1);
960 FirstTable.AcceptChanges ();
961 Assert.AreEqual (32, FirstTable.Rows[0]["age"], "#1");
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");