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.
29 using NUnit.Framework;
32 using MonoTests.System.Data.Test.Utils;
34 namespace MonoTests_System.Data
36 [TestFixture] public class DataTableTest2
38 private bool _EventTriggered = false;
39 private bool EventRaised = false;
40 private bool EventValues = false;
42 class ProtectedTestClass : DataTable
44 public ProtectedTestClass() : base()
46 this.Columns.Add("Id",typeof(int));
47 this.Columns.Add("Value",typeof(string));
48 this.Rows.Add(new object[] {1,"one"});
49 this.Rows.Add(new object[] {2,"two"});
53 public void OnColumnChanged_Test()
55 OnColumnChanged(new DataColumnChangeEventArgs(this.Rows[0],this.Columns["Value"],"NewValue"));
58 public void OnColumnChanging_Test()
60 OnColumnChanging(new DataColumnChangeEventArgs(this.Rows[0],this.Columns["Value"],"NewValue"));
63 public void OnRemoveColumn_Test()
65 OnRemoveColumn(this.Columns[0]);
68 public DataTable CreateInstance_Test()
70 return CreateInstance();
74 [Test] public void AcceptChanges()
76 String sNewValue = "NewValue";
77 DataRow drModified,drDeleted,drAdded;
78 DataTable dt = DataProvider.CreateParentDataTable();
80 drModified = dt.Rows[0];
81 drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
83 drDeleted = dt.Rows[1];
84 drDeleted.Delete(); //DataRowState = Deleted
86 drAdded = dt.NewRow();
87 dt.Rows.Add(drAdded); //DataRowState = Added
91 // AcceptChanges - Unchanged1
92 Assert.AreEqual(DataRowState.Unchanged , drModified.RowState , "DT1");
94 // AcceptChanges - Current
95 Assert.AreEqual(sNewValue , drModified[1,DataRowVersion.Current] , "DT2");
97 // AcceptChanges - Unchanged2
98 Assert.AreEqual(DataRowState.Unchanged , drAdded.RowState , "DT3");
100 // AcceptChanges - Detached
101 Assert.AreEqual(DataRowState.Detached , drDeleted.RowState , "DT4");
104 [Test] public void ChildRelations()
106 DataTable dtChild,dtParent;
107 DataSet ds = new DataSet();
109 dtChild = DataProvider.CreateChildDataTable();
110 dtParent= DataProvider.CreateParentDataTable();
111 //Add tables to dataset
112 ds.Tables.Add(dtChild);
113 ds.Tables.Add(dtParent);
115 DataRelationCollection drlCollection;
116 DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
118 // Checking ChildRelations - default value
120 drlCollection = dtParent.ChildRelations;
121 Assert.AreEqual(0, drlCollection.Count , "DT5");
123 ds.Relations.Add(drl);
124 drlCollection = dtParent.ChildRelations;
126 // Checking ChildRelations Count
127 Assert.AreEqual(1, drlCollection.Count , "DT6");
129 // Checking ChildRelations Value
130 Assert.AreEqual(drl, drlCollection[0] , "DT7");
133 [Test] public void Clear()
135 DataTable dt = DataProvider.CreateParentDataTable();
138 Assert.AreEqual(0, dt.Rows.Count , "DT8");
141 [Test] public void Clone()
143 DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
144 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
145 dt2.Columns[0].DefaultValue=7;
149 for (int i=0; i<dt2.Constraints.Count; i++)
151 // Clone - Constraints[{0}],i)
152 Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT9");
155 for (int i=0; i<dt2.Columns.Count; i++)
157 // Clone - Columns[{0}].ColumnName,i)
158 Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT10");
160 // Clone - Columns[{0}].DataType,i)
161 Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT11");
165 [Test] public void ColumnChanged()
167 DataTable dt = DataProvider.CreateParentDataTable();
169 dt.ColumnChanged += new DataColumnChangeEventHandler( Column_Changed );
171 _EventTriggered=false;
172 // ColumnChanged - EventTriggered
173 dt.Rows[0][1] = "NewValue";
174 Assert.AreEqual(true , _EventTriggered , "DT12");
176 _EventTriggered=false;
177 dt.ColumnChanged -= new DataColumnChangeEventHandler( Column_Changed );
178 // ColumnChanged - NO EventTriggered
179 dt.Rows[0][1] = "VeryNewValue";
180 Assert.AreEqual(false , _EventTriggered , "DT13");
183 private void Column_Changed( object sender, DataColumnChangeEventArgs e )
185 _EventTriggered = true;
188 [Test] public void ColumnChanging()
190 DataTable dt = DataProvider.CreateParentDataTable();
192 dt.ColumnChanging += new DataColumnChangeEventHandler( Column_Changeding );
194 _EventTriggered=false;
195 // ColumnChanged - EventTriggered
196 dt.Rows[0][1] = "NewValue";
197 Assert.AreEqual(true , _EventTriggered , "DT14");
199 _EventTriggered=false;
200 dt.ColumnChanging -= new DataColumnChangeEventHandler( Column_Changeding );
201 // ColumnChanged - NO EventTriggered
202 dt.Rows[0][1] = "VeryNewValue";
203 Assert.AreEqual(false , _EventTriggered , "DT15");
206 private void Column_Changeding( object sender, DataColumnChangeEventArgs e )
208 _EventTriggered = true;
211 [Test] public void Columns()
214 DataColumnCollection dcl;
215 dtParent= DataProvider.CreateParentDataTable();
217 dcl = dtParent.Columns;
219 // Checking ColumnsCollection != null
220 Assert.AreEqual(false, dcl == null , "DT16");
222 // Checking ColumnCollection Count 1
223 Assert.AreEqual(6, dcl.Count , "DT17");
225 // Checking ColumnCollection Count 2
226 dtParent.Columns.Add(new DataColumn("Test"));
227 Assert.AreEqual(7, dcl.Count , "DT18");
229 // Checking ColumnCollection - get columnn by different case
230 DataColumn tmp = dtParent.Columns["TEST"];
231 Assert.AreEqual(dtParent.Columns["Test"], tmp , "DT19");
233 // Checking ColumnCollection colummn name case sensetive
234 dtParent.Columns.Add(new DataColumn("test"));
235 Assert.AreEqual(8, dcl.Count , "DT20");
237 // Checking ColumnCollection - get columnn by different case,ArgumentException
240 DataColumn tmp1 = dtParent.Columns["TEST"];
241 Assert.Fail("DT21: indexer Failed to throw ArgumentException");
243 catch (ArgumentException) {}
244 catch (AssertionException exc) {throw exc;}
245 catch (Exception exc)
247 Assert.Fail("DT22: Indexer. Wrong exception type. Got:" + exc);
251 [Test] public void Compute()
253 DataTable dt = DataProvider.CreateChildDataTable();
256 DataRow[] drArr = dt.Select("ParentId=1");
258 foreach (DataRow dr in drArr)
260 iExSum += (int)dr["ChildId"];
262 object objCompute=null;
263 // Compute - sum values
264 objCompute = dt.Compute("Sum(ChildId)","ParentId=1");
265 Assert.AreEqual(Int64.Parse(objCompute.ToString()) , Int64.Parse(iExSum.ToString()), "DT23");
267 // Compute - sum type
268 Assert.AreEqual(typeof(Int64).FullName , objCompute.GetType().FullName, "DT24");
272 drArr = dt.Select("ParentId=5");
273 foreach (DataRow dr in drArr)
275 iExAvg += (double)dr["ChildDouble"];
277 iExAvg = iExAvg / drArr.Length;
279 // Compute - Avg value
280 objCompute = dt.Compute("Avg(ChildDouble)","ParentId=5");
281 Assert.AreEqual(double.Parse(objCompute.ToString()) , double.Parse(iExAvg.ToString()), "DT25");
283 // Compute - Avg type
284 Assert.AreEqual(typeof(double).FullName , objCompute.GetType().FullName, "DT26");
287 [Test] public void Constraints()
290 ConstraintCollection consColl;
291 dtParent= DataProvider.CreateParentDataTable();
293 consColl = dtParent.Constraints;
294 // Checking Constraints != null
295 Assert.AreEqual(false, consColl == null , "DT27");
297 // Checking Constraints Count
298 Assert.AreEqual(0, consColl.Count , "DT28");
300 // Checking Constraints Count
302 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
303 Assert.AreEqual(1, consColl.Count , "DT29");
306 [Test] public void Copy()
308 DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
309 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
310 dt2.Columns[0].DefaultValue=7;
314 for (int i=0; i<dt2.Constraints.Count; i++)
316 // Copy - Constraints[{0}],i)
317 Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT30");
320 for (int i=0; i<dt2.Columns.Count; i++)
322 // Copy - Columns[{0}].ColumnName,i)
323 Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT31");
325 // Copy - Columns[{0}].DataType,i)
326 Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT32");
329 DataRow[] drArr1,drArr2;
330 drArr1 = dt1.Select("");
331 drArr2 = dt2.Select("");
332 for (int i=0; i<drArr1.Length ; i++)
334 // Copy - Data [ParentId]{0} ,i)
335 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT33");
336 // Copy - Data [String1]{0} ,i)
337 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT34");
338 // Copy - Data [String2]{0} ,i)
339 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2"], "DT35");
343 [Test] public void CreateInstance()
346 ProtectedTestClass C = new ProtectedTestClass();
347 DataTable dt = C.CreateInstance_Test();
348 Assert.AreEqual(true , dt != null , "DT36");
351 [Test] public void DataSet()
355 dtParent= DataProvider.CreateParentDataTable();
357 ds = dtParent.DataSet;
359 // Checking DataSet == null
360 Assert.AreEqual(null, ds, "DT37");
362 // Checking DataSet != null
363 ds = new DataSet("MyDataSet");
364 ds.Tables.Add(dtParent);
365 Assert.AreEqual(true, dtParent.DataSet != null , "DT38");
367 // Checking DataSet Name
368 Assert.AreEqual("MyDataSet", dtParent.DataSet.DataSetName , "DT39");
371 [Test] public void DefaultView()
375 dtParent= DataProvider.CreateParentDataTable();
376 dv = dtParent.DefaultView ;
378 // Checking DataView != null
379 Assert.AreEqual(true, dv != null, "DT40");
382 [Test] public void EndLoadData()
384 DataTable dt = DataProvider.CreateParentDataTable();
385 dt.Columns[0].AllowDBNull = false;
389 dt.LoadDataRow(new object[] {null,"A","B"},false);
393 //ConstraintException will be throw
395 Assert.Fail("DT41: EndLoadData Failed to throw ConstraintException");
397 catch (ConstraintException) {}
398 catch (AssertionException exc) {throw exc;}
399 catch (Exception exc)
401 Assert.Fail("DT42: EndLoadData Wrong exception type. Got:" + exc);
405 [Test] public void GetChanges()
407 DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
408 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
409 dt2.Columns[0].DefaultValue=7;
412 dt2.Rows[0].Delete();
413 dt2.Rows[1].Delete();
414 dt2.Rows[2].Delete();
415 dt2.Rows[3].Delete();
417 dt1 = dt2.GetChanges();
419 for (int i=0; i<dt2.Constraints.Count; i++)
421 // GetChanges - Constraints[{0}],i)
422 Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT43");
425 for (int i=0; i<dt2.Columns.Count; i++)
427 // GetChanges - Columns[{0}].ColumnName,i)
428 Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT44");
430 // GetChanges - Columns[{0}].DataType,i)
431 Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT45");
434 DataRow[] drArr1,drArr2;
436 drArr1 = dt1.Select("","",DataViewRowState.Deleted );
437 drArr2 = dt2.Select("","",DataViewRowState.Deleted );
439 for (int i=0; i<drArr1.Length ; i++)
441 // GetChanges - Data [ParentId]{0} ,i)
442 Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT46");
443 // GetChanges - Data [String1]{0} ,i)
444 Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original], "DT47");
445 // GetChanges - Data [String2]{0} ,i)
446 Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original], "DT48");
450 [Test] public void GetChanges_ByDataRowState()
452 DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
453 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
454 dt2.Columns[0].DefaultValue=7;
457 dt2.Rows[0].Delete(); //DataRowState.Deleted
458 dt2.Rows[1].Delete(); //DataRowState.Deleted
459 dt2.Rows[2].BeginEdit();
460 dt2.Rows[2]["String1"] = "Changed"; //DataRowState.Modified
461 dt2.Rows[2].EndEdit();
463 dt2.Rows.Add(new object[] {"99","Temp1","Temp2"}); //DataRowState.Added
465 // *********** Checking GetChanges - DataRowState.Deleted ************
467 dt1 = dt2.GetChanges(DataRowState.Deleted);
468 CheckTableSchema (dt1,dt2,DataRowState.Deleted.ToString());
469 DataRow[] drArr1,drArr2;
470 drArr1 = dt1.Select("","",DataViewRowState.Deleted );
471 drArr2 = dt2.Select("","",DataViewRowState.Deleted );
473 for (int i=0; i<drArr1.Length ; i++)
475 // GetChanges(Deleted) - Data [ParentId]{0} ,i)
476 Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT49");
477 // GetChanges(Deleted) - Data [String1]{0} ,i)
478 Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original], "DT50");
479 // GetChanges(Deleted) - Data [String2]{0} ,i)
480 Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original], "DT51");
483 // *********** Checking GetChanges - DataRowState.Modified ************
485 dt1 = dt2.GetChanges(DataRowState.Modified);
486 CheckTableSchema (dt1,dt2,DataRowState.Modified.ToString());
487 drArr1 = dt1.Select("","");
488 drArr2 = dt2.Select("","",DataViewRowState.ModifiedCurrent);
490 for (int i=0; i<drArr1.Length ; i++)
492 // GetChanges(Modified) - Data [ParentId]{0} ,i)
493 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT52");
494 // GetChanges(Modified) - Data [String1]{0} ,i)
495 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT53");
496 // GetChanges(Modified) - Data [String2]{0} ,i)
497 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT54");
500 // *********** Checking GetChanges - DataRowState.Added ************
502 dt1 = dt2.GetChanges(DataRowState.Added);
503 CheckTableSchema (dt1,dt2,DataRowState.Added.ToString());
504 drArr1 = dt1.Select("","");
505 drArr2 = dt2.Select("","",DataViewRowState.Added );
507 for (int i=0; i<drArr1.Length ; i++)
509 // GetChanges(Added) - Data [ParentId]{0} ,i)
510 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT55");
511 // GetChanges(Added) - Data [String1]{0} ,i)
512 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT56");
513 // GetChanges(Added) - Data [String2]{0} ,i)
514 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT57");
517 // *********** Checking GetChanges - DataRowState.Unchanged ************
519 dt1 = dt2.GetChanges(DataRowState.Unchanged);
520 CheckTableSchema (dt1,dt2,DataRowState.Unchanged .ToString());
521 drArr1 = dt1.Select("","");
522 drArr2 = dt2.Select("","",DataViewRowState.Unchanged );
524 for (int i=0; i<drArr1.Length ; i++)
526 // GetChanges(Unchanged) - Data [ParentId]{0} ,i)
527 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT58");
528 // GetChanges(Unchanged) - Data [String1]{0} ,i)
529 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT59");
530 // GetChanges(Unchanged) - Data [String2]{0} ,i)
531 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT60");
535 private void CheckTableSchema(DataTable dt1, DataTable dt2,string Description)
537 for (int i=0; i<dt2.Constraints.Count; i++)
539 // GetChanges - Constraints[{0}] - {1},i,Description)
540 Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT61");
543 for (int i=0; i<dt2.Columns.Count; i++)
545 // GetChanges - Columns[{0}].ColumnName - {1},i,Description)
546 Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT62");
548 // GetChanges - Columns[{0}].DataType {1},i,Description)
549 Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT63");
553 [Test] public void GetErrors()
555 DataTable dt = DataProvider.CreateParentDataTable();
556 DataRow[] drArr = new DataRow[3];
557 drArr[0] = dt.Rows[0];
558 drArr[1] = dt.Rows[2];
559 drArr[2] = dt.Rows[5];
561 drArr[0].RowError = "Error1";
562 drArr[1].RowError = "Error2";
563 drArr[2].RowError = "Error3";
566 Assert.AreEqual(dt.GetErrors(), drArr , "DT64");
569 [Test] public new void GetHashCode()
571 DataTable dt = DataProvider.CreateParentDataTable();
573 iHashCode = dt.GetHashCode();
575 for (int i=0; i<10 ;i++)
577 // HashCode - i= + i.ToString()
578 Assert.AreEqual(dt.GetHashCode() , iHashCode , "DT65");
582 [Test] public new void GetType()
584 DataTable dt = DataProvider.CreateParentDataTable();
585 Type tmpType = typeof(DataTable);
588 Assert.AreEqual(tmpType, dt.GetType() , "DT66");
591 [Test] public void HasErrors()
594 dtParent= DataProvider.CreateParentDataTable();
596 // Checking HasErrors default
597 Assert.AreEqual(false, dtParent.HasErrors , "DT67");
599 // Checking HasErrors Get
600 dtParent.Rows[0].RowError = "Error on row 0";
601 dtParent.Rows[2].RowError = "Error on row 2";
602 Assert.AreEqual(true, dtParent.HasErrors , "DT68");
605 [Test] public void ImportRow()
608 dt1 = DataProvider.CreateParentDataTable();
609 dt2 = DataProvider.CreateParentDataTable();
610 DataRow dr = dt2.NewRow();
611 dr.ItemArray = new object[] {99,"",""};
614 // ImportRow - Values
616 Assert.AreEqual(dr.ItemArray, dt1.Rows[dt1.Rows.Count-1].ItemArray , "DT69");
618 // ImportRow - DataRowState
619 Assert.AreEqual(dr.RowState , dt1.Rows[dt1.Rows.Count-1].RowState , "DT70");
622 [Test] public void LoadDataRow()
626 dt = DataProvider.CreateParentDataTable();
627 dt.PrimaryKey= new DataColumn[] {dt.Columns[0]}; //add ParentId as Primary Key
628 dt.Columns["String1"].DefaultValue = "Default";
630 dr = dt.Select("ParentId=1")[0];
632 //Update existing row without accept changes
634 dt.LoadDataRow(new object[] {1,null,"Changed"},false);
637 // LoadDataRow(update1) - check column String1
638 Assert.AreEqual(dr["String1"], dt.Columns["String1"].DefaultValue , "DT71");
640 // LoadDataRow(update1) - check column String2
641 Assert.AreEqual(dr["String2"], "Changed", "DT72");
643 // LoadDataRow(update1) - check row state
644 Assert.AreEqual(DataRowState.Modified , dr.RowState , "DT73");
646 //Update existing row with accept changes
647 dr = dt.Select("ParentId=2")[0];
650 dt.LoadDataRow(new object[] {2,null,"Changed"},true);
653 // LoadDataRow(update2) - check row state
654 Assert.AreEqual(DataRowState.Unchanged , dr.RowState , "DT74");
656 //Add New row without accept changes
658 dt.LoadDataRow(new object[] {99,null,"Changed"},false);
661 // LoadDataRow(insert1) - check column String2
662 dr = dt.Select("ParentId=99")[0];
663 Assert.AreEqual("Changed" , dr["String2"] , "DT75");
665 // LoadDataRow(insert1) - check row state
666 Assert.AreEqual(DataRowState.Added , dr.RowState , "DT76");
668 //Add New row with accept changes
670 dt.LoadDataRow(new object[] {100,null,"Changed"},true);
673 // LoadDataRow(insert2) - check row and values
674 dr = dt.Select("ParentId=100")[0];
675 Assert.AreEqual("Changed" , dr["String2"] , "DT77");
677 // LoadDataRow(insert2) - check row state
678 Assert.AreEqual(DataRowState.Unchanged , dr.RowState , "DT78");
681 [Test] public void Locale()
684 DataSet ds = new DataSet("MyDataSet");
686 dtParent= DataProvider.CreateParentDataTable();
687 ds.Tables.Add(dtParent);
688 System.Globalization.CultureInfo culInfo = System.Globalization.CultureInfo.CurrentCulture ;
690 // Checking Locale default from system
691 Assert.AreEqual(culInfo, dtParent.Locale , "DT79");
693 // Checking Locale default from dataset
694 culInfo = new System.Globalization.CultureInfo("fr"); // = French
696 Assert.AreEqual(culInfo , dtParent.Locale , "DT80");
698 // Checking Locale get/set
699 culInfo = new System.Globalization.CultureInfo("fr"); // = French
700 dtParent.Locale = culInfo ;
701 Assert.AreEqual(culInfo , dtParent.Locale , "DT81");
704 [Test] public void MinimumCapacity()
706 // i get default=50, according to MSDN the value should be 25
707 // // Checking MinimumCapacity default = 25
708 // Assert.AreEqual(25, dtParent.MinimumCapacity , "DT82");
710 DataTable dt = new DataTable();
712 // Checking MinimumCapacity get/set int.MaxValue
713 dt.MinimumCapacity = int.MaxValue;
714 Assert.AreEqual(int.MaxValue, dt.MinimumCapacity , "DT83");
716 // Checking MinimumCapacity get/set 0
717 dt.MinimumCapacity = 0;
718 Assert.AreEqual(0, dt.MinimumCapacity , "DT84");
720 // // Checking MinimumCapacity get/set int.MinValue
721 // dtParent.MinimumCapacity = int.MinValue ;
722 // Assert.AreEqual(int.MinValue, dtParent.MinimumCapacity , "DT85");
726 [Test] public void Namespace()
728 DataTable dtParent = new DataTable();
730 // Checking Namespace default
731 Assert.AreEqual(String.Empty, dtParent.Namespace, "DT86");
733 // Checking Namespace set/get
734 String s = "MyNamespace";
735 dtParent.Namespace=s;
736 Assert.AreEqual(s, dtParent.Namespace, "DT87");
739 [Test] public void NewRow()
743 dt = DataProvider.CreateParentDataTable();
747 Assert.AreEqual(true , dr != null , "DT88");
750 [Test] public void OnColumnChanged()
752 ProtectedTestClass dt = new ProtectedTestClass();
755 dt.OnColumnChanged_Test();
756 // OnColumnChanged Event 1
757 Assert.AreEqual(false , EventRaised , "DT89");
760 dt.ColumnChanged += new DataColumnChangeEventHandler(OnColumnChanged_Handler);
761 dt.OnColumnChanged_Test();
762 // OnColumnChanged Event 2
763 Assert.AreEqual(true , EventRaised , "DT90");
764 // OnColumnChanged Values
765 Assert.AreEqual(true , EventValues , "DT91");
766 dt.ColumnChanged -= new DataColumnChangeEventHandler(OnColumnChanged_Handler);
769 private void OnColumnChanged_Handler(Object sender,DataColumnChangeEventArgs e)
771 DataTable dt = (DataTable)sender;
772 if ( (e.Column.Equals(dt.Columns["Value"]) ) &&
773 (e.Row.Equals(dt.Rows[0]) ) &&
774 (e.ProposedValue.Equals("NewValue")) )
785 [Test] public void OnColumnChanging()
787 ProtectedTestClass dt = new ProtectedTestClass();
790 dt.OnColumnChanging_Test();
791 // OnColumnChanging Event 1
792 Assert.AreEqual(false , EventRaised , "DT92");
795 dt.ColumnChanging += new DataColumnChangeEventHandler(OnColumnChanging_Handler);
796 dt.OnColumnChanging_Test();
797 // OnColumnChanging Event 2
798 Assert.AreEqual(true , EventRaised , "DT93");
799 // OnColumnChanging Values
800 Assert.AreEqual(true , EventValues , "DT94");
801 dt.ColumnChanging -= new DataColumnChangeEventHandler(OnColumnChanging_Handler);
804 private void OnColumnChanging_Handler(Object sender,DataColumnChangeEventArgs e)
806 DataTable dt = (DataTable)sender;
807 if ( (e.Column.Equals(dt.Columns["Value"]) ) &&
808 (e.Row.Equals(dt.Rows[0]) ) &&
809 (e.ProposedValue.Equals("NewValue")) )
820 [Test] public void OnRemoveColumn()
822 ProtectedTestClass dt = new ProtectedTestClass();
825 dt.OnRemoveColumn_Test();
828 [Test] public void ParentRelations()
830 DataTable dtChild,dtParent;
831 DataSet ds = new DataSet();
833 dtChild = DataProvider.CreateChildDataTable();
834 dtParent= DataProvider.CreateParentDataTable();
835 //Add tables to dataset
836 ds.Tables.Add(dtChild);
837 ds.Tables.Add(dtParent);
839 DataRelationCollection drlCollection;
840 DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
842 // Checking ParentRelations - default value
844 drlCollection = dtChild.ParentRelations;
845 Assert.AreEqual(0, drlCollection.Count , "DT96");
847 ds.Relations.Add(drl);
848 drlCollection = dtChild.ParentRelations;
850 // Checking ParentRelations Count
851 Assert.AreEqual(1, drlCollection.Count , "DT97");
853 // Checking ParentRelations Value
854 Assert.AreEqual(drl, drlCollection[0] , "DT98");
857 [Test] public void Prefix()
859 DataTable dtParent = new DataTable();
861 // Checking Prefix default
862 Assert.AreEqual(String.Empty, dtParent.Prefix , "DT99");
864 // Checking Prefix set/get
865 String s = "MyPrefix";
867 Assert.AreEqual(s, dtParent.Prefix, "DT100");
870 [Test] public void RejectChanges()
872 String sNewValue = "NewValue";
873 DataRow drModified,drDeleted,drAdded;
874 DataTable dt = DataProvider.CreateParentDataTable();
876 drModified = dt.Rows[0];
877 drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
879 drDeleted = dt.Rows[1];
880 drDeleted.Delete(); //DataRowState = Deleted
882 drAdded = dt.NewRow();
883 dt.Rows.Add(drAdded); //DataRowState = Added
887 // RejectChanges - Unchanged1
888 Assert.AreEqual(DataRowState.Unchanged , drModified.RowState , "DT101");
890 // RejectChanges - Unchanged2
891 Assert.AreEqual(DataRowState.Detached , drAdded.RowState , "DT102");
893 // RejectChanges - Detached
894 Assert.AreEqual(DataRowState.Unchanged , drDeleted.RowState , "DT103");
897 [Test] public void Reset()
899 DataTable dt1 = DataProvider.CreateParentDataTable();
900 DataTable dt2 = DataProvider.CreateChildDataTable();
901 dt1.PrimaryKey = new DataColumn[] {dt1.Columns[0]};
902 dt2.PrimaryKey = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
903 DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
904 DataSet ds = new DataSet();
905 ds.Tables.AddRange(new DataTable[] {dt1,dt2});
906 ds.Relations.Add(rel);
910 // Reset - ParentRelations
911 Assert.AreEqual(0 , dt2.ParentRelations.Count , "DT104");
912 // Reset - Constraints
913 Assert.AreEqual(0 , dt2.Constraints.Count , "DT105");
915 Assert.AreEqual(0 , dt2.Rows.Count , "DT106");
917 Assert.AreEqual(0 , dt2.Columns.Count , "DT107");
920 [Test] public void RowChanged()
922 DataTable dt = DataProvider.CreateParentDataTable();
924 dt.RowChanged += new DataRowChangeEventHandler ( Row_Changed );
926 _EventTriggered=false;
928 dt.Rows[0][1] = "NewValue";
929 Assert.AreEqual(true , _EventTriggered , "DT108");
931 _EventTriggered=false;
933 dt.Rows[0].BeginEdit();
934 dt.Rows[0][1] = "NewValue";
935 Assert.AreEqual(false , _EventTriggered , "DT109");
937 _EventTriggered=false;
939 dt.Rows[0].EndEdit();
940 Assert.AreEqual(true , _EventTriggered , "DT110");
942 _EventTriggered=false;
943 dt.RowChanged -= new DataRowChangeEventHandler ( Row_Changed );
945 dt.Rows[0][1] = "NewValue A";
946 Assert.AreEqual(false , _EventTriggered , "DT111");
949 private void Row_Changed( object sender, DataRowChangeEventArgs e )
951 _EventTriggered = true;
954 [Test] public void RowChanging()
956 DataTable dt = DataProvider.CreateParentDataTable();
958 dt.RowChanging += new DataRowChangeEventHandler ( Row_Changing );
960 _EventTriggered=false;
962 dt.Rows[0][1] = "NewValue";
963 Assert.AreEqual(true , _EventTriggered , "DT112");
965 _EventTriggered=false;
967 dt.Rows[0].BeginEdit();
968 dt.Rows[0][1] = "NewValue";
969 Assert.AreEqual(false , _EventTriggered , "DT113");
971 _EventTriggered=false;
973 dt.Rows[0].EndEdit();
974 Assert.AreEqual(true , _EventTriggered , "DT114");
976 _EventTriggered=false;
977 dt.RowChanging -= new DataRowChangeEventHandler ( Row_Changing );
979 dt.Rows[0][1] = "NewValue A";
980 Assert.AreEqual(false , _EventTriggered , "DT115");
983 private void Row_Changing( object sender, DataRowChangeEventArgs e )
985 _EventTriggered = true;
988 [Test] public void RowDeleted()
990 DataTable dt = DataProvider.CreateParentDataTable();
992 dt.RowDeleted += new DataRowChangeEventHandler ( Row_Deleted );
994 _EventTriggered=false;
997 Assert.AreEqual(true , _EventTriggered , "DT116");
999 _EventTriggered=false;
1000 dt.RowDeleted -= new DataRowChangeEventHandler ( Row_Deleted );
1002 dt.Rows[1].Delete();
1003 Assert.AreEqual(false , _EventTriggered , "DT117");
1006 private void Row_Deleted( object sender, DataRowChangeEventArgs e )
1008 _EventTriggered = true;
1011 [Test] public void RowDeleting()
1013 DataTable dt = DataProvider.CreateParentDataTable();
1015 dt.RowDeleting += new DataRowChangeEventHandler ( Row_Deleting );
1017 _EventTriggered=false;
1019 dt.Rows[0].Delete();
1020 Assert.AreEqual(true , _EventTriggered , "DT118");
1022 _EventTriggered=false;
1023 dt.RowDeleting -= new DataRowChangeEventHandler ( Row_Deleting );
1025 dt.Rows[1].Delete();
1026 Assert.AreEqual(false , _EventTriggered , "DT119");
1029 private void Row_Deleting( object sender, DataRowChangeEventArgs e )
1031 _EventTriggered = true;
1034 [Test] public void Rows()
1037 dtParent = DataProvider.CreateParentDataTable();
1040 Assert.AreEqual(true, dtParent.Rows != null, "DT120");
1042 // Checking rows count
1043 Assert.AreEqual(true, dtParent.Rows.Count > 0 , "DT121");
1046 [Test] public void Select()
1048 DataTable dt = DataProvider.CreateParentDataTable();
1050 DataRow[] drSelect = dt.Select();
1051 DataRow[] drResult = new DataRow[dt.Rows.Count] ;
1052 dt.Rows.CopyTo(drResult,0);
1056 Assert.AreEqual(drResult, drSelect , "DT122");
1059 [Test] public void Select_ByFilter()
1061 DataSet ds = new DataSet();
1062 ds.Tables.Add(DataProvider.CreateParentDataTable());
1064 DataTable dt = DataProvider.CreateChildDataTable();
1066 DataRow[] drSelect = null;
1067 System.Collections.ArrayList al = new System.Collections.ArrayList();
1069 //add column with special name
1070 DataColumn dc = new DataColumn("Column#",typeof(int));
1074 dt.Rows[0][dc] = 100;
1075 dt.Rows[1][dc] = 200;
1076 dt.Rows[2][dc] = 300;
1077 dt.Rows[4][dc] = -400;
1080 dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + " \t\n ";
1081 dt.Rows[0]["String1"] = " \t\n " + dt.Rows[0]["String1"] ;
1082 dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + " ";
1084 ds.Tables[0].Rows[0]["ParentBool"] = DBNull.Value;
1085 ds.Tables[0].Rows[2]["ParentBool"] = DBNull.Value;
1086 ds.Tables[0].Rows[3]["ParentBool"] = DBNull.Value;
1088 //-------------------------------------------------------------
1090 foreach (DataRow dr in dt.Rows )
1092 if ((int)dr["ChildId"] == 1)
1097 // Select_S - ChildId=1
1098 drSelect = dt.Select("ChildId=1");
1099 Assert.AreEqual(al.ToArray(), drSelect , "DT123");
1101 //-------------------------------------------------------------
1103 foreach (DataRow dr in dt.Rows )
1105 if ((int)dr["ChildId"] == 1)
1110 // Select_S - ChildId='1'
1111 drSelect = dt.Select("ChildId='1'");
1112 Assert.AreEqual(al.ToArray(), drSelect , "DT124");
1113 //-------------------------------------------------------------
1114 // Select_S - ChildId= '1' (whitespace in filter string.
1115 drSelect = dt.Select("ChildId= '1'");
1116 Assert.AreEqual(al.ToArray(), drSelect , "DT125");
1117 //-------------------------------------------------------------
1119 foreach (DataRow dr in dt.Rows ) if (dr["String1"].ToString() == "1-String1") al.Add(dr);
1120 // Select_S - String1='1-String1'
1121 drSelect = dt.Select("String1='1-String1'");
1122 Assert.AreEqual(al.ToArray(), drSelect , "DT126");
1124 //-------------------------------------------------------------
1126 foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" ) al.Add(dr);
1127 // Select_S - ChildId=1 and String1='1-String1'
1128 drSelect = dt.Select("ChildId=1 and String1='1-String1'");
1129 Assert.AreEqual(al.ToArray(), drSelect , "DT127");
1131 //-------------------------------------------------------------
1133 foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] + (int)dr["ParentId"] >= 4 ) al.Add(dr);
1134 // Select_S - ChildId+ParentId >= 4
1135 drSelect = dt.Select("ChildId+ParentId >= 4");
1136 CompareUnSorted(drSelect ,al.ToArray());
1138 //-------------------------------------------------------------
1140 foreach (DataRow dr in dt.Rows )
1142 if ((((int)dr["ChildId"] - (int)dr["ParentId"]) * -1) != 0 )
1147 // Select_S - ChildId-ParentId) * -1 <> 0
1148 drSelect = dt.Select("(ChildId-ParentId) * -1 <> 0");
1149 CompareUnSorted(drSelect ,al.ToArray());
1151 //-------------------------------------------------------------
1153 foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] < ((int)dr["ParentId"]) % 4 ) al.Add(dr);
1154 // Select_S - ChildDouble < ParentId % 4
1155 drSelect = dt.Select("ChildDouble < ParentId % 4");
1156 CompareUnSorted(drSelect ,al.ToArray());
1158 //-------------------------------------------------------------
1160 foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] == 10 || (double)dr["ChildDouble"] == 20 || (double)dr["ChildDouble"] == 25 ) al.Add(dr);
1161 // Select_S - ChildDouble in (10,20,25)
1162 drSelect = dt.Select("ChildDouble in (10,20,25)");
1163 CompareUnSorted(drSelect ,al.ToArray());
1165 //-------------------------------------------------------------
1167 foreach (DataRow dr in dt.Rows ) if ( dr["String2"].ToString().IndexOf("1-S") >= 0 ) al.Add(dr);
1168 // Select_S - String2 like '%1-S%'
1169 drSelect = dt.Select("String2 like '%1-S%'");
1170 Assert.AreEqual(al.ToArray(), drSelect , "DT128");
1172 //-------------------------------------------------------------
1173 //If a column name contains one of the above characters,(ex. #\/=><+-*%&|^'" and so on) the name must be wrapped in brackets. For example to use a column named "Column#" in an expression, you would write "[Column#]":
1175 foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
1176 // Select_S - [Column#] <= 0
1177 drSelect = dt.Select("[Column#] <= 0 ");
1178 CompareUnSorted(drSelect ,al.ToArray());
1179 //-------------------------------------------------------------
1181 foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
1182 // Select_S - [Column#] <= 0
1183 drSelect = dt.Select("[Column#] <= 0");
1184 CompareUnSorted(drSelect ,al.ToArray());
1186 //-------------------------------------------------------------
1188 foreach (DataRow dr in dt.Rows ) if (((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2000,12,12)) > 0 ) al.Add(dr);
1189 // Select_S - ChildDateTime > #12/12/2000#
1190 drSelect = dt.Select("ChildDateTime > #12/12/2000# ");
1191 CompareUnSorted(drSelect ,al.ToArray());
1193 //-------------------------------------------------------------
1196 foreach (DataRow dr in dt.Rows ) if ( ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1999,1,12,12,06,30)) > 0 ) al.Add(dr);
1197 // Select_S - ChildDateTime > #1/12/1999 12:06:30 PM#
1198 drSelect = dt.Select("ChildDateTime > #1/12/1999 12:06:30 PM# ");
1199 CompareUnSorted(drSelect ,al.ToArray());
1201 //-------------------------------------------------------------
1204 foreach (DataRow dr in dt.Rows ) if ( ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2005,12,03,17,06,30)) >= 0 || ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1980,11,03)) <= 0 ) al.Add(dr);
1205 // Select_S - ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980#
1206 drSelect = dt.Select("ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980# ");
1207 CompareUnSorted(drSelect ,al.ToArray());
1210 //-------------------------------------------------------------
1212 foreach (DataRow dr in dt.Rows ) if ( dr["ChildDouble"].ToString().Length > 10) al.Add(dr);
1213 // Select_S - Len(Convert(ChildDouble,'System.String')) > 10
1214 drSelect = dt.Select("Len(Convert(ChildDouble,'System.String')) > 10");
1215 Assert.AreEqual(al.ToArray(), drSelect , "DT129");
1217 //-------------------------------------------------------------
1219 foreach (DataRow dr in dt.Rows ) if ( dr["String1"].ToString().Trim().Substring(0,2) == "1-") al.Add(dr);
1220 // Select_S - SubString(Trim(String1),1,2) = '1-'
1221 drSelect = dt.Select("SubString(Trim(String1),1,2) = '1-'");
1222 Assert.AreEqual(al.ToArray(), drSelect , "DT130");
1223 //-------------------------------------------------------------
1226 foreach (DataRow dr in ds.Tables[0].Rows ) if ( dr.IsNull("ParentBool") || (bool)dr["ParentBool"]) al.Add(dr);
1227 // Select_S - IsNull(ParentBool,true)
1228 drSelect = ds.Tables[0].Select("IsNull(ParentBool,true) ");
1229 Assert.AreEqual(al.ToArray(), drSelect , "DT131");
1231 //-------------------------------------------------------------
1233 // Select_S - Relation not exists, Exception
1236 drSelect = dt.Select("Parent.ParentId = ChildId");
1237 Assert.Fail("DT132: Select Failed to throw IndexOutOfRangeException");
1239 catch (IndexOutOfRangeException) {}
1240 catch (AssertionException exc) {throw exc;}
1241 catch (Exception exc)
1243 Assert.Fail("DT133: Select. Wrong exception type. Got:" + exc);
1245 //-------------------------------------------------------------
1247 ds.Relations.Add(new DataRelation("ParentChild",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
1248 foreach (DataRow dr in dt.Rows ) if ( (int)dr["ChildId"] == (int)dr.GetParentRow("ParentChild")["ParentId"]) al.Add(dr);
1249 // Select_S - Parent.ParentId = ChildId
1250 drSelect = dt.Select("Parent.ParentId = ChildId");
1251 Assert.AreEqual(al.ToArray(), drSelect , "DT134");
1254 private void CompareUnSorted(Array a, Array b)
1256 string msg = string.Format("Failed while comparing(Array a ={0} ({1}), Array b = {2} ({3}))]", a.ToString(), a.GetType().FullName, b.ToString(), b.GetType().FullName);
1257 foreach (object item in a)
1259 if (Array.IndexOf(b, item) < 0) //b does not contain the current item.
1265 foreach (object item in b)
1267 if (Array.IndexOf(a, item) < 0) //a does not contain the current item.
1275 [Test] public void Select_ByFilterDataViewRowState()
1277 DataTable dt = DataProvider.CreateParentDataTable();
1278 DataRow[] drSelect, drResult;
1280 dt.Rows[0].Delete();
1281 dt.Rows[1]["ParentId"] = 1;
1282 dt.Rows[2]["ParentId"] = 1;
1283 dt.Rows[3].Delete();
1284 dt.Rows.Add(new object[] {1,"A","B"});
1285 dt.Rows.Add(new object[] {1,"C","D"});
1286 dt.Rows.Add(new object[] {1,"E","F"});
1288 drSelect = dt.Select("ParentId=1","",DataViewRowState.Added );
1289 drResult = GetResultRows(dt,DataRowState.Added);
1290 // Select_SSD DataViewRowState.Added
1291 Assert.AreEqual(drResult, drSelect , "DT135");
1293 drSelect = dt.Select("ParentId=1","",DataViewRowState.CurrentRows );
1294 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified );
1295 // Select_SSD DataViewRowState.CurrentRows
1296 Assert.AreEqual(drResult, drSelect , "DT136");
1298 drSelect = dt.Select("ParentId=1","",DataViewRowState.Deleted );
1299 drResult = GetResultRows(dt,DataRowState.Deleted );
1300 // Select_SSD DataViewRowState.Deleted
1301 Assert.AreEqual(drResult, drSelect , "DT137");
1303 drSelect = dt.Select("ParentId=1","",DataViewRowState.ModifiedCurrent | DataViewRowState.ModifiedOriginal );
1304 drResult = GetResultRows(dt,DataRowState.Modified );
1305 // Select_SSD ModifiedCurrent or ModifiedOriginal
1306 Assert.AreEqual(drResult, drSelect , "DT138");
1309 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
1311 System.Collections.ArrayList al = new System.Collections.ArrayList();
1312 DataRowVersion drVer = DataRowVersion.Current;
1314 //From MSDN - The row the default version for the current DataRowState.
1315 // For a DataRowState value of Added, Modified or Current,
1316 // the default version is Current.
1317 // For a DataRowState of Deleted, the version is Original.
1318 // For a DataRowState value of Detached, the version is Proposed.
1320 if ( ((State & DataRowState.Added) > 0)
1321 | ((State & DataRowState.Modified) > 0)
1322 | ((State & DataRowState.Unchanged) > 0) )
1323 drVer = DataRowVersion.Current;
1324 if ( (State & DataRowState.Deleted) > 0
1325 | (State & DataRowState.Detached) > 0 )
1326 drVer = DataRowVersion.Original;
1328 foreach (DataRow dr in dt.Rows )
1330 if ( dr.HasVersion(drVer)
1331 && ((int)dr["ParentId", drVer] == 1)
1332 && ((dr.RowState & State) > 0 )
1336 DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
1340 [Test] public void TableName()
1342 DataTable dtParent = new DataTable();
1344 // Checking TableName default
1345 Assert.AreEqual(String.Empty, dtParent.TableName, "DT139");
1347 // Checking TableName set/get
1348 String s = "MyTable";
1349 dtParent.TableName=s;
1350 Assert.AreEqual(s, dtParent.TableName, "DT140");
1353 [Test] public new void ToString()
1355 DataTable dt = DataProvider.CreateParentDataTable();
1356 dt.DisplayExpression = dt.Columns[0].ColumnName ;
1358 string sToString = dt.TableName + " + " + dt.DisplayExpression;
1360 Assert.AreEqual(sToString , dt.ToString() , "DT141");
1363 [Test] public void caseSensitive()
1365 DataTable dtParent = new DataTable();
1368 Assert.AreEqual(false, dtParent.CaseSensitive , "DT142");
1371 dtParent.CaseSensitive = true;
1372 Assert.AreEqual(true, dtParent.CaseSensitive , "DT143");
1375 [Test] public void ctor()
1378 dt = new DataTable();
1381 Assert.AreEqual(false, dt == null, "DT144");
1384 [Test] public void ctor_ByName()
1387 string sName = "MyName";
1389 dt = new DataTable(sName);
1392 Assert.AreEqual(false , dt == null , "DT145");
1395 Assert.AreEqual(sName , dt.TableName , "DT146");
1398 [Test] public void displayExpression()
1401 dtParent= DataProvider.CreateParentDataTable();
1403 // Checking DisplayExpression default
1404 Assert.AreEqual(String.Empty , dtParent.DisplayExpression , "DT147");
1406 // Checking DisplayExpression Set/Get
1407 dtParent.DisplayExpression = dtParent.Columns[0].ColumnName;
1408 Assert.AreEqual(dtParent.Columns[0].ColumnName, dtParent.DisplayExpression , "DT148");
1411 [Test] public void extendedProperties()
1414 PropertyCollection pc;
1415 dtParent= DataProvider.CreateParentDataTable();
1417 pc = dtParent.ExtendedProperties ;
1419 // Checking ExtendedProperties default
1420 Assert.AreEqual(true, pc != null, "DT149");
1422 // Checking ExtendedProperties count
1423 Assert.AreEqual(0, pc.Count , "DT150");
1426 [Test] public void primaryKey()
1429 dtParent = DataProvider.CreateParentDataTable();
1431 // Checking PrimaryKey default
1432 Assert.AreEqual(0, dtParent.PrimaryKey.Length , "DT151");
1434 // Checking PrimaryKey set/get
1435 DataColumn[] dcArr = new DataColumn[] {dtParent.Columns[0]};
1436 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
1437 Assert.AreEqual(dcArr, dtParent.PrimaryKey , "DT152");
1439 dtParent.PrimaryKey=null;
1440 DataSet ds = new DataSet();
1442 ds.Tables.Add(dtParent);
1444 //check primary key - ColumnType String, ds.CaseSensitive = false;
1445 ds.CaseSensitive = false;
1446 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["String1"]};
1447 // check primary key - ColumnType String, ds.CaseSensitive = false;
1448 dr = dtParent.NewRow();
1449 dr.ItemArray = dtParent.Rows[0].ItemArray;
1450 dr["String1"] = dr["String1"].ToString().ToUpper();
1453 dtParent.Rows.Add(dr);
1454 Assert.Fail("DT153: Rows.Add Failed to throw ConstraintException");
1456 catch (ConstraintException) {}
1457 catch (AssertionException exc) {throw exc;}
1458 catch (Exception exc)
1460 Assert.Fail("DT154: Rows.Add. Wrong exception type. Got:" + exc);
1462 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1464 //check primary key - ColumnType String, ds.CaseSensitive = true;
1465 ds.CaseSensitive = true;
1466 // check primary key ConstraintException - ColumnType String, ds.CaseSensitive = true;
1467 dr = dtParent.NewRow();
1468 dr.ItemArray = dtParent.Rows[0].ItemArray;
1469 dr["String1"] = dr["String1"].ToString();
1472 dtParent.Rows.Add(dr);
1473 Assert.Fail("DT155: Rows.Add Failed to throw ConstraintException");
1475 catch (ConstraintException) {}
1476 catch (AssertionException exc) {throw exc;}
1477 catch (Exception exc)
1479 Assert.Fail("DT156: Rows.Add. Wrong exception type. Got:" + exc);
1481 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1483 //check primary key - ColumnType String, ds.CaseSensitive = true;
1484 ds.CaseSensitive = true;
1486 // check primary key - ColumnType String, ds.CaseSensitive = true;
1487 dr = dtParent.NewRow();
1488 dr.ItemArray = dtParent.Rows[0].ItemArray;
1489 dr["String1"] = dr["String1"].ToString().ToUpper() ;
1490 dtParent.Rows.Add(dr);
1491 Assert.AreEqual(true, dtParent.Rows.Contains(dr["String1"]), "DT157");
1493 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1495 dtParent.PrimaryKey=null;
1496 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDateTime"]};
1497 // check primary key - ColumnType DateTime
1498 dr = dtParent.NewRow();
1499 dr.ItemArray = dtParent.Rows[0].ItemArray;
1500 dr["ParentDateTime"] = DateTime.Now;
1501 dtParent.Rows.Add(dr);
1502 Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDateTime"]), "DT158");
1503 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1505 // check primary key ConstraintException- ColumnType DateTime
1506 dr = dtParent.NewRow();
1507 dr.ItemArray = dtParent.Rows[0].ItemArray;
1508 dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1511 dtParent.Rows.Add(dr);
1512 Assert.Fail("DT159: Rows.Add Failed to throw ConstraintException");
1514 catch (ConstraintException) {}
1515 catch (AssertionException exc) {throw exc;}
1516 catch (Exception exc)
1518 Assert.Fail("DT160: Rows.Add. Wrong exception type. Got:" + exc);
1520 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1522 dtParent.PrimaryKey=null;
1523 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"]};
1524 // check primary key - ColumnType ParentDouble, value=Epsilon
1525 dr = dtParent.NewRow();
1526 dr.ItemArray = dtParent.Rows[0].ItemArray;
1527 dr["ParentDouble"] = Double.Epsilon ;
1528 dtParent.Rows.Add(dr);
1529 Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDouble"]), "DT161");
1530 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1532 // check primary key ConstraintException - ColumnType ParentDouble
1533 dr = dtParent.NewRow();
1534 dr.ItemArray = dtParent.Rows[0].ItemArray;
1535 dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1538 dtParent.Rows.Add(dr);
1539 Assert.Fail("DT162: Rows.Add Failed to throw ConstraintException");
1541 catch (ConstraintException) {}
1542 catch (AssertionException exc) {throw exc;}
1543 catch (Exception exc)
1545 Assert.Fail("DT163: Rows.Add. Wrong exception type. Got:" + exc);
1547 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1552 dtParent.PrimaryKey=null;
1553 // check primary key ConstraintException - ColumnType ParentBool
1556 //ParentBool is not unique, will raise exception
1557 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentBool"]};
1558 Assert.Fail("DT164: PrimaryKey Failed to throw ArgumentException");
1560 catch (ArgumentException) {}
1561 catch (AssertionException exc) {throw exc;}
1562 catch (Exception exc)
1564 Assert.Fail("DT165: PrimaryKey. Wrong exception type. Got:" + exc);
1566 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1571 dtParent.PrimaryKey=null;
1572 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"],dtParent.Columns["ParentDateTime"]};
1573 // check primary key - ColumnType Double,DateTime test1
1574 dr = dtParent.NewRow();
1575 dr.ItemArray = dtParent.Rows[0].ItemArray;
1576 dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1577 dr["ParentDateTime"] = DateTime.Now;
1578 dtParent.Rows.Add(dr);
1579 Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT166");
1580 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1582 // check primary key - ColumnType Double,DateTime test2
1583 dr = dtParent.NewRow();
1584 dr.ItemArray = dtParent.Rows[0].ItemArray;
1585 dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1586 dr["ParentDouble"] = 99.399;
1587 dtParent.Rows.Add(dr);
1588 Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT167");
1589 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1591 // check primary key ConstraintException - ColumnType Double,DateTime
1592 dr = dtParent.NewRow();
1593 dr.ItemArray = dtParent.Rows[0].ItemArray;
1594 dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1595 dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1598 dtParent.Rows.Add(dr);
1599 Assert.Fail("DT168: Rows.Add Failed to throw ConstraintException");
1601 catch (ConstraintException) {}
1602 catch (AssertionException exc) {throw exc;}
1603 catch (Exception exc)
1605 Assert.Fail("DT169: Rows.Add. Wrong exception type. Got:" + exc);
1607 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1609 DataTable dtChild = DataProvider.CreateChildDataTable();
1610 ds.Tables.Add(dtChild);
1611 dtParent.PrimaryKey = null;
1612 //this test was addedd to check java exception:
1613 //System.ArgumentException: Cannot remove UniqueConstraint because the ForeignKeyConstraint myRelation exists.
1614 // check add primary key with relation
1615 ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
1616 //the following line will cause java to fail
1617 ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
1618 Assert.AreEqual(2, ds.Tables[0].PrimaryKey.Length , "DT170");