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 System.ComponentModel;
34 using MonoTests.System.Data.Utils;
36 namespace MonoTests.System.Data
38 [TestFixture] public class DataRowTest2
48 [Test] public void AcceptChanges()
50 DataTable myTable = new DataTable("myTable");
52 myRow = myTable.NewRow();
53 myTable.Rows.Add(myRow);
55 // DataRow AcceptChanges
56 // DataRowState.Added -> DataRowState.Unchanged
57 myTable.AcceptChanges();
58 Assert.AreEqual(DataRowState.Unchanged , myRow.RowState , "DRW1");
61 [Test] public void CancelEdit()
63 DataTable myTable = new DataTable("myTable");
64 DataColumn dc = new DataColumn("Id",typeof(int));
66 myTable.Columns.Add(dc);
67 myTable.Rows.Add(new object[] {1});
68 myTable.Rows.Add(new object[] {2});
69 myTable.Rows.Add(new object[] {3});
71 DataRow myRow = myTable.Rows[0];
77 Assert.AreEqual(true , (int)myRow[0] == 1, "DRW2");
80 [Test] public void ClearErrors()
82 DataTable dt = new DataTable("myTable");
83 DataRow dr = dt.NewRow();
86 // DataRow ClearErrors
87 Assert.AreEqual(true , dr.HasErrors , "DRW3");
89 // DataRow ClearErrors
91 Assert.AreEqual(false , dr.HasErrors , "DRW4");
94 [Test] public void Delete()
96 DataTable myTable = new DataTable("myTable");
97 DataColumn dc = new DataColumn("Id",typeof(int));
99 myTable.Columns.Add(dc);
100 myTable.Rows.Add(new object[] {1});
101 myTable.Rows.Add(new object[] {2});
102 myTable.Rows.Add(new object[] {3});
103 myTable.AcceptChanges();
105 DataRow myRow = myTable.Rows[0];
109 Assert.AreEqual(DataRowState.Deleted , myRow.RowState , "DRW5");
112 myTable.AcceptChanges();
113 Assert.AreEqual(DataRowState.Detached , myRow.RowState , "DRW6");
116 [Test] public void EndEdit()
118 DataTable myTable = new DataTable("myTable");
119 DataColumn dc = new DataColumn("Id",typeof(int));
121 myTable.Columns.Add(dc);
122 myTable.Rows.Add(new object[] {1});
123 myTable.Rows.Add(new object[] {2});
124 myTable.Rows.Add(new object[] {3});
126 DataRow myRow = myTable.Rows[0];
129 //After calling the DataRow object's BeginEdit method, if you change the value, the Current and Proposed values become available
132 iProposed = (int)myRow[0,DataRowVersion.Proposed];
136 Assert.AreEqual(iProposed , (int)myRow[0,DataRowVersion.Current] , "DRW7");
139 [Test] public void Equals()
141 DataTable myTable = new DataTable("myTable");
143 dr1 = myTable.NewRow();
144 dr2 = myTable.NewRow();
147 Assert.AreEqual(false , dr1.Equals(dr2), "DRW8");
151 Assert.AreEqual(true , dr1.Equals(dr2), "DRW9");
154 [Test] public void GetChildRows_ByDataRealtion()
157 DataRow[] drArrExcepted,drArrResult;
158 DataTable dtChild,dtParent;
159 DataSet ds = new DataSet();
162 dtChild = DataProvider.CreateChildDataTable();
163 dtParent= DataProvider.CreateParentDataTable();
165 //Add tables to dataset
166 ds.Tables.Add(dtChild);
167 ds.Tables.Add(dtParent);
168 dr = dtParent.Rows[0];
171 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
172 ds.Relations.Add(dRel);
173 //Get Excepted result
174 drArrExcepted = dtChild.Select("ParentId=" + dr["ParentId"]);
176 drArrResult = dr.GetChildRows(dRel);
179 Assert.AreEqual(drArrExcepted, drArrResult, "DRW10");
182 [Test] public void GetChildRows_ByDataRealtionDataRowVersion()
185 DataRow[] drArrExcepted,drArrResult;
186 DataTable dtChild,dtParent;
187 DataSet ds = new DataSet();
189 dtChild = DataProvider.CreateChildDataTable();
190 dtParent= DataProvider.CreateParentDataTable();
191 //Add tables to dataset
192 ds.Tables.Add(dtChild);
193 ds.Tables.Add(dtParent);
195 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
196 ds.Relations.Add(dRel);
198 drParent = dtParent.Rows[0];
200 // Teting: DateTime.Now.ToShortTimeString()
201 //Get Excepted result
202 drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
203 //Get Result DataRowVersion.Current
204 drArrResult = drParent.GetChildRows(dRel,DataRowVersion.Current);
205 Assert.AreEqual(drArrExcepted, drArrResult, "DRW11");
207 // Teting: DataRow.GetParentRows_D_D
208 //Get Excepted result
209 drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.OriginalRows );
210 //Get Result DataRowVersion.Current
211 drArrResult = drParent.GetChildRows(dRel,DataRowVersion.Original );
212 Assert.AreEqual(drArrExcepted, drArrResult, "DRW12");
214 // Teting: DataRow.GetParentRows_D_D
215 //Get Excepted result, in this case Current = Default
216 drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows);
217 //Get Result DataRowVersion.Current
218 drArrResult = drParent.GetChildRows(dRel,DataRowVersion.Default );
219 Assert.AreEqual(drArrExcepted, drArrResult, "DRW13");
221 // Teting: DataRow.GetParentRows_D_D
222 drParent.BeginEdit();
223 drParent["String1"] = "Value";
224 //Get Excepted result
225 drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
226 //Get Result DataRowVersion.Current
227 drArrResult = drParent.GetChildRows(dRel,DataRowVersion.Proposed );
228 Assert.AreEqual(drArrExcepted, drArrResult, "DRW14");
231 [Test] public void GetChildRows_ByName()
234 DataRow[] drArrExcepted,drArrResult;
235 DataTable dtChild,dtParent;
236 DataSet ds = new DataSet();
239 dtChild = DataProvider.CreateChildDataTable();
240 dtParent= DataProvider.CreateParentDataTable();
242 //Add tables to dataset
243 ds.Tables.Add(dtChild);
244 ds.Tables.Add(dtParent);
245 dr = dtParent.Rows[0];
248 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
249 ds.Relations.Add(dRel);
250 //Get Excepted result
251 drArrExcepted = dtChild.Select("ParentId=" + dr["ParentId"]);
253 drArrResult = dr.GetChildRows("Parent-Child");
256 Assert.AreEqual(drArrExcepted, drArrResult, "DRW15");
259 [Test] public void GetChildRows_ByNameDataRowVersion()
262 DataRow[] drArrExcepted,drArrResult;
263 DataTable dtChild,dtParent;
264 DataSet ds = new DataSet();
266 dtChild = DataProvider.CreateChildDataTable();
267 dtParent= DataProvider.CreateParentDataTable();
268 //Add tables to dataset
269 ds.Tables.Add(dtChild);
270 ds.Tables.Add(dtParent);
272 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
273 ds.Relations.Add(dRel);
275 drParent = dtParent.Rows[0];
278 //Get Excepted result
279 drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
280 //Get Result DataRowVersion.Current
281 drArrResult = drParent.GetChildRows("Parent-Child",DataRowVersion.Current);
282 Assert.AreEqual(drArrExcepted, drArrResult, "DRW16");
285 //Get Excepted result
286 drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.OriginalRows );
287 //Get Result DataRowVersion.Current
288 drArrResult = drParent.GetChildRows("Parent-Child",DataRowVersion.Original );
289 Assert.AreEqual(drArrExcepted, drArrResult, "DRW17");
291 // GetParentRows_SD 3
292 //Get Excepted result, in this case Current = Default
293 drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows);
294 //Get Result DataRowVersion.Current
295 drArrResult = drParent.GetChildRows("Parent-Child",DataRowVersion.Default );
296 Assert.AreEqual(drArrExcepted, drArrResult, "DRW18");
298 // GetParentRows_SD 4
299 drParent.BeginEdit();
300 drParent["String1"] = "Value";
301 //Get Excepted result
302 drArrExcepted = dtChild.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
303 //Get Result DataRowVersion.Current
304 drArrResult = drParent.GetChildRows("Parent-Child",DataRowVersion.Proposed );
305 Assert.AreEqual(drArrExcepted, drArrResult, "DRW19");
308 [Test] public void GetColumnError_ByIndex()
310 string sColErr = "Error!";
311 DataTable dt = new DataTable("myTable");
312 DataColumn dc = new DataColumn("Column1");
314 DataRow dr = dt.NewRow();
317 Assert.AreEqual(String.Empty , dr.GetColumnError(0) , "DRW20");
319 dr.SetColumnError(0,sColErr );
322 Assert.AreEqual(sColErr , dr.GetColumnError(0) , "DRW21");
325 [Test] public void GetColumnError_ByName()
327 string sColErr = "Error!";
328 DataTable dt = new DataTable("myTable");
329 DataColumn dc = new DataColumn("Column1");
331 DataRow dr = dt.NewRow();
334 Assert.AreEqual(String.Empty , dr.GetColumnError("Column1") , "DRW22");
336 dr.SetColumnError("Column1",sColErr );
339 Assert.AreEqual(sColErr , dr.GetColumnError("Column1") , "DRW23");
342 [Test] public void GetColumnsInError()
344 string sColErr = "Error!";
346 DataTable dt = new DataTable("myTable");
348 dt.Columns.Add(new DataColumn());
349 dt.Columns.Add(new DataColumn());
350 dt.Columns.Add(new DataColumn());
351 dt.Columns.Add(new DataColumn());
352 dt.Columns.Add(new DataColumn());
355 dt.Rows.Add(new object[] {});
356 dt.Rows.Add(new object[] {});
357 dt.Rows.Add(new object[] {});
359 DataRow dr = dt.Rows[1];
361 dcArr = dr.GetColumnsInError();
363 // GetColumnsInError 1
364 Assert.AreEqual(0, dcArr.Length , "DRW24");
366 dr.SetColumnError(0,sColErr);
367 dr.SetColumnError(2,sColErr);
368 dr.SetColumnError(4,sColErr);
370 dcArr = dr.GetColumnsInError();
372 // GetColumnsInError 2
373 Assert.AreEqual(3, dcArr.Length , "DRW25");
375 //check that the right columns taken
376 // GetColumnsInError 3
377 Assert.AreEqual(dt.Columns[0], dcArr[0], "DRW26");
379 // GetColumnsInError 4
380 Assert.AreEqual(dt.Columns[2], dcArr[1], "DRW27");
382 // GetColumnsInError 5
383 Assert.AreEqual(dt.Columns[4], dcArr[2], "DRW28");
386 [Test] public new void GetHashCode()
390 DataTable dt = new DataTable();
393 iHashCode = dr.GetHashCode();
394 for (int i=0; i<10; i++)
395 { //must return the same value each time
396 // GetHashCode #" + i
397 Assert.AreEqual(dr.GetHashCode() , iHashCode , "DRW29");
401 [Test] public void GetParentRow_ByDataRelation()
403 DataRow drExcepted,drResult,drChild;
404 DataTable dtChild,dtParent;
405 DataSet ds = new DataSet();
408 dtChild = DataProvider.CreateChildDataTable();
409 dtParent = DataProvider.CreateParentDataTable();
411 //Add tables to dataset
412 ds.Tables.Add(dtChild);
413 ds.Tables.Add(dtParent);
416 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
417 ds.Relations.Add(dRel);
420 drExcepted = dtParent.Rows[0];
423 drChild = dtChild.Select("ParentId=" + drExcepted["ParentId"])[0];
424 drResult = drChild.GetParentRow(dRel);
427 Assert.AreEqual(drExcepted.ItemArray, drResult.ItemArray , "DRW30");
430 [Test] public void GetParentRow_ByDataRelationDataRowVersion()
432 DataRow drParent,drChild;
433 DataRow drArrExcepted,drArrResult;
434 DataTable dtChild,dtParent;
435 DataSet ds = new DataSet();
437 dtChild = DataProvider.CreateChildDataTable();
438 dtParent= DataProvider.CreateParentDataTable();
439 //Add tables to dataset
440 ds.Tables.Add(dtChild);
441 ds.Tables.Add(dtParent);
443 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
444 ds.Relations.Add(dRel);
446 drParent = dtParent.Rows[0];
447 drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
450 //Get Excepted result
451 drArrExcepted = drParent;
452 //Get Result DataRowVersion.Current
453 drArrResult = drChild.GetParentRow(dRel,DataRowVersion.Current);
454 Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW31");
457 //Get Excepted result
458 drArrExcepted = drParent;
459 //Get Result DataRowVersion.Current
460 drArrResult = drChild.GetParentRow(dRel,DataRowVersion.Original );
461 Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW32");
464 //Get Excepted result, in this case Current = Default
465 drArrExcepted = drParent;
466 //Get Result DataRowVersion.Current
467 drArrResult = drChild.GetParentRow(dRel,DataRowVersion.Default );
468 Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW33");
472 drChild["String1"] = "Value";
473 //Get Excepted result
474 drArrExcepted = drParent;
475 //Get Result DataRowVersion.Current
476 drArrResult = drChild.GetParentRow(dRel,DataRowVersion.Proposed );
477 Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW34");
480 [Test] public void GetParentRow_ByName()
482 DataRow drExcepted,drResult,drChild;
483 DataTable dtChild,dtParent;
484 DataSet ds = new DataSet();
487 dtChild = DataProvider.CreateChildDataTable();
488 dtParent = DataProvider.CreateParentDataTable();
490 //Add tables to dataset
491 ds.Tables.Add(dtChild);
492 ds.Tables.Add(dtParent);
495 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
496 ds.Relations.Add(dRel);
499 drExcepted = dtParent.Rows[0];
502 drChild = dtChild.Select("ParentId=" + drExcepted["ParentId"])[0];
503 drResult = drChild.GetParentRow("Parent-Child");
506 Assert.AreEqual(drExcepted.ItemArray, drResult.ItemArray , "DRW35");
509 [Test] public void GetParentRow_ByNameDataRowVersion()
511 DataRow drParent,drChild;
512 DataRow drArrExcepted,drArrResult;
513 DataTable dtChild,dtParent;
514 DataSet ds = new DataSet();
516 dtChild = DataProvider.CreateChildDataTable();
517 dtParent= DataProvider.CreateParentDataTable();
518 //Add tables to dataset
519 ds.Tables.Add(dtChild);
520 ds.Tables.Add(dtParent);
522 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
523 ds.Relations.Add(dRel);
525 drParent = dtParent.Rows[0];
526 drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
529 //Get Excepted result
530 drArrExcepted = drParent;
531 //Get Result DataRowVersion.Current
532 drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Current);
533 Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW36");
536 //Get Excepted result
537 drArrExcepted = drParent;
538 //Get Result DataRowVersion.Current
539 drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Original );
540 Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW37");
543 //Get Excepted result, in this case Current = Default
544 drArrExcepted = drParent;
545 //Get Result DataRowVersion.Current
546 drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Default );
547 Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW38");
551 drChild["String1"] = "Value";
552 //Get Excepted result
553 drArrExcepted = drParent;
554 //Get Result DataRowVersion.Current
555 drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Proposed );
556 Assert.AreEqual(drArrExcepted.ItemArray, drArrResult.ItemArray , "DRW39");
559 [Test] public void GetParentRows_ByDataRelation()
562 DataRow[] drArrExcepted,drArrResult;
563 DataTable dtChild,dtParent;
564 DataSet ds = new DataSet();
567 dtChild = DataProvider.CreateChildDataTable();
568 dtParent = DataProvider.CreateParentDataTable();
570 //Add tables to dataset
571 ds.Tables.Add(dtChild);
572 ds.Tables.Add(dtParent);
573 dr = dtParent.Rows[0];
575 //Duplicate several rows in order to create Many to Many relation
576 dtParent.ImportRow(dr);
577 dtParent.ImportRow(dr);
578 dtParent.ImportRow(dr);
581 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"],false);
582 ds.Relations.Add(dRel);
583 //Get Excepted result
584 drArrExcepted = dtParent.Select("ParentId=" + dr["ParentId"]);
585 dr = dtChild.Select("ParentId=" + dr["ParentId"])[0];
587 drArrResult = dr.GetParentRows(dRel);
590 Assert.AreEqual(drArrExcepted, drArrResult, "DRW40");
593 [Test] public void GetParentRows_ByName()
596 DataRow[] drArrExcepted,drArrResult;
597 DataTable dtChild,dtParent;
598 DataSet ds = new DataSet();
601 dtChild = DataProvider.CreateChildDataTable();
602 dtParent = DataProvider.CreateParentDataTable();
604 //Add tables to dataset
605 ds.Tables.Add(dtChild);
606 ds.Tables.Add(dtParent);
607 dr = dtParent.Rows[0];
609 //Duplicate several rows in order to create Many to Many relation
610 dtParent.ImportRow(dr);
611 dtParent.ImportRow(dr);
612 dtParent.ImportRow(dr);
615 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"],false);
616 ds.Relations.Add(dRel);
617 //Get Excepted result
618 drArrExcepted = dtParent.Select("ParentId=" + dr["ParentId"]);
619 dr = dtChild.Select("ParentId=" + dr["ParentId"])[0];
621 drArrResult = dr.GetParentRows("Parent-Child");
624 Assert.AreEqual(drArrExcepted, drArrResult, "DRW41");
627 [Test] public void GetParentRows_ByNameDataRowVersion()
629 DataRow drParent,drChild;
630 DataRow[] drArrExcepted,drArrResult;
631 DataTable dtChild,dtParent;
632 DataSet ds = new DataSet();
634 dtChild = DataProvider.CreateChildDataTable();
635 dtParent= DataProvider.CreateParentDataTable();
636 //Add tables to dataset
637 ds.Tables.Add(dtChild);
638 ds.Tables.Add(dtParent);
640 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"],false);
641 ds.Relations.Add(dRel);
643 //Create several copies of the first row
644 drParent = dtParent.Rows[0]; //row[0] has versions: Default,Current,Original
645 dtParent.ImportRow(drParent); //row[1] has versions: Default,Current,Original
646 dtParent.ImportRow(drParent); //row[2] has versions: Default,Current,Original
647 dtParent.ImportRow(drParent); //row[3] has versions: Default,Current,Original
648 dtParent.ImportRow(drParent); //row[4] has versions: Default,Current,Original
649 dtParent.ImportRow(drParent); //row[5] has versions: Default,Current,Original
650 dtParent.AcceptChanges();
652 //Get the first child row for drParent
653 drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
655 DataRow[] drTemp = dtParent.Select("ParentId=" + drParent["ParentId"]);
656 // Console.WriteLine("********");
657 // foreach (DataRow d in drTemp)
659 // CheckRowVersion(d);
661 drTemp[0].BeginEdit();
662 drTemp[0]["String1"] = "NewValue"; //row now has versions: Proposed,Current,Original,Default
663 drTemp[1].BeginEdit();
664 drTemp[1]["String1"] = "NewValue"; //row now has versions: Proposed,Current,Original,Default
666 // Console.WriteLine("********");
667 // foreach (DataRow d in drTemp)
669 // CheckRowVersion(d);
671 // Console.WriteLine("********");
673 // Check DataRowVersion.Current
674 //Check DataRowVersion.Current
675 drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
676 drArrResult = drChild.GetParentRows("Parent-Child",DataRowVersion.Current);
677 Assert.AreEqual(drArrExcepted, drArrResult, "DRW42");
679 //Check DataRowVersion.Current
680 // Teting: DataRow.GetParentRows_D_D ,DataRowVersion.Original
681 drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.OriginalRows );
682 drArrResult = drChild.GetParentRows("Parent-Child",DataRowVersion.Original );
683 Assert.AreEqual(drArrExcepted, drArrResult, "DRW43");
685 //Check DataRowVersion.Default
686 // Teting: DataRow.GetParentRows_D_D ,DataRowVersion.Default
687 drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows);
688 drArrResult = drChild.GetParentRows("Parent-Child",DataRowVersion.Default );
689 Assert.AreEqual(drArrExcepted, drArrResult, "DRW44");
691 /* .Net don't work as expected
692 //Check DataRowVersion.Proposed
693 // Teting: DataRow.GetParentRows_D_D ,DataRowVersion.Proposed
694 drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.ModifiedCurrent);
695 //drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.ModifiedOriginal );
697 drArrResult = drChild.GetParentRows("Parent-Child",DataRowVersion.Proposed );
698 Assert.AreEqual(drArrExcepted, drArrResult, "DRW45");
702 private void CheckRowVersion(DataRow dr)
704 Console.WriteLine("");
705 if (dr.HasVersion(DataRowVersion.Current)) Console.WriteLine("Has " + DataRowVersion.Current.ToString());
706 if (dr.HasVersion(DataRowVersion.Default)) Console.WriteLine("Has " + DataRowVersion.Default.ToString());
707 if (dr.HasVersion(DataRowVersion.Original)) Console.WriteLine("Has " + DataRowVersion.Original.ToString());
708 if (dr.HasVersion(DataRowVersion.Proposed)) Console.WriteLine("Has " + DataRowVersion.Proposed.ToString());
711 [Test] public new void GetType()
714 DataTable dt = new DataTable();
715 DataRow dr = dt.NewRow();
716 myType = typeof(DataRow);
719 Assert.AreEqual(typeof(DataRow), myType , "DRW46");
722 [Test] public void HasErrors()
724 DataTable dt = new DataTable("myTable");
725 DataRow dr = dt.NewRow();
727 // HasErrors (default)
728 Assert.AreEqual(false, dr.HasErrors, "DRW47");
732 // HasErrors (set/get)
733 Assert.AreEqual(true , dr.HasErrors , "DRW48");
736 [Test] public void HasVersion_ByDataRowVersion()
738 DataTable t = new DataTable("atable");
739 t.Columns.Add("id", typeof(int));
740 t.Columns.Add("name", typeof(string));
741 t.Columns[0].DefaultValue = 1;
742 t.Columns[1].DefaultValue = "something";
745 DataRow r = t.NewRow();
747 // HasVersion Test #10
748 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Current) , "DRW49");
750 // HasVersion Test #11
751 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Original) , "DRW50");
753 // HasVersion Test #12
754 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW51");
756 // HasVersion Test #13
757 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Proposed) , "DRW52");
762 // HasVersion Test #20
763 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Current) , "DRW53");
765 // HasVersion Test #21
766 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Original) , "DRW54");
768 // HasVersion Test #22
769 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW55");
771 // HasVersion Test #23
772 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Proposed) , "DRW56");
777 // HasVersion Test #30
778 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW57");
780 // HasVersion Test #31
781 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Original) , "DRW58");
783 // HasVersion Test #32
784 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW59");
786 // HasVersion Test #33
787 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW60");
790 // now it is "unchanged"
792 // HasVersion Test #40
793 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW61");
795 // HasVersion Test #41
796 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW62");
798 // HasVersion Test #42
799 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW63");
801 // HasVersion Test #43
802 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW64");
807 // HasVersion Test #50
808 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW65");
810 // HasVersion Test #51
811 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW66");
813 // HasVersion Test #52
814 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW67");
816 // HasVersion Test #53
817 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Proposed) , "DRW68");
820 // now it is "modified"
821 // HasVersion Test #60
822 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW69");
824 // HasVersion Test #61
825 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW70");
827 // HasVersion Test #62
828 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW71");
830 // HasVersion Test #63
831 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW72");
833 // this or t.AcceptChanges
835 // now it is "unchanged" again
836 // HasVersion Test #70
837 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Current) , "DRW73");
839 // HasVersion Test #71
840 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW74");
842 // HasVersion Test #72
843 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Default) , "DRW75");
845 // HasVersion Test #73
846 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW76");
849 // now it is "deleted"
851 // HasVersion Test #80
852 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Current) , "DRW77");
854 // HasVersion Test #81
855 Assert.AreEqual(true , r.HasVersion(DataRowVersion.Original) , "DRW78");
857 // HasVersion Test #82
858 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Default) , "DRW79");
860 // HasVersion Test #83
861 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW80");
865 // HasVersion Test #90
866 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Current) , "DRW81");
868 // HasVersion Test #91
869 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Original) , "DRW82");
871 // HasVersion Test #92
872 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Default) , "DRW83");
874 // HasVersion Test #93
875 Assert.AreEqual(false , r.HasVersion(DataRowVersion.Proposed) , "DRW84");
878 [Test] public void IsNull_ByDataColumn()
880 DataTable dt = new DataTable();
881 DataColumn dc0 = new DataColumn("Col0",typeof(int));
882 DataColumn dc1 = new DataColumn("Col1",typeof(int));
885 dt.Rows.Add(new object[] {1234});
886 DataRow dr = dt.Rows[0];
889 Assert.AreEqual(false , dr.IsNull(dc0) , "DRW85");
892 Assert.AreEqual(true , dr.IsNull(dc1) , "DRW86");
895 [Test] public void IsNull_ByDataColumnDataRowVersion()
897 DataTable dt = new DataTable();
898 DataColumn dc0 = new DataColumn("Col0",typeof(int));
899 DataColumn dc1 = new DataColumn("Col1",typeof(int));
902 dt.Rows.Add(new object[] {1234});
903 DataRow dr = dt.Rows[0];
905 // IsNull - col0 Current
906 Assert.AreEqual(false, dr.IsNull(dc0,DataRowVersion.Current) , "DRW87");
908 // IsNull - col1 Current
909 Assert.AreEqual(true, dr.IsNull(dc1,DataRowVersion.Current) , "DRW88");
911 // IsNull - col0 Default
912 Assert.AreEqual(false, dr.IsNull(dc0,DataRowVersion.Default) , "DRW89");
913 // IsNull - col1 Default
914 Assert.AreEqual(true, dr.IsNull(dc1,DataRowVersion.Default) , "DRW90");
917 dr[0] = 9; //Change value, Create RowVersion Proposed
919 // IsNull - col0 Proposed
920 Assert.AreEqual(false, dr.IsNull(dc0,DataRowVersion.Proposed) , "DRW91");
921 // IsNull - col1 Proposed
922 Assert.AreEqual(true, dr.IsNull(dc1,DataRowVersion.Proposed) , "DRW92");
927 // IsNull - col0 Original
928 Assert.AreEqual(false, dr.IsNull(dc0,DataRowVersion.Original) , "DRW93");
931 [Test] public void IsNull_ByIndex()
933 DataTable dt = new DataTable();
934 DataColumn dc0 = new DataColumn("Col0",typeof(int));
935 DataColumn dc1 = new DataColumn("Col1",typeof(int));
938 dt.Rows.Add(new object[] {1234});
939 DataRow dr = dt.Rows[0];
942 Assert.AreEqual(false , dr.IsNull(0) , "DRW94");
945 Assert.AreEqual(true , dr.IsNull(1) , "DRW95");
948 [Test] public void IsNull_ByName()
950 DataTable dt = new DataTable();
951 DataColumn dc0 = new DataColumn("Col0",typeof(int));
952 DataColumn dc1 = new DataColumn("Col1",typeof(int));
955 dt.Rows.Add(new object[] {1234});
956 DataRow dr = dt.Rows[0];
958 #region --- assignment ----
960 Assert.AreEqual(false, dr.IsNull("Col0"), "DRW96");
963 Assert.AreEqual(true, dr.IsNull("Col1"), "DRW97");
966 #region --- bug 3124 ---
969 MemoryStream st = new MemoryStream();
970 StreamWriter sw = new StreamWriter(st);
971 sw.Write("<?xml version=\"1.0\" standalone=\"yes\"?><NewDataSet>");
972 sw.Write("<Table><EmployeeNo>9</EmployeeNo></Table>");
973 sw.Write("</NewDataSet>");
976 DataSet ds = new DataSet();
978 // Here we add the expression column
979 ds.Tables[0].Columns.Add("ValueListValueMember", typeof(object), "EmployeeNo");
981 foreach( DataRow row in ds.Tables[0].Rows )
983 // Console.WriteLine(row["ValueListValueMember"].ToString() + " " );
984 if( row.IsNull("ValueListValueMember") == true )
985 Assert.AreEqual("Failed", "SubTest", "DRW98");
987 Assert.AreEqual("Passed", "Passed", "DRW99");
993 [Test] public void Item()
995 // init table with columns
996 DataTable myTable = new DataTable("myTable");
998 myTable.Columns.Add(new DataColumn("Id",typeof(int)));
999 myTable.Columns.Add(new DataColumn("Name",typeof(string)));
1000 DataColumn dc = myTable.Columns[0];
1002 myTable.Rows.Add(new object[] {1,"Ofer"});
1003 myTable.Rows.Add(new object[] {2,"Ofer"});
1005 myTable.AcceptChanges();
1007 DataRow myRow = myTable.Rows[0];
1012 Assert.AreEqual(1 , (int)myRow[0] , "DRW100");
1015 Assert.AreEqual(1 , (int)myRow["Id"] , "DRW101");
1018 Assert.AreEqual(1 , (int)myRow[dc] , "DRW102");
1020 // Item - index,Current
1021 Assert.AreEqual(1 , (int)myRow[0,DataRowVersion.Current ] , "DRW103");
1023 // Item - string,Current
1024 Assert.AreEqual(1 , (int)myRow["Id",DataRowVersion.Current] , "DRW104");
1026 // Item - columnn,Current
1027 Assert.AreEqual(1 , (int)myRow[dc,DataRowVersion.Current] , "DRW105");
1032 /*public void testMore()
1034 DataTable dt = DataProvider.CreateParentDataTable();
1035 dt.Rows[0].BeginEdit();
1037 dt.Rows[0].EndEdit();
1041 [Test] public void RejectChanges()
1043 DataTable dt = new DataTable();
1044 DataColumn dc0 = new DataColumn("Col0",typeof(int));
1045 DataColumn dc1 = new DataColumn("Col1",typeof(int));
1046 dt.Columns.Add(dc0);
1047 dt.Columns.Add(dc1);
1048 dt.Rows.Add(new object[] {1234});
1050 DataRow dr = dt.Rows[0];
1056 // RejectChanges - row 0
1057 Assert.AreEqual(1234 , (int)dr[0], "DRW106");
1059 // RejectChanges - row 1
1060 Assert.AreEqual(DBNull.Value , dr[1] , "DRW107");
1065 // RejectChanges - count
1066 Assert.AreEqual(1 , dt.Rows.Count , "DRW108");
1069 [Test] public void RowState()
1071 DataTable myTable = new DataTable("myTable");
1072 DataColumn dc = new DataColumn("Name",typeof(string));
1073 myTable.Columns.Add(dc);
1076 // Create a new DataRow.
1077 myRow = myTable.NewRow();
1082 Assert.AreEqual(DataRowState.Detached , myRow.RowState , "DRW109");
1084 myTable.Rows.Add(myRow);
1088 Assert.AreEqual(DataRowState.Added , myRow.RowState , "DRW110");
1090 myTable.AcceptChanges();
1094 Assert.AreEqual(DataRowState.Unchanged , myRow.RowState , "DRW111");
1096 myRow["Name"] = "Scott";
1100 Assert.AreEqual(DataRowState.Modified , myRow.RowState , "DRW112");
1106 Assert.AreEqual(DataRowState.Deleted , myRow.RowState , "DRW113");
1109 [Test] public void SetColumnError_ByDataColumnError()
1111 string sColErr = "Error!";
1112 DataTable dt = new DataTable("myTable");
1113 DataColumn dc = new DataColumn("Column1");
1115 DataRow dr = dt.NewRow();
1118 Assert.AreEqual(String.Empty, dr.GetColumnError(dc) , "DRW114");
1120 dr.SetColumnError(dc,sColErr );
1123 Assert.AreEqual(sColErr, dr.GetColumnError(dc) , "DRW115");
1126 [Test] public void SetColumnError_ByIndexError()
1128 string sColErr = "Error!";
1129 DataTable dt = new DataTable("myTable");
1130 DataColumn dc = new DataColumn("Column1");
1132 DataRow dr = dt.NewRow();
1135 Assert.AreEqual(String.Empty , dr.GetColumnError(0) , "DRW116");
1137 dr.SetColumnError(0,sColErr );
1140 Assert.AreEqual(sColErr , dr.GetColumnError(0) , "DRW117");
1141 dr.SetColumnError (0, "");
1142 Assert.AreEqual("", dr.GetColumnError (0) , "DRW118");
1145 [Test] public void SetColumnError_ByColumnNameError()
1147 string sColErr = "Error!";
1148 DataTable dt = new DataTable("myTable");
1149 DataColumn dc = new DataColumn("Column1");
1151 DataRow dr = dt.NewRow();
1154 Assert.AreEqual(String.Empty, dr.GetColumnError("Column1") , "DRW118");
1156 dr.SetColumnError("Column1",sColErr );
1159 Assert.AreEqual(sColErr, dr.GetColumnError("Column1") , "DRW119");
1162 [Test] public void SetParentRow_ByDataRow()
1164 DataRow drParent,drChild;
1165 DataRow drArrExcepted,drArrResult;
1166 DataTable dtChild,dtParent;
1167 DataSet ds = new DataSet();
1169 dtChild = DataProvider.CreateChildDataTable();
1170 dtParent= DataProvider.CreateParentDataTable();
1171 //Add tables to dataset
1172 ds.Tables.Add(dtChild);
1173 ds.Tables.Add(dtParent);
1175 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
1176 ds.Relations.Add(dRel);
1178 drParent = dtParent.Rows[0];
1179 drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
1181 drChild.SetParentRow(drParent);
1183 //Get Excepted result
1184 drArrExcepted = drParent;
1185 //Get Result DataRowVersion.Current
1186 drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Current);
1189 Assert.AreEqual(drArrExcepted , drArrResult, "DRW120");
1193 public void testMore()
1195 DataSet ds = DataProvider.CreateForigenConstraint();
1196 DataRow drParent = ds.Tables[0].Rows[0];
1197 //DataRow[] drArray = ds.Tables[1].Rows[0].GetParentRows(ds.Tables[1].ParentRelations[0]);
1198 ds.Tables[1].Rows[0].SetParentRow(drParent);
1204 // test SetParentRow
1205 DataTable parent = DataProvider.CreateParentDataTable();
1206 DataTable child = DataProvider.CreateChildDataTable();
1207 DataRow dr = parent.Rows[0];
1209 parent.AcceptChanges();
1211 child.Rows[0].SetParentRow(dr);
1214 public void checkForLoops()
1216 DataSet ds = new DataSet();
1218 DataTable dtChild = DataProvider.CreateChildDataTable();
1219 DataTable dtParent= DataProvider.CreateParentDataTable();
1220 //Add tables to dataset
1221 ds.Tables.Add(dtChild);
1222 ds.Tables.Add(dtParent);
1224 dtChild.Rows.Clear();
1225 dtParent.Rows.Clear();
1227 dtParent.ChildRelations.Add(dtParent.Columns[0],dtChild.Columns[0]);
1228 dtChild.ChildRelations.Add(dtChild.Columns[0],dtParent.Columns[0]);
1230 dtChild.Rows[0].SetParentRow(dtParent.Rows[0]);
1231 dtParent.Rows[0].SetParentRow(dtChild.Rows[0]);
1234 public void checkForLoopsAdvenced()
1237 DataTable dtChild = new DataTable();
1238 dtChild.Columns.Add("Col1",typeof(int));
1239 dtChild.Columns.Add("Col2",typeof(int));
1241 DataRelation drl = new DataRelation("drl1",dtChild.Columns[0],dtChild.Columns[1]);
1242 dtChild.ChildRelations.Add(drl);
1243 dtChild.Rows[0].SetParentRow(dtChild.Rows[1]);
1244 dtChild.Rows[1].SetParentRow(dtChild.Rows[0]);
1247 [Test] public void SetParentRow_ByDataRowDataRelation()
1249 DataRow drParent,drChild;
1250 DataRow drArrExcepted,drArrResult;
1251 DataTable dtChild,dtParent;
1252 DataSet ds = new DataSet();
1254 dtChild = DataProvider.CreateChildDataTable();
1255 dtParent= DataProvider.CreateParentDataTable();
1256 //Add tables to dataset
1257 ds.Tables.Add(dtChild);
1258 ds.Tables.Add(dtParent);
1260 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
1261 ds.Relations.Add(dRel);
1263 drParent = dtParent.Rows[0];
1264 drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
1266 drChild.SetParentRow(drParent ,dRel);
1268 //Get Excepted result
1269 drArrExcepted = drParent;
1270 //Get Result DataRowVersion.Current
1271 drArrResult = drChild.GetParentRow("Parent-Child",DataRowVersion.Current);
1274 Assert.AreEqual(drArrExcepted , drArrResult, "DRW121");
1277 [Test] public void Table()
1280 dt2 = new DataTable("myTable");
1281 DataRow dr = dt2.NewRow();
1285 Assert.AreEqual(dt2, dt1 , "DRW122");
1288 [Test] public new void ToString()
1292 dtParent= DataProvider.CreateParentDataTable();
1293 dr = dtParent.Rows[0];
1296 Assert.AreEqual(true, dr.ToString().ToLower().StartsWith("system.data.datarow") , "DRW123");
1299 [Test] public void DataRow_RowError()
1301 DataTable dt = new DataTable ("myTable");
1302 DataRow dr = dt.NewRow ();
1304 Assert.AreEqual ( dr.RowError, string.Empty );
1306 dr.RowError = "Err";
1307 Assert.AreEqual ( dr.RowError , "Err" );
1311 [ExpectedException (typeof (ConstraintException))]
1312 public void DataRow_RowError2()
1314 DataTable dt1 = DataProvider.CreateUniqueConstraint();
1316 dt1.BeginLoadData();
1318 DataRow dr = dt1.NewRow();
1325 [ExpectedException (typeof (ConstraintException))]
1326 public void DataRow_RowError3()
1328 DataSet ds= DataProvider.CreateForigenConstraint();
1329 ds.Tables[0].BeginLoadData();
1330 ds.Tables[0].Rows[0][0] = 10;
1331 ds.Tables[0].EndLoadData(); //Foreign constraint violation
1336 public void TestRowErrors ()
1338 DataTable table = new DataTable ();
1339 DataColumn col1 = table.Columns.Add ("col1", typeof (int));
1340 DataColumn col2 = table.Columns.Add ("col2", typeof (int));
1341 DataColumn col3 = table.Columns.Add ("col3", typeof (int));
1343 col1.AllowDBNull = false;
1344 table.Constraints.Add ("uc", new DataColumn[] {col2,col3}, false);
1345 table.BeginLoadData ();
1346 table.Rows.Add (new object[] {null,1,1});
1347 table.Rows.Add (new object[] {1,1,1});
1349 table.EndLoadData ();
1351 } catch (ConstraintException) {}
1352 Assert.IsTrue (table.HasErrors, "#1");
1353 DataRow[] rows = table.GetErrors ();
1355 Assert.AreEqual (2, rows.Length, "#2");
1356 Assert.AreEqual ("Column 'col1' does not allow DBNull.Value.", table.Rows [0].RowError, "#3");
1357 Assert.AreEqual ("Column 'col2, col3' is constrained to be unique. Value '1, 1' is already present."
1358 , table.Rows [1].RowError, "#4");
1360 Assert.AreEqual (table.Rows [0].RowError, table.Rows [0].GetColumnError (0), "#5");
1361 Assert.AreEqual (table.Rows [1].RowError, table.Rows [0].GetColumnError (1), "#6");
1362 Assert.AreEqual (table.Rows [1].RowError, table.Rows [0].GetColumnError (2), "#7");
1364 Assert.AreEqual ("", table.Rows [1].GetColumnError (0), "#8");
1365 Assert.AreEqual (table.Rows [1].RowError, table.Rows [1].GetColumnError (1), "#9");
1366 Assert.AreEqual (table.Rows [1].RowError, table.Rows [1].GetColumnError (2), "#10");
1370 public void BeginEdit()
1372 DataTable myTable = new DataTable("myTable");
1373 DataColumn dc = new DataColumn("Id",typeof(int));
1375 myTable.Columns.Add(dc);
1376 myTable.Rows.Add(new object[] {1});
1377 myTable.Rows.Add(new object[] {2});
1378 myTable.Rows.Add(new object[] {3});
1380 DataRow myRow = myTable.Rows[0];
1384 myRow[0] = 2; //row[0] now conflict with row[1]
1385 Assert.Fail("DRW121: failed to throw ConstraintException");
1387 catch (ConstraintException) {}
1388 catch (AssertionException exc) {throw exc;}
1389 catch (Exception exc)
1391 Assert.Fail("DRW122: Add. Wrong exception type. Got:" + exc);
1394 //Will NOT! throw exception
1396 myRow[0] = 2; //row[0] now conflict with row[1]
1398 DataTable dt = DataProvider.CreateParentDataTable();
1399 DataRow dr = dt.Rows[0];
1404 Assert.Fail("DRW123: failed to throw DeletedRowInaccessibleException");
1406 catch (DeletedRowInaccessibleException) {}
1407 catch (AssertionException exc) {throw exc;}
1408 catch (Exception exc)
1410 Assert.Fail("DRW124: Add. Wrong exception type. Got:" + exc);
1415 public void GetChildRows_DataRelation()
1418 DataRow[] drArrExcepted,drArrResult;
1419 DataTable dtChild,dtParent;
1420 DataSet ds = new DataSet();
1423 dtChild = DataProvider.CreateChildDataTable();
1424 dtParent= DataProvider.CreateParentDataTable();
1426 //Add tables to dataset
1427 ds.Tables.Add(dtChild);
1428 ds.Tables.Add(dtParent);
1429 dr = dtParent.Rows[0];
1432 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
1433 ds.Relations.Add(dRel);
1434 //Get Excepted result
1435 drArrExcepted = dtChild.Select("ParentId=" + dr["ParentId"]);
1437 drArrResult = dr.GetChildRows(dRel);
1439 Assert.AreEqual(drArrExcepted, drArrResult, "DRW125");
1443 public void GetParentRows_DataRelation_DataRowVersion()
1445 DataRow drParent,drChild;
1446 DataRow[] drArrExcepted,drArrResult;
1447 DataTable dtChild,dtParent;
1448 DataSet ds = new DataSet();
1450 dtChild = DataProvider.CreateChildDataTable();
1451 dtParent= DataProvider.CreateParentDataTable();
1452 //Add tables to dataset
1453 ds.Tables.Add(dtChild);
1454 ds.Tables.Add(dtParent);
1456 drParent = dtParent.Rows[0];
1457 drChild = dtChild.Select("ParentId=" + drParent["ParentId"])[0];
1459 //Duplicate several rows in order to create Many to Many relation
1460 dtParent.ImportRow(drParent);
1461 dtParent.ImportRow(drParent);
1462 dtParent.ImportRow(drParent);
1465 DataRelation dRel = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"],false);
1466 ds.Relations.Add(dRel);
1468 //Get Excepted result
1469 drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows );
1470 //Get Result DataRowVersion.Current
1471 drArrResult = drChild.GetParentRows(dRel,DataRowVersion.Current);
1472 Assert.AreEqual(drArrExcepted, drArrResult, "DRW126");
1474 //Get Excepted result
1475 drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.OriginalRows );
1476 //Get Result DataRowVersion.Current
1477 drArrResult = drChild.GetParentRows(dRel,DataRowVersion.Original );
1478 Assert.AreEqual(drArrExcepted, drArrResult, "DRW127");
1480 //Get Excepted result, in this case Current = Default
1481 drArrExcepted = dtParent.Select("ParentId=" + drParent["ParentId"],"",DataViewRowState.CurrentRows);
1482 //Get Result DataRowVersion.Current
1483 drArrResult = drChild.GetParentRows(dRel,DataRowVersion.Default );
1484 Assert.AreEqual(drArrExcepted, drArrResult, "DRW128");
1488 DataTable dtOtherParent = DataProvider.CreateParentDataTable();
1489 DataTable dtOtherChild = DataProvider.CreateChildDataTable();
1491 DataRelation drl = new DataRelation("newRelation",dtOtherParent.Columns[0],dtOtherChild.Columns[0]);
1492 drChild.GetParentRows(drl,DataRowVersion.Current);
1493 Assert.Fail("DRW129: failed to throw ArgumentException");
1495 catch (ArgumentException) {}
1496 catch (AssertionException exc) {throw exc;}
1497 catch (Exception exc)
1499 Assert.Fail("DRW130: Add. Wrong exception type. Got:" + exc);
1504 public void ItemArray()
1506 DataTable dt = GetDataTable();
1507 DataRow dr = dt.Rows[0];
1509 Assert.AreEqual(1, (int)dr.ItemArray[0] , "DRW131" );
1511 Assert.AreEqual("Ofer", (string)dr.ItemArray[1] , "DRW132" );
1513 dt = GetDataTable();
1517 //Changing row via itemArray
1519 dt.Rows[0].ItemArray = new object[] {2,"Oren"};
1521 Assert.AreEqual(2, (Int32)dr.ItemArray[0] , "DRW133" );
1522 Assert.AreEqual("Oren", (string)dr.ItemArray[1] , "DRW134" );
1526 dt.Rows[0].ItemArray = new object[] {2,"Oren","some1else"};
1527 Assert.Fail("DRW135: failed to throw ArgumentException");
1529 catch (ArgumentException) {}
1530 catch (AssertionException exc) {throw exc;}
1531 catch (Exception exc)
1533 Assert.Fail("DRW136: Add. Wrong exception type. Got:" + exc);
1538 public void ItemArray_NewTable ()
1540 DataTable dt = new DataTable("Customers");
1542 dt.Columns.Add("name", typeof (string));
1543 dt.Columns.Add("address", typeof (string));
1544 dt.Columns.Add("phone", typeof (string));
1546 DataRow dr = dt.NewRow();
1547 dr["name"] = "myName";
1548 dr["address"] = "myAddress";
1549 dr["phone"] = "myPhone";
1551 // Should not throw RowNotInTableException
1552 object[] obj = dr.ItemArray;
1555 private DataTable GetDataTable()
1557 DataTable dt = new DataTable("myTable");
1558 dt.Columns.Add("Id",typeof(int));
1559 dt.Columns.Add("Name",typeof(string));
1561 DataRow dr = dt.NewRow();
1562 dr.ItemArray = new object[] {1,"Ofer"};
1570 public void RowError()
1572 DataTable dt = new DataTable("myTable");
1573 DataRow dr = dt.NewRow();
1575 Assert.AreEqual(string.Empty , dr.RowError, "DRW137");
1577 dr.RowError = "Err";
1579 Assert.AreEqual("Err", dr.RowError , "DRW138" );
1581 DataTable dt1 = DataProvider.CreateUniqueConstraint();
1585 dt1.BeginLoadData();
1591 Assert.Fail("DRW139: failed to throw ConstraintException");
1593 catch (ConstraintException)
1595 Assert.AreEqual(2,dt1.GetErrors().Length,"DRW141");
1596 Assert.AreEqual(true,dt1.GetErrors()[0].RowError.Length > 10,"DRW142");
1597 Assert.AreEqual(true,dt1.GetErrors()[1].RowError.Length > 10,"DRW143");
1599 catch (AssertionException exc) {throw exc;}
1600 catch (Exception exc)
1602 Assert.Fail("DRW144: Wrong exception type. Got:" + exc);
1609 ds= DataProvider.CreateForigenConstraint();
1610 ds.Tables[0].BeginLoadData();
1611 ds.Tables[0].Rows[0][0] = 10; //Forigen constraint violation
1612 //ds.Tables[0].AcceptChanges();
1613 ds.Tables[0].EndLoadData();
1614 Assert.Fail("DRW139: failed to throw ConstraintException");
1616 catch (ConstraintException)
1618 Assert.AreEqual(3,ds.Tables[1].GetErrors().Length,"DRW145");
1619 for(int index=0;index<3;index++)
1621 Assert.AreEqual(true,ds.Tables[1].GetErrors()[index].RowError.Length > 10,"RDW146");
1624 catch (AssertionException exc) {throw exc;}
1625 catch (Exception exc)
1627 Assert.Fail("DRW147: Wrong exception type. Got:" + exc);
1632 public void bug78885 ()
1634 DataSet ds = new DataSet ();
1635 DataTable t = ds.Tables.Add ("table");
1638 id = t.Columns.Add ("userID", Type.GetType ("System.Int32"));
1639 id.AutoIncrement = true;
1640 t.Columns.Add ("name", Type.GetType ("System.String"));
1641 t.Columns.Add ("address", Type.GetType ("System.String"));
1642 t.Columns.Add ("zipcode", Type.GetType ("System.Int32"));
1643 t.PrimaryKey = new DataColumn [] { id };
1646 tempRow = t.NewRow ();
1647 tempRow ["name"] = "Joan";
1648 tempRow ["address"] = "Balmes 152";
1649 tempRow ["zipcode"] = "1";
1650 t.Rows.Add (tempRow);
1652 t.RowChanged += new DataRowChangeEventHandler (RowChangedHandler);
1654 /* neither of the calls to EndEdit below generate a RowChangedHandler on MS. the first one does on mono */
1655 t.DefaultView [0].BeginEdit ();
1656 t.DefaultView [0].EndEdit (); /* this generates a call to the row changed handler */
1657 t.DefaultView [0].EndEdit (); /* this doesn't */
1659 Assert.IsFalse (_rowChanged);
1662 private void RowChangedHandler (object sender, DataRowChangeEventArgs e)
1668 string SetAddedModified_ErrMsg = "SetAdded and SetModified can only be called on DataRows with Unchanged DataRowState.";
1670 public void SetAdded_test()
1672 DataTable table = new DataTable();
1674 DataRow row = table.NewRow();
1678 } catch (InvalidOperationException e) {
1679 Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
1682 table.Columns.Add("col1", typeof(int));
1683 table.Columns.Add("col2", typeof(int));
1684 table.Columns.Add("col3", typeof(int));
1686 row = table.Rows.Add(new object[] { 1, 2, 3 });
1687 Assert.AreEqual(DataRowState.Added, row.RowState, "#1");
1691 } catch (InvalidOperationException e) {
1692 Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
1694 Assert.AreEqual(DataRowState.Added, row.RowState, "#2");
1696 row.AcceptChanges();
1698 Assert.AreEqual(DataRowState.Modified, row.RowState, "#5");
1702 } catch (InvalidOperationException e) {
1703 Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
1706 row.AcceptChanges();
1707 Assert.AreEqual(DataRowState.Unchanged, row.RowState, "#3");
1709 Assert.AreEqual(DataRowState.Added, row.RowState, "#4");
1713 public void setAdded_testRollback ()
1715 DataTable table = new DataTable ();
1716 table.Columns.Add ("col1", typeof (int));
1717 table.Columns.Add ("col2", typeof (int));
1719 table.Rows.Add (new object[] {1,1});
1720 table.AcceptChanges ();
1722 table.Rows [0].SetAdded ();
1723 table.RejectChanges ();
1724 Assert.AreEqual (0, table.Rows.Count, "#1");
1728 public void SetModified_test()
1730 DataTable table = new DataTable();
1732 DataRow row = table.NewRow();
1735 } catch (InvalidOperationException) {}
1737 table.Columns.Add("col1", typeof(int));
1738 table.Columns.Add("col2", typeof(int));
1739 table.Columns.Add("col3", typeof(int));
1741 row = table.Rows.Add(new object[] { 1, 2, 3 });
1742 Assert.AreEqual(DataRowState.Added, row.RowState, "#1");
1746 } catch (InvalidOperationException e) {
1747 Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
1750 row.AcceptChanges();
1752 Assert.AreEqual(DataRowState.Modified, row.RowState, "#5");
1756 } catch (InvalidOperationException e) {
1757 Assert.AreEqual (SetAddedModified_ErrMsg, e.Message, "#2");
1760 row.AcceptChanges();
1761 Assert.AreEqual(DataRowState.Unchanged, row.RowState, "#3");
1763 Assert.AreEqual(DataRowState.Modified, row.RowState, "#4");
1767 public void setModified_testRollback()
1769 DataTable table = new DataTable();
1770 table.Columns.Add("col1", typeof(int));
1771 table.Columns.Add("col2", typeof(int));
1773 DataRow row = table.Rows.Add(new object[] { 1, 1 });
1774 table.AcceptChanges();
1777 Assert.AreEqual(row.RowState, DataRowState.Modified, "#0");
1778 Assert.AreEqual(1, row [0, DataRowVersion.Current], "#1");
1779 Assert.AreEqual(1, row [0, DataRowVersion.Original], "#2");
1780 table.RejectChanges ();
1781 Assert.AreEqual(row.RowState, DataRowState.Unchanged, "#3");