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 RowEditTest()
132 private void DeleteRowTest ()
137 for (int i
= 1; i
<= 2; i
++) {
138 newRow
= table
.NewRow ();
139 newRow
["FName"] = "Name " + i
;
140 newRow
["LName"] = " Last Name" + i
;
141 table
.Rows
.Add (newRow
);
143 table
.AcceptChanges ();
146 for (int i
= 1; i
< table
.Rows
.Count
; i
++) {
147 DataRow r
= table
.Rows
[i
];
148 AssertEquals ("#A03", "Name " + cnt
, r
["fName"]);
153 // Create a DataView with the table.
154 DataRowCollection rc
= table
.Rows
;
159 AssertEquals ("#A04", "Deleted", rc
[0].RowState
.ToString ());
160 AssertEquals ("#A05", "Deleted", rc
[2].RowState
.ToString ());
164 table
.AcceptChanges ();
165 AssertEquals ("#A06", "Name 1", (table
.Rows
[0]) [1]);
167 Console
.WriteLine (rc
[2]);
170 catch (Exception e
) {
171 AssertEquals ("#A08", "There is no row at position 2.", e
.Message
);
175 private void EditModeTest ()
178 //Clear all existing values from table
179 for (int i
= 0; i
< table
.Rows
.Count
; i
++) {
180 table
.Rows
[i
].Delete ();
182 table
.AcceptChanges ();
183 row
= table
.NewRow ();
184 row
["FName"] = "My FName";
185 table
.Rows
.Add (row
);
189 //Initially: After Add (Row) But Before Accept Changes");
190 AssertEquals ("#A09", "My FName", row
[1, DataRowVersion
.Default
]);
191 AssertEquals ("#A10", "LName", row
[2, DataRowVersion
.Default
]);
193 AssertEquals ("#A11", "My FName", row
[1, DataRowVersion
.Current
]);
194 AssertEquals ("#A12", "LName", row
[2, DataRowVersion
.Current
]);
197 Console
.WriteLine (row
[1, DataRowVersion
.Original
]);
198 Console
.WriteLine (row
[1, DataRowVersion
.Proposed
]);
201 catch (Exception e
) {
202 if (e
.GetType () != typeof (AssertionException
)) {
203 AssertEquals ("#A14", typeof (VersionNotFoundException
), e
.GetType ());
208 //After Accept Changes
209 table
.AcceptChanges ();
210 AssertEquals ("#A15", "My FName", row
[1, DataRowVersion
.Default
]);
211 AssertEquals ("#A16", "LName", row
[2, DataRowVersion
.Default
]);
214 AssertEquals ("#A17", "My FName", row
[1, DataRowVersion
.Current
]);
215 AssertEquals ("#A18", "LName", row
[2, DataRowVersion
.Current
]);
218 Console
.WriteLine (row
[1, DataRowVersion
.Proposed
]);
221 catch (Exception e
) {
222 if (e
.GetType () != typeof (AssertionException
)) {
223 AssertEquals ("#A20", typeof (VersionNotFoundException
), e
.GetType ());
228 // Stage 3 // Edit Mode
229 table
.Rows
[0].BeginEdit ();
230 table
.Rows
[0] ["LName"] = "My LName";
232 AssertEquals ("#A21", "My FName", row
[1, DataRowVersion
.Default
]);
233 AssertEquals ("#A22", "My LName", row
[2, DataRowVersion
.Default
]);
235 AssertEquals ("#A23", "My FName", row
[1, DataRowVersion
.Current
]);
236 AssertEquals ("#A24", "LName", row
[2, DataRowVersion
.Current
]);
239 AssertEquals ("#A25", "My FName", row
[1, DataRowVersion
.Original
]); AssertEquals ("#A26", "LName", row
[2, DataRowVersion
.Original
]);
241 AssertEquals ("#A26", "My FName", row
[1, DataRowVersion
.Proposed
]);
242 AssertEquals ("#A27", "My LName", row
[2, DataRowVersion
.Proposed
]);
246 //After Edit sessions
247 for (int i
=0; i
< table
.Rows
.Count
;i
++)
248 table
.Rows
[i
].EndEdit ();
249 AssertEquals ("#A28", "My FName", row
[1, DataRowVersion
.Default
]);
250 AssertEquals ("#A29", "My LName", row
[2, DataRowVersion
.Default
]);
252 AssertEquals ("#A30", "My FName", row
[1, DataRowVersion
.Original
]); AssertEquals ("#A31", "LName", row
[2, DataRowVersion
.Original
]);
255 AssertEquals ("#A32", "My FName", row
[1, DataRowVersion
.Current
]);
256 AssertEquals ("#A33", "My LName", row
[2, DataRowVersion
.Current
]);
259 Console
.WriteLine (row
[1, DataRowVersion
.Proposed
]);
262 catch (Exception e
) {
263 if (e
.GetType ()!=typeof (AssertionException
)) {
264 AssertEquals ("#A35", typeof (VersionNotFoundException
), e
.GetType ());
269 //After Accept Changes
270 table
.AcceptChanges ();
271 AssertEquals ("#A36", "My FName", row
[1, DataRowVersion
.Default
]);
272 AssertEquals ("#A37", "My LName", row
[2, DataRowVersion
.Default
]);
275 AssertEquals ("#A38", "My FName", row
[1, DataRowVersion
.Original
]); AssertEquals ("#A39", "My LName", row
[2, DataRowVersion
.Original
]);
277 AssertEquals ("#A40", "My FName", row
[1, DataRowVersion
.Current
]);
278 AssertEquals ("#A41", "My LName", row
[2, DataRowVersion
.Current
]);
282 Console
.WriteLine (row
[1, DataRowVersion
.Proposed
]);
285 catch (Exception e
) {
286 if (e
.GetType () != typeof (AssertionException
)) {
287 AssertEquals ("#A43", typeof (VersionNotFoundException
),
295 Console
.WriteLine (e
+ "" + e
.StackTrace
);
298 private void ParentRowTest (){
300 //Clear all existing values from table
301 for (int i
= 0; i
< table
.Rows
.Count
; i
++) {
302 table
.Rows
[i
].Delete ();
304 table
.AcceptChanges ();
305 row
= table
.NewRow ();
306 row
["FName"] = "My FName";
308 table
.Rows
.Add (row
);
310 DataTable tableC
= new DataTable ("Child");
314 colC
= new DataColumn ();
315 colC
.DataType
= Type
.GetType ("System.Int32");
316 colC
.ColumnName
= "Id";
317 colC
.AutoIncrement
=true;
318 tableC
.Columns
.Add (colC
);
321 colC
= new DataColumn ();
322 colC
.DataType
= Type
.GetType ("System.String");
323 colC
.ColumnName
= "Name";
324 tableC
.Columns
.Add (colC
);
326 rowC
= tableC
.NewRow ();
327 rowC
["Name"] = "My FName";
328 tableC
.Rows
.Add (rowC
);
329 DataSet ds
= new DataSet ();
330 ds
.Tables
.Add (table
);
331 ds
.Tables
.Add (tableC
);
332 DataRelation dr
= new DataRelation ("PO", table
.Columns
["Id"], tableC
.Columns
["Id"]);
333 ds
.Relations
.Add (dr
);
335 rowC
.SetParentRow (table
.Rows
[0], dr
);
337 AssertEquals ("#A44", table
.Rows
[0], (tableC
.Rows
[0]).GetParentRow (dr
));
342 // tests item at row, column in table to be DBNull.Value
343 private void DBNullTest (string message
, DataTable dt
, int row
, int column
)
345 object val
= dt
.Rows
[row
].ItemArray
[column
];
346 AssertEquals(message
, DBNull
.Value
, val
);
349 // tests item at row, column in table to be null
350 private void NullTest (string message
, DataTable dt
, int row
, int column
)
352 object val
= dt
.Rows
[row
].ItemArray
[column
];
353 AssertEquals(message
, null, val
);
356 // tests item at row, column in table to be
357 private void ValueTest (string message
, DataTable dt
, int row
, int column
, object value)
359 object val
= dt
.Rows
[row
].ItemArray
[column
];
360 AssertEquals(message
, value, val
);
363 // test set null, DBNull.Value, and ItemArray short count
365 public void NullInItemArray ()
367 string zero
= "zero";
371 DataTable table
= new DataTable();
372 table
.Columns
.Add(new DataColumn(zero
, typeof(string)));
373 table
.Columns
.Add(new DataColumn(one
, typeof(string)));
374 table
.Columns
.Add(new DataColumn(two
, typeof(string)));
376 object[] obj
= new object[3];
377 // -- normal -----------------
382 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
383 // table.Rows[0].ItemArray.ItemArray[1] = "one"
384 // table.Rows[0].ItemArray.ItemArray[2] = "two"
386 DataRow row
= table
.NewRow();
391 catch(Exception e1
) {
392 Fail("DR1: Exception Caught: " + e1
);
397 // -- null ----------
400 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
401 // table.Rows[1].ItemArray.ItemArray[1] = DBNull.Value
402 // table.Rows[1].ItemArray.ItemArray[2] = "two"
404 row
= table
.NewRow();
409 catch(Exception e2
) {
410 Fail("DR2: Exception Caught: " + e2
);
415 // -- DBNull.Value -------------
416 obj
[1] = DBNull
.Value
;
418 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
419 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
420 // table.Rows[2].ItemArray.ItemArray[2] = "two"
422 row
= table
.NewRow();
427 catch(Exception e3
) {
428 Fail("DR3: Exception Caught: " + e3
);
433 // -- object array smaller than number of columns -----
440 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
441 // table.Rows[3].ItemArray.ItemArray[1] = "def"
442 // table.Rows[3].ItemArray.ItemArray[2] = DBNull.Value;
444 row
= table
.NewRow();
449 catch(Exception e3
) {
450 Fail("DR4: Exception Caught: " + e3
);
455 // -- normal -----------------
456 ValueTest("DR5: normal value test", table
, 0, 0, zero
);
457 ValueTest("DR6: normal value test", table
, 0, 1, one
);
458 ValueTest("DR7: normal value test", table
, 0, 2, two
);
460 // -- null ----------
461 ValueTest("DR8: null value test", table
, 1, 0, zero
);
462 ValueTest("DR9: null value test", table
, 1, 1, DBNull
.Value
);
463 ValueTest("DR10: null value test", table
, 1, 2, two
);
465 // -- DBNull.Value -------------
466 ValueTest("DR11: DBNull.Value value test", table
, 2, 0, zero
);
467 ValueTest("DR12: DBNull.Value value test", table
, 2, 1, DBNull
.Value
);
468 ValueTest("DR13: DBNull.Value value test", table
, 2, 2, two
);
470 // -- object array smaller than number of columns -----
471 ValueTest("DR14: array smaller value test", table
, 3, 0, abc
);
472 ValueTest("DR15: array smaller value test", table
, 3, 1, def
);
473 ValueTest("DR16: array smaller value test", table
, 3, 2, DBNull
.Value
);
476 // test DefaultValue when setting ItemArray
478 public void DefaultValueInItemArray () {
479 string zero
= "zero";
481 DataTable table
= new DataTable();
482 table
.Columns
.Add(new DataColumn("zero", typeof(string)));
484 DataColumn column
= new DataColumn("num", typeof(int));
485 column
.DefaultValue
= 15;
486 table
.Columns
.Add(column
);
488 object[] obj
= new object[2];
489 // -- normal -----------------
493 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
494 // table.Rows[0].ItemArray.ItemArray[1] = 8
496 DataRow row
= table
.NewRow();
501 catch(Exception e1
) {
502 Fail("DR17: Exception Caught: " + e1
);
507 // -- null ----------
510 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
511 // table.Rows[1].ItemArray.ItemArray[1] = 15
513 row
= table
.NewRow();
518 catch(Exception e2
) {
519 Fail("DR18: Exception Caught: " + e2
);
524 // -- DBNull.Value -------------
525 obj
[1] = DBNull
.Value
;
527 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
528 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
529 // even though internally, the v
531 row
= table
.NewRow();
536 catch(Exception e3
) {
537 Fail("DR19: Exception Caught: " + e3
);
542 // -- object array smaller than number of columns -----
548 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
549 // table.Rows[3].ItemArray.ItemArray[1] = DBNull.Value
551 row
= table
.NewRow();
556 catch(Exception e3
) {
557 Fail("DR20: Exception Caught: " + e3
);
562 // -- normal -----------------
563 ValueTest("DR20: normal value test", table
, 0, 0, zero
);
564 ValueTest("DR21: normal value test", table
, 0, 1, 8);
566 // -- null ----------
567 ValueTest("DR22: null value test", table
, 1, 0, zero
);
568 ValueTest("DR23: null value test", table
, 1, 1, 15);
570 // -- DBNull.Value -------------
571 ValueTest("DR24: DBNull.Value value test", table
, 2, 0, zero
);
572 DBNullTest("DR25: DBNull.Value value test", table
, 2, 1);
574 // -- object array smaller than number of columns -----
575 ValueTest("DR26: array smaller value test", table
, 3, 0, abc
);
576 ValueTest("DR27: array smaller value test", table
, 3, 1, 15);
579 // test AutoIncrement when setting ItemArray
581 public void AutoIncrementInItemArray () {
582 string zero
= "zero";
585 DataTable table
= new DataTable();
586 table
.Columns
.Add(new DataColumn(zero
, typeof(string)));
588 DataColumn column
= new DataColumn("num", typeof(int));
589 column
.AutoIncrement
= true;
590 table
.Columns
.Add(column
);
592 object[] obj
= new object[2];
593 // -- normal -----------------
597 // table.Rows[0].ItemArray.ItemArray[0] = "zero"
598 // table.Rows[0].ItemArray.ItemArray[1] = 8
600 DataRow row
= table
.NewRow();
605 catch(Exception e1
) {
606 Fail("DR28: Exception Caught: " + e1
);
611 // -- null 1----------
614 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
615 // table.Rows[1].ItemArray.ItemArray[1] = 9
617 row
= table
.NewRow();
622 catch(Exception e2
) {
623 Fail("DR29: Exception Caught: " + e2
);
628 // -- null 2----------
631 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
632 // table.Rows[1].ItemArray.ItemArray[1] = 10
634 row
= table
.NewRow();
639 catch(Exception e2
) {
640 Fail("DR30: Exception Caught: " + e2
);
645 // -- null 3----------
648 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
649 // table.Rows[1].ItemArray.ItemArray[1] = 11
651 row
= table
.NewRow();
656 catch(Exception e2
) {
657 Fail("DR31: Exception Caught: " + e2
);
662 // -- DBNull.Value -------------
663 obj
[1] = DBNull
.Value
;
665 // table.Rows[2].ItemArray.ItemArray[0] = "zero"
666 // table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
667 // even though internally, the AutoIncrement value
670 row
= table
.NewRow();
675 catch(Exception e3
) {
676 Fail("DR32: Exception Caught: " + e3
);
681 // -- null 4----------
684 // table.Rows[1].ItemArray.ItemArray[0] = "zero"
685 // table.Rows[1].ItemArray.ItemArray[1] = 13
687 row
= table
.NewRow();
692 catch(Exception e2
) {
693 Fail("DR48: Exception Caught: " + e2
);
698 // -- object array smaller than number of columns -----
704 // table.Rows[3].ItemArray.ItemArray[0] = "abc"
705 // table.Rows[3].ItemArray.ItemArray[1] = 14
707 row
= table
.NewRow();
712 catch(Exception e3
) {
713 Fail("DR33: Exception Caught: " + e3
);
718 // -- normal -----------------
719 ValueTest("DR34: normal value test", table
, 0, 0, zero
);
720 ValueTest("DR35: normal value test", table
, 0, 1, 8);
722 // -- null 1----------
723 ValueTest("DR36: null value test", table
, 1, 0, zero
);
724 ValueTest("DR37: null value test", table
, 1, 1, 9);
726 // -- null 2----------
727 ValueTest("DR38: null value test", table
, 2, 0, zero
);
728 ValueTest("DR39: null value test", table
, 2, 1, 10);
730 // -- null 3----------
731 ValueTest("DR40: null value test", table
, 3, 0, zero
);
732 ValueTest("DR41: null value test", table
, 3, 1, 11);
734 // -- DBNull.Value -------------
735 ValueTest("DR42: DBNull.Value value test", table
, 4, 0, zero
);
736 ValueTest("DR43: DBNull.Value value test", table
, 4, 1, DBNull
.Value
);
738 // -- null 4----------
739 ValueTest("DR44: null value test", table
, 5, 0, zero
);
740 ValueTest("DR45: null value test", table
, 5, 1, 13);
742 // -- object array smaller than number of columns -----
743 ValueTest("DR46: array smaller value test", table
, 6, 0, abc
);
744 ValueTest("DR47: array smaller value test", table
, 6, 1, 14);
748 public void AutoIncrementColumnIntegrity ()
750 // AutoIncrement-column shouldn't raise index out of range
751 // exception because of size mismatch of internal itemarray.
752 DataTable dt
= new DataTable ();
753 dt
.Columns
.Add ("foo");
754 dt
.Rows
.Add (new object [] {"value"}
);
755 DataColumn col
= new DataColumn ("bar");
756 col
.AutoIncrement
= true;
757 dt
.Columns
.Add (col
);
758 dt
.Rows
[0] [0] = "test";
762 public void EnforceConstraint ()
766 DataSet ds
= new DataSet();
767 DataTable parent
= ds
.Tables
.Add("parent");
768 parent
.Columns
.Add("id", typeof(int));
769 DataTable child
= ds
.Tables
.Add("child");
770 child
.Columns
.Add("idref", typeof(int));
771 Constraint uniqueId
= null;
772 parent
.Constraints
.Add(uniqueId
= new UniqueConstraint("uniqueId",
773 new DataColumn
[] {parent.Columns["id"]}
, true));
774 ForeignKeyConstraint fkc
= new ForeignKeyConstraint("ParentChildConstraint", new DataColumn
[] { parent.Columns["id"] }
,
775 new DataColumn
[] { child.Columns["idref"]}
);
777 child
.Constraints
.Add(fkc
);
779 DataRelation relateParentChild
= new DataRelation("relateParentChild",
780 new DataColumn
[] {parent.Columns["id"] }
,
781 new DataColumn
[] {child.Columns["idref"] }
,
783 ds
.Relations
.Add(relateParentChild
);
785 ds
.EnforceConstraints
= false;
786 DataRow parentRow
= parent
.Rows
.Add(new object[] { id }
);
787 DataRow childRow
= child
.Rows
.Add(new object[] { id }
);
788 if (parentRow
== childRow
.GetParentRow(relateParentChild
)) {
789 foreach(DataColumn dc
in parent
.Columns
)
790 AssertEquals(100,parentRow
[dc
]);