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.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();
73 public void OnRowChanged_Test(DataRowAction drAction )
\r
75 base.OnRowChanged(new DataRowChangeEventArgs(this.Rows[0],drAction ));
\r
78 public void OnRowChanging_Test(DataRowAction drAction )
\r
80 base.OnRowChanging(new DataRowChangeEventArgs(this.Rows[0],drAction ));
\r
83 public void OnRowDeleted_Test(DataRowAction drAction )
\r
85 base.OnRowDeleted(new DataRowChangeEventArgs(this.Rows[0],drAction ));
\r
88 public void OnRowDeleting_Test(DataRowAction drAction )
\r
90 base.OnRowDeleting(new DataRowChangeEventArgs(this.Rows[0],drAction ));
\r
94 [Test] public void AcceptChanges()
96 String sNewValue = "NewValue";
97 DataRow drModified,drDeleted,drAdded;
98 DataTable dt = DataProvider.CreateParentDataTable();
100 drModified = dt.Rows[0];
101 drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
103 drDeleted = dt.Rows[1];
104 drDeleted.Delete(); //DataRowState = Deleted
106 drAdded = dt.NewRow();
107 dt.Rows.Add(drAdded); //DataRowState = Added
111 // AcceptChanges - Unchanged1
112 Assert.AreEqual(DataRowState.Unchanged , drModified.RowState , "DT1");
114 // AcceptChanges - Current
115 Assert.AreEqual(sNewValue , drModified[1,DataRowVersion.Current] , "DT2");
117 // AcceptChanges - Unchanged2
118 Assert.AreEqual(DataRowState.Unchanged , drAdded.RowState , "DT3");
120 // AcceptChanges - Detached
121 Assert.AreEqual(DataRowState.Detached , drDeleted.RowState , "DT4");
124 [Test] public void ChildRelations()
126 DataTable dtChild,dtParent;
127 DataSet ds = new DataSet();
129 dtChild = DataProvider.CreateChildDataTable();
130 dtParent= DataProvider.CreateParentDataTable();
131 //Add tables to dataset
132 ds.Tables.Add(dtChild);
133 ds.Tables.Add(dtParent);
135 DataRelationCollection drlCollection;
136 DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
138 // Checking ChildRelations - default value
140 drlCollection = dtParent.ChildRelations;
141 Assert.AreEqual(0, drlCollection.Count , "DT5");
143 ds.Relations.Add(drl);
144 drlCollection = dtParent.ChildRelations;
146 // Checking ChildRelations Count
147 Assert.AreEqual(1, drlCollection.Count , "DT6");
149 // Checking ChildRelations Value
150 Assert.AreEqual(drl, drlCollection[0] , "DT7");
153 [Test] public void Clear()
155 DataTable dt = DataProvider.CreateParentDataTable();
158 Assert.AreEqual(0, dt.Rows.Count , "DT8");
161 [Test] public void Clone()
163 DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
164 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
165 dt2.Columns[0].DefaultValue=7;
169 for (int i=0; i<dt2.Constraints.Count; i++)
171 // Clone - Constraints[{0}],i)
172 Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT9");
175 for (int i=0; i<dt2.Columns.Count; i++)
177 // Clone - Columns[{0}].ColumnName,i)
178 Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT10");
180 // Clone - Columns[{0}].DataType,i)
181 Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT11");
185 [Test] public void ColumnChanged()
187 DataTable dt = DataProvider.CreateParentDataTable();
189 dt.ColumnChanged += new DataColumnChangeEventHandler( Column_Changed );
191 _EventTriggered=false;
192 // ColumnChanged - EventTriggered
193 dt.Rows[0][1] = "NewValue";
194 Assert.AreEqual(true , _EventTriggered , "DT12");
196 _EventTriggered=false;
197 dt.ColumnChanged -= new DataColumnChangeEventHandler( Column_Changed );
198 // ColumnChanged - NO EventTriggered
199 dt.Rows[0][1] = "VeryNewValue";
200 Assert.AreEqual(false , _EventTriggered , "DT13");
203 private void Column_Changed( object sender, DataColumnChangeEventArgs e )
205 _EventTriggered = true;
208 [Test] public void ColumnChanging()
210 DataTable dt = DataProvider.CreateParentDataTable();
212 dt.ColumnChanging += new DataColumnChangeEventHandler( Column_Changeding );
214 _EventTriggered=false;
215 // ColumnChanged - EventTriggered
216 dt.Rows[0][1] = "NewValue";
217 Assert.AreEqual(true , _EventTriggered , "DT14");
219 _EventTriggered=false;
220 dt.ColumnChanging -= new DataColumnChangeEventHandler( Column_Changeding );
221 // ColumnChanged - NO EventTriggered
222 dt.Rows[0][1] = "VeryNewValue";
223 Assert.AreEqual(false , _EventTriggered , "DT15");
226 private void Column_Changeding( object sender, DataColumnChangeEventArgs e )
228 _EventTriggered = true;
231 [Test] public void Columns()
234 DataColumnCollection dcl;
235 dtParent= DataProvider.CreateParentDataTable();
237 dcl = dtParent.Columns;
239 // Checking ColumnsCollection != null
240 Assert.AreEqual(false, dcl == null , "DT16");
242 // Checking ColumnCollection Count 1
243 Assert.AreEqual(6, dcl.Count , "DT17");
245 // Checking ColumnCollection Count 2
246 dtParent.Columns.Add(new DataColumn("Test"));
247 Assert.AreEqual(7, dcl.Count , "DT18");
249 // Checking ColumnCollection - get columnn by different case
250 DataColumn tmp = dtParent.Columns["TEST"];
251 Assert.AreEqual(dtParent.Columns["Test"], tmp , "DT19");
253 // Checking ColumnCollection colummn name case sensetive
254 dtParent.Columns.Add(new DataColumn("test"));
255 Assert.AreEqual(8, dcl.Count , "DT20");
257 // Checking ColumnCollection - get columnn by different case,ArgumentException
260 DataColumn tmp1 = dtParent.Columns["TEST"];
261 Assert.Fail("DT21: indexer Failed to throw ArgumentException");
263 catch (ArgumentException) {}
264 catch (AssertionException exc) {throw exc;}
265 catch (Exception exc)
267 Assert.Fail("DT22: Indexer. Wrong exception type. Got:" + exc);
271 [Test] public void Compute()
273 DataTable dt = DataProvider.CreateChildDataTable();
276 DataRow[] drArr = dt.Select("ParentId=1");
278 foreach (DataRow dr in drArr)
280 iExSum += (int)dr["ChildId"];
282 object objCompute=null;
283 // Compute - sum values
284 objCompute = dt.Compute("Sum(ChildId)","ParentId=1");
285 Assert.AreEqual(Int64.Parse(objCompute.ToString()) , Int64.Parse(iExSum.ToString()), "DT23");
287 // Compute - sum type
288 Assert.AreEqual(typeof(Int64).FullName , objCompute.GetType().FullName, "DT24");
292 drArr = dt.Select("ParentId=5");
293 foreach (DataRow dr in drArr)
295 iExAvg += (double)dr["ChildDouble"];
297 iExAvg = iExAvg / drArr.Length;
299 // Compute - Avg value
300 objCompute = dt.Compute("Avg(ChildDouble)","ParentId=5");
301 Assert.AreEqual(double.Parse(objCompute.ToString()) , double.Parse(iExAvg.ToString()), "DT25");
303 // Compute - Avg type
304 Assert.AreEqual(typeof(double).FullName , objCompute.GetType().FullName, "DT26");
307 [Test] public void Constraints()
310 ConstraintCollection consColl;
311 dtParent= DataProvider.CreateParentDataTable();
313 consColl = dtParent.Constraints;
314 // Checking Constraints != null
315 Assert.AreEqual(false, consColl == null , "DT27");
317 // Checking Constraints Count
318 Assert.AreEqual(0, consColl.Count , "DT28");
320 // Checking Constraints Count
322 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
323 Assert.AreEqual(1, consColl.Count , "DT29");
326 [Test] public void Copy()
328 DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
329 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
330 dt2.Columns[0].DefaultValue=7;
334 for (int i=0; i<dt2.Constraints.Count; i++)
336 // Copy - Constraints[{0}],i)
337 Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT30");
340 for (int i=0; i<dt2.Columns.Count; i++)
342 // Copy - Columns[{0}].ColumnName,i)
343 Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT31");
345 // Copy - Columns[{0}].DataType,i)
346 Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT32");
349 DataRow[] drArr1,drArr2;
350 drArr1 = dt1.Select("");
351 drArr2 = dt2.Select("");
352 for (int i=0; i<drArr1.Length ; i++)
354 // Copy - Data [ParentId]{0} ,i)
355 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT33");
356 // Copy - Data [String1]{0} ,i)
357 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT34");
358 // Copy - Data [String2]{0} ,i)
359 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2"], "DT35");
363 [Test] public void CreateInstance()
366 ProtectedTestClass C = new ProtectedTestClass();
367 DataTable dt = C.CreateInstance_Test();
368 Assert.AreEqual(true , dt != null , "DT36");
371 [Test] public void DataSet()
375 dtParent= DataProvider.CreateParentDataTable();
377 ds = dtParent.DataSet;
379 // Checking DataSet == null
380 Assert.AreEqual(null, ds, "DT37");
382 // Checking DataSet != null
383 ds = new DataSet("MyDataSet");
384 ds.Tables.Add(dtParent);
385 Assert.AreEqual(true, dtParent.DataSet != null , "DT38");
387 // Checking DataSet Name
388 Assert.AreEqual("MyDataSet", dtParent.DataSet.DataSetName , "DT39");
391 [Test] public void DefaultView()
395 dtParent= DataProvider.CreateParentDataTable();
396 dv = dtParent.DefaultView ;
398 // Checking DataView != null
399 Assert.AreEqual(true, dv != null, "DT40");
402 [Test] public void EndLoadData()
404 DataTable dt = DataProvider.CreateParentDataTable();
405 dt.Columns[0].AllowDBNull = false;
409 dt.LoadDataRow(new object[] {null,"A","B"},false);
413 //ConstraintException will be throw
415 Assert.Fail("DT41: EndLoadData Failed to throw ConstraintException");
417 catch (ConstraintException) {}
418 catch (AssertionException exc) {throw exc;}
419 catch (Exception exc)
421 Assert.Fail("DT42: EndLoadData Wrong exception type. Got:" + exc);
425 [Test] public void GetChanges()
427 DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
428 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
429 dt2.Columns[0].DefaultValue=7;
432 dt2.Rows[0].Delete();
433 dt2.Rows[1].Delete();
434 dt2.Rows[2].Delete();
435 dt2.Rows[3].Delete();
437 dt1 = dt2.GetChanges();
439 for (int i=0; i<dt2.Constraints.Count; i++)
441 // GetChanges - Constraints[{0}],i)
442 Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT43");
445 for (int i=0; i<dt2.Columns.Count; i++)
447 // GetChanges - Columns[{0}].ColumnName,i)
448 Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT44");
450 // GetChanges - Columns[{0}].DataType,i)
451 Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT45");
454 DataRow[] drArr1,drArr2;
456 drArr1 = dt1.Select("","",DataViewRowState.Deleted );
457 drArr2 = dt2.Select("","",DataViewRowState.Deleted );
459 for (int i=0; i<drArr1.Length ; i++)
461 // GetChanges - Data [ParentId]{0} ,i)
462 Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT46");
463 // GetChanges - Data [String1]{0} ,i)
464 Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original], "DT47");
465 // GetChanges - Data [String2]{0} ,i)
466 Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original], "DT48");
470 [Test] public void GetChanges_ByDataRowState()
472 DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
473 dt2.Constraints.Add("Unique",dt2.Columns[0],true);
474 dt2.Columns[0].DefaultValue=7;
477 dt2.Rows[0].Delete(); //DataRowState.Deleted
478 dt2.Rows[1].Delete(); //DataRowState.Deleted
479 dt2.Rows[2].BeginEdit();
480 dt2.Rows[2]["String1"] = "Changed"; //DataRowState.Modified
481 dt2.Rows[2].EndEdit();
483 dt2.Rows.Add(new object[] {"99","Temp1","Temp2"}); //DataRowState.Added
485 // *********** Checking GetChanges - DataRowState.Deleted ************
487 dt1 = dt2.GetChanges(DataRowState.Deleted);
488 CheckTableSchema (dt1,dt2,DataRowState.Deleted.ToString());
489 DataRow[] drArr1,drArr2;
490 drArr1 = dt1.Select("","",DataViewRowState.Deleted );
491 drArr2 = dt2.Select("","",DataViewRowState.Deleted );
493 for (int i=0; i<drArr1.Length ; i++)
495 // GetChanges(Deleted) - Data [ParentId]{0} ,i)
496 Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT49");
497 // GetChanges(Deleted) - Data [String1]{0} ,i)
498 Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original], "DT50");
499 // GetChanges(Deleted) - Data [String2]{0} ,i)
500 Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original], "DT51");
503 // *********** Checking GetChanges - DataRowState.Modified ************
505 dt1 = dt2.GetChanges(DataRowState.Modified);
506 CheckTableSchema (dt1,dt2,DataRowState.Modified.ToString());
507 drArr1 = dt1.Select("","");
508 drArr2 = dt2.Select("","",DataViewRowState.ModifiedCurrent);
510 for (int i=0; i<drArr1.Length ; i++)
512 // GetChanges(Modified) - Data [ParentId]{0} ,i)
513 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT52");
514 // GetChanges(Modified) - Data [String1]{0} ,i)
515 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT53");
516 // GetChanges(Modified) - Data [String2]{0} ,i)
517 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT54");
520 // *********** Checking GetChanges - DataRowState.Added ************
522 dt1 = dt2.GetChanges(DataRowState.Added);
523 CheckTableSchema (dt1,dt2,DataRowState.Added.ToString());
524 drArr1 = dt1.Select("","");
525 drArr2 = dt2.Select("","",DataViewRowState.Added );
527 for (int i=0; i<drArr1.Length ; i++)
529 // GetChanges(Added) - Data [ParentId]{0} ,i)
530 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT55");
531 // GetChanges(Added) - Data [String1]{0} ,i)
532 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT56");
533 // GetChanges(Added) - Data [String2]{0} ,i)
534 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT57");
537 // *********** Checking GetChanges - DataRowState.Unchanged ************
539 dt1 = dt2.GetChanges(DataRowState.Unchanged);
540 CheckTableSchema (dt1,dt2,DataRowState.Unchanged .ToString());
541 drArr1 = dt1.Select("","");
542 drArr2 = dt2.Select("","",DataViewRowState.Unchanged );
544 for (int i=0; i<drArr1.Length ; i++)
546 // GetChanges(Unchanged) - Data [ParentId]{0} ,i)
547 Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT58");
548 // GetChanges(Unchanged) - Data [String1]{0} ,i)
549 Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT59");
550 // GetChanges(Unchanged) - Data [String2]{0} ,i)
551 Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT60");
555 private void CheckTableSchema(DataTable dt1, DataTable dt2,string Description)
557 for (int i=0; i<dt2.Constraints.Count; i++)
559 // GetChanges - Constraints[{0}] - {1},i,Description)
560 Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT61");
563 for (int i=0; i<dt2.Columns.Count; i++)
565 // GetChanges - Columns[{0}].ColumnName - {1},i,Description)
566 Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT62");
568 // GetChanges - Columns[{0}].DataType {1},i,Description)
569 Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT63");
573 [Test] public void GetErrors()
575 DataTable dt = DataProvider.CreateParentDataTable();
576 DataRow[] drArr = new DataRow[3];
577 drArr[0] = dt.Rows[0];
578 drArr[1] = dt.Rows[2];
579 drArr[2] = dt.Rows[5];
581 drArr[0].RowError = "Error1";
582 drArr[1].RowError = "Error2";
583 drArr[2].RowError = "Error3";
586 Assert.AreEqual(dt.GetErrors(), drArr , "DT64");
589 [Test] public new void GetHashCode()
591 DataTable dt = DataProvider.CreateParentDataTable();
593 iHashCode = dt.GetHashCode();
595 for (int i=0; i<10 ;i++)
597 // HashCode - i= + i.ToString()
598 Assert.AreEqual(dt.GetHashCode() , iHashCode , "DT65");
602 [Test] public new void GetType()
604 DataTable dt = DataProvider.CreateParentDataTable();
605 Type tmpType = typeof(DataTable);
608 Assert.AreEqual(tmpType, dt.GetType() , "DT66");
611 [Test] public void HasErrors()
614 dtParent= DataProvider.CreateParentDataTable();
616 // Checking HasErrors default
617 Assert.AreEqual(false, dtParent.HasErrors , "DT67");
619 // Checking HasErrors Get
620 dtParent.Rows[0].RowError = "Error on row 0";
621 dtParent.Rows[2].RowError = "Error on row 2";
622 Assert.AreEqual(true, dtParent.HasErrors , "DT68");
625 [Test] public void ImportRow()
628 dt1 = DataProvider.CreateParentDataTable();
629 dt2 = DataProvider.CreateParentDataTable();
630 DataRow dr = dt2.NewRow();
631 dr.ItemArray = new object[] {99,"",""};
634 // ImportRow - Values
636 Assert.AreEqual(dr.ItemArray, dt1.Rows[dt1.Rows.Count-1].ItemArray , "DT69");
638 // ImportRow - DataRowState
639 Assert.AreEqual(dr.RowState , dt1.Rows[dt1.Rows.Count-1].RowState , "DT70");
642 [Test] public void LoadDataRow()
646 dt = DataProvider.CreateParentDataTable();
647 dt.PrimaryKey= new DataColumn[] {dt.Columns[0]}; //add ParentId as Primary Key
648 dt.Columns["String1"].DefaultValue = "Default";
650 dr = dt.Select("ParentId=1")[0];
652 //Update existing row without accept changes
654 dt.LoadDataRow(new object[] {1,null,"Changed"},false);
657 // LoadDataRow(update1) - check column String1
658 Assert.AreEqual(dr["String1"], dt.Columns["String1"].DefaultValue , "DT71");
660 // LoadDataRow(update1) - check column String2
661 Assert.AreEqual(dr["String2"], "Changed", "DT72");
663 // LoadDataRow(update1) - check row state
664 Assert.AreEqual(DataRowState.Modified , dr.RowState , "DT73");
666 //Update existing row with accept changes
667 dr = dt.Select("ParentId=2")[0];
670 dt.LoadDataRow(new object[] {2,null,"Changed"},true);
673 // LoadDataRow(update2) - check row state
674 Assert.AreEqual(DataRowState.Unchanged , dr.RowState , "DT74");
676 //Add New row without accept changes
678 dt.LoadDataRow(new object[] {99,null,"Changed"},false);
681 // LoadDataRow(insert1) - check column String2
682 dr = dt.Select("ParentId=99")[0];
683 Assert.AreEqual("Changed" , dr["String2"] , "DT75");
685 // LoadDataRow(insert1) - check row state
686 Assert.AreEqual(DataRowState.Added , dr.RowState , "DT76");
688 //Add New row with accept changes
690 dt.LoadDataRow(new object[] {100,null,"Changed"},true);
693 // LoadDataRow(insert2) - check row and values
694 dr = dt.Select("ParentId=100")[0];
695 Assert.AreEqual("Changed" , dr["String2"] , "DT77");
697 // LoadDataRow(insert2) - check row state
698 Assert.AreEqual(DataRowState.Unchanged , dr.RowState , "DT78");
701 [Test] public void Locale()
704 DataSet ds = new DataSet("MyDataSet");
706 dtParent= DataProvider.CreateParentDataTable();
707 ds.Tables.Add(dtParent);
708 System.Globalization.CultureInfo culInfo = System.Globalization.CultureInfo.CurrentCulture ;
710 // Checking Locale default from system
711 Assert.AreEqual(culInfo, dtParent.Locale , "DT79");
713 // Checking Locale default from dataset
714 culInfo = new System.Globalization.CultureInfo("fr"); // = French
716 Assert.AreEqual(culInfo , dtParent.Locale , "DT80");
718 // Checking Locale get/set
719 culInfo = new System.Globalization.CultureInfo("fr"); // = French
720 dtParent.Locale = culInfo ;
721 Assert.AreEqual(culInfo , dtParent.Locale , "DT81");
724 [Test] public void MinimumCapacity()
726 // i get default=50, according to MSDN the value should be 25
727 // // Checking MinimumCapacity default = 25
728 // Assert.AreEqual(25, dtParent.MinimumCapacity , "DT82");
730 DataTable dt = new DataTable();
732 // Checking MinimumCapacity get/set int.MaxValue
733 dt.MinimumCapacity = int.MaxValue;
734 Assert.AreEqual(int.MaxValue, dt.MinimumCapacity , "DT83");
736 // Checking MinimumCapacity get/set 0
737 dt.MinimumCapacity = 0;
738 Assert.AreEqual(0, dt.MinimumCapacity , "DT84");
740 // // Checking MinimumCapacity get/set int.MinValue
741 // dtParent.MinimumCapacity = int.MinValue ;
742 // Assert.AreEqual(int.MinValue, dtParent.MinimumCapacity , "DT85");
746 [Test] public void Namespace()
748 DataTable dtParent = new DataTable();
750 // Checking Namespace default
751 Assert.AreEqual(String.Empty, dtParent.Namespace, "DT86");
753 // Checking Namespace set/get
754 String s = "MyNamespace";
755 dtParent.Namespace=s;
756 Assert.AreEqual(s, dtParent.Namespace, "DT87");
759 [Test] public void NewRow()
763 dt = DataProvider.CreateParentDataTable();
767 Assert.AreEqual(true , dr != null , "DT88");
770 [Test] public void OnColumnChanged()
772 ProtectedTestClass dt = new ProtectedTestClass();
775 dt.OnColumnChanged_Test();
776 // OnColumnChanged Event 1
777 Assert.AreEqual(false , EventRaised , "DT89");
780 dt.ColumnChanged += new DataColumnChangeEventHandler(OnColumnChanged_Handler);
781 dt.OnColumnChanged_Test();
782 // OnColumnChanged Event 2
783 Assert.AreEqual(true , EventRaised , "DT90");
784 // OnColumnChanged Values
785 Assert.AreEqual(true , EventValues , "DT91");
786 dt.ColumnChanged -= new DataColumnChangeEventHandler(OnColumnChanged_Handler);
789 private void OnColumnChanged_Handler(Object sender,DataColumnChangeEventArgs e)
791 DataTable dt = (DataTable)sender;
792 if ( (e.Column.Equals(dt.Columns["Value"]) ) &&
793 (e.Row.Equals(dt.Rows[0]) ) &&
794 (e.ProposedValue.Equals("NewValue")) )
805 [Test] public void OnColumnChanging()
807 ProtectedTestClass dt = new ProtectedTestClass();
810 dt.OnColumnChanging_Test();
811 // OnColumnChanging Event 1
812 Assert.AreEqual(false , EventRaised , "DT92");
815 dt.ColumnChanging += new DataColumnChangeEventHandler(OnColumnChanging_Handler);
816 dt.OnColumnChanging_Test();
817 // OnColumnChanging Event 2
818 Assert.AreEqual(true , EventRaised , "DT93");
819 // OnColumnChanging Values
820 Assert.AreEqual(true , EventValues , "DT94");
821 dt.ColumnChanging -= new DataColumnChangeEventHandler(OnColumnChanging_Handler);
824 private void OnColumnChanging_Handler(Object sender,DataColumnChangeEventArgs e)
826 DataTable dt = (DataTable)sender;
827 if ( (e.Column.Equals(dt.Columns["Value"]) ) &&
828 (e.Row.Equals(dt.Rows[0]) ) &&
829 (e.ProposedValue.Equals("NewValue")) )
840 [Test] public void OnRemoveColumn()
842 ProtectedTestClass dt = new ProtectedTestClass();
845 dt.OnRemoveColumn_Test();
848 [Test] public void ParentRelations()
850 DataTable dtChild,dtParent;
851 DataSet ds = new DataSet();
853 dtChild = DataProvider.CreateChildDataTable();
854 dtParent= DataProvider.CreateParentDataTable();
855 //Add tables to dataset
856 ds.Tables.Add(dtChild);
857 ds.Tables.Add(dtParent);
859 DataRelationCollection drlCollection;
860 DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
862 // Checking ParentRelations - default value
864 drlCollection = dtChild.ParentRelations;
865 Assert.AreEqual(0, drlCollection.Count , "DT96");
867 ds.Relations.Add(drl);
868 drlCollection = dtChild.ParentRelations;
870 // Checking ParentRelations Count
871 Assert.AreEqual(1, drlCollection.Count , "DT97");
873 // Checking ParentRelations Value
874 Assert.AreEqual(drl, drlCollection[0] , "DT98");
877 [Test] public void Prefix()
879 DataTable dtParent = new DataTable();
881 // Checking Prefix default
882 Assert.AreEqual(String.Empty, dtParent.Prefix , "DT99");
884 // Checking Prefix set/get
885 String s = "MyPrefix";
887 Assert.AreEqual(s, dtParent.Prefix, "DT100");
890 [Test] public void RejectChanges()
892 String sNewValue = "NewValue";
893 DataRow drModified,drDeleted,drAdded;
894 DataTable dt = DataProvider.CreateParentDataTable();
896 drModified = dt.Rows[0];
897 drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
899 drDeleted = dt.Rows[1];
900 drDeleted.Delete(); //DataRowState = Deleted
902 drAdded = dt.NewRow();
903 dt.Rows.Add(drAdded); //DataRowState = Added
907 // RejectChanges - Unchanged1
908 Assert.AreEqual(DataRowState.Unchanged , drModified.RowState , "DT101");
910 // RejectChanges - Unchanged2
911 Assert.AreEqual(DataRowState.Detached , drAdded.RowState , "DT102");
913 // RejectChanges - Detached
914 Assert.AreEqual(DataRowState.Unchanged , drDeleted.RowState , "DT103");
917 [Test] public void Reset()
919 DataTable dt1 = DataProvider.CreateParentDataTable();
920 DataTable dt2 = DataProvider.CreateChildDataTable();
921 dt1.PrimaryKey = new DataColumn[] {dt1.Columns[0]};
922 dt2.PrimaryKey = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
923 DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
924 DataSet ds = new DataSet();
925 ds.Tables.AddRange(new DataTable[] {dt1,dt2});
926 ds.Relations.Add(rel);
930 // Reset - ParentRelations
931 Assert.AreEqual(0 , dt2.ParentRelations.Count , "DT104");
932 // Reset - Constraints
933 Assert.AreEqual(0 , dt2.Constraints.Count , "DT105");
935 Assert.AreEqual(0 , dt2.Rows.Count , "DT106");
937 Assert.AreEqual(0 , dt2.Columns.Count , "DT107");
940 [Test] public void RowChanged()
942 DataTable dt = DataProvider.CreateParentDataTable();
944 dt.RowChanged += new DataRowChangeEventHandler ( Row_Changed );
946 _EventTriggered=false;
948 dt.Rows[0][1] = "NewValue";
949 Assert.AreEqual(true , _EventTriggered , "DT108");
951 _EventTriggered=false;
953 dt.Rows[0].BeginEdit();
954 dt.Rows[0][1] = "NewValue";
955 Assert.AreEqual(false , _EventTriggered , "DT109");
957 _EventTriggered=false;
959 dt.Rows[0].EndEdit();
960 Assert.AreEqual(true , _EventTriggered , "DT110");
962 _EventTriggered=false;
963 dt.RowChanged -= new DataRowChangeEventHandler ( Row_Changed );
965 dt.Rows[0][1] = "NewValue A";
966 Assert.AreEqual(false , _EventTriggered , "DT111");
969 private void Row_Changed( object sender, DataRowChangeEventArgs e )
971 _EventTriggered = true;
974 [Test] public void RowChanging()
976 DataTable dt = DataProvider.CreateParentDataTable();
978 dt.RowChanging += new DataRowChangeEventHandler ( Row_Changing );
980 _EventTriggered=false;
982 dt.Rows[0][1] = "NewValue";
983 Assert.AreEqual(true , _EventTriggered , "DT112");
985 _EventTriggered=false;
987 dt.Rows[0].BeginEdit();
988 dt.Rows[0][1] = "NewValue";
989 Assert.AreEqual(false , _EventTriggered , "DT113");
991 _EventTriggered=false;
993 dt.Rows[0].EndEdit();
994 Assert.AreEqual(true , _EventTriggered , "DT114");
996 _EventTriggered=false;
997 dt.RowChanging -= new DataRowChangeEventHandler ( Row_Changing );
999 dt.Rows[0][1] = "NewValue A";
1000 Assert.AreEqual(false , _EventTriggered , "DT115");
1003 private void Row_Changing( object sender, DataRowChangeEventArgs e )
1005 _EventTriggered = true;
1008 [Test] public void RowDeleted()
1010 DataTable dt = DataProvider.CreateParentDataTable();
1012 dt.RowDeleted += new DataRowChangeEventHandler ( Row_Deleted );
1014 _EventTriggered=false;
1016 dt.Rows[0].Delete();
1017 Assert.AreEqual(true , _EventTriggered , "DT116");
1019 _EventTriggered=false;
1020 dt.RowDeleted -= new DataRowChangeEventHandler ( Row_Deleted );
1022 dt.Rows[1].Delete();
1023 Assert.AreEqual(false , _EventTriggered , "DT117");
1026 private void Row_Deleted( object sender, DataRowChangeEventArgs e )
1028 _EventTriggered = true;
1031 [Test] public void RowDeleting()
1033 DataTable dt = DataProvider.CreateParentDataTable();
1035 dt.RowDeleting += new DataRowChangeEventHandler ( Row_Deleting );
1037 _EventTriggered=false;
1039 dt.Rows[0].Delete();
1040 Assert.AreEqual(true , _EventTriggered , "DT118");
1042 _EventTriggered=false;
1043 dt.RowDeleting -= new DataRowChangeEventHandler ( Row_Deleting );
1045 dt.Rows[1].Delete();
1046 Assert.AreEqual(false , _EventTriggered , "DT119");
1049 private void Row_Deleting( object sender, DataRowChangeEventArgs e )
1051 _EventTriggered = true;
1054 [Test] public void Rows()
1057 dtParent = DataProvider.CreateParentDataTable();
1060 Assert.AreEqual(true, dtParent.Rows != null, "DT120");
1062 // Checking rows count
1063 Assert.AreEqual(true, dtParent.Rows.Count > 0 , "DT121");
1066 [Test] public void Select()
1068 DataTable dt = DataProvider.CreateParentDataTable();
1070 DataRow[] drSelect = dt.Select();
1071 DataRow[] drResult = new DataRow[dt.Rows.Count] ;
1072 dt.Rows.CopyTo(drResult,0);
1076 Assert.AreEqual(drResult, drSelect , "DT122");
1079 [Test] public void Select_ByFilter()
1081 DataSet ds = new DataSet();
1082 ds.Tables.Add(DataProvider.CreateParentDataTable());
1084 DataTable dt = DataProvider.CreateChildDataTable();
1086 DataRow[] drSelect = null;
1087 System.Collections.ArrayList al = new System.Collections.ArrayList();
1089 //add column with special name
1090 DataColumn dc = new DataColumn("Column#",typeof(int));
1094 dt.Rows[0][dc] = 100;
1095 dt.Rows[1][dc] = 200;
1096 dt.Rows[2][dc] = 300;
1097 dt.Rows[4][dc] = -400;
1100 dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + " \t\n ";
1101 dt.Rows[0]["String1"] = " \t\n " + dt.Rows[0]["String1"] ;
1102 dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + " ";
1104 ds.Tables[0].Rows[0]["ParentBool"] = DBNull.Value;
1105 ds.Tables[0].Rows[2]["ParentBool"] = DBNull.Value;
1106 ds.Tables[0].Rows[3]["ParentBool"] = DBNull.Value;
1108 //-------------------------------------------------------------
1110 foreach (DataRow dr in dt.Rows )
1112 if ((int)dr["ChildId"] == 1)
1117 // Select_S - ChildId=1
1118 drSelect = dt.Select("ChildId=1");
1119 Assert.AreEqual(al.ToArray(), drSelect , "DT123");
1121 //-------------------------------------------------------------
1123 foreach (DataRow dr in dt.Rows )
1125 if ((int)dr["ChildId"] == 1)
1130 // Select_S - ChildId='1'
1131 drSelect = dt.Select("ChildId='1'");
1132 Assert.AreEqual(al.ToArray(), drSelect , "DT124");
1133 //-------------------------------------------------------------
1134 // Select_S - ChildId= '1' (whitespace in filter string.
1135 drSelect = dt.Select("ChildId= '1'");
1136 Assert.AreEqual(al.ToArray(), drSelect , "DT125");
1137 //-------------------------------------------------------------
1139 foreach (DataRow dr in dt.Rows ) if (dr["String1"].ToString() == "1-String1") al.Add(dr);
1140 // Select_S - String1='1-String1'
1141 drSelect = dt.Select("String1='1-String1'");
1142 Assert.AreEqual(al.ToArray(), drSelect , "DT126");
1144 //-------------------------------------------------------------
1146 foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" ) al.Add(dr);
1147 // Select_S - ChildId=1 and String1='1-String1'
1148 drSelect = dt.Select("ChildId=1 and String1='1-String1'");
1149 Assert.AreEqual(al.ToArray(), drSelect , "DT127");
1151 //-------------------------------------------------------------
1153 foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] + (int)dr["ParentId"] >= 4 ) al.Add(dr);
1154 // Select_S - ChildId+ParentId >= 4
1155 drSelect = dt.Select("ChildId+ParentId >= 4");
1156 CompareUnSorted(drSelect ,al.ToArray());
1158 //-------------------------------------------------------------
1160 foreach (DataRow dr in dt.Rows )
1162 if ((((int)dr["ChildId"] - (int)dr["ParentId"]) * -1) != 0 )
1167 // Select_S - ChildId-ParentId) * -1 <> 0
1168 drSelect = dt.Select("(ChildId-ParentId) * -1 <> 0");
1169 CompareUnSorted(drSelect ,al.ToArray());
1171 //-------------------------------------------------------------
1173 foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] < ((int)dr["ParentId"]) % 4 ) al.Add(dr);
1174 // Select_S - ChildDouble < ParentId % 4
1175 drSelect = dt.Select("ChildDouble < ParentId % 4");
1176 CompareUnSorted(drSelect ,al.ToArray());
1178 //-------------------------------------------------------------
1180 foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] == 10 || (double)dr["ChildDouble"] == 20 || (double)dr["ChildDouble"] == 25 ) al.Add(dr);
1181 // Select_S - ChildDouble in (10,20,25)
1182 drSelect = dt.Select("ChildDouble in (10,20,25)");
1183 CompareUnSorted(drSelect ,al.ToArray());
1185 //-------------------------------------------------------------
1187 foreach (DataRow dr in dt.Rows ) if ( dr["String2"].ToString().IndexOf("1-S") >= 0 ) al.Add(dr);
1188 // Select_S - String2 like '%1-S%'
1189 drSelect = dt.Select("String2 like '%1-S%'");
1190 Assert.AreEqual(al.ToArray(), drSelect , "DT128");
1192 //-------------------------------------------------------------
1193 //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#]":
1195 foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
1196 // Select_S - [Column#] <= 0
1197 drSelect = dt.Select("[Column#] <= 0 ");
1198 CompareUnSorted(drSelect ,al.ToArray());
1199 //-------------------------------------------------------------
1201 foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
1202 // Select_S - [Column#] <= 0
1203 drSelect = dt.Select("[Column#] <= 0");
1204 CompareUnSorted(drSelect ,al.ToArray());
1206 //-------------------------------------------------------------
1208 foreach (DataRow dr in dt.Rows ) if (((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2000,12,12)) > 0 ) al.Add(dr);
1209 // Select_S - ChildDateTime > #12/12/2000#
1210 drSelect = dt.Select("ChildDateTime > #12/12/2000# ");
1211 CompareUnSorted(drSelect ,al.ToArray());
1213 //-------------------------------------------------------------
1216 foreach (DataRow dr in dt.Rows ) if ( ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1999,1,12,12,06,30)) > 0 ) al.Add(dr);
1217 // Select_S - ChildDateTime > #1/12/1999 12:06:30 PM#
1218 drSelect = dt.Select("ChildDateTime > #1/12/1999 12:06:30 PM# ");
1219 CompareUnSorted(drSelect ,al.ToArray());
1221 //-------------------------------------------------------------
1224 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);
1225 // Select_S - ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980#
1226 drSelect = dt.Select("ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980# ");
1227 CompareUnSorted(drSelect ,al.ToArray());
1230 //-------------------------------------------------------------
1232 foreach (DataRow dr in dt.Rows ) if ( dr["ChildDouble"].ToString().Length > 10) al.Add(dr);
1233 // Select_S - Len(Convert(ChildDouble,'System.String')) > 10
1234 drSelect = dt.Select("Len(Convert(ChildDouble,'System.String')) > 10");
1235 Assert.AreEqual(al.ToArray(), drSelect , "DT129");
1237 //-------------------------------------------------------------
1239 foreach (DataRow dr in dt.Rows ) if ( dr["String1"].ToString().Trim().Substring(0,2) == "1-") al.Add(dr);
1240 // Select_S - SubString(Trim(String1),1,2) = '1-'
1241 drSelect = dt.Select("SubString(Trim(String1),1,2) = '1-'");
1242 Assert.AreEqual(al.ToArray(), drSelect , "DT130");
1243 //-------------------------------------------------------------
1246 foreach (DataRow dr in ds.Tables[0].Rows ) if ( dr.IsNull("ParentBool") || (bool)dr["ParentBool"]) al.Add(dr);
1247 // Select_S - IsNull(ParentBool,true)
1248 drSelect = ds.Tables[0].Select("IsNull(ParentBool,true) ");
1249 Assert.AreEqual(al.ToArray(), drSelect , "DT131");
1251 //-------------------------------------------------------------
1253 // Select_S - Relation not exists, Exception
1256 drSelect = dt.Select("Parent.ParentId = ChildId");
1257 Assert.Fail("DT132: Select Failed to throw IndexOutOfRangeException");
1259 catch (IndexOutOfRangeException) {}
1260 catch (AssertionException exc) {throw exc;}
1261 catch (Exception exc)
1263 Assert.Fail("DT133: Select. Wrong exception type. Got:" + exc);
1265 //-------------------------------------------------------------
1267 ds.Relations.Add(new DataRelation("ParentChild",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
1268 foreach (DataRow dr in dt.Rows ) if ( (int)dr["ChildId"] == (int)dr.GetParentRow("ParentChild")["ParentId"]) al.Add(dr);
1269 // Select_S - Parent.ParentId = ChildId
1270 drSelect = dt.Select("Parent.ParentId = ChildId");
1271 Assert.AreEqual(al.ToArray(), drSelect , "DT134");
1274 private void CompareUnSorted(Array a, Array b)
1276 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);
1277 foreach (object item in a)
1279 if (Array.IndexOf(b, item) < 0) //b does not contain the current item.
1285 foreach (object item in b)
1287 if (Array.IndexOf(a, item) < 0) //a does not contain the current item.
1295 [Test] public void Select_ByFilterDataViewRowState()
1297 DataTable dt = DataProvider.CreateParentDataTable();
1298 DataRow[] drSelect, drResult;
1300 dt.Rows[0].Delete();
1301 dt.Rows[1]["ParentId"] = 1;
1302 dt.Rows[2]["ParentId"] = 1;
1303 dt.Rows[3].Delete();
1304 dt.Rows.Add(new object[] {1,"A","B"});
1305 dt.Rows.Add(new object[] {1,"C","D"});
1306 dt.Rows.Add(new object[] {1,"E","F"});
1308 drSelect = dt.Select("ParentId=1","",DataViewRowState.Added );
1309 drResult = GetResultRows(dt,DataRowState.Added);
1310 // Select_SSD DataViewRowState.Added
1311 Assert.AreEqual(drResult, drSelect , "DT135");
1313 drSelect = dt.Select("ParentId=1","",DataViewRowState.CurrentRows );
1314 drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified );
1315 // Select_SSD DataViewRowState.CurrentRows
1316 Assert.AreEqual(drResult, drSelect , "DT136");
1318 drSelect = dt.Select("ParentId=1","",DataViewRowState.Deleted );
1319 drResult = GetResultRows(dt,DataRowState.Deleted );
1320 // Select_SSD DataViewRowState.Deleted
1321 Assert.AreEqual(drResult, drSelect , "DT137");
1323 drSelect = dt.Select("ParentId=1","",DataViewRowState.ModifiedCurrent | DataViewRowState.ModifiedOriginal );
1324 drResult = GetResultRows(dt,DataRowState.Modified );
1325 // Select_SSD ModifiedCurrent or ModifiedOriginal
1326 Assert.AreEqual(drResult, drSelect , "DT138");
1329 private DataRow[] GetResultRows(DataTable dt,DataRowState State)
1331 System.Collections.ArrayList al = new System.Collections.ArrayList();
1332 DataRowVersion drVer = DataRowVersion.Current;
1334 //From MSDN - The row the default version for the current DataRowState.
1335 // For a DataRowState value of Added, Modified or Current,
1336 // the default version is Current.
1337 // For a DataRowState of Deleted, the version is Original.
1338 // For a DataRowState value of Detached, the version is Proposed.
1340 if ( ((State & DataRowState.Added) > 0)
1341 | ((State & DataRowState.Modified) > 0)
1342 | ((State & DataRowState.Unchanged) > 0) )
1343 drVer = DataRowVersion.Current;
1344 if ( (State & DataRowState.Deleted) > 0
1345 | (State & DataRowState.Detached) > 0 )
1346 drVer = DataRowVersion.Original;
1348 foreach (DataRow dr in dt.Rows )
1350 if ( dr.HasVersion(drVer)
1351 && ((int)dr["ParentId", drVer] == 1)
1352 && ((dr.RowState & State) > 0 )
1356 DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
1360 [Test] public void TableName()
1362 DataTable dtParent = new DataTable();
1364 // Checking TableName default
1365 Assert.AreEqual(String.Empty, dtParent.TableName, "DT139");
1367 // Checking TableName set/get
1368 String s = "MyTable";
1369 dtParent.TableName=s;
1370 Assert.AreEqual(s, dtParent.TableName, "DT140");
1373 [Test] public new void ToString()
1375 DataTable dt = DataProvider.CreateParentDataTable();
1376 dt.DisplayExpression = dt.Columns[0].ColumnName ;
1378 string sToString = dt.TableName + " + " + dt.DisplayExpression;
1380 Assert.AreEqual(sToString , dt.ToString() , "DT141");
1383 [Test] public void caseSensitive()
1385 DataTable dtParent = new DataTable();
1388 Assert.AreEqual(false, dtParent.CaseSensitive , "DT142");
1391 dtParent.CaseSensitive = true;
1392 Assert.AreEqual(true, dtParent.CaseSensitive , "DT143");
1395 [Test] public void ctor()
1398 dt = new DataTable();
1401 Assert.AreEqual(false, dt == null, "DT144");
1404 [Test] public void ctor_ByName()
1407 string sName = "MyName";
1409 dt = new DataTable(sName);
1412 Assert.AreEqual(false , dt == null , "DT145");
1415 Assert.AreEqual(sName , dt.TableName , "DT146");
1418 [Test] public void displayExpression()
1421 dtParent= DataProvider.CreateParentDataTable();
1423 // Checking DisplayExpression default
1424 Assert.AreEqual(String.Empty , dtParent.DisplayExpression , "DT147");
1426 // Checking DisplayExpression Set/Get
1427 dtParent.DisplayExpression = dtParent.Columns[0].ColumnName;
1428 Assert.AreEqual(dtParent.Columns[0].ColumnName, dtParent.DisplayExpression , "DT148");
1431 [Test] public void extendedProperties()
1434 PropertyCollection pc;
1435 dtParent= DataProvider.CreateParentDataTable();
1437 pc = dtParent.ExtendedProperties ;
1439 // Checking ExtendedProperties default
1440 Assert.AreEqual(true, pc != null, "DT149");
1442 // Checking ExtendedProperties count
1443 Assert.AreEqual(0, pc.Count , "DT150");
1446 [Test] public void primaryKey()
1449 dtParent = DataProvider.CreateParentDataTable();
1451 // Checking PrimaryKey default
1452 Assert.AreEqual(0, dtParent.PrimaryKey.Length , "DT151");
1454 // Checking PrimaryKey set/get
1455 DataColumn[] dcArr = new DataColumn[] {dtParent.Columns[0]};
1456 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
1457 Assert.AreEqual(dcArr, dtParent.PrimaryKey , "DT152");
1459 dtParent.PrimaryKey=null;
1460 DataSet ds = new DataSet();
1462 ds.Tables.Add(dtParent);
1464 //check primary key - ColumnType String, ds.CaseSensitive = false;
1465 ds.CaseSensitive = false;
1466 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["String1"]};
1467 // check primary key - ColumnType String, ds.CaseSensitive = false;
1468 dr = dtParent.NewRow();
1469 dr.ItemArray = dtParent.Rows[0].ItemArray;
1470 dr["String1"] = dr["String1"].ToString().ToUpper();
1473 dtParent.Rows.Add(dr);
1474 Assert.Fail("DT153: Rows.Add Failed to throw ConstraintException");
1476 catch (ConstraintException) {}
1477 catch (AssertionException exc) {throw exc;}
1478 catch (Exception exc)
1480 Assert.Fail("DT154: Rows.Add. Wrong exception type. Got:" + exc);
1482 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1484 //check primary key - ColumnType String, ds.CaseSensitive = true;
1485 ds.CaseSensitive = true;
1486 // check primary key ConstraintException - ColumnType String, ds.CaseSensitive = true;
1487 dr = dtParent.NewRow();
1488 dr.ItemArray = dtParent.Rows[0].ItemArray;
1489 dr["String1"] = dr["String1"].ToString();
1492 dtParent.Rows.Add(dr);
1493 Assert.Fail("DT155: Rows.Add Failed to throw ConstraintException");
1495 catch (ConstraintException) {}
1496 catch (AssertionException exc) {throw exc;}
1497 catch (Exception exc)
1499 Assert.Fail("DT156: Rows.Add. Wrong exception type. Got:" + exc);
1501 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1503 //check primary key - ColumnType String, ds.CaseSensitive = true;
1504 ds.CaseSensitive = true;
1506 // check primary key - ColumnType String, ds.CaseSensitive = true;
1507 dr = dtParent.NewRow();
1508 dr.ItemArray = dtParent.Rows[0].ItemArray;
1509 dr["String1"] = dr["String1"].ToString().ToUpper() ;
1510 dtParent.Rows.Add(dr);
1511 Assert.AreEqual(true, dtParent.Rows.Contains(dr["String1"]), "DT157");
1513 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1515 dtParent.PrimaryKey=null;
1516 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDateTime"]};
1517 // check primary key - ColumnType DateTime
1518 dr = dtParent.NewRow();
1519 dr.ItemArray = dtParent.Rows[0].ItemArray;
1520 dr["ParentDateTime"] = DateTime.Now;
1521 dtParent.Rows.Add(dr);
1522 Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDateTime"]), "DT158");
1523 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1525 // check primary key ConstraintException- ColumnType DateTime
1526 dr = dtParent.NewRow();
1527 dr.ItemArray = dtParent.Rows[0].ItemArray;
1528 dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1531 dtParent.Rows.Add(dr);
1532 Assert.Fail("DT159: Rows.Add Failed to throw ConstraintException");
1534 catch (ConstraintException) {}
1535 catch (AssertionException exc) {throw exc;}
1536 catch (Exception exc)
1538 Assert.Fail("DT160: Rows.Add. Wrong exception type. Got:" + exc);
1540 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1542 dtParent.PrimaryKey=null;
1543 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"]};
1544 // check primary key - ColumnType ParentDouble, value=Epsilon
1545 dr = dtParent.NewRow();
1546 dr.ItemArray = dtParent.Rows[0].ItemArray;
1547 dr["ParentDouble"] = Double.Epsilon ;
1548 dtParent.Rows.Add(dr);
1549 Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDouble"]), "DT161");
1550 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1552 // check primary key ConstraintException - ColumnType ParentDouble
1553 dr = dtParent.NewRow();
1554 dr.ItemArray = dtParent.Rows[0].ItemArray;
1555 dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1558 dtParent.Rows.Add(dr);
1559 Assert.Fail("DT162: Rows.Add Failed to throw ConstraintException");
1561 catch (ConstraintException) {}
1562 catch (AssertionException exc) {throw exc;}
1563 catch (Exception exc)
1565 Assert.Fail("DT163: Rows.Add. Wrong exception type. Got:" + exc);
1567 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1572 dtParent.PrimaryKey=null;
1573 // check primary key ConstraintException - ColumnType ParentBool
1576 //ParentBool is not unique, will raise exception
1577 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentBool"]};
1578 Assert.Fail("DT164: PrimaryKey Failed to throw ArgumentException");
1580 catch (ArgumentException) {}
1581 catch (AssertionException exc) {throw exc;}
1582 catch (Exception exc)
1584 Assert.Fail("DT165: PrimaryKey. Wrong exception type. Got:" + exc);
1586 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1591 dtParent.PrimaryKey=null;
1592 dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"],dtParent.Columns["ParentDateTime"]};
1593 // check primary key - ColumnType Double,DateTime test1
1594 dr = dtParent.NewRow();
1595 dr.ItemArray = dtParent.Rows[0].ItemArray;
1596 dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1597 dr["ParentDateTime"] = DateTime.Now;
1598 dtParent.Rows.Add(dr);
1599 Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT166");
1600 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1602 // check primary key - ColumnType Double,DateTime test2
1603 dr = dtParent.NewRow();
1604 dr.ItemArray = dtParent.Rows[0].ItemArray;
1605 dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1606 dr["ParentDouble"] = 99.399;
1607 dtParent.Rows.Add(dr);
1608 Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT167");
1609 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1611 // check primary key ConstraintException - ColumnType Double,DateTime
1612 dr = dtParent.NewRow();
1613 dr.ItemArray = dtParent.Rows[0].ItemArray;
1614 dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
1615 dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
1618 dtParent.Rows.Add(dr);
1619 Assert.Fail("DT168: Rows.Add Failed to throw ConstraintException");
1621 catch (ConstraintException) {}
1622 catch (AssertionException exc) {throw exc;}
1623 catch (Exception exc)
1625 Assert.Fail("DT169: Rows.Add. Wrong exception type. Got:" + exc);
1627 if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
1629 DataTable dtChild = DataProvider.CreateChildDataTable();
1630 ds.Tables.Add(dtChild);
1631 dtParent.PrimaryKey = null;
1632 //this test was addedd to check java exception:
1633 //System.ArgumentException: Cannot remove UniqueConstraint because the ForeignKeyConstraint myRelation exists.
1634 // check add primary key with relation
1635 ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
1636 //the following line will cause java to fail
1637 ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
1638 Assert.AreEqual(2, ds.Tables[0].PrimaryKey.Length , "DT170");
1641 // Test for bug #76213
1643 public void Compute_WithoutSchemaData_Test()
1645 DataSet ds = new DataSet ("TestData");
1646 DataTable table = ds.Tables.Add ("TestTable");
1648 table.Columns.Add ("Id");
1649 table.Columns.Add ("Value");
1651 table.Rows.Add (new object[] {"1","4.5"});
1652 table.Rows.Add (new object[] {"2","7.5"});
1653 table.Rows.Add (new object[] {"3","2.5"});
1654 table.Rows.Add (new object[] {"4","3.5"});
1656 Assert.AreEqual ("1",
1657 table.Compute ("Min(Id)",String.Empty),"#1");
1658 Assert.AreEqual ("4",
1659 table.Compute ("Max(Id)",String.Empty),"#2");
1660 Assert.AreEqual ("2.5",
1661 table.Compute ("Min(Value)",String.Empty),"#3");
1662 Assert.AreEqual ("7.5",
1663 table.Compute ("Max(Value)",String.Empty),"#4");
1667 public void BeginLoadData()
\r
1669 DataTable dt = DataProvider.CreateParentDataTable();
\r
1670 dt.Columns[0].AllowDBNull = false;
\r
1672 //dt.BeginLoadData();
\r
1675 //if BeginLoadData has not been called, an exception will be throw
\r
1676 dt.LoadDataRow(new object[] {null,"A","B"},false);
\r
1677 Assert.Fail("DT170: Failed to throw NoNullAllowedException");
1679 catch (NoNullAllowedException) {}
1680 catch (AssertionException exc) {throw exc;}
1681 catch (Exception exc)
1683 Assert.Fail("DT171: Wrong exception type. Got:" + exc);
1687 DataTable dt1 = DataProvider.CreateUniqueConstraint();
\r
1689 bool excptionOccurd = false;
\r
1693 dt1.BeginLoadData();
\r
1695 DataRow dr = dt1.NewRow();
\r
1698 dt1.EndLoadData(); // constraint violation
\r
1700 Assert.Fail("DT172: Failed to throw ConstraintException");
1702 catch (ConstraintException)
1704 Assert.AreEqual(2,dt1.GetErrors().Length,"DT173");
\r
1705 Assert.AreEqual(true,dt1.GetErrors()[0].RowError.Length > 10,"DT174");
\r
1706 Assert.AreEqual(true,dt1.GetErrors()[1].RowError.Length > 10,"DT175");
1708 catch (AssertionException exc) {throw exc;}
1709 catch (Exception exc)
1711 Assert.Fail("DT176: Wrong exception type. Got:" + exc);
1716 excptionOccurd = false;
\r
1719 ds= DataProvider.CreateForigenConstraint();
\r
1720 ds.Tables[0].BeginLoadData();
\r
1721 ds.Tables[0].Rows[0][0] = 10; //Forigen constraint violation
\r
1722 //ds.Tables[0].AcceptChanges();
\r
1723 ds.Tables[0].EndLoadData();
\r
1725 Assert.Fail("DT177: Failed to throw ConstraintException");
1727 catch (ConstraintException)
1729 Assert.AreEqual(3,ds.Tables[1].GetErrors().Length,"DT178");
\r
1730 for(int index=0;index<3;index++)
\r
1732 Assert.AreEqual(true,ds.Tables[1].GetErrors()[index].RowError.Length > 10,"DT179");
\r
1735 catch (AssertionException exc) {throw exc;}
1736 catch (Exception exc)
1738 Assert.Fail("DT180: Wrong exception type. Got:" + exc);
1742 private DataRowAction drExpectedAction;
\r
1744 public void OnRowChanged()
\r
1746 ProtectedTestClass dt = new ProtectedTestClass();
\r
1748 EventRaised = false;
\r
1749 dt.OnRowChanged_Test(DataRowAction.Nothing );
\r
1751 Assert.IsFalse(EventRaised ,"DT181" );
\r
1753 dt.RowChanged += new DataRowChangeEventHandler(OnRowChanged_Handler);
\r
1754 foreach (int i in Enum.GetValues(typeof(DataRowAction)))
\r
1756 EventRaised = false;
\r
1757 EventValues = false;
\r
1758 drExpectedAction = (DataRowAction)i;
\r
1759 dt.OnRowChanged_Test(drExpectedAction);
\r
1761 Assert.IsTrue(EventRaised ,"DT182" );
\r
1763 Assert.IsTrue(EventValues ,"DT183" );
\r
1765 dt.RowChanged -= new DataRowChangeEventHandler(OnRowChanged_Handler);
\r
1768 private void OnRowChanged_Handler(Object sender,DataRowChangeEventArgs e)
\r
1771 DataTable dt = (DataTable)sender;
\r
1772 if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)
\r
1773 EventValues = true;
\r
1774 EventRaised = true;
\r
1778 public void OnRowChanging()
\r
1780 ProtectedTestClass dt = new ProtectedTestClass();
\r
1782 EventRaised = false;
\r
1783 dt.OnRowChanging_Test(DataRowAction.Nothing );
\r
1785 Assert.IsFalse(EventRaised ,"DT184" );
\r
1787 dt.RowChanging += new DataRowChangeEventHandler(OnRowChanging_Handler);
\r
1788 foreach (int i in Enum.GetValues(typeof(DataRowAction)))
\r
1790 EventRaised = false;
\r
1791 EventValues = false;
\r
1792 drExpectedAction = (DataRowAction)i;
\r
1793 dt.OnRowChanging_Test(drExpectedAction);
\r
1795 Assert.IsTrue(EventRaised ,"DT185" );
\r
1797 Assert.IsTrue(EventValues ,"DT186" );
\r
1799 dt.RowChanging -= new DataRowChangeEventHandler(OnRowChanging_Handler);
\r
1802 private void OnRowChanging_Handler(Object sender,DataRowChangeEventArgs e)
\r
1805 DataTable dt = (DataTable)sender;
\r
1806 if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)
\r
1807 EventValues = true;
\r
1808 EventRaised = true;
\r
1812 public void OnRowDeleted()
\r
1814 ProtectedTestClass dt = new ProtectedTestClass();
\r
1816 EventRaised = false;
\r
1817 dt.OnRowDeleted_Test(DataRowAction.Nothing );
\r
1819 Assert.IsFalse(EventRaised ,"DT187" );
\r
1821 dt.RowDeleted += new DataRowChangeEventHandler(OnRowDeleted_Handler);
\r
1822 foreach (int i in Enum.GetValues(typeof(DataRowAction)))
\r
1824 EventRaised = false;
\r
1825 EventValues = false;
\r
1826 drExpectedAction = (DataRowAction)i;
\r
1827 dt.OnRowDeleted_Test(drExpectedAction);
\r
1829 Assert.IsTrue(EventRaised ,"DT188" );
\r
1831 Assert.IsTrue(EventValues ,"DT189" );
\r
1833 dt.RowDeleted -= new DataRowChangeEventHandler(OnRowDeleted_Handler);
\r
1837 private void OnRowDeleted_Handler(Object sender,DataRowChangeEventArgs e)
\r
1839 DataTable dt = (DataTable)sender;
\r
1840 if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)
\r
1841 EventValues = true;
\r
1842 EventRaised = true;
\r
1845 public void OnRowDeleting()
\r
1847 ProtectedTestClass dt = new ProtectedTestClass();
\r
1849 EventRaised = false;
\r
1850 dt.OnRowDeleting_Test(DataRowAction.Nothing );
\r
1852 Assert.IsFalse(EventRaised ,"DT190" );
\r
1854 dt.RowDeleting += new DataRowChangeEventHandler(OnRowDeleting_Handler);
\r
1855 foreach (int i in Enum.GetValues(typeof(DataRowAction)))
\r
1857 EventRaised = false;
\r
1858 EventValues = false;
\r
1859 drExpectedAction = (DataRowAction)i;
\r
1860 dt.OnRowDeleting_Test(drExpectedAction);
\r
1862 Assert.IsTrue(EventRaised ,"DT191" );
\r
1864 Assert.IsTrue(EventValues ,"DT192" );
\r
1866 dt.RowDeleting -= new DataRowChangeEventHandler(OnRowDeleting_Handler);
\r
1869 private void OnRowDeleting_Handler(Object sender,DataRowChangeEventArgs e)
\r
1872 DataTable dt = (DataTable)sender;
\r
1873 if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)
\r
1874 EventValues = true;
\r
1875 EventRaised = true;
\r
1880 [Category ("NotWorking")]
\r
1882 public void Select_StringString()
\r
1884 DataTable dt = DataProvider.CreateChildDataTable();
\r
1886 DataRow[] drSelect;
\r
1887 System.Collections.ArrayList al;
\r
1890 dt.Rows.Add(new object[] {99,88,"bla","wowww"});
\r
1891 dt.Rows.Add(new object[] {999,888,"","woowww"});
\r
1893 //get excepted resault
\r
1894 al = new System.Collections.ArrayList();
\r
1895 foreach (DataRow dr in dt.Rows )
\r
1897 if ((int)dr["ChildId"] == 1)
\r
1901 al.Sort(new DataRowsComparer("ParentId", "Desc"));
\r
1903 drSelect = dt.Select("ChildId=1","ParentId Desc");
\r
1904 Assert.AreEqual(al.ToArray(),drSelect ,"DT193");
\r
1907 //get excepted resault
\r
1908 al = new System.Collections.ArrayList();
\r
1909 foreach (DataRow dr in dt.Rows )
\r
1911 if (dr["String1"].ToString() == "1-String1")
\r
1915 al.Sort(new DataRowsComparer("ParentId", "Desc"));
\r
1917 drSelect = dt.Select("String1='1-String1'","ParentId Desc");
\r
1918 Assert.AreEqual(al.ToArray(),drSelect ,"DT194");
\r
1921 //get excepted resault
\r
1922 al = new System.Collections.ArrayList();
\r
1923 foreach (DataRow dr in dt.Rows )
\r
1925 if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1")
\r
1929 al.Sort(new DataRowsComparer("ParentId", "Desc"));
\r
1931 drSelect = dt.Select("ChildId=1 and String1='1-String1'","ParentId Desc");
\r
1932 Assert.AreEqual(al.ToArray(),drSelect ,"DT195");
\r
1935 //get excepted resault
\r
1936 al = new System.Collections.ArrayList();
\r
1937 foreach (DataRow dr in dt.Rows )
\r
1939 if (dr["String1"].ToString().Length < 4 )
\r
1943 al.Sort(new DataRowsComparer("ParentId", "Desc"));
\r
1945 drSelect = dt.Select("Len(String1) < 4 ","ParentId Desc");
\r
1946 Assert.AreEqual(al.ToArray(),drSelect ,"DT196");
\r
1949 //get excepted resault
\r
1950 al = new System.Collections.ArrayList();
\r
1951 foreach (DataRow dr in dt.Rows )
\r
1953 if ( dr["String1"].ToString().IndexOf("String") > 0 )
\r
1957 al.Sort(new DataRowsComparer("ParentId", "Desc"));
\r
1959 drSelect = dt.Select("String1 like '%%String*' ","ParentId Desc");
\r
1960 Assert.AreEqual(al.ToArray(),drSelect ,"DT197");
\r
1963 //get excepted resault
\r
1964 al = new System.Collections.ArrayList();
\r
1965 foreach (DataRow dr in dt.Rows )
\r
1967 if (((int)dr["ChildId"] == 2) || ((int)dr["ChildId"] == 3))
\r
1971 al.Sort(new DataRowsComparer("ParentId", "Desc"));
\r
1973 drSelect = dt.Select("ChildId in (2,3) ","ParentId Desc");
\r
1974 Assert.AreEqual(al.ToArray(),drSelect ,"DT198");
\r
1977 //get excepted resault
\r
1978 al = new System.Collections.ArrayList();
\r
1979 foreach (DataRow dr in dt.Rows )
\r
1981 if ((((int)dr["ChildId"] * (int)dr["ParentId"]) > 5 ))
\r
1984 al.Sort(new DataRowsComparer("ChildId", "Asc"));
\r
1986 drSelect = dt.Select("ChildId * ParentId > 5 ","ChildId Asc");
\r
1987 Assert.AreEqual(al.ToArray(),drSelect ,"DT199");
\r
1990 //get excepted resault
\r
1991 al = new System.Collections.ArrayList();
\r
1992 foreach (DataRow dr in dt.Rows )
\r
1994 if (dr["String2"].ToString().Substring(2,3) == "Str" )
\r
1997 al.Sort(new DataRowsComparer("ParentId", "Desc"));
\r
1999 drSelect = dt.Select("SubString(String2,3,3) like 'Str' ","ParentId Desc");
\r
2000 Assert.AreEqual(al.ToArray(),drSelect ,"DT200");
\r
2004 [ExpectedException(typeof(ArgumentException))]
\r
2006 [Category ("NotWorking")]
\r
2008 public void Select_StringString_2()
\r
2010 DataTable dt = DataProvider.CreateParentDataTable();
\r
2011 //Checking the parsing of the sort string
\r
2012 dt.Select(dt.Columns[0].ColumnName + "=1","[x");
\r
2016 [ExpectedException(typeof(IndexOutOfRangeException))]
\r
2017 public void Select_StringString_3()
\r
2019 DataTable dt = DataProvider.CreateParentDataTable();
\r
2020 //Select - parse sort string checking 1");
\r
2021 dt.Select(dt.Columns[0].ColumnName,dt.Columns[0].ColumnName + "1");
\r
2024 internal class DataRowsComparer : System.Collections.IComparer
\r
2027 private string _columnName;
\r
2028 private string _direction;
\r
2031 #region Constructors
\r
2032 public DataRowsComparer(string columnName, string direction)
\r
2034 _columnName = columnName;
\r
2035 if (direction.ToLower() != "asc" && direction.ToLower() != "desc")
\r
2037 throw new ArgumentException("Direction can only be one of: 'asc' or 'desc'");
\r
2039 _direction = direction;
\r
2043 #region IComparer Members
\r
2045 public int Compare(object x, object y)
\r
2047 DataRow drX = (DataRow)x;
\r
2048 DataRow drY = (DataRow)y;
\r
2050 object objX = drX[_columnName];
\r
2051 object objY = drY[_columnName];
\r
2053 int compareResult = System.Collections.Comparer.Default.Compare(objX, objY);
\r
2055 //If we are comparing desc we need to reverse the result.
\r
2056 if (_direction.ToLower() == "desc")
\r
2058 compareResult = -compareResult;
\r
2061 return compareResult;
\r