Ifdef code that is not supposed to pass in TARGET_JVM by now
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowCollectionTest.cs
1 // DataRowCollectionTest.cs - NUnit Test Cases for System.DataRowCollection
2 //
3 // Authors:
4 //   Franklin Wise (gracenote@earthlink.net)
5 //   Martin Willemoes Hansen (mwh@sysrq.dk)
6 //
7 // (C) Copyright 2002 Franklin Wise
8 // (C) Copyright 2003 Martin Willemoes Hansen
9 // 
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
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:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
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.
32 //
33
34
35 using NUnit.Framework;
36 using System;
37 using System.Data;
38
39 namespace MonoTests.System.Data
40 {
41         [TestFixture]
42         public class DataRowCollectionTest : Assertion
43         {
44                 private DataTable _tbl; 
45
46                 [SetUp]
47                 public void GetReady()
48                 {
49                         _tbl = new DataTable();
50                 }
51
52                 [Test]
53                 public void AutoIncrement()
54                 {
55                         DataColumn col = new DataColumn("Auto");
56                         col.AutoIncrement = true;
57                         col.AutoIncrementSeed = 0;
58                         col.AutoIncrementStep = 1;
59                         
60                         _tbl.Columns.Add(col);
61                         _tbl.Rows.Add(_tbl.NewRow());
62
63                         AssertEquals("test#01" , 0, Convert.ToInt32(_tbl.Rows[0]["Auto"] ));
64                                 
65                         _tbl.Rows.Add(_tbl.NewRow());
66                         AssertEquals("test#02" , 1, Convert.ToInt32(_tbl.Rows[1]["Auto"] ));
67                         
68                         col.AutoIncrement = false;
69                         AssertEquals("test#03" , 1, Convert.ToInt32(_tbl.Rows[1]["Auto"] ));
70
71                         _tbl.Rows.Add(_tbl.NewRow());
72                         AssertEquals("test#04" , DBNull.Value, _tbl.Rows[2]["Auto"]);
73
74                         col.AutoIncrement = true;
75                         col.AutoIncrementSeed = 10;
76                         col.AutoIncrementStep = 2;
77                         
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"] ));
82
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);
89                         
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 ());
93
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);
101                 }
102
103                 [Test]
104                 public void Add ()
105                 {
106                         _tbl.Columns.Add ();
107                         _tbl.Columns.Add ();
108                         DataRow Row = _tbl.NewRow ();
109                         DataRowCollection Rows = _tbl.Rows;
110                         
111                         Rows.Add (Row);
112                         AssertEquals ("test#01", 1, Rows.Count);
113                         AssertEquals ("test#02", false, Rows.IsReadOnly);
114                         AssertEquals ("test#03", false, Rows.IsSynchronized);
115 #if !TARGET_JVM
116                         AssertEquals ("test#04", "System.Data.DataRowCollection", Rows.ToString ());
117 #endif
118                         
119                         string [] cols = new string [2];
120                         cols [0] = "first";
121                         cols [1] = "second";
122                         
123                         Rows.Add (cols);
124                         cols [0] = "something";
125                         cols [1] = "else";
126                         Rows.Add (cols);
127                         
128                         AssertEquals ("test#05", 3, Rows.Count);
129 #if !TARGET_JVM
130                         AssertEquals ("test#06", "System.Data.DataRow",  Rows [0].ToString ());
131 #endif
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]);
138                         
139                         try {
140                                 Rows.Add (Row);
141                                 Fail ("test#13");
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);
145                         }
146                         
147                         try {
148                                 Row = null;
149                                 Rows.Add (Row);
150                                 Fail ("test#16");
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);
154                         }
155                         
156                         DataColumn Column = new DataColumn ("not_null");
157                         Column.AllowDBNull = false;
158                         _tbl.Columns.Add (Column);
159                         
160                         cols = new string [3];
161                         cols [0] = "first";
162                         cols [1] = "second";
163                         cols [2] = null;
164                         
165                         try {
166                                 Rows.Add (cols);
167                                 Fail ("test#19");
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);
171                         }
172                         
173                         Column = _tbl.Columns [0];                      
174                         Column.Unique = true;
175
176                         cols = new string [3];
177                         cols [0] = "first";
178                         cols [1] = "second";
179                         cols [2] = "blabal";
180                         
181                         try {
182                                 Rows.Add (cols);
183                                 Fail ("test#22");
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);
187                         }
188                        
189                         Column = new DataColumn ("integer");
190                         Column.DataType = typeof (short);
191                         _tbl.Columns.Add (Column);
192                         
193                         object [] obs = new object [4];
194                         obs [0] = "_first";
195                         obs [1] = "second";
196                         obs [2] = "blabal";
197                         obs [3] = "ads";
198                         
199                         try {
200                                 Rows.Add (obs);
201                                 Fail ("test#25");
202                         } catch (ArgumentException e) {
203                                 // LAMESPEC: MSDN says this exception is InvalidCastException
204 //                              AssertEquals ("test#26", typeof (ArgumentException), e.GetType ());
205                         }
206
207                         object [] obs1 = new object [5];
208                         obs1 [0] = "A";
209                         obs1 [1] = "B";
210                         obs1 [2] = "C";
211                         obs1 [3] = 38;
212                         obs1 [4] = "Extra";
213                         try {
214                                 Rows.Add (obs1);
215                                 Fail ("test#27");
216                         } catch (Exception e) {
217                                 AssertEquals ("test#28", typeof(ArgumentException), e.GetType ());
218                         }
219                 }
220
221                 [Test]
222                 public void Add_ByValuesNullTest ()
223                 {
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));
228
229                         t.Columns [0].AutoIncrement = true;
230                         t.Columns [0].AutoIncrementSeed = 10;
231                         t.Columns [0].AutoIncrementStep = 5;
232
233                         t.Columns [1].DefaultValue = "testme";
234                         
235
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]);
241
242                         // dbNull test
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]);
247
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]);
253                 }
254                 
255                 [Test]
256                 public void Clear ()
257                 {
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));
262                         
263                         _tbl.Columns.Add ("first", typeof (int));
264                         _tbl.Columns.Add ("second", typeof (float));
265
266                         string [] cols = new string [2];
267                         cols [0] = "1";
268                         cols [1] = "1,1";
269                         Rows.Add (cols);
270                         
271                         cols [0] = "2";
272                         cols [1] = "2,1";
273                         Rows.Add (cols);
274                         
275                         cols [0] = "3";
276                         cols [1] = "3,1";
277                         Rows.Add (cols);
278                         
279                         AssertEquals ("test#01", 3, Rows.Count);
280                         Rows.Clear ();
281                         
282                         // hmm... TODO: better tests
283                         AssertEquals ("test#02", 0, Rows.Count);
284                         
285                         cols [0] = "1";
286                         cols [1] = "1,1";
287                         Rows.Add (cols);
288                         
289                         cols [0] = "2";
290                         cols [1] = "2,1";
291                         Rows.Add (cols);
292                         
293                         cols [0] = "3";
294                         cols [1] = "3,1";
295                         Rows.Add (cols);
296
297                         cols [0] = "1";
298                         cols [1] = "test";
299                         Table.Rows.Add (cols);
300                         
301                         cols [0] = "2";
302                         cols [1] = "test2";
303                         Table.Rows.Add (cols);
304                         
305                         cols [0] = "3";
306                         cols [1] = "test3";
307                         Table.Rows.Add (cols);                  
308                         
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);
314                         
315                         try {
316                                 Rows.Clear ();
317                                 Fail ("test#03");
318                         } catch (InvalidConstraintException) {
319                         }
320                         
321                         AssertEquals ("test#06", 3, Table.Rows.Count);
322                         Table.Rows.Clear ();
323                         AssertEquals ("test#07", 0, Table.Rows.Count);
324                 }
325                 
326                 [Test]
327                 public void Contains ()
328                 {
329                         DataColumn C = new DataColumn ("key");
330                         C.Unique = true;                        
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));
338                         
339                         DataRowCollection Rows = _tbl.Rows;
340                         
341                         DataRow Row = _tbl.NewRow ();
342                         _tbl.Rows.Add (Row);
343                         Row = _tbl.NewRow ();
344                         _tbl.Rows.Add (Row);
345                         Row = _tbl.NewRow ();
346                         _tbl.Rows.Add (Row);
347                         Row = _tbl.NewRow ();
348                         _tbl.Rows.Add (Row);
349                         
350                         Rows [0] [1] = "test0";
351                         Rows [0] [2] = 0;
352                         Rows [1] [1] = "test1";
353                         Rows [1] [2] = 1;
354                         Rows [2] [1] = "test2";
355                         Rows [2] [2] = 2;
356                         Rows [3] [1] = "test3";
357                         Rows [3] [2] = 3;
358                         
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]);
365                         
366                         try {
367                                 Rows.Contains (1);
368                                 Fail ("test#07");
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);                       
372                         }
373                         
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));
378                         
379                         try {
380                                 Rows.Contains (new object [] {64, "test0"});
381                                 Fail ("test#13");
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);
385                         }
386                         
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"}));
392                         
393                         try {
394                                 Rows.Contains (new object [] {2});
395                                 Fail ("test#20");
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);
399                         }
400                 }
401                 
402                 [Test]
403                 public void CopyTo ()
404                 {
405                         _tbl.Columns.Add ();
406                         _tbl.Columns.Add ();
407                         _tbl.Columns.Add ();
408                         
409                         DataRowCollection Rows = _tbl.Rows;
410                         
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"});
418                         
419                         DataRow [] dr = new DataRow [10];
420                         
421                         try {
422                                 Rows.CopyTo (dr, 4);
423                                 Fail ("test#01");
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);
427                         }
428                         
429                         dr = new DataRow [11];
430                         Rows.CopyTo (dr, 4);
431                         
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]);
442                 }
443                 
444                 [Test]
445                 public void Equals ()
446                 {
447                         _tbl.Columns.Add ();
448                         _tbl.Columns.Add ();
449                         _tbl.Columns.Add ();
450                         
451                         DataRowCollection Rows1 = _tbl.Rows;
452                         
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"});
460                         
461                         DataRowCollection Rows2 = _tbl.Rows;
462                         
463                         AssertEquals ("test#01", true, Rows2.Equals (Rows1));
464                         AssertEquals ("test#02", true, Rows1.Equals (Rows2));
465                         AssertEquals ("test#03", true, Rows1.Equals (Rows1));
466                         
467                         DataTable Table = new DataTable ();
468                         Table.Columns.Add ();
469                         Table.Columns.Add ();
470                         Table.Columns.Add ();
471                         DataRowCollection Rows3 = Table.Rows;
472
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"});
480                         
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));
485                 }
486                 
487                 [Test]
488                 public void Find ()
489                 {
490                         DataColumn Col = new DataColumn ("test_1");
491                         Col.AllowDBNull = false;
492                         Col.Unique = true;
493                         Col.DataType = typeof (long);
494                         _tbl.Columns.Add (Col);
495                         
496                         Col = new DataColumn ("test_2");
497                         Col.DataType = typeof (string);
498                         _tbl.Columns.Add (Col);
499                         
500                         DataRowCollection Rows = _tbl.Rows;
501                         
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"});
507                         
508                         try {
509                                 Rows.Find (1);
510                                 Fail ("test#01");
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);              
514                         }
515                         
516                         _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0]};
517                         DataRow row = Rows.Find (1);
518                         AssertEquals ("test#04", 1L, row [0]);
519                         row = Rows.Find (2);                    
520                         AssertEquals ("test#05", 2L, row [0]);
521                         row = Rows.Find ("2");
522                         AssertEquals ("test#06", 2L, row [0]);
523                         
524                         try {
525                                 row = Rows.Find ("test");
526                                 Fail ("test#07");
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);
530                         }
531                         
532                         String tes = null;                      
533                         row = Rows.Find (tes);                  
534                         AssertEquals ("test#10", null, row);
535                         _tbl.PrimaryKey = null;
536                         
537                         try {
538                                 Rows.Find (new object [] {1, "fir"});
539                                 Fail ("test#11");
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);
543                         }
544                         
545                         _tbl.PrimaryKey = new DataColumn [] {_tbl.Columns [0], _tbl.Columns [1]};
546                         
547                         try {
548                                 Rows.Find (1);
549                                 Fail ("test#14");
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);
553                         }
554                         
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]);
559                 }
560                 
561                 [Test]
562                 public void Find2 ()
563                 {
564                         DataSet ds = new DataSet ();\r
565                         ds.EnforceConstraints = false;\r
566 \r
567                         DataTable dt = new DataTable ();\r
568                         ds.Tables.Add (dt);\r
569 \r
570                         DataColumn dc = new DataColumn ("Column A");\r
571                         dt.Columns.Add (dc);\r
572 \r
573                         dt.PrimaryKey = new DataColumn [] {dc};\r
574 \r
575                         DataRow dr = dt.NewRow ();\r
576                         dr [0] = "a";\r
577                         dt.Rows.Add (dr);\r
578 \r
579                         dr = dt.NewRow ();\r
580                         dr [0] = "b";\r
581                         dt.Rows.Add (dr);\r
582 \r
583                         dr = dt.NewRow ();\r
584                         dr [0] = "c";\r
585                         dt.Rows.Add (dr);\r
586 \r
587                         DataRow row = (DataRow) ds.Tables [0].Rows.Find (new object [] {"a"});
588                         
589                         AssertNotNull (row);
590                 }
591                 
592                 [Test]
593                 public void InsertAt ()
594                 {
595                         _tbl.Columns.Add ();
596                         _tbl.Columns.Add ();
597                         _tbl.Columns.Add ();
598                         DataRowCollection Rows = _tbl.Rows;
599                         
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"});
604                         
605                         DataRow Row = _tbl.NewRow ();
606                         Row [0] = "e";
607                         Row [1] = "ee";
608                         Row [2] = "eee";
609                         
610                         try {
611                                 Rows.InsertAt (Row, -1);
612                                 Fail ("test#01");
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);
616                         }
617                         
618                         Rows.InsertAt (Row, 0);
619                         AssertEquals ("test#04", "e", Rows [0][0]);
620                         AssertEquals ("test#05", "a", Rows [1][0]);
621                         
622                         Row = _tbl.NewRow ();
623                         Row [0] = "f";
624                         Row [1] = "ff";
625                         Row [2] = "fff";
626                         
627                         Rows.InsertAt (Row, 5);
628                         AssertEquals ("test#06", "f", Rows [5][0]);
629                         
630                         Row = _tbl.NewRow ();
631                         Row [0] = "g";
632                         Row [1] = "gg";
633                         Row [2] = "ggg";
634
635                         Rows.InsertAt (Row, 500);
636                         AssertEquals ("test#07", "g", Rows [6][0]);
637
638                         try {
639                                 Rows.InsertAt (Row, 6); //Row already belongs to the table
640                                 Fail ("test#08");
641                         }
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);
645                         }
646
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);
653                         try {
654                                 Rows.InsertAt (Row, 6);
655                                 Fail ("test#11");
656                         }
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);
660                         }
661
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);
668                         
669                         Row = table.NewRow ();
670                         Row ["Name"] = "aaa";
671                         table.Rows.InsertAt (Row, 0);
672         
673                         Row = table.NewRow ();
674                         Row ["Name"] = "aaa";
675                         try {
676                                 table.Rows.InsertAt (Row, 1);
677                                 Fail ("test#14");
678                         }
679                         catch (Exception e) {
680                                 AssertEquals ("test#15", typeof (ConstraintException), e.GetType ());
681                         }
682                         try {
683                                 table.Rows.InsertAt (null, 1);
684                         }
685                         catch (Exception e) {
686                                 AssertEquals ("test#16", typeof (ArgumentNullException), e.GetType ());
687                         }
688                 }
689                 
690                 [Test]
691                 public void Remove ()
692                 {
693                         _tbl.Columns.Add ();
694                         _tbl.Columns.Add ();
695                         _tbl.Columns.Add ();
696                         DataRowCollection Rows = _tbl.Rows;
697                         
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"});
702                         
703                         AssertEquals ("test#01", 4, _tbl.Rows.Count);
704                         
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]);
710                         
711                         try {
712                                 Rows.Remove (null);
713                                 Fail ("test#06");
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);
717                         }
718                         
719                         DataRow Row = new DataTable ().NewRow ();
720                         
721                         try {
722                                 Rows.Remove (Row);
723                                 Fail ("test#09");
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);
727                         }
728                         
729                         try {
730                                 Rows.RemoveAt (-1);
731                                 Fail ("test#12");
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);
735                         }
736                         
737                         try { 
738                                 Rows.RemoveAt (64);
739                                 Fail ("test#15");
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);
743                         }
744                         
745                         Rows.RemoveAt (0);
746                         Rows.RemoveAt (1);
747                         AssertEquals ("test#18", 1, Rows.Count);
748                         AssertEquals ("test#19", "c", Rows [0] [0]);
749                 }
750         }
751 }