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 {
46 private DataTable _tbl;
47 private DataTable table;
51 public void GetReady() {
52 _tbl = new DataTable();
54 row = table.NewRow ();
55 row ["FName"] = "Hello";
56 row ["LName"] = "World";
60 private DataTable MakeTable ()
62 DataTable namesTable = new DataTable ("Names");
63 DataColumn idColumn = new DataColumn ();
66 idColumn.DataType = Type.GetType ("System.Int32");
67 idColumn.ColumnName = "Id";
68 idColumn.AutoIncrement = true;
69 namesTable.Columns.Add (idColumn);
72 DataColumn fNameColumn = new DataColumn ();
73 fNameColumn.DataType = Type.GetType ("System.String");
74 fNameColumn.ColumnName = "Fname";
75 fNameColumn.DefaultValue = "Fname";
76 namesTable.Columns.Add (fNameColumn);
78 DataColumn lNameColumn = new DataColumn ();
79 lNameColumn.DataType = Type.GetType ("System.String");
80 lNameColumn.ColumnName = "LName";
81 lNameColumn.DefaultValue="LName";
82 namesTable.Columns.Add (lNameColumn);
85 // Set the primary key for the table
86 DataColumn [] keys = new DataColumn [1];
88 namesTable.PrimaryKey = keys;
89 // Return the new DataTable.
94 public void SetColumnErrorTest ()
97 errorString = "Some error!";
98 // Set the error for the specified column of the row.
99 row.SetColumnError (1, errorString);
100 GetColumnErrorTest ();
104 private void GetColumnErrorTest ()
106 // Print the error of a specified column.
107 AssertEquals ("#A01", "Some error!", row.GetColumnError (1));
110 private void GetAllErrorsTest ()
112 DataColumn [] colArr;
115 colArr = row.GetColumnsInError ();
117 for (int i = 0; i < colArr.Length; i++) {
118 AssertEquals ("#A02", table.Columns [1], colArr [i]);
125 public void DeleteRowTest ()
130 for (int i = 1; i <= 2; i++) {
131 newRow = table.NewRow ();
132 newRow ["FName"] = "Name " + i;
133 newRow ["LName"] = " Last Name" + i;
134 table.Rows.Add (newRow);
136 table.AcceptChanges ();
139 for (int i = 1; i < table.Rows.Count; i++) {
140 DataRow r = table.Rows [i];
141 AssertEquals ("#A03", "Name " + cnt, r ["fName"]);
146 // Create a DataView with the table.
147 DataRowCollection rc = table.Rows;
152 AssertEquals ("#A04", "Deleted", rc [0].RowState.ToString ());
153 AssertEquals ("#A05", "Deleted", rc [2].RowState.ToString ());
157 table.AcceptChanges ();
158 AssertEquals ("#A06", "Name 1", (table.Rows [0]) [1]);
163 catch (Exception e) {
164 AssertEquals ("#A08", "There is no row at position 2.", e.Message);
169 public void EditModeTest ()
172 //Clear all existing values from table
173 for (int i = 0; i < table.Rows.Count; i++) {
174 table.Rows[i].Delete ();
176 table.AcceptChanges ();
177 row = table.NewRow ();
178 row["FName"] = "My FName";
179 table.Rows.Add (row);
183 //Initially: After Add (Row) But Before Accept Changes");
184 AssertEquals ("#A09", "My FName", row [1, DataRowVersion.Default]);
185 AssertEquals ("#A10", "LName", row [2, DataRowVersion.Default]);
187 AssertEquals ("#A11", "My FName", row [1, DataRowVersion.Current]);
188 AssertEquals ("#A12", "LName", row [2, DataRowVersion.Current]);
191 object o = row [1, DataRowVersion.Original];
192 o = row [1, DataRowVersion.Proposed];
195 catch (Exception e) {
196 if (e.GetType () != typeof (AssertionException)) {
197 AssertEquals ("#A14", typeof (VersionNotFoundException), e.GetType ());
202 //After Accept Changes
203 table.AcceptChanges ();
204 AssertEquals ("#A15", "My FName", row [1, DataRowVersion.Default]);
205 AssertEquals ("#A16", "LName", row [2, DataRowVersion.Default]);
208 AssertEquals ("#A17", "My FName", row [1, DataRowVersion.Current]);
209 AssertEquals ("#A18", "LName", row [2, DataRowVersion.Current]);
212 object o = row [1, DataRowVersion.Proposed];
215 catch (Exception e) {
216 if (e.GetType () != typeof (AssertionException)) {
217 AssertEquals ("#A20", typeof (VersionNotFoundException), e.GetType ());
222 // Stage 3 // Edit Mode
223 table.Rows [0].BeginEdit ();
224 table.Rows [0] ["LName"] = "My LName";
226 AssertEquals ("#A21", "My FName", row [1, DataRowVersion.Default]);
227 AssertEquals ("#A22", "My LName", row [2, DataRowVersion.Default]);
229 AssertEquals ("#A23", "My FName", row [1, DataRowVersion.Current]);
230 AssertEquals ("#A24", "LName", row [2, DataRowVersion.Current]);
233 AssertEquals ("#A25", "My FName", row [1, DataRowVersion.Original]); AssertEquals ("#A26", "LName", row [2, DataRowVersion.Original]);
235 AssertEquals ("#A26", "My FName", row [1, DataRowVersion.Proposed]);
236 AssertEquals ("#A27", "My LName", row [2, DataRowVersion.Proposed]);
240 //After Edit sessions
241 for (int i=0; i < table.Rows.Count;i++)
242 table.Rows [i].EndEdit ();
243 AssertEquals ("#A28", "My FName", row [1, DataRowVersion.Default]);
244 AssertEquals ("#A29", "My LName", row [2, DataRowVersion.Default]);
246 AssertEquals ("#A30", "My FName", row [1, DataRowVersion.Original]); AssertEquals ("#A31", "LName", row [2, DataRowVersion.Original]);
249 AssertEquals ("#A32", "My FName", row [1, DataRowVersion.Current]);
250 AssertEquals ("#A33", "My LName", row [2, DataRowVersion.Current]);
253 object o = row [1, DataRowVersion.Proposed];
256 catch (Exception e) {
257 if (e.GetType ()!=typeof (AssertionException)) {
258 AssertEquals ("#A35", typeof (VersionNotFoundException), e.GetType ());
263 //After Accept Changes
264 table.AcceptChanges ();
265 AssertEquals ("#A36", "My FName", row [1, DataRowVersion.Default]);
266 AssertEquals ("#A37", "My LName", row [2, DataRowVersion.Default]);
269 AssertEquals ("#A38", "My FName", row [1, DataRowVersion.Original]); AssertEquals ("#A39", "My LName", row [2, DataRowVersion.Original]);
271 AssertEquals ("#A40", "My FName", row [1, DataRowVersion.Current]);
272 AssertEquals ("#A41", "My LName", row [2, DataRowVersion.Current]);
276 object o = row [1, DataRowVersion.Proposed];
279 catch (Exception e) {
280 if (e.GetType () != typeof (AssertionException)) {
281 AssertEquals ("#A43", typeof (VersionNotFoundException),
289 // Console.WriteLine (e + "" + e.StackTrace);
293 public void ParentRowTest ()
296 //Clear all existing values from table
297 for (int i = 0; i < table.Rows.Count; i++) {
298 table.Rows[i].Delete ();
300 table.AcceptChanges ();
301 row = table.NewRow ();
302 row["FName"] = "My FName";
304 table.Rows.Add (row);
306 DataTable tableC = new DataTable ("Child");
310 colC = new DataColumn ();
311 colC.DataType = Type.GetType ("System.Int32");
312 colC.ColumnName = "Id";
313 colC.AutoIncrement=true;
314 tableC.Columns.Add (colC);
317 colC = new DataColumn ();
318 colC.DataType = Type.GetType ("System.String");
319 colC.ColumnName = "Name";
320 tableC.Columns.Add (colC);
322 rowC = tableC.NewRow ();
323 rowC["Name"] = "My FName";
324 tableC.Rows.Add (rowC);
325 DataSet ds = new DataSet ();
326 ds.Tables.Add (table);
327 ds.Tables.Add (tableC);
328 DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
329 ds.Relations.Add (dr);
331 rowC.SetParentRow (table.Rows [0], dr);
333 AssertEquals ("#PRT-01", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
334 AssertEquals ("#PRT-02", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
336 ds.Relations.Clear ();
337 dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
338 ds.Relations.Add (dr);
339 rowC.SetParentRow (table.Rows [0], dr);
340 AssertEquals ("#PRT-03", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
341 AssertEquals ("#PRT-04", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
343 ds.Relations.Clear ();
344 dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
345 tableC.ParentRelations.Add (dr);
346 rowC.SetParentRow (table.Rows [0]);
347 AssertEquals ("#PRT-05", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
348 AssertEquals ("#PRT-06", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
353 public void ParentRowTest2 ()
355 DataSet ds = new DataSet ();
356 DataTable tableP = ds.Tables.Add ("Parent");
357 DataTable tableC = ds.Tables.Add ("Child");
361 colC = new DataColumn ();
362 colC.DataType = Type.GetType ("System.Int32");
363 colC.ColumnName = "Id";
364 colC.AutoIncrement = true;
365 tableP.Columns.Add (colC);
367 colC = new DataColumn ();
368 colC.DataType = Type.GetType ("System.Int32");
369 colC.ColumnName = "Id";
370 tableC.Columns.Add (colC);
372 row = tableP.Rows.Add (new object [0]);
373 rowC = tableC.NewRow ();
375 ds.EnforceConstraints = false;
376 DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
377 ds.Relations.Add (dr);
379 rowC.SetParentRow (row, dr);
380 DataRow [] rows = rowC.GetParentRows (dr);
382 AssertEquals ("#A49", 1, rows.Length);
383 AssertEquals ("#A50", tableP.Rows [0], rows [0]);
386 rows = row.GetParentRows (dr);
387 }catch(InvalidConstraintException){
391 Fail("#A51, InvalidConstraintException expected, got : " + e);
394 Fail("#A52, InvalidConstraintException expected but got none.");
398 public void ChildRowTest ()
401 //Clear all existing values from table
402 for (int i = 0; i < table.Rows.Count; i++) {
403 table.Rows [i].Delete ();
405 table.AcceptChanges ();
406 row = table.NewRow ();
407 row ["FName"] = "My FName";
409 table.Rows.Add (row);
411 DataTable tableC = new DataTable ("Child");
415 colC = new DataColumn ();
416 colC.DataType = Type.GetType ("System.Int32");
417 colC.ColumnName = "Id";
418 colC.AutoIncrement = true;
419 tableC.Columns.Add (colC);
421 colC = new DataColumn ();
422 colC.DataType = Type.GetType ("System.String");
423 colC.ColumnName = "Name";
424 tableC.Columns.Add (colC);
426 rowC = tableC.NewRow ();
427 rowC ["Name"] = "My FName";
428 tableC.Rows.Add (rowC);
429 DataSet ds = new DataSet ();
430 ds.Tables.Add (table);
431 ds.Tables.Add (tableC);
432 DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
433 ds.Relations.Add (dr);
435 rowC.SetParentRow (table.Rows [0], dr);
437 DataRow [] rows = (table.Rows [0]).GetChildRows (dr);
439 AssertEquals ("#A45", 1, rows.Length);
440 AssertEquals ("#A46", tableC.Rows [0], rows [0]);
445 public void ChildRowTest2 ()
447 DataSet ds = new DataSet ();
448 DataTable tableP = ds.Tables.Add ("Parent");
449 DataTable tableC = ds.Tables.Add ("Child");
453 colC = new DataColumn ();
454 colC.DataType = Type.GetType ("System.Int32");
455 colC.ColumnName = "Id";
456 colC.AutoIncrement = true;
457 tableP.Columns.Add (colC);
459 colC = new DataColumn ();
460 colC.DataType = Type.GetType ("System.Int32");
461 colC.ColumnName = "Id";
462 tableC.Columns.Add (colC);
464 row = tableP.NewRow ();
465 rowC = tableC.Rows.Add (new object [0]);
467 ds.EnforceConstraints = false;
468 DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
469 ds.Relations.Add (dr);
471 rowC.SetParentRow (row, dr);
472 DataRow [] rows = row.GetChildRows (dr);
474 AssertEquals ("#A47", 1, rows.Length);
475 AssertEquals ("#A48", tableC.Rows [0], rows [0]);
478 rows = rowC.GetChildRows (dr);
479 }catch(InvalidConstraintException){
483 Fail("#A53, InvalidConstraintException expected, got : " + e);
486 Fail("#A54, InvalidConstraintException expected but got none.");
490 public void ParentChildRowVersionTest ()
492 DataSet ds = new DataSet ();
493 DataTable tableP = ds.Tables.Add ("Parent");
494 DataTable tableC = ds.Tables.Add ("Child");
498 colC = new DataColumn ();
499 colC.DataType = Type.GetType ("System.Int32");
500 colC.ColumnName = "Id";
501 colC.AutoIncrement = true;
502 tableP.Columns.Add (colC);
504 colC = new DataColumn ();
505 colC.DataType = Type.GetType ("System.Int32");
506 colC.ColumnName = "Id";
507 tableC.Columns.Add (colC);
509 row = tableP.NewRow ();
510 rowC = tableC.Rows.Add (new object [0]);
512 ds.EnforceConstraints = false;
513 DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
514 ds.Relations.Add (dr);
516 rowC.SetParentRow (row, dr);
520 rows = row.GetChildRows (dr, DataRowVersion.Current);
521 }catch (VersionNotFoundException v) {
522 //Check for GetParentRows
524 //Child Row should be in Detached state for the next test
525 rowC = tableC.NewRow();
527 rows = rowC.GetParentRows (dr, DataRowVersion.Current);
528 }catch (VersionNotFoundException v2) {
531 }catch (Exception e){
532 Fail ("#A55, VersionNotFoundException expected, got : " + e);
534 Fail ("#A56, VersionNotFoundException expected but got none.");
535 }catch (Exception e){
536 Fail ("#A57, VersionNotFoundException expected, got : " + e);
539 Fail("#A58, VersionNotFoundException expected but got none.");
542 // tests item at row, column in table to be DBNull.Value
543 private void DBNullTest (string message, DataTable dt, int row, int column)
545 object val = dt.Rows[row].ItemArray[column];
546 AssertEquals(message, DBNull.Value, val);
549 // tests item at row, column in table to be null
550 private void NullTest (string message, DataTable dt, int row, int column)
552 object val = dt.Rows[row].ItemArray[column];
553 AssertEquals(message, null, val);
556 // tests item at row, column in table to be
557 private void ValueTest (string message, DataTable dt, int row, int column, object value)
559 object val = dt.Rows[row].ItemArray[column];
560 AssertEquals(message, value, val);
563 // test set null, DBNull.Value, and ItemArray short count
565 public void NullInItemArray ()
567 string zero = "zero";
571 DataTable table = new DataTable();
572 table.Columns.Add(new DataColumn(zero, typeof(string)));
573 table.Columns.Add(new DataColumn(one, typeof(string)));
574 table.Columns.Add(new DataColumn(two, typeof(string)));
576 object[] obj = new object[3];
577 // -- normal -----------------
582 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
583 // table.Rows[0].ItemArray.ItemArray[1] = "one"
584 // table.Rows[0].ItemArray.ItemArray[2] = "two"
586 DataRow row = table.NewRow();
591 catch(Exception e1) {
592 Fail("DR1: Exception Caught: " + e1);
597 // -- null ----------
600 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
601 // table.Rows[1].ItemArray.ItemArray[1] = DBNull.Value
602 // table.Rows[1].ItemArray.ItemArray[2] = "two"
604 row = table.NewRow();
609 catch(Exception e2) {
610 Fail("DR2: Exception Caught: " + e2);
615 // -- DBNull.Value -------------
616 obj[1] = DBNull.Value;
618 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
619 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
620 // table.Rows[2].ItemArray.ItemArray[2] = "two"
622 row = table.NewRow();
627 catch(Exception e3) {
628 Fail("DR3: Exception Caught: " + e3);
633 // -- object array smaller than number of columns -----
640 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
641 // table.Rows[3].ItemArray.ItemArray[1] = "def"
642 // table.Rows[3].ItemArray.ItemArray[2] = DBNull.Value;
644 row = table.NewRow();
649 catch(Exception e3) {
650 Fail("DR4: Exception Caught: " + e3);
655 // -- normal -----------------
656 ValueTest("DR5: normal value test", table, 0, 0, zero);
657 ValueTest("DR6: normal value test", table, 0, 1, one);
658 ValueTest("DR7: normal value test", table, 0, 2, two);
660 // -- null ----------
661 ValueTest("DR8: null value test", table, 1, 0, zero);
662 ValueTest("DR9: null value test", table, 1, 1, DBNull.Value);
663 ValueTest("DR10: null value test", table, 1, 2, two);
665 // -- DBNull.Value -------------
666 ValueTest("DR11: DBNull.Value value test", table, 2, 0, zero);
667 ValueTest("DR12: DBNull.Value value test", table, 2, 1, DBNull.Value);
668 ValueTest("DR13: DBNull.Value value test", table, 2, 2, two);
670 // -- object array smaller than number of columns -----
671 ValueTest("DR14: array smaller value test", table, 3, 0, abc);
672 ValueTest("DR15: array smaller value test", table, 3, 1, def);
673 ValueTest("DR16: array smaller value test", table, 3, 2, DBNull.Value);
676 // test DefaultValue when setting ItemArray
678 public void DefaultValueInItemArray () {
679 string zero = "zero";
681 DataTable table = new DataTable();
682 table.Columns.Add(new DataColumn("zero", typeof(string)));
684 DataColumn column = new DataColumn("num", typeof(int));
685 column.DefaultValue = 15;
686 table.Columns.Add(column);
688 object[] obj = new object[2];
689 // -- normal -----------------
693 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
694 // table.Rows[0].ItemArray.ItemArray[1] = 8
696 DataRow row = table.NewRow();
701 catch(Exception e1) {
702 Fail("DR17: Exception Caught: " + e1);
707 // -- null ----------
710 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
711 // table.Rows[1].ItemArray.ItemArray[1] = 15
713 row = table.NewRow();
718 catch(Exception e2) {
719 Fail("DR18: Exception Caught: " + e2);
724 // -- DBNull.Value -------------
725 obj[1] = DBNull.Value;
727 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
728 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
729 // even though internally, the v
731 row = table.NewRow();
736 catch(Exception e3) {
737 Fail("DR19: Exception Caught: " + e3);
742 // -- object array smaller than number of columns -----
748 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
749 // table.Rows[3].ItemArray.ItemArray[1] = DBNull.Value
751 row = table.NewRow();
756 catch(Exception e3) {
757 Fail("DR20: Exception Caught: " + e3);
762 // -- normal -----------------
763 ValueTest("DR20: normal value test", table, 0, 0, zero);
764 ValueTest("DR21: normal value test", table, 0, 1, 8);
766 // -- null ----------
767 ValueTest("DR22: null value test", table, 1, 0, zero);
768 ValueTest("DR23: null value test", table, 1, 1, 15);
770 // -- DBNull.Value -------------
771 ValueTest("DR24: DBNull.Value value test", table, 2, 0, zero);
772 DBNullTest("DR25: DBNull.Value value test", table, 2, 1);
774 // -- object array smaller than number of columns -----
775 ValueTest("DR26: array smaller value test", table, 3, 0, abc);
776 ValueTest("DR27: array smaller value test", table, 3, 1, 15);
779 // test AutoIncrement when setting ItemArray
781 public void AutoIncrementInItemArray () {
782 string zero = "zero";
785 DataTable table = new DataTable();
786 table.Columns.Add(new DataColumn(zero, typeof(string)));
788 DataColumn column = new DataColumn("num", typeof(int));
789 column.AutoIncrement = true;
790 table.Columns.Add(column);
792 object[] obj = new object[2];
793 // -- normal -----------------
797 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
798 // table.Rows[0].ItemArray.ItemArray[1] = 8
800 DataRow row = table.NewRow();
805 catch(Exception e1) {
806 Fail("DR28: Exception Caught: " + e1);
811 // -- null 1----------
814 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
815 // table.Rows[1].ItemArray.ItemArray[1] = 9
817 row = table.NewRow();
822 catch(Exception e2) {
823 Fail("DR29: Exception Caught: " + e2);
828 // -- null 2----------
831 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
832 // table.Rows[1].ItemArray.ItemArray[1] = 10
834 row = table.NewRow();
839 catch(Exception e2) {
840 Fail("DR30: Exception Caught: " + e2);
845 // -- null 3----------
848 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
849 // table.Rows[1].ItemArray.ItemArray[1] = 11
851 row = table.NewRow();
856 catch(Exception e2) {
857 Fail("DR31: Exception Caught: " + e2);
862 // -- DBNull.Value -------------
863 obj[1] = DBNull.Value;
865 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
866 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
867 // even though internally, the AutoIncrement value
870 row = table.NewRow();
875 catch(Exception e3) {
876 Fail("DR32: Exception Caught: " + e3);
881 // -- null 4----------
884 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
885 // table.Rows[1].ItemArray.ItemArray[1] = 13
887 row = table.NewRow();
892 catch(Exception e2) {
893 Fail("DR48: Exception Caught: " + e2);
898 // -- object array smaller than number of columns -----
904 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
905 // table.Rows[3].ItemArray.ItemArray[1] = 14
907 row = table.NewRow();
912 catch(Exception e3) {
913 Fail("DR33: Exception Caught: " + e3);
918 // -- normal -----------------
919 ValueTest("DR34: normal value test", table, 0, 0, zero);
920 ValueTest("DR35: normal value test", table, 0, 1, 8);
922 // -- null 1----------
923 ValueTest("DR36: null value test", table, 1, 0, zero);
924 ValueTest("DR37: null value test", table, 1, 1, 9);
926 // -- null 2----------
927 ValueTest("DR38: null value test", table, 2, 0, zero);
928 ValueTest("DR39: null value test", table, 2, 1, 10);
930 // -- null 3----------
931 ValueTest("DR40: null value test", table, 3, 0, zero);
932 ValueTest("DR41: null value test", table, 3, 1, 11);
934 // -- DBNull.Value -------------
935 ValueTest("DR42: DBNull.Value value test", table, 4, 0, zero);
936 ValueTest("DR43: DBNull.Value value test", table, 4, 1, DBNull.Value);
938 // -- null 4----------
939 ValueTest("DR44: null value test", table, 5, 0, zero);
940 ValueTest("DR45: null value test", table, 5, 1, 13);
942 // -- object array smaller than number of columns -----
943 ValueTest("DR46: array smaller value test", table, 6, 0, abc);
944 ValueTest("DR47: array smaller value test", table, 6, 1, 14);
948 public void AutoIncrementColumnIntegrity ()
950 // AutoIncrement-column shouldn't raise index out of range
951 // exception because of size mismatch of internal itemarray.
952 DataTable dt = new DataTable ();
953 dt.Columns.Add ("foo");
954 dt.Rows.Add (new object [] {"value"});
955 DataColumn col = new DataColumn ("bar");
956 col.AutoIncrement = true;
957 dt.Columns.Add (col);
958 dt.Rows [0] [0] = "test";
962 public void EnforceConstraint ()
966 DataSet ds = new DataSet();
967 DataTable parent = ds.Tables.Add("parent");
968 parent.Columns.Add("id", typeof(int));
969 DataTable child = ds.Tables.Add("child");
970 child.Columns.Add("idref", typeof(int));
971 Constraint uniqueId = null;
972 parent.Constraints.Add(uniqueId = new UniqueConstraint("uniqueId",
973 new DataColumn[] {parent.Columns["id"]}, true));
974 ForeignKeyConstraint fkc = new ForeignKeyConstraint("ParentChildConstraint", new DataColumn[] { parent.Columns["id"] },
975 new DataColumn[] { child.Columns["idref"]});
977 child.Constraints.Add(fkc);
979 DataRelation relateParentChild = new DataRelation("relateParentChild",
980 new DataColumn[] {parent.Columns["id"] },
981 new DataColumn[] {child.Columns["idref"] },
983 ds.Relations.Add(relateParentChild);
985 ds.EnforceConstraints = false;
986 DataRow parentRow = parent.Rows.Add(new object[] { id });
987 DataRow childRow = child.Rows.Add(new object[] { id });
988 if (parentRow == childRow.GetParentRow(relateParentChild)) {
989 foreach(DataColumn dc in parent.Columns)
990 AssertEquals(100,parentRow[dc]);
998 [ExpectedException (typeof (RowNotInTableException))]
999 public void DetachedRowItemException ()
1001 DataTable dt = new DataTable ("table");
1002 dt.Columns.Add ("col");
1003 dt.Rows.Add ((new object [] {"val"}));
1005 DataRow dr = dt.NewRow ();
1006 AssertEquals (DataRowState.Detached, dr.RowState);
1008 AssertEquals (DataRowState.Detached, dr.RowState);
1009 object o = dr ["col"];