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 using NUnit.Framework;
16 namespace MonoTests.System.Data
19 public class DataRowCollectionTest : Assertion
21 private DataTable _tbl;
24 public void GetReady()
26 _tbl = new DataTable();
30 public void AutoIncrement()
32 DataColumn col = new DataColumn("Auto");
33 col.AutoIncrement = true;
34 col.AutoIncrementSeed = 0;
35 col.AutoIncrementStep = 1;
37 _tbl.Columns.Add(col);
38 _tbl.Rows.Add(_tbl.NewRow());
40 AssertEquals("test#01" , 0, Convert.ToInt32(_tbl.Rows[0]["Auto"] ));
42 _tbl.Rows.Add(_tbl.NewRow());
43 AssertEquals("test#02" , 1, Convert.ToInt32(_tbl.Rows[1]["Auto"] ));
45 col.AutoIncrement = false;
46 AssertEquals("test#03" , 1, Convert.ToInt32(_tbl.Rows[1]["Auto"] ));
48 _tbl.Rows.Add(_tbl.NewRow());
49 AssertEquals("test#04" , DBNull.Value, _tbl.Rows[2]["Auto"]);
51 col.AutoIncrement = true;
52 col.AutoIncrementSeed = 10;
53 col.AutoIncrementStep = 2;
55 _tbl.Rows.Add(_tbl.NewRow());
56 AssertEquals("test#05" , 10, Convert.ToInt32(_tbl.Rows[3]["Auto"] ));
57 _tbl.Rows.Add(_tbl.NewRow());
58 AssertEquals("test#06" , 12, Convert.ToInt32(_tbl.Rows[4]["Auto"] ));
60 col = new DataColumn ("Auto2");
61 col.DataType = typeof(string);
62 col.AutoIncrement = true;
63 col.AutoIncrementSeed = 0;
64 col.AutoIncrementStep = 1;
65 _tbl.Columns.Add (col);
67 _tbl.Rows.Add(_tbl.NewRow());
68 AssertEquals ("test#07", typeof (int), _tbl.Columns [1].DataType);
69 AssertEquals ("test#08" , typeof (int), _tbl.Rows[5]["Auto2"].GetType ());
71 col = new DataColumn ("Auto3");
72 col.AutoIncrement = true;
73 col.AutoIncrementSeed = 0;
74 col.AutoIncrementStep = 1;
75 col.DataType = typeof(string);
76 AssertEquals ("test#09", typeof (string), col.DataType);
77 AssertEquals ("test#10", false, col.AutoIncrement);
85 DataRow Row = _tbl.NewRow ();
86 DataRowCollection Rows = _tbl.Rows;
89 AssertEquals ("test#01", 1, Rows.Count);
90 AssertEquals ("test#02", false, Rows.IsReadOnly);
91 AssertEquals ("test#03", false, Rows.IsSynchronized);
92 AssertEquals ("test#04", "System.Data.DataRowCollection", Rows.ToString ());
94 string [] cols = new string [2];
99 cols [0] = "something";
103 AssertEquals ("test#05", 3, Rows.Count);
104 AssertEquals ("test#06", "System.Data.DataRow", Rows [0].ToString ());
105 AssertEquals ("test#07", DBNull.Value, Rows [0] [0]);
106 AssertEquals ("test#08", DBNull.Value, Rows [0] [1]);
107 AssertEquals ("test#09", "first", Rows [1] [0]);
108 AssertEquals ("test#10", "something", Rows [2] [0]);
109 AssertEquals ("test#11", "second", Rows [1] [1]);
110 AssertEquals ("test#12", "else", Rows [2] [1]);
115 } catch (Exception e) {
116 AssertEquals ("test#14", typeof (ArgumentException), e.GetType ());
117 AssertEquals ("test#15", "This row already belongs to this table.", e.Message);
124 } catch (Exception e) {
125 AssertEquals ("test#17", typeof (ArgumentNullException), e.GetType ());
126 //AssertEquals ("test#18", "'row' argument cannot be null.\r\nParameter name: row", e.Message);
129 DataColumn Column = new DataColumn ("not_null");
130 Column.AllowDBNull = false;
131 _tbl.Columns.Add (Column);
133 cols = new string [3];
141 } catch (Exception e) {
142 AssertEquals ("test#20", typeof (NoNullAllowedException), e.GetType ());
143 //AssertEquals ("test#21", "Column 'not_null' does not allow nulls.", e.Message);
146 Column = _tbl.Columns [0];
147 Column.Unique = true;
149 cols = new string [3];
157 } catch (Exception e) {
158 AssertEquals ("test#23", typeof (ConstraintException), e.GetType ());
159 AssertEquals ("test#24", "Column 'Column1' is constrained to be unique. Value 'first' is already present.", e.Message);
162 Column = new DataColumn ("integer");
163 Column.DataType = typeof (short);
164 _tbl.Columns.Add (Column);
166 object [] obs = new object [4];
175 } catch (Exception e) {
176 // LAMESPEC: MSDN says this exception is InvalidCastException
177 AssertEquals ("test#26", typeof (ArgumentException), e.GetType ());
180 object [] obs1 = new object [5];
189 } catch (Exception e) {
190 AssertEquals ("test#28", typeof(ArgumentException), e.GetType ());
197 DataRowCollection Rows = _tbl.Rows;
198 DataTable Table = new DataTable ("child");
199 Table.Columns.Add ("first", typeof (int));
200 Table.Columns.Add ("second", typeof (string));
202 _tbl.Columns.Add ("first", typeof (int));
203 _tbl.Columns.Add ("second", typeof (float));
205 string [] cols = new string [2];
218 AssertEquals ("test#01", 3, Rows.Count);
221 // hmm... TODO: better tests
222 AssertEquals ("test#02", 0, Rows.Count);
238 Table.Rows.Add (cols);
242 Table.Rows.Add (cols);
246 Table.Rows.Add (cols);
248 DataRelation Rel = new DataRelation ("REL", _tbl.Columns [0], Table.Columns [0]);
249 DataSet Set = new DataSet ();
250 Set.Tables.Add (_tbl);
251 Set.Tables.Add (Table);
252 Set.Relations.Add (Rel);
257 } catch (Exception e) {
258 AssertEquals ("test#04", typeof (InvalidConstraintException), e.GetType ());
259 AssertEquals ("test#05", "Cannot clear table Table1 because ForeignKeyConstraint REL enforces constraints and there are child rows in child.", e.Message);
262 AssertEquals ("test#06", 3, Table.Rows.Count);
264 AssertEquals ("test#07", 0, Table.Rows.Count);
268 public void Contains ()
270 DataColumn C = new DataColumn ("key");
272 C.DataType = typeof (int);
273 C.AutoIncrement = true;
274 C.AutoIncrementSeed = 0;
275 C.AutoIncrementStep = 1;
276 _tbl.Columns.Add (C);
277 _tbl.Columns.Add ("first", typeof (string));
278 _tbl.Columns.Add ("second", typeof (decimal));
280 DataRowCollection Rows = _tbl.Rows;
282 DataRow Row = _tbl.NewRow ();
284 Row = _tbl.NewRow ();
286 Row = _tbl.NewRow ();
288 Row = _tbl.NewRow ();
291 Rows [0] [1] = "test0";
293 Rows [1] [1] = "test1";
295 Rows [2] [1] = "test2";
297 Rows [3] [1] = "test3";
300 AssertEquals ("test#01", 3, _tbl.Columns.Count);
301 AssertEquals ("test#02", 4, _tbl.Rows.Count);
302 AssertEquals ("test#03", 0, _tbl.Rows [0] [0]);
303 AssertEquals ("test#04", 1, _tbl.Rows [1] [0]);
304 AssertEquals ("test#05", 2, _tbl.Rows [2] [0]);
305 AssertEquals ("test#06", 3, _tbl.Rows [3] [0]);
310 } catch (Exception e) {
311 AssertEquals ("test#08", typeof (MissingPrimaryKeyException), e.GetType ());
312 AssertEquals ("test#09", "Table doesn't have a primary key.", e.Message);
315 _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0]};
316 AssertEquals ("test#10", true, Rows.Contains (1));
317 AssertEquals ("test#11", true, Rows.Contains (2));
318 AssertEquals ("test#12", false, Rows.Contains (4));
321 Rows.Contains (new object [] {64, "test0"});
323 } catch (Exception e) {
324 AssertEquals ("test#14", typeof (ArgumentException), e.GetType ());
325 AssertEquals ("test#15", "Expecting 1 value(s) for the key being indexed, but received 2 value(s).", e.Message);
328 _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0], _tbl.Columns [1]};
329 AssertEquals ("test#16", false, Rows.Contains (new object [] {64, "test0"}));
330 AssertEquals ("test#17", false, Rows.Contains (new object [] {0, "test1"}));
331 AssertEquals ("test#18", true, Rows.Contains (new object [] {1, "test1"}));
332 AssertEquals ("test#19", true, Rows.Contains (new object [] {2, "test2"}));
335 Rows.Contains (new object [] {2});
337 } catch (Exception e) {
338 AssertEquals ("test#21", typeof (ArgumentException), e.GetType ());
339 AssertEquals ("test#22", "Expecting 2 value(s) for the key being indexed, but received 1 value(s).", e.Message);
344 public void CopyTo ()
350 DataRowCollection Rows = _tbl.Rows;
352 Rows.Add (new object [] {"1", "1", "1"});
353 Rows.Add (new object [] {"2", "2", "2"});
354 Rows.Add (new object [] {"3", "3", "3"});
355 Rows.Add (new object [] {"4", "4", "4"});
356 Rows.Add (new object [] {"5", "5", "5"});
357 Rows.Add (new object [] {"6", "6", "6"});
358 Rows.Add (new object [] {"7", "7", "7"});
360 DataRow [] dr = new DataRow [10];
365 } catch (Exception e) {
366 AssertEquals ("test#02", typeof (ArgumentException), e.GetType ());
367 //AssertEquals ("test#03", "Destination array was not long enough. Check destIndex and length, and the array's lower bounds.", e.Message);
370 dr = new DataRow [11];
373 AssertEquals ("test#04", null, dr [0]);
374 AssertEquals ("test#05", null, dr [1]);
375 AssertEquals ("test#06", null, dr [2]);
376 AssertEquals ("test#07", null, dr [3]);
377 AssertEquals ("test#08", "1", dr [4] [0]);
378 AssertEquals ("test#09", "2", dr [5] [0]);
379 AssertEquals ("test#10", "3", dr [6] [0]);
380 AssertEquals ("test#11", "4", dr [7] [0]);
381 AssertEquals ("test#12", "5", dr [8] [0]);
382 AssertEquals ("test#13", "6", dr [9] [0]);
386 public void Equals ()
392 DataRowCollection Rows1 = _tbl.Rows;
394 Rows1.Add (new object [] {"1", "1", "1"});
395 Rows1.Add (new object [] {"2", "2", "2"});
396 Rows1.Add (new object [] {"3", "3", "3"});
397 Rows1.Add (new object [] {"4", "4", "4"});
398 Rows1.Add (new object [] {"5", "5", "5"});
399 Rows1.Add (new object [] {"6", "6", "6"});
400 Rows1.Add (new object [] {"7", "7", "7"});
402 DataRowCollection Rows2 = _tbl.Rows;
404 AssertEquals ("test#01", true, Rows2.Equals (Rows1));
405 AssertEquals ("test#02", true, Rows1.Equals (Rows2));
406 AssertEquals ("test#03", true, Rows1.Equals (Rows1));
408 DataTable Table = new DataTable ();
409 Table.Columns.Add ();
410 Table.Columns.Add ();
411 Table.Columns.Add ();
412 DataRowCollection Rows3 = Table.Rows;
414 Rows3.Add (new object [] {"1", "1", "1"});
415 Rows3.Add (new object [] {"2", "2", "2"});
416 Rows3.Add (new object [] {"3", "3", "3"});
417 Rows3.Add (new object [] {"4", "4", "4"});
418 Rows3.Add (new object [] {"5", "5", "5"});
419 Rows3.Add (new object [] {"6", "6", "6"});
420 Rows3.Add (new object [] {"7", "7", "7"});
422 AssertEquals ("test#04", false, Rows3.Equals (Rows1));
423 AssertEquals ("test#05", false, Rows3.Equals (Rows2));
424 AssertEquals ("test#06", false, Rows1.Equals (Rows3));
425 AssertEquals ("test#07", false, Rows2.Equals (Rows3));
431 DataColumn Col = new DataColumn ("test_1");
432 Col.AllowDBNull = false;
434 Col.DataType = typeof (long);
435 _tbl.Columns.Add (Col);
437 Col = new DataColumn ("test_2");
438 Col.DataType = typeof (string);
439 _tbl.Columns.Add (Col);
441 DataRowCollection Rows = _tbl.Rows;
443 Rows.Add (new object [] {1, "first"});
444 Rows.Add (new object [] {2, "second"});
445 Rows.Add (new object [] {3, "third"});
446 Rows.Add (new object [] {4, "fourth"});
447 Rows.Add (new object [] {5, "fifth"});
452 } catch (Exception e) {
453 AssertEquals ("test#02", typeof (MissingPrimaryKeyException), e.GetType ());
454 AssertEquals ("test#03", "Table doesn't have a primary key.", e.Message);
457 _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0]};
458 DataRow row = Rows.Find (1);
459 AssertEquals ("test#04", 1L, row [0]);
461 AssertEquals ("test#05", 2L, row [0]);
462 row = Rows.Find ("2");
463 AssertEquals ("test#06", 2L, row [0]);
466 row = Rows.Find ("test");
468 } catch (Exception e) {
469 AssertEquals ("test#08", typeof (FormatException), e.GetType ());
470 //AssertEquals ("test#09", "Input string was not in a correct format.", e.Message);
474 row = Rows.Find (tes);
475 AssertEquals ("test#10", null, row);
476 _tbl.PrimaryKey = null;
479 Rows.Find (new object [] {1, "fir"});
481 } catch (Exception e) {
482 AssertEquals ("test#12", typeof (MissingPrimaryKeyException), e.GetType ());
483 AssertEquals ("tets#13", "Table doesn't have a primary key.", e.Message);
486 _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0], _tbl.Columns [1]};
491 } catch (Exception e) {
492 AssertEquals ("test#15", typeof (ArgumentException), e.GetType ());
493 AssertEquals ("test#16", "Expecting 2 value(s) for the key being indexed, but received 1 value(s).", e.Message);
496 row = Rows.Find (new object [] {1, "fir"});
497 AssertEquals ("test#16", null, row);
498 row = Rows.Find (new object [] {1, "first"});
499 AssertEquals ("test#17", 1L, row [0]);
503 public void InsertAt ()
508 DataRowCollection Rows = _tbl.Rows;
510 Rows.Add (new object [] {"a", "aa", "aaa"});
511 Rows.Add (new object [] {"b", "bb", "bbb"});
512 Rows.Add (new object [] {"c", "cc", "ccc"});
513 Rows.Add (new object [] {"d", "dd", "ddd"});
515 DataRow Row = _tbl.NewRow ();
521 Rows.InsertAt (Row, -1);
523 } catch (Exception e) {
524 AssertEquals ("test#02", typeof (IndexOutOfRangeException), e.GetType ());
525 AssertEquals ("test#03", "The row insert position -1 is invalid.", e.Message);
528 Rows.InsertAt (Row, 0);
529 AssertEquals ("test#04", "e", Rows [0][0]);
530 AssertEquals ("test#05", "a", Rows [1][0]);
532 Row = _tbl.NewRow ();
537 Rows.InsertAt (Row, 5);
538 AssertEquals ("test#06", "f", Rows [5][0]);
540 Row = _tbl.NewRow ();
545 Rows.InsertAt (Row, 500);
546 AssertEquals ("test#07", "g", Rows [6][0]);
549 Rows.InsertAt (Row, 6); //Row already belongs to the table
552 catch (Exception e) {
553 AssertEquals ("test#09", typeof (ArgumentException), e.GetType ());
554 AssertEquals ("test#10", "This row already belongs to this table.", e.Message);
557 DataTable table = new DataTable ();
558 DataColumn col = new DataColumn ("Name");
559 table.Columns.Add (col);
560 Row = table.NewRow ();
561 Row ["Name"] = "Abc";
562 table.Rows.Add (Row);
564 Rows.InsertAt (Row, 6);
567 catch (Exception e) {
568 AssertEquals ("test#12", typeof (ArgumentException), e.GetType ());
569 AssertEquals ("test#13", "This row already belongs to another table.", e.Message);
572 table = new DataTable ();
573 col = new DataColumn ("Name");
574 col.DataType = typeof (string);
575 table.Columns.Add (col);
576 UniqueConstraint uk = new UniqueConstraint (col);
577 table.Constraints.Add (uk);
579 Row = table.NewRow ();
580 Row ["Name"] = "aaa";
581 table.Rows.InsertAt (Row, 0);
583 Row = table.NewRow ();
584 Row ["Name"] = "aaa";
586 table.Rows.InsertAt (Row, 1);
589 catch (Exception e) {
590 AssertEquals ("test#15", typeof (ConstraintException), e.GetType ());
593 table.Rows.InsertAt (null, 1);
595 catch (Exception e) {
596 AssertEquals ("test#16", typeof (ArgumentNullException), e.GetType ());
601 public void Remove ()
606 DataRowCollection Rows = _tbl.Rows;
608 Rows.Add (new object [] {"a", "aa", "aaa"});
609 Rows.Add (new object [] {"b", "bb", "bbb"});
610 Rows.Add (new object [] {"c", "cc", "ccc"});
611 Rows.Add (new object [] {"d", "dd", "ddd"});
613 AssertEquals ("test#01", 4, _tbl.Rows.Count);
615 Rows.Remove (_tbl.Rows [1]);
616 AssertEquals ("test#02", 3, _tbl.Rows.Count);
617 AssertEquals ("test#03", "a", _tbl.Rows [0] [0]);
618 AssertEquals ("test#04", "c", _tbl.Rows [1] [0]);
619 AssertEquals ("test#05", "d", _tbl.Rows [2] [0]);
624 } catch (Exception e) {
625 AssertEquals ("test#07", typeof (IndexOutOfRangeException), e.GetType ());
626 AssertEquals ("test#08", "The given datarow is not in the current DataRowCollection.", e.Message);
629 DataRow Row = new DataTable ().NewRow ();
634 } catch (Exception e) {
635 AssertEquals ("test#10", typeof (IndexOutOfRangeException), e.GetType ());
636 AssertEquals ("test#11", "The given datarow is not in the current DataRowCollection.", e.Message);
642 } catch (Exception e) {
643 AssertEquals ("test#13", typeof (IndexOutOfRangeException), e.GetType ());
644 AssertEquals ("test#14", "There is no row at position -1.", e.Message);
650 } catch (Exception e) {
651 AssertEquals ("test#16", typeof (IndexOutOfRangeException), e.GetType ());
652 AssertEquals ("test#17", "There is no row at position 64.", e.Message);
657 AssertEquals ("test#18", 1, Rows.Count);
658 AssertEquals ("test#19", "c", Rows [0] [0]);