Fix spelling
[mono.git] / mcs / class / System.Data / Test / System.Data / DataRowTest.cs
index a82637dc9cde48d581a0fdbc2b1934540fdb94b1..0a6a601b9afac3ee9ed3ff9c4dc7cd5c78b4337a 100644 (file)
@@ -121,15 +121,8 @@ namespace MonoTests.System.Data
                         }
                 }
 
-               [Test]
-                public void RowEditTest()
-                {
-                        DeleteRowTest ();
-                        EditModeTest ();
-                        ParentRowTest ();
-                }
-                                                                                                    
-                private void DeleteRowTest ()
+                [Test]
+                public void DeleteRowTest ()
                 {
                         DataRow newRow;
                                                                                                     
@@ -164,7 +157,7 @@ namespace MonoTests.System.Data
                         table.AcceptChanges ();
                         AssertEquals ("#A06", "Name 1", (table.Rows [0]) [1]);
                         try  {
-                                Console.WriteLine (rc [2]);
+                                object o = rc [2];
                                 Fail ("#A07");
                         }
                         catch (Exception e) {
@@ -172,7 +165,8 @@ namespace MonoTests.System.Data
                         }
                 }
 
-                private void EditModeTest ()
+                [Test]
+                public void EditModeTest ()
                 {
                         try {
                                 //Clear all existing values from table
@@ -194,8 +188,8 @@ namespace MonoTests.System.Data
                                 AssertEquals ("#A12", "LName", row [2, DataRowVersion.Current]);
                                                                                                     
                                 try {
-                                        Console.WriteLine (row [1, DataRowVersion.Original]);
-                                        Console.WriteLine (row [1, DataRowVersion.Proposed]);
+                                      object o = row [1, DataRowVersion.Original];
+                                      o = row [1, DataRowVersion.Proposed];
                                         Fail ("#A13");
                                 }
                                 catch (Exception e) {
@@ -215,7 +209,7 @@ namespace MonoTests.System.Data
                                 AssertEquals ("#A18", "LName", row [2, DataRowVersion.Current]);
                                 
                                try {
-                                        Console.WriteLine (row [1, DataRowVersion.Proposed]);
+                                      object o = row [1, DataRowVersion.Proposed];
                                         Fail ("#A19");
                                 }
                                 catch (Exception e) {
@@ -256,7 +250,7 @@ namespace MonoTests.System.Data
                                 AssertEquals ("#A33", "My LName", row [2, DataRowVersion.Current]);
                                                                                                     
                                 try {
-                                        Console.WriteLine (row [1, DataRowVersion.Proposed]);
+                                      object o = row [1, DataRowVersion.Proposed];
                                         Fail ("#A34");
                                 }
                                 catch (Exception e) {
@@ -279,7 +273,7 @@ namespace MonoTests.System.Data
                                                                                                     
                                                                                                     
                                 try {
-                                        Console.WriteLine (row [1, DataRowVersion.Proposed]);
+                                      object o = row [1, DataRowVersion.Proposed];
                                         Fail ("#A42");
                                 }
                                 catch (Exception e) {
@@ -292,10 +286,12 @@ namespace MonoTests.System.Data
                                                                                                     
                         }
                         catch (Exception e){
-                                Console.WriteLine (e + "" + e.StackTrace);
+//                              Console.WriteLine (e + "" + e.StackTrace);
                         }
                 }                                                                                                     
-               private void ParentRowTest (){
+                [Test]
+                public void ParentRowTest ()
+                {
 
                         //Clear all existing values from table
                         for (int i = 0; i < table.Rows.Count; i++) {
@@ -334,9 +330,214 @@ namespace MonoTests.System.Data
                                                                                                     
                         rowC.SetParentRow (table.Rows [0], dr);
                                                                                                     
-                        AssertEquals ("#A44", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
+                        AssertEquals ("#PRT-01", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
+                       AssertEquals ("#PRT-02", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
+
+                        ds.Relations.Clear ();
+                        dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
+                        ds.Relations.Add (dr);
+                        rowC.SetParentRow (table.Rows [0], dr);
+                        AssertEquals ("#PRT-03", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
+                       AssertEquals ("#PRT-04", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
+
+                        ds.Relations.Clear ();
+                        dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"], false);
+                        tableC.ParentRelations.Add (dr);
+                        rowC.SetParentRow (table.Rows [0]);
+                        AssertEquals ("#PRT-05", table.Rows [0], (tableC.Rows [0]).GetParentRow (dr));
+                        AssertEquals ("#PRT-06", tableC.Rows [0], (table.Rows [0]).GetChildRows (dr) [0]);
+                                               
                 } 
 
+                [Test]
+                public void ParentRowTest2 ()
+                {
+                        DataSet ds = new DataSet ();
+                        DataTable tableP = ds.Tables.Add ("Parent");
+                        DataTable tableC = ds.Tables.Add ("Child");
+                        DataColumn colC;
+                        DataRow rowC;
+                                                                                                    
+                        colC = new DataColumn ();
+                        colC.DataType = Type.GetType ("System.Int32");
+                        colC.ColumnName = "Id";
+                        colC.AutoIncrement = true;
+                        tableP.Columns.Add (colC);
+                        
+                        colC = new DataColumn ();
+                        colC.DataType = Type.GetType ("System.Int32");
+                        colC.ColumnName = "Id";
+                        tableC.Columns.Add (colC);
+                        row = tableP.Rows.Add (new object [0]);
+                        rowC = tableC.NewRow ();
+                        ds.EnforceConstraints = false;
+                        DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
+                        ds.Relations.Add (dr);
+
+                        rowC.SetParentRow (row, dr);
+                        DataRow [] rows = rowC.GetParentRows (dr);
+
+                        AssertEquals ("#A49", 1, rows.Length);
+                        AssertEquals ("#A50", tableP.Rows [0], rows [0]);
+
+                        try{
+                                rows = row.GetParentRows (dr);
+                        }catch(InvalidConstraintException){
+                                //Test done
+                                return ;
+                        }catch(Exception e){
+                                Fail("#A51, InvalidConstraintException expected, got : " + e);
+                        }
+                        
+                        Fail("#A52, InvalidConstraintException expected but got none.");
+                }
+
+                [Test]
+                public void ChildRowTest ()
+                {
+
+                        //Clear all existing values from table
+                        for (int i = 0; i < table.Rows.Count; i++) {
+                                        table.Rows [i].Delete ();
+                        }
+                        table.AcceptChanges ();
+                        row = table.NewRow ();
+                        row ["FName"] = "My FName";
+                        row ["Id"] = 0;
+                        table.Rows.Add (row);
+                                                                                                    
+                        DataTable tableC = new DataTable ("Child");
+                        DataColumn colC;
+                        DataRow rowC;
+
+                        colC = new DataColumn ();
+                        colC.DataType = Type.GetType ("System.Int32");
+                        colC.ColumnName = "Id";
+                        colC.AutoIncrement = true;
+                        tableC.Columns.Add (colC);
+                                                                                                    
+                        colC = new DataColumn ();
+                        colC.DataType = Type.GetType ("System.String");
+                        colC.ColumnName = "Name";
+                        tableC.Columns.Add (colC);
+                                                                                                    
+                        rowC = tableC.NewRow ();
+                        rowC ["Name"] = "My FName";
+                        tableC.Rows.Add (rowC);
+                        DataSet ds = new DataSet ();
+                        ds.Tables.Add (table);
+                        ds.Tables.Add (tableC);
+                        DataRelation dr = new DataRelation ("PO", table.Columns ["Id"], tableC.Columns ["Id"]);
+                        ds.Relations.Add (dr);
+                                                                                                    
+                        rowC.SetParentRow (table.Rows [0], dr);
+                                                                                                    
+                        DataRow [] rows = (table.Rows [0]).GetChildRows (dr);
+
+                        AssertEquals ("#A45", 1, rows.Length);
+                        AssertEquals ("#A46", tableC.Rows [0], rows [0]);
+                        
+                } 
+
+                [Test]
+                public void ChildRowTest2 ()
+                {
+                        DataSet ds = new DataSet ();
+                        DataTable tableP = ds.Tables.Add ("Parent");
+                        DataTable tableC = ds.Tables.Add ("Child");
+                        DataColumn colC;
+                        DataRow rowC;
+                                                                                                    
+                        colC = new DataColumn ();
+                        colC.DataType = Type.GetType ("System.Int32");
+                        colC.ColumnName = "Id";
+                        colC.AutoIncrement = true;
+                        tableP.Columns.Add (colC);
+                        
+                        colC = new DataColumn ();
+                        colC.DataType = Type.GetType ("System.Int32");
+                        colC.ColumnName = "Id";
+                        tableC.Columns.Add (colC);
+
+                        row = tableP.NewRow ();
+                        rowC = tableC.Rows.Add (new object [0]);
+
+                        ds.EnforceConstraints = false;
+                        DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
+                        ds.Relations.Add (dr);
+
+                        rowC.SetParentRow (row, dr);
+                        DataRow [] rows = row.GetChildRows (dr);
+                        
+                        AssertEquals ("#A47", 1, rows.Length);
+                        AssertEquals ("#A48", tableC.Rows [0], rows [0]);
+
+                        try{
+                            rows = rowC.GetChildRows (dr);
+                        }catch(InvalidConstraintException){
+                            //Test done
+                            return ;
+                        }catch(Exception e){
+                            Fail("#A53, InvalidConstraintException expected, got : " + e);
+                        }
+                        
+                        Fail("#A54, InvalidConstraintException expected but got none.");
+                }
+
+                [Test]
+                public void ParentChildRowVersionTest ()
+                {
+                        DataSet ds = new DataSet ();
+                        DataTable tableP = ds.Tables.Add ("Parent");
+                        DataTable tableC = ds.Tables.Add ("Child");
+                        DataColumn colC;
+                        DataRow rowC;
+                                                                                                    
+                        colC = new DataColumn ();
+                        colC.DataType = Type.GetType ("System.Int32");
+                        colC.ColumnName = "Id";
+                        colC.AutoIncrement = true;
+                        tableP.Columns.Add (colC);
+                        
+                        colC = new DataColumn ();
+                        colC.DataType = Type.GetType ("System.Int32");
+                        colC.ColumnName = "Id";
+                        tableC.Columns.Add (colC);
+
+                        row = tableP.NewRow ();
+                        rowC = tableC.Rows.Add (new object [0]);
+
+                        ds.EnforceConstraints = false;
+                        DataRelation dr = new DataRelation ("PO", tableP.Columns ["Id"], tableC.Columns ["Id"]);
+                        ds.Relations.Add (dr);
+
+                        rowC.SetParentRow (row, dr);
+                        DataRow [] rows;
+
+                        try {
+                            rows = row.GetChildRows (dr, DataRowVersion.Current);
+                        }catch (VersionNotFoundException v) {
+                            //Check for GetParentRows
+                            try{
+                                //Child Row should be in Detached state for the next test
+                                rowC = tableC.NewRow();
+
+                                rows = rowC.GetParentRows (dr, DataRowVersion.Current);
+                            }catch (VersionNotFoundException v2) {
+                                //Test Done
+                                return ;
+                            }catch (Exception e){
+                                Fail ("#A55, VersionNotFoundException expected, got : " + e);
+                            }
+                            Fail ("#A56, VersionNotFoundException expected but got none.");
+                        }catch (Exception e){
+                            Fail ("#A57, VersionNotFoundException expected, got : " + e);
+                        }
+                        
+                        Fail("#A58, VersionNotFoundException expected but got none.");
+                }
 
                // tests item at row, column in table to be DBNull.Value
                private void DBNullTest (string message, DataTable dt, int row, int column) 
@@ -748,13 +949,64 @@ namespace MonoTests.System.Data
                {
                        // AutoIncrement-column shouldn't raise index out of range
                        // exception because of size mismatch of internal itemarray.
-                       DataTable dt = new DataTable ();\r
-                       dt.Columns.Add ("foo");\r
-                       dt.Rows.Add (new object [] {"value"});\r
-                       DataColumn col = new DataColumn ("bar");\r
-                       col.AutoIncrement = true;\r
-                       dt.Columns.Add (col);\r
+                       DataTable dt = new DataTable ();
+                       dt.Columns.Add ("foo");
+                       dt.Rows.Add (new object [] {"value"});
+                       DataColumn col = new DataColumn ("bar");
+                       col.AutoIncrement = true;
+                       dt.Columns.Add (col);
                        dt.Rows [0] [0] = "test";
                }
+
+               [Test]
+               public void EnforceConstraint ()
+               {
+                        int id = 100;
+                       // Setup stuff
+                       DataSet ds = new DataSet();
+                       DataTable parent = ds.Tables.Add("parent");
+                       parent.Columns.Add("id", typeof(int));
+                       DataTable child = ds.Tables.Add("child");
+                       child.Columns.Add("idref", typeof(int));
+                       Constraint uniqueId = null;
+                       parent.Constraints.Add(uniqueId = new UniqueConstraint("uniqueId",
+                                      new DataColumn[] {parent.Columns["id"]}, true));
+                       ForeignKeyConstraint fkc = new ForeignKeyConstraint("ParentChildConstraint",                                      new DataColumn[] { parent.Columns["id"] },
+                                     new DataColumn[] { child.Columns["idref"]});
+        
+                       child.Constraints.Add(fkc);
+        
+                       DataRelation relateParentChild = new DataRelation("relateParentChild",
+                                             new DataColumn[] {parent.Columns["id"] },
+                                             new DataColumn[] {child.Columns["idref"] },
+                                             false);
+                       ds.Relations.Add(relateParentChild);
+        
+                       ds.EnforceConstraints = false;
+                       DataRow parentRow = parent.Rows.Add(new object[] { id });
+                       DataRow childRow = child.Rows.Add(new object[] { id });
+                       if (parentRow == childRow.GetParentRow(relateParentChild)) {
+                           foreach(DataColumn dc in parent.Columns)
+                               AssertEquals(100,parentRow[dc]);
+                           
+                       }
+                                       
+        
+               }
+
+               [Test]
+               [ExpectedException (typeof (RowNotInTableException))]
+               public void DetachedRowItemException ()
+               {
+                       DataTable dt = new DataTable ("table");
+                       dt.Columns.Add ("col");
+                       dt.Rows.Add ((new object [] {"val"}));
+
+                       DataRow dr = dt.NewRow ();
+                       AssertEquals (DataRowState.Detached, dr.RowState);
+                       dr.CancelEdit ();
+                       AssertEquals (DataRowState.Detached, dr.RowState);
+                       object o = dr ["col"];
+               }
        }
 }