1 // DataRowCollectionTest.cs - NUnit Test Cases for System.DataRowCollection
4 // Franklin Wise (gracenote@earthlink.net)
5 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 // (C) Copyright 2002 Franklin Wise
8 // (C) Copyright 2003 Martin Willemoes Hansen
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using NUnit.Framework;
39 namespace MonoTests.System.Data
42 public class DataRowCollectionTest : Assertion
44 private DataTable _tbl;
47 public void GetReady()
49 _tbl = new DataTable();
53 public void AutoIncrement()
55 DataColumn col = new DataColumn("Auto");
56 col.AutoIncrement = true;
57 col.AutoIncrementSeed = 0;
58 col.AutoIncrementStep = 1;
60 _tbl.Columns.Add(col);
61 _tbl.Rows.Add(_tbl.NewRow());
63 AssertEquals("test#01" , 0, Convert.ToInt32(_tbl.Rows[0]["Auto"] ));
65 _tbl.Rows.Add(_tbl.NewRow());
66 AssertEquals("test#02" , 1, Convert.ToInt32(_tbl.Rows[1]["Auto"] ));
68 col.AutoIncrement = false;
69 AssertEquals("test#03" , 1, Convert.ToInt32(_tbl.Rows[1]["Auto"] ));
71 _tbl.Rows.Add(_tbl.NewRow());
72 AssertEquals("test#04" , DBNull.Value, _tbl.Rows[2]["Auto"]);
74 col.AutoIncrement = true;
75 col.AutoIncrementSeed = 10;
76 col.AutoIncrementStep = 2;
78 _tbl.Rows.Add(_tbl.NewRow());
79 AssertEquals("test#05" , 10, Convert.ToInt32(_tbl.Rows[3]["Auto"] ));
80 _tbl.Rows.Add(_tbl.NewRow());
81 AssertEquals("test#06" , 12, Convert.ToInt32(_tbl.Rows[4]["Auto"] ));
83 col = new DataColumn ("Auto2");
84 col.DataType = typeof(string);
85 col.AutoIncrement = true;
86 col.AutoIncrementSeed = 0;
87 col.AutoIncrementStep = 1;
88 _tbl.Columns.Add (col);
90 _tbl.Rows.Add(_tbl.NewRow());
91 AssertEquals ("test#07", typeof (int), _tbl.Columns [1].DataType);
92 AssertEquals ("test#08" , typeof (int), _tbl.Rows[5]["Auto2"].GetType ());
94 col = new DataColumn ("Auto3");
95 col.AutoIncrement = true;
96 col.AutoIncrementSeed = 0;
97 col.AutoIncrementStep = 1;
98 col.DataType = typeof(string);
99 AssertEquals ("test#09", typeof (string), col.DataType);
100 AssertEquals ("test#10", false, col.AutoIncrement);
108 DataRow Row = _tbl.NewRow ();
109 DataRowCollection Rows = _tbl.Rows;
112 AssertEquals ("test#01", 1, Rows.Count);
113 AssertEquals ("test#02", false, Rows.IsReadOnly);
114 AssertEquals ("test#03", false, Rows.IsSynchronized);
116 AssertEquals ("test#04", "System.Data.DataRowCollection", Rows.ToString ());
119 string [] cols = new string [2];
124 cols [0] = "something";
128 AssertEquals ("test#05", 3, Rows.Count);
130 AssertEquals ("test#06", "System.Data.DataRow", Rows [0].ToString ());
132 AssertEquals ("test#07", DBNull.Value, Rows [0] [0]);
133 AssertEquals ("test#08", DBNull.Value, Rows [0] [1]);
134 AssertEquals ("test#09", "first", Rows [1] [0]);
135 AssertEquals ("test#10", "something", Rows [2] [0]);
136 AssertEquals ("test#11", "second", Rows [1] [1]);
137 AssertEquals ("test#12", "else", Rows [2] [1]);
142 } catch (Exception e) {
143 AssertEquals ("test#14", typeof (ArgumentException), e.GetType ());
144 AssertEquals ("test#15", "This row already belongs to this table.", e.Message);
151 } catch (Exception e) {
152 AssertEquals ("test#17", typeof (ArgumentNullException), e.GetType ());
153 //AssertEquals ("test#18", "'row' argument cannot be null.\r\nParameter name: row", e.Message);
156 DataColumn Column = new DataColumn ("not_null");
157 Column.AllowDBNull = false;
158 _tbl.Columns.Add (Column);
160 cols = new string [3];
168 } catch (Exception e) {
169 AssertEquals ("test#20", typeof (NoNullAllowedException), e.GetType ());
170 //AssertEquals ("test#21", "Column 'not_null' does not allow nulls.", e.Message);
173 Column = _tbl.Columns [0];
174 Column.Unique = true;
176 cols = new string [3];
184 } catch (Exception e) {
185 AssertEquals ("test#23", typeof (ConstraintException), e.GetType ());
186 AssertEquals ("test#24", "Column 'Column1' is constrained to be unique. Value 'first' is already present.", e.Message);
189 Column = new DataColumn ("integer");
190 Column.DataType = typeof (short);
191 _tbl.Columns.Add (Column);
193 object [] obs = new object [4];
202 } catch (ArgumentException e) {
203 // LAMESPEC: MSDN says this exception is InvalidCastException
204 // AssertEquals ("test#26", typeof (ArgumentException), e.GetType ());
207 object [] obs1 = new object [5];
216 } catch (Exception e) {
217 AssertEquals ("test#28", typeof(ArgumentException), e.GetType ());
222 public void Add_ByValuesNullTest ()
224 DataTable t = new DataTable ("test");
225 t.Columns.Add ("id", typeof (int));
226 t.Columns.Add ("name", typeof (string));
227 t.Columns.Add ("nullable", typeof (string));
229 t.Columns [0].AutoIncrement = true;
230 t.Columns [0].AutoIncrementSeed = 10;
231 t.Columns [0].AutoIncrementStep = 5;
233 t.Columns [1].DefaultValue = "testme";
236 // null test & missing columns
237 DataRow r = t.Rows.Add (new object [] { null, null});
238 AssertEquals ("#ABV1", 10, (int) r [0]);
239 AssertEquals ("#ABV2", "testme", (string) r [1]);
240 AssertEquals ("#ABV3", DBNull.Value, r [2]);
243 r = t.Rows.Add (new object [] { DBNull.Value, DBNull.Value, DBNull.Value});
244 AssertEquals ("#ABV4", DBNull.Value, r [0]);
245 AssertEquals ("#ABV5", DBNull.Value, r [1]);
246 AssertEquals ("#ABV6", DBNull.Value, r [2]);
248 // ai test & no default value test
249 r = t.Rows.Add (new object [] { null, null, null});
250 AssertEquals ("#ABV7", 15, (int) r [0]);
251 AssertEquals ("#ABV8", "testme", (string) r [1]);
252 AssertEquals ("#ABV9", DBNull.Value, r [2]);
258 DataRowCollection Rows = _tbl.Rows;
259 DataTable Table = new DataTable ("child");
260 Table.Columns.Add ("first", typeof (int));
261 Table.Columns.Add ("second", typeof (string));
263 _tbl.Columns.Add ("first", typeof (int));
264 _tbl.Columns.Add ("second", typeof (float));
266 string [] cols = new string [2];
279 AssertEquals ("test#01", 3, Rows.Count);
282 // hmm... TODO: better tests
283 AssertEquals ("test#02", 0, Rows.Count);
299 Table.Rows.Add (cols);
303 Table.Rows.Add (cols);
307 Table.Rows.Add (cols);
309 DataRelation Rel = new DataRelation ("REL", _tbl.Columns [0], Table.Columns [0]);
310 DataSet Set = new DataSet ();
311 Set.Tables.Add (_tbl);
312 Set.Tables.Add (Table);
313 Set.Relations.Add (Rel);
318 } catch (InvalidConstraintException) {
321 AssertEquals ("test#06", 3, Table.Rows.Count);
323 AssertEquals ("test#07", 0, Table.Rows.Count);
327 public void Contains ()
329 DataColumn C = new DataColumn ("key");
331 C.DataType = typeof (int);
332 C.AutoIncrement = true;
333 C.AutoIncrementSeed = 0;
334 C.AutoIncrementStep = 1;
335 _tbl.Columns.Add (C);
336 _tbl.Columns.Add ("first", typeof (string));
337 _tbl.Columns.Add ("second", typeof (decimal));
339 DataRowCollection Rows = _tbl.Rows;
341 DataRow Row = _tbl.NewRow ();
343 Row = _tbl.NewRow ();
345 Row = _tbl.NewRow ();
347 Row = _tbl.NewRow ();
350 Rows [0] [1] = "test0";
352 Rows [1] [1] = "test1";
354 Rows [2] [1] = "test2";
356 Rows [3] [1] = "test3";
359 AssertEquals ("test#01", 3, _tbl.Columns.Count);
360 AssertEquals ("test#02", 4, _tbl.Rows.Count);
361 AssertEquals ("test#03", 0, _tbl.Rows [0] [0]);
362 AssertEquals ("test#04", 1, _tbl.Rows [1] [0]);
363 AssertEquals ("test#05", 2, _tbl.Rows [2] [0]);
364 AssertEquals ("test#06", 3, _tbl.Rows [3] [0]);
369 } catch (Exception e) {
370 AssertEquals ("test#08", typeof (MissingPrimaryKeyException), e.GetType ());
371 AssertEquals ("test#09", "Table doesn't have a primary key.", e.Message);
374 _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0]};
375 AssertEquals ("test#10", true, Rows.Contains (1));
376 AssertEquals ("test#11", true, Rows.Contains (2));
377 AssertEquals ("test#12", false, Rows.Contains (4));
380 Rows.Contains (new object [] {64, "test0"});
382 } catch (Exception e) {
383 AssertEquals ("test#14", typeof (ArgumentException), e.GetType ());
384 AssertEquals ("test#15", "Expecting 1 value(s) for the key being indexed, but received 2 value(s).", e.Message);
387 _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0], _tbl.Columns [1]};
388 AssertEquals ("test#16", false, Rows.Contains (new object [] {64, "test0"}));
389 AssertEquals ("test#17", false, Rows.Contains (new object [] {0, "test1"}));
390 AssertEquals ("test#18", true, Rows.Contains (new object [] {1, "test1"}));
391 AssertEquals ("test#19", true, Rows.Contains (new object [] {2, "test2"}));
394 Rows.Contains (new object [] {2});
396 } catch (Exception e) {
397 AssertEquals ("test#21", typeof (ArgumentException), e.GetType ());
398 AssertEquals ("test#22", "Expecting 2 value(s) for the key being indexed, but received 1 value(s).", e.Message);
403 public void CopyTo ()
409 DataRowCollection Rows = _tbl.Rows;
411 Rows.Add (new object [] {"1", "1", "1"});
412 Rows.Add (new object [] {"2", "2", "2"});
413 Rows.Add (new object [] {"3", "3", "3"});
414 Rows.Add (new object [] {"4", "4", "4"});
415 Rows.Add (new object [] {"5", "5", "5"});
416 Rows.Add (new object [] {"6", "6", "6"});
417 Rows.Add (new object [] {"7", "7", "7"});
419 DataRow [] dr = new DataRow [10];
424 } catch (Exception e) {
425 AssertEquals ("test#02", typeof (ArgumentException), e.GetType ());
426 //AssertEquals ("test#03", "Destination array was not long enough. Check destIndex and length, and the array's lower bounds.", e.Message);
429 dr = new DataRow [11];
432 AssertEquals ("test#04", null, dr [0]);
433 AssertEquals ("test#05", null, dr [1]);
434 AssertEquals ("test#06", null, dr [2]);
435 AssertEquals ("test#07", null, dr [3]);
436 AssertEquals ("test#08", "1", dr [4] [0]);
437 AssertEquals ("test#09", "2", dr [5] [0]);
438 AssertEquals ("test#10", "3", dr [6] [0]);
439 AssertEquals ("test#11", "4", dr [7] [0]);
440 AssertEquals ("test#12", "5", dr [8] [0]);
441 AssertEquals ("test#13", "6", dr [9] [0]);
445 public void Equals ()
451 DataRowCollection Rows1 = _tbl.Rows;
453 Rows1.Add (new object [] {"1", "1", "1"});
454 Rows1.Add (new object [] {"2", "2", "2"});
455 Rows1.Add (new object [] {"3", "3", "3"});
456 Rows1.Add (new object [] {"4", "4", "4"});
457 Rows1.Add (new object [] {"5", "5", "5"});
458 Rows1.Add (new object [] {"6", "6", "6"});
459 Rows1.Add (new object [] {"7", "7", "7"});
461 DataRowCollection Rows2 = _tbl.Rows;
463 AssertEquals ("test#01", true, Rows2.Equals (Rows1));
464 AssertEquals ("test#02", true, Rows1.Equals (Rows2));
465 AssertEquals ("test#03", true, Rows1.Equals (Rows1));
467 DataTable Table = new DataTable ();
468 Table.Columns.Add ();
469 Table.Columns.Add ();
470 Table.Columns.Add ();
471 DataRowCollection Rows3 = Table.Rows;
473 Rows3.Add (new object [] {"1", "1", "1"});
474 Rows3.Add (new object [] {"2", "2", "2"});
475 Rows3.Add (new object [] {"3", "3", "3"});
476 Rows3.Add (new object [] {"4", "4", "4"});
477 Rows3.Add (new object [] {"5", "5", "5"});
478 Rows3.Add (new object [] {"6", "6", "6"});
479 Rows3.Add (new object [] {"7", "7", "7"});
481 AssertEquals ("test#04", false, Rows3.Equals (Rows1));
482 AssertEquals ("test#05", false, Rows3.Equals (Rows2));
483 AssertEquals ("test#06", false, Rows1.Equals (Rows3));
484 AssertEquals ("test#07", false, Rows2.Equals (Rows3));
490 DataColumn Col = new DataColumn ("test_1");
491 Col.AllowDBNull = false;
493 Col.DataType = typeof (long);
494 _tbl.Columns.Add (Col);
496 Col = new DataColumn ("test_2");
497 Col.DataType = typeof (string);
498 _tbl.Columns.Add (Col);
500 DataRowCollection Rows = _tbl.Rows;
502 Rows.Add (new object [] {1, "first"});
503 Rows.Add (new object [] {2, "second"});
504 Rows.Add (new object [] {3, "third"});
505 Rows.Add (new object [] {4, "fourth"});
506 Rows.Add (new object [] {5, "fifth"});
511 } catch (Exception e) {
512 AssertEquals ("test#02", typeof (MissingPrimaryKeyException), e.GetType ());
513 AssertEquals ("test#03", "Table doesn't have a primary key.", e.Message);
516 _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0]};
517 DataRow row = Rows.Find (1);
518 AssertEquals ("test#04", 1L, row [0]);
520 AssertEquals ("test#05", 2L, row [0]);
521 row = Rows.Find ("2");
522 AssertEquals ("test#06", 2L, row [0]);
525 row = Rows.Find ("test");
527 } catch (Exception e) {
528 AssertEquals ("test#08", typeof (FormatException), e.GetType ());
529 //AssertEquals ("test#09", "Input string was not in a correct format.", e.Message);
533 row = Rows.Find (tes);
534 AssertEquals ("test#10", null, row);
535 _tbl.PrimaryKey = null;
538 Rows.Find (new object [] {1, "fir"});
540 } catch (Exception e) {
541 AssertEquals ("test#12", typeof (MissingPrimaryKeyException), e.GetType ());
542 AssertEquals ("tets#13", "Table doesn't have a primary key.", e.Message);
545 _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0], _tbl.Columns [1]};
550 } catch (Exception e) {
551 AssertEquals ("test#15", typeof (ArgumentException), e.GetType ());
552 AssertEquals ("test#16", "Expecting 2 value(s) for the key being indexed, but received 1 value(s).", e.Message);
555 row = Rows.Find (new object [] {1, "fir"});
556 AssertEquals ("test#16", null, row);
557 row = Rows.Find (new object [] {1, "first"});
558 AssertEquals ("test#17", 1L, row [0]);
564 DataSet ds = new DataSet ();
\r
565 ds.EnforceConstraints = false;
\r
567 DataTable dt = new DataTable ();
\r
568 ds.Tables.Add (dt);
\r
570 DataColumn dc = new DataColumn ("Column A");
\r
571 dt.Columns.Add (dc);
\r
573 dt.PrimaryKey = new DataColumn [] {dc};
\r
575 DataRow dr = dt.NewRow ();
\r
587 DataRow row = (DataRow) ds.Tables [0].Rows.Find (new object [] {"a"});
593 public void InsertAt ()
598 DataRowCollection Rows = _tbl.Rows;
600 Rows.Add (new object [] {"a", "aa", "aaa"});
601 Rows.Add (new object [] {"b", "bb", "bbb"});
602 Rows.Add (new object [] {"c", "cc", "ccc"});
603 Rows.Add (new object [] {"d", "dd", "ddd"});
605 DataRow Row = _tbl.NewRow ();
611 Rows.InsertAt (Row, -1);
613 } catch (Exception e) {
614 AssertEquals ("test#02", typeof (IndexOutOfRangeException), e.GetType ());
615 AssertEquals ("test#03", "The row insert position -1 is invalid.", e.Message);
618 Rows.InsertAt (Row, 0);
619 AssertEquals ("test#04", "e", Rows [0][0]);
620 AssertEquals ("test#05", "a", Rows [1][0]);
622 Row = _tbl.NewRow ();
627 Rows.InsertAt (Row, 5);
628 AssertEquals ("test#06", "f", Rows [5][0]);
630 Row = _tbl.NewRow ();
635 Rows.InsertAt (Row, 500);
636 AssertEquals ("test#07", "g", Rows [6][0]);
639 Rows.InsertAt (Row, 6); //Row already belongs to the table
642 catch (Exception e) {
643 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
644 AssertEquals ("test#10", "This row already belongs to this table.", e.Message);
647 DataTable table = new DataTable ();
648 DataColumn col = new DataColumn ("Name");
649 table.Columns.Add (col);
650 Row = table.NewRow ();
651 Row ["Name"] = "Abc";
652 table.Rows.Add (Row);
654 Rows.InsertAt (Row, 6);
657 catch (Exception e) {
658 AssertEquals ("test#12", typeof (ArgumentException), e.GetType ());
659 AssertEquals ("test#13", "This row already belongs to another table.", e.Message);
662 table = new DataTable ();
663 col = new DataColumn ("Name");
664 col.DataType = typeof (string);
665 table.Columns.Add (col);
666 UniqueConstraint uk = new UniqueConstraint (col);
667 table.Constraints.Add (uk);
669 Row = table.NewRow ();
670 Row ["Name"] = "aaa";
671 table.Rows.InsertAt (Row, 0);
673 Row = table.NewRow ();
674 Row ["Name"] = "aaa";
676 table.Rows.InsertAt (Row, 1);
679 catch (Exception e) {
680 AssertEquals ("test#15", typeof (ConstraintException), e.GetType ());
683 table.Rows.InsertAt (null, 1);
685 catch (Exception e) {
686 AssertEquals ("test#16", typeof (ArgumentNullException), e.GetType ());
691 public void Remove ()
696 DataRowCollection Rows = _tbl.Rows;
698 Rows.Add (new object [] {"a", "aa", "aaa"});
699 Rows.Add (new object [] {"b", "bb", "bbb"});
700 Rows.Add (new object [] {"c", "cc", "ccc"});
701 Rows.Add (new object [] {"d", "dd", "ddd"});
703 AssertEquals ("test#01", 4, _tbl.Rows.Count);
705 Rows.Remove (_tbl.Rows [1]);
706 AssertEquals ("test#02", 3, _tbl.Rows.Count);
707 AssertEquals ("test#03", "a", _tbl.Rows [0] [0]);
708 AssertEquals ("test#04", "c", _tbl.Rows [1] [0]);
709 AssertEquals ("test#05", "d", _tbl.Rows [2] [0]);
714 } catch (Exception e) {
715 AssertEquals ("test#07", typeof (IndexOutOfRangeException), e.GetType ());
716 AssertEquals ("test#08", "The given datarow is not in the current DataRowCollection.", e.Message);
719 DataRow Row = new DataTable ().NewRow ();
724 } catch (Exception e) {
725 AssertEquals ("test#10", typeof (IndexOutOfRangeException), e.GetType ());
726 AssertEquals ("test#11", "The given datarow is not in the current DataRowCollection.", e.Message);
732 } catch (Exception e) {
733 AssertEquals ("test#13", typeof (IndexOutOfRangeException), e.GetType ());
734 AssertEquals ("test#14", "There is no row at position -1.", e.Message);
740 } catch (Exception e) {
741 AssertEquals ("test#16", typeof (IndexOutOfRangeException), e.GetType ());
742 AssertEquals ("test#17", "There is no row at position 64.", e.Message);
747 AssertEquals ("test#18", 1, Rows.Count);
748 AssertEquals ("test#19", "c", Rows [0] [0]);