1 // DataRowTest.cs - NUnit Test Cases for System.DataRow
4 // Franklin Wise (gracenote@earthlink.net)
5 // Daniel Morgan <danmorg@sc.rr.com>
6 // Roopa Wilson (rowilson@novell.com)
8 // (C) Copyright 2002 Franklin Wise
9 // (C) Copyright 2003 Daniel Morgan
10 // (C) Copyright 2003 Martin Willemoes Hansen
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 using NUnit.Framework;
41 namespace MonoTests.System.Data
44 public class DataRowTest : Assertion {
45 private DataTable table;
49 public void GetReady() {
51 row = table.NewRow ();
52 row ["FName"] = "Hello";
53 row ["LName"] = "World";
57 private DataTable MakeTable ()
59 DataTable namesTable = new DataTable ("Names");
60 DataColumn idColumn = new DataColumn ();
63 idColumn.DataType = Type.GetType ("System.Int32");
64 idColumn.ColumnName = "Id";
65 idColumn.AutoIncrement = true;
66 namesTable.Columns.Add (idColumn);
69 DataColumn fNameColumn = new DataColumn ();
70 fNameColumn.DataType = Type.GetType ("System.String");
71 fNameColumn.ColumnName = "Fname";
72 fNameColumn.DefaultValue = "Fname";
73 namesTable.Columns.Add (fNameColumn);
75 DataColumn lNameColumn = new DataColumn ();
76 lNameColumn.DataType = Type.GetType ("System.String");
77 lNameColumn.ColumnName = "LName";
78 lNameColumn.DefaultValue="LName";
79 namesTable.Columns.Add (lNameColumn);
82 // Set the primary key for the table
83 DataColumn [] keys = new DataColumn [1];
85 namesTable.PrimaryKey = keys;
86 // Return the new DataTable.
91 public void SetColumnErrorTest ()
94 errorString = "Some error!";
95 // Set the error for the specified column of the row.
96 row.SetColumnError (1, errorString);
97 GetColumnErrorTest ();
101 private void GetColumnErrorTest ()
103 // Print the error of a specified column.
104 AssertEquals ("#A01", "Some error!", row.GetColumnError (1));
107 private void GetAllErrorsTest ()
109 DataColumn [] colArr;
112 colArr = row.GetColumnsInError ();
114 for (int i = 0; i < colArr.Length; i++) {
115 AssertEquals ("#A02", table.Columns [1], colArr [i]);
122 public void DeleteRowTest ()
127 for (int i = 1; i <= 2; i++) {
128 newRow = table.NewRow ();
129 newRow ["FName"] = "Name " + i;
130 newRow ["LName"] = " Last Name" + i;
131 table.Rows.Add (newRow);
133 table.AcceptChanges ();
136 for (int i = 1; i < table.Rows.Count; i++) {
137 DataRow r = table.Rows [i];
138 AssertEquals ("#A03", "Name " + cnt, r ["fName"]);
143 // Create a DataView with the table.
144 DataRowCollection rc = table.Rows;
149 AssertEquals ("#A04", "Deleted", rc [0].RowState.ToString ());
150 AssertEquals ("#A05", "Deleted", rc [2].RowState.ToString ());
154 table.AcceptChanges ();
155 AssertEquals ("#A06", "Name 1", (table.Rows [0]) [1]);
160 catch (Exception e) {
161 // Never premise English.
162 //AssertEquals ("#A08", "There is no row at position 2.", e.Message);
167 public void EditModeTest ()
170 //Clear all existing values from table
171 for (int i = 0; i < table.Rows.Count; i++) {
172 table.Rows[i].Delete ();
174 table.AcceptChanges ();
175 row = table.NewRow ();
176 row["FName"] = "My FName";
177 table.Rows.Add (row);
181 //Initially: After Add (Row) But Before Accept Changes");
182 AssertEquals ("#A09", "My FName", row [1, DataRowVersion.Default]);
183 AssertEquals ("#A10", "LName", row [2, DataRowVersion.Default]);
185 AssertEquals ("#A11", "My FName", row [1, DataRowVersion.Current]);
186 AssertEquals ("#A12", "LName", row [2, DataRowVersion.Current]);
189 object o = row [1, DataRowVersion.Original];
190 o = row [1, DataRowVersion.Proposed];
193 catch (Exception e) {
194 if (e.GetType () != typeof (AssertionException)) {
195 AssertEquals ("#A14", typeof (VersionNotFoundException), e.GetType ());
200 //After Accept Changes
201 table.AcceptChanges ();
202 AssertEquals ("#A15", "My FName", row [1, DataRowVersion.Default]);
203 AssertEquals ("#A16", "LName", row [2, DataRowVersion.Default]);
206 AssertEquals ("#A17", "My FName", row [1, DataRowVersion.Current]);
207 AssertEquals ("#A18", "LName", row [2, DataRowVersion.Current]);
210 object o = row [1, DataRowVersion.Proposed];
213 catch (Exception e) {
214 if (e.GetType () != typeof (AssertionException)) {
215 AssertEquals ("#A20", typeof (VersionNotFoundException), e.GetType ());
220 // Stage 3 // Edit Mode
221 table.Rows [0].BeginEdit ();
222 table.Rows [0] ["LName"] = "My LName";
224 AssertEquals ("#A21", "My FName", row [1, DataRowVersion.Default]);
225 AssertEquals ("#A22", "My LName", row [2, DataRowVersion.Default]);
227 AssertEquals ("#A23", "My FName", row [1, DataRowVersion.Current]);
228 AssertEquals ("#A24", "LName", row [2, DataRowVersion.Current]);
231 AssertEquals ("#A25", "My FName", row [1, DataRowVersion.Original]); AssertEquals ("#A26", "LName", row [2, DataRowVersion.Original]);
233 AssertEquals ("#A26", "My FName", row [1, DataRowVersion.Proposed]);
234 AssertEquals ("#A27", "My LName", row [2, DataRowVersion.Proposed]);
238 //After Edit sessions
239 for (int i=0; i < table.Rows.Count;i++)
240 table.Rows [i].EndEdit ();
241 AssertEquals ("#A28", "My FName", row [1, DataRowVersion.Default]);
242 AssertEquals ("#A29", "My LName", row [2, DataRowVersion.Default]);
244 AssertEquals ("#A30", "My FName", row [1, DataRowVersion.Original]); AssertEquals ("#A31", "LName", row [2, DataRowVersion.Original]);
247 AssertEquals ("#A32", "My FName", row [1, DataRowVersion.Current]);
248 AssertEquals ("#A33", "My LName", row [2, DataRowVersion.Current]);
251 object o = row [1, DataRowVersion.Proposed];
254 catch (Exception e) {
255 if (e.GetType ()!=typeof (AssertionException)) {
256 AssertEquals ("#A35", typeof (VersionNotFoundException), e.GetType ());
261 //After Accept Changes
262 table.AcceptChanges ();
263 AssertEquals ("#A36", "My FName", row [1, DataRowVersion.Default]);
264 AssertEquals ("#A37", "My LName", row [2, DataRowVersion.Default]);
267 AssertEquals ("#A38", "My FName", row [1, DataRowVersion.Original]); AssertEquals ("#A39", "My LName", row [2, DataRowVersion.Original]);
269 AssertEquals ("#A40", "My FName", row [1, DataRowVersion.Current]);
270 AssertEquals ("#A41", "My LName", row [2, DataRowVersion.Current]);
274 object o = row [1, DataRowVersion.Proposed];
277 catch (Exception e) {
278 if (e.GetType () != typeof (AssertionException)) {
279 AssertEquals ("#A43", typeof (VersionNotFoundException),
287 // Console.WriteLine (e + "" + e.StackTrace);
291 public void ParentRowTest ()
294 //Clear all existing values from table
295 for (int i = 0; i < table.Rows.Count; i++) {
296 table.Rows[i].Delete ();
298 table.AcceptChanges ();
299 row = table.NewRow ();
300 row["FName"] = "My FName";
302 table.Rows.Add (row);
304 DataTable tableC = new DataTable ("Child");
308 colC = new DataColumn ();
309 colC.DataType = Type.GetType ("System.Int32");
310 colC.ColumnName = "Id";
311 colC.AutoIncrement=true;
312 tableC.Columns.Add (colC);
315 colC = new DataColumn ();
316 colC.DataType = Type.GetType ("System.String");
317 colC.ColumnName = "Name";
318 tableC.Columns.Add (colC);
320 rowC = tableC.NewRow ();
321 rowC["Name"] = "My FName";
322 tableC.Rows.Add (rowC);
323 DataSet ds = new DataSet ();
324 ds.Tables.Add (table);
325 ds.Tables.Add (tableC);
326 DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
327 ds.Relations.Add (dr);
329 rowC.SetParentRow (table.Rows [0], dr);
331 AssertEquals ("#PRT-01", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
332 AssertEquals ("#PRT-02", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
334 ds.Relations.Clear ();
335 dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
336 ds.Relations.Add (dr);
337 rowC.SetParentRow (table.Rows [0], dr);
338 AssertEquals ("#PRT-03", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
339 AssertEquals ("#PRT-04", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
341 ds.Relations.Clear ();
342 dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
343 tableC.ParentRelations.Add (dr);
344 rowC.SetParentRow (table.Rows [0]);
345 AssertEquals ("#PRT-05", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
346 AssertEquals ("#PRT-06", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
351 public void ParentRowTest2 ()
353 DataSet ds = new DataSet ();
354 DataTable tableP = ds.Tables.Add ("Parent");
355 DataTable tableC = ds.Tables.Add ("Child");
359 colC = new DataColumn ();
360 colC.DataType = Type.GetType ("System.Int32");
361 colC.ColumnName = "Id";
362 colC.AutoIncrement = true;
363 tableP.Columns.Add (colC);
365 colC = new DataColumn ();
366 colC.DataType = Type.GetType ("System.Int32");
367 colC.ColumnName = "Id";
368 tableC.Columns.Add (colC);
370 row = tableP.Rows.Add (new object [0]);
371 rowC = tableC.NewRow ();
373 ds.EnforceConstraints = false;
374 DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
375 ds.Relations.Add (dr);
377 rowC.SetParentRow (row, dr);
378 DataRow [] rows = rowC.GetParentRows (dr);
380 AssertEquals ("#A49", 1, rows.Length);
381 AssertEquals ("#A50", tableP.Rows [0], rows [0]);
384 rows = row.GetParentRows (dr);
385 }catch(InvalidConstraintException){
389 Fail("#A51, InvalidConstraintException expected, got : " + e);
392 Fail("#A52, InvalidConstraintException expected but got none.");
396 public void ChildRowTest ()
399 //Clear all existing values from table
400 for (int i = 0; i < table.Rows.Count; i++) {
401 table.Rows [i].Delete ();
403 table.AcceptChanges ();
404 row = table.NewRow ();
405 row ["FName"] = "My FName";
407 table.Rows.Add (row);
409 DataTable tableC = new DataTable ("Child");
413 colC = new DataColumn ();
414 colC.DataType = Type.GetType ("System.Int32");
415 colC.ColumnName = "Id";
416 colC.AutoIncrement = true;
417 tableC.Columns.Add (colC);
419 colC = new DataColumn ();
420 colC.DataType = Type.GetType ("System.String");
421 colC.ColumnName = "Name";
422 tableC.Columns.Add (colC);
424 rowC = tableC.NewRow ();
425 rowC ["Name"] = "My FName";
426 tableC.Rows.Add (rowC);
427 DataSet ds = new DataSet ();
428 ds.Tables.Add (table);
429 ds.Tables.Add (tableC);
430 DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
431 ds.Relations.Add (dr);
433 rowC.SetParentRow (table.Rows [0], dr);
435 DataRow [] rows = (table.Rows [0]).GetChildRows (dr);
437 AssertEquals ("#A45", 1, rows.Length);
438 AssertEquals ("#A46", tableC.Rows [0], rows [0]);
443 public void ChildRowTest2 ()
445 DataSet ds = new DataSet ();
446 DataTable tableP = ds.Tables.Add ("Parent");
447 DataTable tableC = ds.Tables.Add ("Child");
451 colC = new DataColumn ();
452 colC.DataType = Type.GetType ("System.Int32");
453 colC.ColumnName = "Id";
454 colC.AutoIncrement = true;
455 tableP.Columns.Add (colC);
457 colC = new DataColumn ();
458 colC.DataType = Type.GetType ("System.Int32");
459 colC.ColumnName = "Id";
460 tableC.Columns.Add (colC);
462 row = tableP.NewRow ();
463 rowC = tableC.Rows.Add (new object [0]);
465 ds.EnforceConstraints = false;
466 DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
467 ds.Relations.Add (dr);
469 rowC.SetParentRow (row, dr);
470 DataRow [] rows = row.GetChildRows (dr);
472 AssertEquals ("#A47", 1, rows.Length);
473 AssertEquals ("#A48", tableC.Rows [0], rows [0]);
476 rows = rowC.GetChildRows (dr);
477 }catch(InvalidConstraintException){
481 Fail("#A53, InvalidConstraintException expected, got : " + e);
484 Fail("#A54, InvalidConstraintException expected but got none.");
487 [Category ("NotWorking")] //Mismatch in Exception namespace/class reference
489 public void ParentChildRowVersionTest ()
491 DataSet ds = new DataSet ();
492 DataTable tableP = ds.Tables.Add ("Parent");
493 DataTable tableC = ds.Tables.Add ("Child");
497 colC = new DataColumn ();
498 colC.DataType = Type.GetType ("System.Int32");
499 colC.ColumnName = "Id";
500 colC.AutoIncrement = true;
501 tableP.Columns.Add (colC);
503 colC = new DataColumn ();
504 colC.DataType = Type.GetType ("System.Int32");
505 colC.ColumnName = "Id";
506 tableC.Columns.Add (colC);
508 row = tableP.NewRow ();
509 rowC = tableC.Rows.Add (new object [0]);
511 ds.EnforceConstraints = false;
512 DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
513 ds.Relations.Add (dr);
515 rowC.SetParentRow (row, dr);
519 rows = row.GetChildRows (dr, DataRowVersion.Current);
520 }catch (VersionNotFoundException v) {
521 //Check for GetParentRows
523 //Child Row should be in Detached state for the next test
524 rowC = tableC.NewRow();
526 rows = rowC.GetParentRows (dr, DataRowVersion.Current);
527 }catch (VersionNotFoundException v2) {
530 }catch (Exception e){
531 Fail ("#A55, VersionNotFoundException expected, got : " + e);
533 Fail ("#A56, VersionNotFoundException expected but got none.");
534 }catch (Exception e){
535 Fail ("#A57, VersionNotFoundException expected, got : " + e);
538 Fail("#A58, VersionNotFoundException expected but got none.");
541 // tests item at row, column in table to be DBNull.Value
542 private void DBNullTest (string message, DataTable dt, int row, int column)
544 object val = dt.Rows[row].ItemArray[column];
545 AssertEquals(message, DBNull.Value, val);
548 // tests item at row, column in table to be null
549 private void NullTest (string message, DataTable dt, int row, int column)
551 object val = dt.Rows[row].ItemArray[column];
552 AssertEquals(message, null, val);
555 // tests item at row, column in table to be
556 private void ValueTest (string message, DataTable dt, int row, int column, object value)
558 object val = dt.Rows[row].ItemArray[column];
559 AssertEquals(message, value, val);
562 // test set null, DBNull.Value, and ItemArray short count
564 public void NullInItemArray ()
566 string zero = "zero";
570 DataTable table = new DataTable();
571 table.Columns.Add(new DataColumn(zero, typeof(string)));
572 table.Columns.Add(new DataColumn(one, typeof(string)));
573 table.Columns.Add(new DataColumn(two, typeof(string)));
575 object[] obj = new object[3];
576 // -- normal -----------------
581 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
582 // table.Rows[0].ItemArray.ItemArray[1] = "one"
583 // table.Rows[0].ItemArray.ItemArray[2] = "two"
585 DataRow row = table.NewRow();
590 catch(Exception e1) {
591 Fail("DR1: Exception Caught: " + e1);
596 // -- null ----------
599 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
600 // table.Rows[1].ItemArray.ItemArray[1] = DBNull.Value
601 // table.Rows[1].ItemArray.ItemArray[2] = "two"
603 row = table.NewRow();
608 catch(Exception e2) {
609 Fail("DR2: Exception Caught: " + e2);
614 // -- DBNull.Value -------------
615 obj[1] = DBNull.Value;
617 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
618 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
619 // table.Rows[2].ItemArray.ItemArray[2] = "two"
621 row = table.NewRow();
626 catch(Exception e3) {
627 Fail("DR3: Exception Caught: " + e3);
632 // -- object array smaller than number of columns -----
639 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
640 // table.Rows[3].ItemArray.ItemArray[1] = "def"
641 // table.Rows[3].ItemArray.ItemArray[2] = DBNull.Value;
643 row = table.NewRow();
648 catch(Exception e3) {
649 Fail("DR4: Exception Caught: " + e3);
654 // -- normal -----------------
655 ValueTest("DR5: normal value test", table, 0, 0, zero);
656 ValueTest("DR6: normal value test", table, 0, 1, one);
657 ValueTest("DR7: normal value test", table, 0, 2, two);
659 // -- null ----------
660 ValueTest("DR8: null value test", table, 1, 0, zero);
661 ValueTest("DR9: null value test", table, 1, 1, DBNull.Value);
662 ValueTest("DR10: null value test", table, 1, 2, two);
664 // -- DBNull.Value -------------
665 ValueTest("DR11: DBNull.Value value test", table, 2, 0, zero);
666 ValueTest("DR12: DBNull.Value value test", table, 2, 1, DBNull.Value);
667 ValueTest("DR13: DBNull.Value value test", table, 2, 2, two);
669 // -- object array smaller than number of columns -----
670 ValueTest("DR14: array smaller value test", table, 3, 0, abc);
671 ValueTest("DR15: array smaller value test", table, 3, 1, def);
672 ValueTest("DR16: array smaller value test", table, 3, 2, DBNull.Value);
675 // test DefaultValue when setting ItemArray
677 public void DefaultValueInItemArray () {
678 string zero = "zero";
680 DataTable table = new DataTable();
681 table.Columns.Add(new DataColumn("zero", typeof(string)));
683 DataColumn column = new DataColumn("num", typeof(int));
684 column.DefaultValue = 15;
685 table.Columns.Add(column);
687 object[] obj = new object[2];
688 // -- normal -----------------
692 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
693 // table.Rows[0].ItemArray.ItemArray[1] = 8
695 DataRow row = table.NewRow();
700 catch(Exception e1) {
701 Fail("DR17: Exception Caught: " + e1);
706 // -- null ----------
709 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
710 // table.Rows[1].ItemArray.ItemArray[1] = 15
712 row = table.NewRow();
717 catch(Exception e2) {
718 Fail("DR18: Exception Caught: " + e2);
723 // -- DBNull.Value -------------
724 obj[1] = DBNull.Value;
726 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
727 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
728 // even though internally, the v
730 row = table.NewRow();
735 catch(Exception e3) {
736 Fail("DR19: Exception Caught: " + e3);
741 // -- object array smaller than number of columns -----
747 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
748 // table.Rows[3].ItemArray.ItemArray[1] = DBNull.Value
750 row = table.NewRow();
755 catch(Exception e3) {
756 Fail("DR20: Exception Caught: " + e3);
761 // -- normal -----------------
762 ValueTest("DR20: normal value test", table, 0, 0, zero);
763 ValueTest("DR21: normal value test", table, 0, 1, 8);
765 // -- null ----------
766 ValueTest("DR22: null value test", table, 1, 0, zero);
767 ValueTest("DR23: null value test", table, 1, 1, 15);
769 // -- DBNull.Value -------------
770 ValueTest("DR24: DBNull.Value value test", table, 2, 0, zero);
771 DBNullTest("DR25: DBNull.Value value test", table, 2, 1);
773 // -- object array smaller than number of columns -----
774 ValueTest("DR26: array smaller value test", table, 3, 0, abc);
775 ValueTest("DR27: array smaller value test", table, 3, 1, 15);
778 // test AutoIncrement when setting ItemArray
780 public void AutoIncrementInItemArray () {
781 string zero = "zero";
784 DataTable table = new DataTable();
785 table.Columns.Add(new DataColumn(zero, typeof(string)));
787 DataColumn column = new DataColumn("num", typeof(int));
788 column.AutoIncrement = true;
789 table.Columns.Add(column);
791 object[] obj = new object[2];
792 // -- normal -----------------
796 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
797 // table.Rows[0].ItemArray.ItemArray[1] = 8
799 DataRow row = table.NewRow();
804 catch(Exception e1) {
805 Fail("DR28: Exception Caught: " + e1);
810 // -- null 1----------
813 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
814 // table.Rows[1].ItemArray.ItemArray[1] = 9
816 row = table.NewRow();
821 catch(Exception e2) {
822 Fail("DR29: Exception Caught: " + e2);
827 // -- null 2----------
830 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
831 // table.Rows[1].ItemArray.ItemArray[1] = 10
833 row = table.NewRow();
838 catch(Exception e2) {
839 Fail("DR30: Exception Caught: " + e2);
844 // -- null 3----------
847 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
848 // table.Rows[1].ItemArray.ItemArray[1] = 11
850 row = table.NewRow();
855 catch(Exception e2) {
856 Fail("DR31: Exception Caught: " + e2);
861 // -- DBNull.Value -------------
862 obj[1] = DBNull.Value;
864 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
865 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
866 // even though internally, the AutoIncrement value
869 row = table.NewRow();
874 catch(Exception e3) {
875 Fail("DR32: Exception Caught: " + e3);
880 // -- null 4----------
883 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
884 // table.Rows[1].ItemArray.ItemArray[1] = 13
886 row = table.NewRow();
891 catch(Exception e2) {
892 Fail("DR48: Exception Caught: " + e2);
897 // -- object array smaller than number of columns -----
903 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
904 // table.Rows[3].ItemArray.ItemArray[1] = 14
906 row = table.NewRow();
911 catch(Exception e3) {
912 Fail("DR33: Exception Caught: " + e3);
917 // -- normal -----------------
918 ValueTest("DR34: normal value test", table, 0, 0, zero);
919 ValueTest("DR35: normal value test", table, 0, 1, 8);
921 // -- null 1----------
922 ValueTest("DR36: null value test", table, 1, 0, zero);
923 ValueTest("DR37: null value test", table, 1, 1, 9);
925 // -- null 2----------
926 ValueTest("DR38: null value test", table, 2, 0, zero);
927 ValueTest("DR39: null value test", table, 2, 1, 10);
929 // -- null 3----------
930 ValueTest("DR40: null value test", table, 3, 0, zero);
931 ValueTest("DR41: null value test", table, 3, 1, 11);
933 // -- DBNull.Value -------------
934 ValueTest("DR42: DBNull.Value value test", table, 4, 0, zero);
935 ValueTest("DR43: DBNull.Value value test", table, 4, 1, DBNull.Value);
937 // -- null 4----------
938 ValueTest("DR44: null value test", table, 5, 0, zero);
939 ValueTest("DR45: null value test", table, 5, 1, 13);
941 // -- object array smaller than number of columns -----
942 ValueTest("DR46: array smaller value test", table, 6, 0, abc);
943 ValueTest("DR47: array smaller value test", table, 6, 1, 14);
947 public void AutoIncrementColumnIntegrity ()
949 // AutoIncrement-column shouldn't raise index out of range
950 // exception because of size mismatch of internal itemarray.
951 DataTable dt = new DataTable ();
952 dt.Columns.Add ("foo");
953 dt.Rows.Add (new object [] {"value"});
954 DataColumn col = new DataColumn ("bar");
955 col.AutoIncrement = true;
956 dt.Columns.Add (col);
957 dt.Rows [0] [0] = "test";
961 public void EnforceConstraint ()
965 DataSet ds = new DataSet();
966 DataTable parent = ds.Tables.Add("parent");
967 parent.Columns.Add("id", typeof(int));
968 DataTable child = ds.Tables.Add("child");
969 child.Columns.Add("idref", typeof(int));
970 Constraint uniqueId = null;
971 parent.Constraints.Add(uniqueId = new UniqueConstraint("uniqueId",
972 new DataColumn[] {parent.Columns["id"]}, true));
973 ForeignKeyConstraint fkc = new ForeignKeyConstraint("ParentChildConstraint", new DataColumn[] { parent.Columns["id"] },
974 new DataColumn[] { child.Columns["idref"]});
976 child.Constraints.Add(fkc);
978 DataRelation relateParentChild = new DataRelation("relateParentChild",
979 new DataColumn[] {parent.Columns["id"] },
980 new DataColumn[] {child.Columns["idref"] },
982 ds.Relations.Add(relateParentChild);
984 ds.EnforceConstraints = false;
985 DataRow parentRow = parent.Rows.Add(new object[] { id });
986 DataRow childRow = child.Rows.Add(new object[] { id });
987 if (parentRow == childRow.GetParentRow(relateParentChild)) {
988 foreach(DataColumn dc in parent.Columns)
989 AssertEquals(100,parentRow[dc]);
997 [ExpectedException (typeof (RowNotInTableException))]
998 public void DetachedRowItemException ()
1000 DataTable dt = new DataTable ("table");
1001 dt.Columns.Add ("col");
1002 dt.Rows.Add ((new object [] {"val"}));
1004 DataRow dr = dt.NewRow ();
1005 AssertEquals (DataRowState.Detached, dr.RowState);
1007 AssertEquals (DataRowState.Detached, dr.RowState);
1008 object o = dr ["col"];
1012 public void SetParentRow_Null ()
1014 DataSet ds = new DataSet();
1016 DataTable child = ds.Tables.Add("child");
1017 child.Columns.Add("column1");
1019 DataRow r1 = child.NewRow();
1021 r1.SetParentRow(null);
1025 public void SetParentRow_DataInheritance ()
1027 var ds = new DataSet() ;
1029 var child = ds.Tables.Add("child") ;
1031 var childColumn1 = child.Columns.Add("column1");
1032 var childColumn2 = child.Columns.Add("column2");
1034 var parent1 = ds.Tables.Add("parent1");
1035 var parent1Column1 = parent1.Columns.Add("column1");
1036 var parent1Column2 = parent1.Columns.Add("column2");
1038 var parent2 = ds.Tables.Add("parent2");
1039 var parent2Column1 = parent2.Columns.Add("column1");
1040 var parent2Column2 = parent2.Columns.Add("column2");
1042 var relation1 = ds.Relations.Add("parent1-child", parent1Column1, childColumn1);
1043 ds.Relations.Add("parent2-child", parent2Column2, childColumn2);
1045 var childRow1 = child.NewRow();
1046 var parent1Row = parent1.NewRow();
1047 var parent2Row = parent2.NewRow();
1049 parent1Row[parent1Column1] = "p1c1";
1050 parent1Row[parent1Column2] = "p1c2";
1051 parent2Row[parent2Column1] = "p2c1";
1052 parent2Row[parent2Column2] = "p2c2";
1054 child.Rows.Add(childRow1);
1055 parent1.Rows.Add(parent1Row);
1056 parent2.Rows.Add(parent2Row);
1058 childRow1.SetParentRow(parent1Row);
1059 AssertEquals ("p1c1", childRow1[childColumn1]);
1060 AssertEquals (DBNull.Value, childRow1[childColumn2]);
1062 childRow1.SetParentRow(parent2Row);
1063 AssertEquals ("p1c1", childRow1[childColumn1]);
1064 AssertEquals ("p2c2", childRow1[childColumn2]);
1066 childRow1.SetParentRow(null);
1067 AssertEquals (DBNull.Value, childRow1[childColumn1]);
1068 AssertEquals (DBNull.Value, childRow1[childColumn2]);
1070 childRow1.SetParentRow(parent2Row);
1071 AssertEquals (DBNull.Value, childRow1[childColumn1]);
1072 AssertEquals ("p2c2", childRow1[childColumn2]);
1076 public void SetParentRow_with_Relation ()
1078 var ds = new DataSet() ;
1080 var child = ds.Tables.Add("child") ;
1082 var childColumn1 = child.Columns.Add("column1");
1083 var childColumn2 = child.Columns.Add("column2");
1085 var parent1 = ds.Tables.Add("parent1");
1086 var parent1Column1 = parent1.Columns.Add("column1");
1087 var parent1Column2 = parent1.Columns.Add("column2");
1089 var parent2 = ds.Tables.Add("parent2");
1090 var parent2Column1 = parent2.Columns.Add("column1");
1091 var parent2Column2 = parent2.Columns.Add("column2");
1093 var relation1 = ds.Relations.Add("parent1-child", parent1Column1, childColumn1) ;
1094 var relation2 = ds.Relations.Add("parent2-child", parent2Column2, childColumn2) ;
1096 var childRow1 = child.NewRow();
1097 var parent1Row = parent1.NewRow();
1098 var parent2Row = parent2.NewRow();
1100 parent1Row[parent1Column1] = "p1c1";
1101 parent1Row[parent1Column2] = "p1c2";
1102 parent2Row[parent2Column1] = "p2c1";
1103 parent2Row[parent2Column2] = "p2c2";
1105 child.Rows.Add(childRow1);
1106 parent1.Rows.Add(parent1Row);
1107 parent2.Rows.Add(parent2Row);
1110 childRow1.SetParentRow (null, relation2);
1111 AssertEquals (DBNull.Value, childRow1[childColumn1]);
1112 AssertEquals (DBNull.Value, childRow1[childColumn2]);
1115 childRow1.SetParentRow(parent1Row, relation2);
1116 Fail ("Must throw InvalidConstaintException");
1117 } catch (InvalidConstraintException e) {
1119 AssertEquals (DBNull.Value, childRow1[childColumn1]);
1120 AssertEquals (DBNull.Value, childRow1[childColumn2]);
1122 childRow1.SetParentRow(parent1Row, relation1);
1123 AssertEquals ("p1c1", childRow1[childColumn1]);
1124 AssertEquals (DBNull.Value, childRow1[childColumn2]);
1127 childRow1.SetParentRow (null, relation2);
1128 AssertEquals ("p1c1", childRow1[childColumn1]);
1129 AssertEquals (DBNull.Value, childRow1[childColumn2]);
1131 childRow1.SetParentRow (null, relation1);
1132 AssertEquals (DBNull.Value, childRow1[childColumn1]);
1133 AssertEquals (DBNull.Value, childRow1[childColumn2]);
1137 public void SetParent_missing_ParentRow ()
1139 var ds = new DataSet() ;
1141 var child = ds.Tables.Add("child") ;
1143 var childColumn1 = child.Columns.Add("column1");
1144 var childColumn2 = child.Columns.Add("column2");
1146 var parent1 = ds.Tables.Add("parent1");
1147 var parentColumn1 = parent1.Columns.Add("column1");
1149 var parent2 = ds.Tables.Add("parent2");
1150 var parentColumn2 = parent2.Columns.Add("column2");
1152 ds.Relations.Add("parent1-child", parentColumn1, childColumn1);
1153 ds.Relations.Add("parent2-child", parentColumn2, childColumn2);
1155 var childRow = child.NewRow();
1156 var parentRow = parent2.NewRow();
1158 parentRow[parentColumn2] = "value";
1160 child.Rows.Add(childRow);
1161 parent2.Rows.Add(parentRow);
1163 childRow.SetParentRow(parentRow);
1164 AssertEquals (DBNull.Value, childRow[childColumn1]);
1165 AssertEquals ("value", childRow[childColumn2]);