In Test/System.Data:
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowTest.cs
1 // DataRowTest.cs - NUnit Test Cases for System.DataRow
2 //
3 // Authors:
4 //   Franklin Wise (gracenote@earthlink.net)
5 //   Daniel Morgan <danmorg@sc.rr.com>
6 //   Roopa Wilson (rowilson@novell.com)
7 //
8 // (C) Copyright 2002 Franklin Wise
9 // (C) Copyright 2003 Daniel Morgan
10 // (C) Copyright 2003 Martin Willemoes Hansen
11 // 
12
13 //
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 //
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:
23 // 
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
26 // 
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.
34 //
35
36
37 using NUnit.Framework;
38 using System;
39 using System.Data;
40
41 namespace MonoTests.System.Data
42 {
43         [TestFixture]
44         public class DataRowTest : Assertion {
45         
46                 private DataTable _tbl; 
47                 private DataTable table;                                                
48                 private DataRow row;    
49
50                 [SetUp]
51                 public void GetReady() {
52                         _tbl = new DataTable();
53                         table = MakeTable ();                                           
54                         row = table.NewRow ();                                          
55                         row ["FName"] = "Hello";                                        
56                         row ["LName"] = "World";                                        
57                         table.Rows.Add (row);  
58                 }
59                 
60                 private DataTable MakeTable ()
61                 {
62                         DataTable namesTable = new DataTable ("Names");
63                         DataColumn idColumn = new  DataColumn ();
64                                                                                                     
65                                                                                                     
66                         idColumn.DataType = Type.GetType ("System.Int32");
67                         idColumn.ColumnName = "Id";
68                         idColumn.AutoIncrement = true;
69                         namesTable.Columns.Add (idColumn);
70                                                                                                     
71                                                                                                     
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);
77                                                                                                     
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);
83                                                                                                     
84                                                                                                     
85                         // Set the primary key for the table
86                         DataColumn [] keys = new DataColumn [1];
87                         keys [0] = idColumn;
88                         namesTable.PrimaryKey = keys;
89                         // Return the new DataTable.
90                         return namesTable;
91                 }
92
93                 [Test]
94                 public void SetColumnErrorTest ()
95                 {
96                         string errorString;
97                         errorString = "Some error!";
98                         // Set the error for the specified column of the row.
99                         row.SetColumnError (1, errorString);
100                         GetColumnErrorTest ();
101                         GetAllErrorsTest ();
102                 }
103
104                 private  void GetColumnErrorTest ()
105                 {
106                         // Print the error of a specified column.
107                         AssertEquals ("#A01", "Some error!", row.GetColumnError (1));
108                 }
109
110                 private void GetAllErrorsTest ()
111                 {
112                         DataColumn [] colArr;
113                                                                                                     
114                         if (row.HasErrors) {
115                                 colArr = row.GetColumnsInError ();
116                                                                                                     
117                                 for (int i = 0; i < colArr.Length; i++) {
118                                         AssertEquals ("#A02", table.Columns [1], colArr [i]);
119                                 }
120                                 row.ClearErrors ();
121                         }
122                 }
123
124                 [Test]
125                 public void DeleteRowTest ()
126                 {
127                         DataRow newRow;
128                                                                                                     
129                                                                                                     
130                         for (int i = 1; i <= 2; i++) {
131                                 newRow = table.NewRow ();
132                                 newRow ["FName"] = "Name " + i;
133                                 newRow ["LName"] = " Last Name" + i;
134                                 table.Rows.Add (newRow);
135                         }
136                         table.AcceptChanges ();
137                                                                                                     
138                         int cnt = 1;
139                         for (int i = 1; i < table.Rows.Count; i++) {
140                                 DataRow r = table.Rows [i];
141                                 AssertEquals ("#A03", "Name " + cnt, r ["fName"]);
142                                 cnt++;
143                         }
144                                                                                                     
145                                                                                                     
146                         // Create a DataView with the table.
147                         DataRowCollection rc = table.Rows;
148                         rc [0].Delete ();
149                         rc [2].Delete ();
150                                                                                                     
151                                                                                                     
152                         AssertEquals ("#A04", "Deleted", rc [0].RowState.ToString ());
153                         AssertEquals ("#A05", "Deleted", rc [2].RowState.ToString ());
154                                                                                                     
155                                                                                                     
156                         // Accept changes
157                         table.AcceptChanges ();
158                         AssertEquals ("#A06", "Name 1", (table.Rows [0]) [1]);
159                         try  {
160                                 object o = rc [2];
161                                 Fail ("#A07");
162                         }
163                         catch (Exception e) {
164                                 AssertEquals ("#A08", "There is no row at position 2.", e.Message);
165                         }
166                 }
167
168                 [Test]
169                 public void EditModeTest ()
170                 {
171                         try {
172                                 //Clear all existing values from table
173                                 for (int i = 0; i < table.Rows.Count; i++) {
174                                         table.Rows[i].Delete ();
175                                 }
176                                 table.AcceptChanges ();
177                                 row = table.NewRow ();
178                                 row["FName"] = "My FName";
179                                 table.Rows.Add (row);
180                                                                                                     
181                                                                                                     
182                                 // Stage 1
183                                 //Initially: After Add (Row) But Before Accept Changes");
184                                 AssertEquals ("#A09", "My FName", row [1, DataRowVersion.Default]);
185                                 AssertEquals ("#A10", "LName", row [2, DataRowVersion.Default]);
186                                                                                                     
187                                 AssertEquals ("#A11", "My FName", row [1, DataRowVersion.Current]);
188                                 AssertEquals ("#A12", "LName", row [2, DataRowVersion.Current]);
189                                                                                                     
190                                 try {
191                                       object o = row [1, DataRowVersion.Original];
192                                       o = row [1, DataRowVersion.Proposed];
193                                         Fail ("#A13");
194                                 }
195                                 catch (Exception e) {
196                                         if (e.GetType () != typeof (AssertionException)) {
197                                                 AssertEquals ("#A14", typeof (VersionNotFoundException), e.GetType ());
198                                         }
199                                 }
200                                                                                                     
201                                 // Stage 2
202                                 //After Accept Changes
203                                 table.AcceptChanges ();
204                                 AssertEquals ("#A15", "My FName", row [1, DataRowVersion.Default]);
205                                 AssertEquals ("#A16", "LName", row [2, DataRowVersion.Default]);
206                                                                                                     
207                                                                                                     
208                                 AssertEquals ("#A17", "My FName", row [1, DataRowVersion.Current]);
209                                 AssertEquals ("#A18", "LName", row [2, DataRowVersion.Current]);
210                                 
211                                 try {
212                                       object o = row [1, DataRowVersion.Proposed];
213                                         Fail ("#A19");
214                                 }
215                                 catch (Exception e) {
216                                         if (e.GetType () != typeof (AssertionException)) {
217                                                 AssertEquals ("#A20", typeof (VersionNotFoundException), e.GetType ());
218                                         }
219                                 }
220                                                                                                     
221                                                                                                                                                                                                          
222                                 // Stage 3                                 // Edit Mode
223                                 table.Rows [0].BeginEdit ();
224                                 table.Rows [0] ["LName"] = "My LName";
225                                                                                                     
226                                 AssertEquals ("#A21", "My FName", row [1, DataRowVersion.Default]);
227                                 AssertEquals ("#A22", "My LName", row [2, DataRowVersion.Default]);
228                                                                                                                                                                                                          
229                                 AssertEquals ("#A23", "My FName", row [1, DataRowVersion.Current]);
230                                 AssertEquals ("#A24", "LName", row [2, DataRowVersion.Current]);
231                                                                                                     
232                                                                                                     
233                                 AssertEquals ("#A25", "My FName", row [1, DataRowVersion.Original]);                                AssertEquals ("#A26", "LName", row [2, DataRowVersion.Original]);
234                                                                                                     
235                                 AssertEquals ("#A26", "My FName", row [1, DataRowVersion.Proposed]);
236                                 AssertEquals ("#A27", "My LName", row [2, DataRowVersion.Proposed]);                                                                                                    
237                                                                                                     
238                                                                                                     
239                                 // Stage 4
240                                 //After Edit sessions
241                                 for (int i=0; i < table.Rows.Count;i++)
242                                         table.Rows [i].EndEdit ();
243                                 AssertEquals ("#A28", "My FName", row [1, DataRowVersion.Default]);
244                                 AssertEquals ("#A29", "My LName", row [2, DataRowVersion.Default]);
245                                                                                                                                                                                                          
246                                 AssertEquals ("#A30", "My FName", row [1, DataRowVersion.Original]);                                AssertEquals ("#A31", "LName", row [2, DataRowVersion.Original]);
247                                                                                                     
248                                                                                                     
249                                 AssertEquals ("#A32", "My FName", row [1, DataRowVersion.Current]);
250                                 AssertEquals ("#A33", "My LName", row [2, DataRowVersion.Current]);
251                                                                                                     
252                                 try {
253                                       object o = row [1, DataRowVersion.Proposed];
254                                         Fail ("#A34");
255                                 }
256                                 catch (Exception e) {
257                                         if (e.GetType ()!=typeof (AssertionException)) {
258                                                 AssertEquals ("#A35", typeof (VersionNotFoundException), e.GetType ());
259                                         }
260                                 }
261                                                                                                     
262                                 //Stage 5
263                                 //After Accept Changes
264                                 table.AcceptChanges ();
265                                 AssertEquals ("#A36", "My FName", row [1, DataRowVersion.Default]);
266                                 AssertEquals ("#A37", "My LName", row [2, DataRowVersion.Default]);
267                                                                                                     
268                                                                                                     
269                                 AssertEquals ("#A38", "My FName", row [1, DataRowVersion.Original]);                                AssertEquals ("#A39", "My LName", row [2, DataRowVersion.Original]);                                                                                                    
270                                                                                                     
271                                 AssertEquals ("#A40", "My FName", row [1, DataRowVersion.Current]);
272                                 AssertEquals ("#A41", "My LName", row [2, DataRowVersion.Current]);
273                                                                                                     
274                                                                                                     
275                                 try {
276                                       object o = row [1, DataRowVersion.Proposed];
277                                         Fail ("#A42");
278                                 }
279                                 catch (Exception e) {
280                                                 if (e.GetType () != typeof (AssertionException)) {
281                                                         AssertEquals ("#A43", typeof (VersionNotFoundException),
282                                                                 e.GetType ());
283                                                 }
284                                         }
285                                                                                                     
286                                                                                                     
287                         }
288                         catch (Exception e){
289 //                              Console.WriteLine (e + "" + e.StackTrace);
290                         }
291                 }                                                                                                     
292                 [Test]
293                 public void ParentRowTest ()
294                 {
295
296                         //Clear all existing values from table
297                         for (int i = 0; i < table.Rows.Count; i++) {
298                                         table.Rows[i].Delete ();
299                         }
300                         table.AcceptChanges ();
301                         row = table.NewRow ();
302                         row["FName"] = "My FName";
303                         row["Id"] = 0;
304                         table.Rows.Add (row);
305                                                                                                     
306                         DataTable tableC = new DataTable ("Child");
307                         DataColumn colC;
308                         DataRow rowC;
309                                                                                                     
310                         colC = new DataColumn ();
311                         colC.DataType = Type.GetType ("System.Int32");
312                         colC.ColumnName = "Id";
313                         colC.AutoIncrement=true;
314                         tableC.Columns.Add (colC);
315                                                                                                     
316                                                                                                     
317                         colC = new DataColumn ();
318                         colC.DataType = Type.GetType ("System.String");
319                         colC.ColumnName = "Name";
320                         tableC.Columns.Add (colC);
321                                                                                                     
322                         rowC = tableC.NewRow ();
323                         rowC["Name"] = "My FName";
324                         tableC.Rows.Add (rowC);
325                         DataSet ds = new DataSet ();
326                         ds.Tables.Add (table);
327                         ds.Tables.Add (tableC);
328                         DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
329                         ds.Relations.Add (dr);
330                                                                                                     
331                         rowC.SetParentRow (table.Rows [0], dr);
332                                                                                                     
333                         AssertEquals ("#PRT-01", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
334                         AssertEquals ("#PRT-02", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
335
336                         ds.Relations.Clear ();
337                         dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
338                         ds.Relations.Add (dr);
339                         rowC.SetParentRow (table.Rows [0], dr);
340                         AssertEquals ("#PRT-03", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
341                         AssertEquals ("#PRT-04", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
342
343                         ds.Relations.Clear ();
344                         dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
345                         tableC.ParentRelations.Add (dr);
346                         rowC.SetParentRow (table.Rows [0]);
347                         AssertEquals ("#PRT-05", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
348                         AssertEquals ("#PRT-06", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
349                                                 
350                 } 
351
352                 [Test]
353                 public void ParentRowTest2 ()
354                 {
355                         DataSet ds = new DataSet ();
356                         DataTable tableP = ds.Tables.Add ("Parent");
357                         DataTable tableC = ds.Tables.Add ("Child");
358                         DataColumn colC;
359                         DataRow rowC;
360                                                                                                     
361                         colC = new DataColumn ();
362                         colC.DataType = Type.GetType ("System.Int32");
363                         colC.ColumnName = "Id";
364                         colC.AutoIncrement = true;
365                         tableP.Columns.Add (colC);
366                         
367                         colC = new DataColumn ();
368                         colC.DataType = Type.GetType ("System.Int32");
369                         colC.ColumnName = "Id";
370                         tableC.Columns.Add (colC);
371  
372                         row = tableP.Rows.Add (new object [0]);
373                         rowC = tableC.NewRow ();
374  
375                         ds.EnforceConstraints = false;
376                         DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
377                         ds.Relations.Add (dr);
378
379                         rowC.SetParentRow (row, dr);
380                         DataRow [] rows = rowC.GetParentRows (dr);
381
382                         AssertEquals ("#A49", 1, rows.Length);
383                         AssertEquals ("#A50", tableP.Rows [0], rows [0]);
384
385                         try{
386                                 rows = row.GetParentRows (dr);
387                         }catch(InvalidConstraintException){
388                                 //Test done
389                                 return ;
390                         }catch(Exception e){
391                                 Fail("#A51, InvalidConstraintException expected, got : " + e);
392                         }
393                         
394                         Fail("#A52, InvalidConstraintException expected but got none.");
395                 }
396
397                 [Test]
398                 public void ChildRowTest ()
399                 {
400
401                         //Clear all existing values from table
402                         for (int i = 0; i < table.Rows.Count; i++) {
403                                         table.Rows [i].Delete ();
404                         }
405                         table.AcceptChanges ();
406                         row = table.NewRow ();
407                         row ["FName"] = "My FName";
408                         row ["Id"] = 0;
409                         table.Rows.Add (row);
410                                                                                                     
411                         DataTable tableC = new DataTable ("Child");
412                         DataColumn colC;
413                         DataRow rowC;
414
415                         colC = new DataColumn ();
416                         colC.DataType = Type.GetType ("System.Int32");
417                         colC.ColumnName = "Id";
418                         colC.AutoIncrement = true;
419                         tableC.Columns.Add (colC);
420                                                                                                     
421                         colC = new DataColumn ();
422                         colC.DataType = Type.GetType ("System.String");
423                         colC.ColumnName = "Name";
424                         tableC.Columns.Add (colC);
425                                                                                                     
426                         rowC = tableC.NewRow ();
427                         rowC ["Name"] = "My FName";
428                         tableC.Rows.Add (rowC);
429                         DataSet ds = new DataSet ();
430                         ds.Tables.Add (table);
431                         ds.Tables.Add (tableC);
432                         DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
433                         ds.Relations.Add (dr);
434                                                                                                     
435                         rowC.SetParentRow (table.Rows [0], dr);
436                                                                                                     
437                         DataRow [] rows = (table.Rows [0]).GetChildRows (dr);
438
439                         AssertEquals ("#A45", 1, rows.Length);
440                         AssertEquals ("#A46", tableC.Rows [0], rows [0]);
441                         
442                 } 
443
444                 [Test]
445                 public void ChildRowTest2 ()
446                 {
447                         DataSet ds = new DataSet ();
448                         DataTable tableP = ds.Tables.Add ("Parent");
449                         DataTable tableC = ds.Tables.Add ("Child");
450                         DataColumn colC;
451                         DataRow rowC;
452                                                                                                     
453                         colC = new DataColumn ();
454                         colC.DataType = Type.GetType ("System.Int32");
455                         colC.ColumnName = "Id";
456                         colC.AutoIncrement = true;
457                         tableP.Columns.Add (colC);
458                         
459                         colC = new DataColumn ();
460                         colC.DataType = Type.GetType ("System.Int32");
461                         colC.ColumnName = "Id";
462                         tableC.Columns.Add (colC);
463
464                         row = tableP.NewRow ();
465                         rowC = tableC.Rows.Add (new object [0]);
466
467                         ds.EnforceConstraints = false;
468                         DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
469                         ds.Relations.Add (dr);
470
471                         rowC.SetParentRow (row, dr);
472                         DataRow [] rows = row.GetChildRows (dr);
473                         
474                         AssertEquals ("#A47", 1, rows.Length);
475                         AssertEquals ("#A48", tableC.Rows [0], rows [0]);
476
477                         try{
478                             rows = rowC.GetChildRows (dr);
479                         }catch(InvalidConstraintException){
480                             //Test done
481                             return ;
482                         }catch(Exception e){
483                             Fail("#A53, InvalidConstraintException expected, got : " + e);
484                         }
485                         
486                         Fail("#A54, InvalidConstraintException expected but got none.");
487                 }
488
489                 [Test]
490                 public void ParentChildRowVersionTest ()
491                 {
492                         DataSet ds = new DataSet ();
493                         DataTable tableP = ds.Tables.Add ("Parent");
494                         DataTable tableC = ds.Tables.Add ("Child");
495                         DataColumn colC;
496                         DataRow rowC;
497                                                                                                     
498                         colC = new DataColumn ();
499                         colC.DataType = Type.GetType ("System.Int32");
500                         colC.ColumnName = "Id";
501                         colC.AutoIncrement = true;
502                         tableP.Columns.Add (colC);
503                         
504                         colC = new DataColumn ();
505                         colC.DataType = Type.GetType ("System.Int32");
506                         colC.ColumnName = "Id";
507                         tableC.Columns.Add (colC);
508
509                         row = tableP.NewRow ();
510                         rowC = tableC.Rows.Add (new object [0]);
511
512                         ds.EnforceConstraints = false;
513                         DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
514                         ds.Relations.Add (dr);
515
516                         rowC.SetParentRow (row, dr);
517                         DataRow [] rows;
518
519                         try {
520                             rows = row.GetChildRows (dr, DataRowVersion.Current);
521                         }catch (VersionNotFoundException v) {
522                             //Check for GetParentRows
523                             try{
524                                 //Child Row should be in Detached state for the next test
525                                 rowC = tableC.NewRow();
526
527                                 rows = rowC.GetParentRows (dr, DataRowVersion.Current);
528                             }catch (VersionNotFoundException v2) {
529                                 //Test Done
530                                 return ;
531                             }catch (Exception e){
532                                 Fail ("#A55, VersionNotFoundException expected, got : " + e);
533                             }
534                             Fail ("#A56, VersionNotFoundException expected but got none.");
535                         }catch (Exception e){
536                             Fail ("#A57, VersionNotFoundException expected, got : " + e);
537                         }
538                         
539                         Fail("#A58, VersionNotFoundException expected but got none.");
540                 }
541
542                 // tests item at row, column in table to be DBNull.Value
543                 private void DBNullTest (string message, DataTable dt, int row, int column) 
544                 {
545                         object val = dt.Rows[row].ItemArray[column];
546                         AssertEquals(message, DBNull.Value, val);
547                 }
548
549                 // tests item at row, column in table to be null
550                 private void NullTest (string message, DataTable dt, int row, int column) 
551                 {
552                         object val = dt.Rows[row].ItemArray[column];
553                         AssertEquals(message, null, val);
554                 }
555
556                 // tests item at row, column in table to be 
557                 private void ValueTest (string message, DataTable dt, int row, int column, object value) 
558                 {
559                         object val = dt.Rows[row].ItemArray[column];
560                         AssertEquals(message, value, val);
561                 }
562
563                 // test set null, DBNull.Value, and ItemArray short count
564                 [Test]
565                 public void NullInItemArray () 
566                 {
567                         string zero = "zero";
568                         string one = "one";
569                         string two = "two";
570
571                         DataTable table = new DataTable();
572                         table.Columns.Add(new DataColumn(zero, typeof(string)));
573                         table.Columns.Add(new DataColumn(one, typeof(string)));
574                         table.Columns.Add(new DataColumn(two, typeof(string)));
575
576                         object[] obj = new object[3];
577                         // -- normal -----------------
578                         obj[0] = zero;
579                         obj[1] = one;
580                         obj[2] = two;
581                         // results:
582                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
583                         //   table.Rows[0].ItemArray.ItemArray[1] = "one"
584                         //   table.Rows[0].ItemArray.ItemArray[2] = "two"
585                         
586                         DataRow row = table.NewRow();
587                         
588                         try {
589                                 row.ItemArray = obj;
590                         }
591                         catch(Exception e1) {
592                                 Fail("DR1: Exception Caught: " + e1);
593                         }
594                         
595                         table.Rows.Add(row);
596
597                         // -- null ----------
598                         obj[1] = null;
599                         // results:
600                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
601                         //   table.Rows[1].ItemArray.ItemArray[1] = DBNull.Value
602                         //   table.Rows[1].ItemArray.ItemArray[2] = "two"
603
604                         row = table.NewRow();
605                         
606                         try {
607                                 row.ItemArray = obj;
608                         }
609                         catch(Exception e2) {
610                                 Fail("DR2: Exception Caught: " + e2);
611                         }
612                         
613                         table.Rows.Add(row);
614
615                         // -- DBNull.Value -------------
616                         obj[1] = DBNull.Value;
617                         // results:
618                         //   table.Rows[2].ItemArray.ItemArray[0] = "zero"
619                         //   table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
620                         //   table.Rows[2].ItemArray.ItemArray[2] = "two"
621
622                         row = table.NewRow();
623                         
624                         try {
625                                 row.ItemArray = obj;
626                         }
627                         catch(Exception e3) {
628                                 Fail("DR3: Exception Caught: " + e3);
629                         }
630                         
631                         table.Rows.Add(row);
632
633                         // -- object array smaller than number of columns -----
634                         string abc = "abc";
635                         string def = "def";
636                         obj = new object[2];
637                         obj[0] = abc;
638                         obj[1] = def;
639                         // results:
640                         //   table.Rows[3].ItemArray.ItemArray[0] = "abc"
641                         //   table.Rows[3].ItemArray.ItemArray[1] = "def"
642                         //   table.Rows[3].ItemArray.ItemArray[2] = DBNull.Value;
643                         
644                         row = table.NewRow();
645                         
646                         try {
647                                 row.ItemArray = obj;
648                         }
649                         catch(Exception e3) {
650                                 Fail("DR4: Exception Caught: " + e3);
651                         }
652                         
653                         table.Rows.Add(row);
654
655                         // -- normal -----------------
656                         ValueTest("DR5: normal value test", table, 0, 0, zero);
657                         ValueTest("DR6: normal value test", table, 0, 1, one);
658                         ValueTest("DR7: normal value test", table, 0, 2, two);
659
660                         // -- null ----------
661                         ValueTest("DR8: null value test", table, 1, 0, zero);
662                         ValueTest("DR9: null value test", table, 1, 1, DBNull.Value);
663                         ValueTest("DR10: null value test", table, 1, 2, two);
664
665                         // -- DBNull.Value -------------
666                         ValueTest("DR11: DBNull.Value value test", table, 2, 0, zero);
667                         ValueTest("DR12: DBNull.Value value test", table, 2, 1, DBNull.Value);
668                         ValueTest("DR13: DBNull.Value value test", table, 2, 2, two);
669
670                         // -- object array smaller than number of columns -----
671                         ValueTest("DR14: array smaller value test", table, 3, 0, abc);
672                         ValueTest("DR15: array smaller value test", table, 3, 1, def);
673                         ValueTest("DR16: array smaller value test", table, 3, 2, DBNull.Value);
674                 }
675         
676                 // test DefaultValue when setting ItemArray
677                 [Test]
678                 public void DefaultValueInItemArray () {                
679                         string zero = "zero";
680
681                         DataTable table = new DataTable();
682                         table.Columns.Add(new DataColumn("zero", typeof(string)));              
683                         
684                         DataColumn column = new DataColumn("num", typeof(int));
685                         column.DefaultValue = 15;
686                         table.Columns.Add(column);
687                         
688                         object[] obj = new object[2];
689                         // -- normal -----------------
690                         obj[0] = "zero";
691                         obj[1] = 8;
692                         // results:
693                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
694                         //   table.Rows[0].ItemArray.ItemArray[1] = 8
695                                                 
696                         DataRow row = table.NewRow();
697                         
698                         try {
699                                 row.ItemArray = obj;
700                         }
701                         catch(Exception e1) {
702                                 Fail("DR17: Exception Caught: " + e1);
703                         }
704                         
705                         table.Rows.Add(row);
706
707                         // -- null ----------
708                         obj[1] = null;
709                         // results:
710                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
711                         //   table.Rows[1].ItemArray.ItemArray[1] = 15
712                         
713                         row = table.NewRow();
714                         
715                         try {
716                                 row.ItemArray = obj;
717                         }
718                         catch(Exception e2) {
719                                 Fail("DR18: Exception Caught: " + e2);
720                         }
721                         
722                         table.Rows.Add(row);
723
724                         // -- DBNull.Value -------------
725                         obj[1] = DBNull.Value;
726                         // results:
727                         //   table.Rows[2].ItemArray.ItemArray[0] = "zero"
728                         //   table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
729                         //      even though internally, the v
730                         
731                         row = table.NewRow();
732                         
733                         try {
734                                 row.ItemArray = obj;
735                         }
736                         catch(Exception e3) {
737                                 Fail("DR19: Exception Caught: " + e3);
738                         }
739                         
740                         table.Rows.Add(row);
741
742                         // -- object array smaller than number of columns -----
743                         string abc = "abc";
744                         string def = "def";
745                         obj = new object[2];
746                         obj[0] = abc;
747                         // results:
748                         //   table.Rows[3].ItemArray.ItemArray[0] = "abc"
749                         //   table.Rows[3].ItemArray.ItemArray[1] = DBNull.Value
750                                                 
751                         row = table.NewRow();
752                         
753                         try {
754                                 row.ItemArray = obj;
755                         }
756                         catch(Exception e3) {
757                                 Fail("DR20: Exception Caught: " + e3);
758                         }
759                         
760                         table.Rows.Add(row);
761
762                         // -- normal -----------------
763                         ValueTest("DR20: normal value test", table, 0, 0, zero);
764                         ValueTest("DR21: normal value test", table, 0, 1, 8);
765                         
766                         // -- null ----------
767                         ValueTest("DR22: null value test", table, 1, 0, zero);
768                         ValueTest("DR23: null value test", table, 1, 1, 15);
769                         
770                         // -- DBNull.Value -------------
771                         ValueTest("DR24: DBNull.Value value test", table, 2, 0, zero);
772                         DBNullTest("DR25: DBNull.Value value test", table, 2, 1);
773                         
774                         // -- object array smaller than number of columns -----
775                         ValueTest("DR26: array smaller value test", table, 3, 0, abc);
776                         ValueTest("DR27: array smaller value test", table, 3, 1, 15);
777                 }
778
779                 // test AutoIncrement when setting ItemArray
780                 [Test]
781                 public void AutoIncrementInItemArray () {
782                         string zero = "zero";
783                         string num = "num";
784                         
785                         DataTable table = new DataTable();
786                         table.Columns.Add(new DataColumn(zero, typeof(string)));                
787                         
788                         DataColumn column = new DataColumn("num", typeof(int));
789                         column.AutoIncrement = true;
790                         table.Columns.Add(column);
791                         
792                         object[] obj = new object[2];
793                         // -- normal -----------------
794                         obj[0] = "zero";
795                         obj[1] = 8;
796                         // results:
797                         //   table.Rows[0].ItemArray.ItemArray[0] = "zero"
798                         //   table.Rows[0].ItemArray.ItemArray[1] = 8
799                                                 
800                         DataRow row = table.NewRow();
801                         
802                         try {
803                                 row.ItemArray = obj;
804                         }
805                         catch(Exception e1) {
806                                 Fail("DR28:  Exception Caught: " + e1);
807                         }
808                         
809                         table.Rows.Add(row);
810
811                         // -- null 1----------
812                         obj[1] = null;
813                         // results:
814                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
815                         //   table.Rows[1].ItemArray.ItemArray[1] = 9
816                         
817                         row = table.NewRow();
818                         
819                         try {
820                                 row.ItemArray = obj;
821                         }
822                         catch(Exception e2) {
823                                 Fail("DR29:  Exception Caught: " + e2);
824                         }
825                         
826                         table.Rows.Add(row);
827
828                         // -- null 2----------
829                         obj[1] = null;
830                         // results:
831                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
832                         //   table.Rows[1].ItemArray.ItemArray[1] = 10
833                         
834                         row = table.NewRow();
835                         
836                         try {
837                                 row.ItemArray = obj;
838                         }
839                         catch(Exception e2) {
840                                 Fail("DR30: Exception Caught: " + e2);
841                         }
842                         
843                         table.Rows.Add(row);
844
845                         // -- null 3----------
846                         obj[1] = null;
847                         // results:
848                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
849                         //   table.Rows[1].ItemArray.ItemArray[1] = 11
850                         
851                         row = table.NewRow();
852                         
853                         try {
854                                 row.ItemArray = obj;
855                         }
856                         catch(Exception e2) {
857                                 Fail("DR31: Exception Caught: " + e2);
858                         }
859                         
860                         table.Rows.Add(row);
861
862                         // -- DBNull.Value -------------
863                         obj[1] = DBNull.Value;
864                         // results:
865                         //   table.Rows[2].ItemArray.ItemArray[0] = "zero"
866                         //   table.Rows[2].ItemArray.ItemArray[1] = DBNull.Value
867                         //      even though internally, the AutoIncrement value
868                         //      is incremented
869                         
870                         row = table.NewRow();
871                         
872                         try {
873                                 row.ItemArray = obj;
874                         }
875                         catch(Exception e3) {
876                                 Fail("DR32: Exception Caught: " + e3);
877                         }
878                         
879                         table.Rows.Add(row);
880
881                         // -- null 4----------
882                         obj[1] = null;
883                         // results:
884                         //   table.Rows[1].ItemArray.ItemArray[0] = "zero"
885                         //   table.Rows[1].ItemArray.ItemArray[1] = 13
886                         
887                         row = table.NewRow();
888                         
889                         try {
890                                 row.ItemArray = obj;
891                         }
892                         catch(Exception e2) {
893                                 Fail("DR48: Exception Caught: " + e2);
894                         }
895                         
896                         table.Rows.Add(row);
897
898                         // -- object array smaller than number of columns -----
899                         string abc = "abc";
900                         string def = "def";
901                         obj = new object[2];
902                         obj[0] = abc;
903                         // results:
904                         //   table.Rows[3].ItemArray.ItemArray[0] = "abc"
905                         //   table.Rows[3].ItemArray.ItemArray[1] = 14
906                                                 
907                         row = table.NewRow();
908                         
909                         try {
910                                 row.ItemArray = obj;
911                         }
912                         catch(Exception e3) {
913                                 Fail("DR33: Exception Caught: " + e3);
914                         }
915                         
916                         table.Rows.Add(row);
917
918                         // -- normal -----------------
919                         ValueTest("DR34: normal value test", table, 0, 0, zero);
920                         ValueTest("DR35: normal value test", table, 0, 1, 8);
921                         
922                         // -- null 1----------
923                         ValueTest("DR36: null value test", table, 1, 0, zero);
924                         ValueTest("DR37: null value test", table, 1, 1, 9);
925
926                         // -- null 2----------
927                         ValueTest("DR38: null value test", table, 2, 0, zero);
928                         ValueTest("DR39: null value test", table, 2, 1, 10);
929
930                         // -- null 3----------
931                         ValueTest("DR40: null value test", table, 3, 0, zero);
932                         ValueTest("DR41: null value test", table, 3, 1, 11);
933
934                         // -- DBNull.Value -------------
935                         ValueTest("DR42: DBNull.Value value test", table, 4, 0, zero);
936                         ValueTest("DR43: DBNull.Value value test", table, 4, 1, DBNull.Value);
937
938                         // -- null 4----------
939                         ValueTest("DR44: null value test", table, 5, 0, zero);
940                         ValueTest("DR45: null value test", table, 5, 1, 13);
941
942                         // -- object array smaller than number of columns -----
943                         ValueTest("DR46: array smaller value test", table, 6, 0, abc);
944                         ValueTest("DR47: array smaller value test", table, 6, 1, 14);
945                 }
946
947                 [Test]
948                 public void AutoIncrementColumnIntegrity ()
949                 {
950                         // AutoIncrement-column shouldn't raise index out of range
951                         // exception because of size mismatch of internal itemarray.
952                         DataTable dt = new DataTable ();
953                         dt.Columns.Add ("foo");
954                         dt.Rows.Add (new object [] {"value"});
955                         DataColumn col = new DataColumn ("bar");
956                         col.AutoIncrement = true;
957                         dt.Columns.Add (col);
958                         dt.Rows [0] [0] = "test";
959                 }
960
961                 [Test]
962                 public void EnforceConstraint ()
963                 {
964                          int id = 100;
965                         // Setup stuff
966                         DataSet ds = new DataSet();
967                         DataTable parent = ds.Tables.Add("parent");
968                         parent.Columns.Add("id", typeof(int));
969                         DataTable child = ds.Tables.Add("child");
970                         child.Columns.Add("idref", typeof(int));
971                         Constraint uniqueId = null;
972                         parent.Constraints.Add(uniqueId = new UniqueConstraint("uniqueId",
973                                       new DataColumn[] {parent.Columns["id"]}, true));
974                         ForeignKeyConstraint fkc = new ForeignKeyConstraint("ParentChildConstraint",                                      new DataColumn[] { parent.Columns["id"] },
975                                       new DataColumn[] { child.Columns["idref"]});
976         
977                         child.Constraints.Add(fkc);
978         
979                         DataRelation relateParentChild = new DataRelation("relateParentChild",
980                                              new DataColumn[] {parent.Columns["id"] },
981                                              new DataColumn[] {child.Columns["idref"] },
982                                              false);
983                         ds.Relations.Add(relateParentChild);
984         
985                         ds.EnforceConstraints = false;
986                         DataRow parentRow = parent.Rows.Add(new object[] { id });
987                         DataRow childRow = child.Rows.Add(new object[] { id });
988                         if (parentRow == childRow.GetParentRow(relateParentChild)) {
989                             foreach(DataColumn dc in parent.Columns)
990                                 AssertEquals(100,parentRow[dc]);
991                             
992                         }
993                                         
994         
995                 }
996
997                 [Test]
998                 [ExpectedException (typeof (RowNotInTableException))]
999                 public void DetachedRowItemException ()
1000                 {
1001                         DataTable dt = new DataTable ("table");
1002                         dt.Columns.Add ("col");
1003                         dt.Rows.Add ((new object [] {"val"}));
1004
1005                         DataRow dr = dt.NewRow ();
1006                         AssertEquals (DataRowState.Detached, dr.RowState);
1007                         dr.CancelEdit ();
1008                         AssertEquals (DataRowState.Detached, dr.RowState);
1009                         object o = dr ["col"];
1010                 }
1011         }
1012 }