Fix spelling
[mono.git] / mcs / class / System.Data / Test / System.Data / DataTableTest.cs
index 748275977aab3104c21cc1a70912b24de6c62c59..c432b539409f1a38c5992e25acecd6cf4e91f1dd 100644 (file)
@@ -34,6 +34,7 @@
 using NUnit.Framework;
 using System;
 using System.Data;
+using System.Data.SqlTypes;
 using System.Globalization;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
@@ -117,6 +118,11 @@ namespace MonoTests.System.Data
                        Row [0] = "Teresa";
                        Row [1] = "Mack";
                        Mom.Rows.Add (Row);
+
+                       Row = Mom.NewRow ();
+                       Row [0] = "'Jhon O'' Collenal'";
+                       Row [1] = "Pack";
+                       Mom.Rows.Add (Row);
                        
                        Row = Child.NewRow ();
                        Row [0] = "Nick";
@@ -147,9 +153,18 @@ namespace MonoTests.System.Data
                        Row [0] = "Mack";
                        Row [1] = 99;
                        Child.Rows.Add (Row);
+
+                       Row = Child.NewRow ();
+                       Row [0] = "Pack";
+                       Row [1] = 66;
+                       Child.Rows.Add (Row);
                        
                        DataRow [] Rows = Mom.Select ("Name = 'Teresa'");
                        AssertEquals ("test#01", 2, Rows.Length);
+
+                       // test with apos escaped
+                       Rows = Mom.Select ("Name = '''Jhon O'''' Collenal'''");
+                       AssertEquals ("test#01.1", 1, Rows.Length);
                        
                        Rows = Mom.Select ("Name = 'Teresa' and ChildName = 'Nick'");
                        AssertEquals ("test#02", 0, Rows.Length);
@@ -161,7 +176,7 @@ namespace MonoTests.System.Data
                        AssertEquals ("test#04", "Mack", Rows [0] [1]);
                        
                        Rows = Mom.Select ("Name = 'Teresa' or ChildName <> 'Jack'");
-                       AssertEquals ("test#05", 5, Rows.Length);
+                       AssertEquals ("test#05", 6, Rows.Length);
                        
                        Rows = Child.Select ("age = 20 - 1");
                        AssertEquals ("test#06", 1, Rows.Length);
@@ -170,7 +185,7 @@ namespace MonoTests.System.Data
                        AssertEquals ("test#07", 3, Rows.Length);
                        
                        Rows = Child.Select ("age >= 20");
-                       AssertEquals ("test#08", 3, Rows.Length);
+                       AssertEquals ("test#08", 4, Rows.Length);
                        
                        Rows = Child.Select ("age >= 20 and name = 'Mack' or name = 'Nick'");
                        AssertEquals ("test#09", 2, Rows.Length);
@@ -180,7 +195,7 @@ namespace MonoTests.System.Data
                        AssertEquals ("test#11", "Mack", Rows [0] [0]);
                        
                        Rows = Child.Select ("not (Name = 'Jack')");
-                       AssertEquals ("test#12", 5, Rows.Length);
+                       AssertEquals ("test#12", 6, Rows.Length);
                 }
                 
                [Test]
@@ -705,6 +720,19 @@ namespace MonoTests.System.Data
                        
                }
 
+               [Test]
+               public void SelectRowState()
+               {
+                       DataTable d = new DataTable();
+                       d.Columns.Add (new DataColumn ("aaa"));
+                       DataRow [] rows = d.Select (null, null, DataViewRowState.Deleted);
+                       AssertEquals(0, rows.Length);
+                       d.Rows.Add (new object [] {"bbb"});
+                       d.Rows.Add (new object [] {"bbb"});
+                       rows = d.Select (null, null, DataViewRowState.Deleted);
+                       AssertEquals(0, rows.Length);
+               }
+
                [Test]
                public void ToStringTest()
                {
@@ -1076,41 +1104,72 @@ namespace MonoTests.System.Data
                        AssertEquals ("#A03", "Abc" , (table.Rows [0]) ["Name"]);
                        AssertEquals ("#A04", 2, table.Rows.Count);
                }
-               [Test]
-               public void ImportRow ()
-               {
-                       DataTable table = new DataTable ();
-                       DataColumn col = new DataColumn ();
-                       col.ColumnName = "Id";
-                       col.DataType = Type.GetType ("System.Int32");
-                       table.Columns.Add (col);
+               
+                [Test]
+                public void ImportRowTest ()
+                {
+                        // build source table
+                        DataTable src = new DataTable ();
+                        src.Columns.Add ("id", typeof (int));
+                        src.Columns.Add ("name", typeof (string));
 
-                       col = new DataColumn ();
-                       col.ColumnName = "Name";
-                       col.DataType = Type.GetType ("System.String");
-                       table.Columns.Add (col);
-                        
-                       DataRow row = table.NewRow ();
-                       row ["Id"] = 147;
-                       row ["name"] = "Abc";
-                       table.Rows.Add (row);
-                       table.AcceptChanges ();
-                                                                                                     
-                       row = table.NewRow ();
-                       row ["Id"] = 47;
-                       row ["name"] = "Efg";
-                       table.Rows.Add (row);
+                        src.PrimaryKey = new DataColumn [] {src.Columns [0]} ;
 
-                       (table.Rows [0]) ["Name"] = "AaBbCc";
-               
-                       table.ImportRow (table.Rows [0]);
-                       table.ImportRow (table.Rows [1]);
+                        src.Rows.Add (new object [] { 1, "mono 1" });
+                        src.Rows.Add (new object [] { 2, "mono 2" });
+                        src.Rows.Add (new object [] { 3, "mono 3" });
+                        src.AcceptChanges ();
 
-                       AssertEquals ("#A01", 147, table.Rows [2]["Id"]);
-                       AssertEquals ("#A02", 47, table.Rows [3]["Id"]);
-                       AssertEquals ("#A03", DataRowState.Modified, table.Rows [2].RowState);
-                       AssertEquals ("#A04", DataRowState.Added, table.Rows [3].RowState);
-               }
+                        src.Rows [0] [1] = "mono changed 1";  // modify 1st row
+                        src.Rows [1].Delete ();              // delete 2nd row
+                        // 3rd row is unchanged
+                        src.Rows.Add (new object [] { 4, "mono 4" }); // add 4th row
+
+                        // build target table
+                        DataTable target = new DataTable ();
+                        target.Columns.Add ("id", typeof (int));
+                        target.Columns.Add ("name", typeof (string));
+
+                        target.PrimaryKey = new DataColumn [] {target.Columns [0]} ;
+
+                        // import all rows
+                        target.ImportRow (src.Rows [0]);     // import 1st row
+                        target.ImportRow (src.Rows [1]);     // import 2nd row
+                        target.ImportRow (src.Rows [2]);     // import 3rd row
+                        target.ImportRow (src.Rows [3]);     // import 4th row
+
+                        try {
+                                target.ImportRow (src.Rows [2]); // import 3rd row again
+                                Fail ("#AA1 Should have thrown exception violativ PK");
+                        } catch (ConstraintException e) {}
+
+                        // check row states
+                        AssertEquals ("#A1", src.Rows [0].RowState, target.Rows [0].RowState);
+                        AssertEquals ("#A2", src.Rows [1].RowState, target.Rows [1].RowState);
+                        AssertEquals ("#A3", src.Rows [2].RowState, target.Rows [2].RowState);
+                        AssertEquals ("#A4", src.Rows [3].RowState, target.Rows [3].RowState);
+
+                        // check for modified row (1st row)
+                        AssertEquals ("#B1", (string) src.Rows [0] [1], (string) target.Rows [0] [1]);
+                        AssertEquals ("#B2", (string) src.Rows [0] [1, DataRowVersion.Default], (string) target.Rows [0] [1, DataRowVersion.Default]);
+                        AssertEquals ("#B3", (string) src.Rows [0] [1, DataRowVersion.Original], (string) target.Rows [0] [1, DataRowVersion.Original]);
+                        AssertEquals ("#B4", (string) src.Rows [0] [1, DataRowVersion.Current], (string) target.Rows [0] [1, DataRowVersion.Current]);
+                        AssertEquals ("#B5", false, target.Rows [0].HasVersion(DataRowVersion.Proposed));
+
+                        // check for deleted row (2nd row)
+                        AssertEquals ("#C1", (string) src.Rows [1] [1, DataRowVersion.Original], (string) target.Rows [1] [1, DataRowVersion.Original]);
+
+                        // check for unchanged row (3rd row)
+                        AssertEquals ("#D1", (string) src.Rows [2] [1], (string) target.Rows [2] [1]);
+                        AssertEquals ("#D2", (string) src.Rows [2] [1, DataRowVersion.Default], (string) target.Rows [2] [1, DataRowVersion.Default]);
+                        AssertEquals ("#D3", (string) src.Rows [2] [1, DataRowVersion.Original], (string) target.Rows [2] [1, DataRowVersion.Original]);
+                        AssertEquals ("#D4", (string) src.Rows [2] [1, DataRowVersion.Current], (string) target.Rows [2] [1, DataRowVersion.Current]);
+
+                        // check for newly added row (4th row)
+                        AssertEquals ("#E1", (string) src.Rows [3] [1], (string) target.Rows [3] [1]);
+                        AssertEquals ("#E2", (string) src.Rows [3] [1, DataRowVersion.Default], (string) target.Rows [3] [1, DataRowVersion.Default]);
+                        AssertEquals ("#E3", (string) src.Rows [3] [1, DataRowVersion.Current], (string) target.Rows [3] [1, DataRowVersion.Current]);
+                }
 
                 [Test]
                public void ImportRowDetachedTest ()
@@ -1133,7 +1192,52 @@ namespace MonoTests.System.Data
                        row ["name"] = "Abc";
 
                         // keep silent as ms.net ;-), though this is not useful.
-                        table.ImportRow (row); 
+                        table.ImportRow (row);
+
+                       //if RowState is detached, then dont import the row.
+                       AssertEquals ("#1", 0, table.Rows.Count);
+               }
+
+               [Test]
+               [Category ("NotWorking")]
+               public void ImportRowDeletedTest ()
+               {
+                       DataTable table = new DataTable ();
+                       table.Columns.Add ("col", typeof (int));
+                       table.Columns.Add ("col1", typeof (int));
+
+                       DataRow row = table.Rows.Add (new object[] {1,2});
+                       table.PrimaryKey = new DataColumn[] {table.Columns[0]};
+                       table.AcceptChanges ();
+
+                       // If row is in Deleted state, then ImportRow loads the
+                       // row.
+                       row.Delete ();
+                       table.ImportRow (row);
+                       AssertEquals ("#1", 2, table.Rows.Count);
+
+                       // Both the deleted rows shud be now gone
+                       table.AcceptChanges ();
+                       AssertEquals ("#2", 0, table.Rows.Count);
+
+                       //just add another row
+                       row = table.Rows.Add (new object[] {1,2});
+                       // no exception shud be thrown
+                       table.AcceptChanges ();
+
+                       // If row is in Deleted state, then ImportRow loads the
+                       // row and validate only on RejectChanges
+                       row.Delete ();
+                       table.ImportRow (row);
+                       AssertEquals ("#3", 2, table.Rows.Count);
+                       AssertEquals ("#4", DataRowState.Deleted, table.Rows[1].RowState);
+
+                       //FIXME : Currenty this fails.
+                       try {
+                               table.RejectChanges ();
+                               Fail ("#5");
+                       } catch (ConstraintException e) {
+                       }
                }
 
                [Test]
@@ -1212,8 +1316,15 @@ namespace MonoTests.System.Data
                         table.Rows.Add (new object [] { 4, "mono 4" });
 
                         table.AcceptChanges ();
+#if NET_2_0
+                        _tableClearedEventFired = false;
+                        table.TableCleared += new DataTableClearEventHandler (OnTableCleared);
+#endif // NET_2_0
                         
                         table.Clear ();
+#if NET_2_0
+                        AssertEquals ("#0 should have fired cleared event", true, _tableClearedEventFired);
+#endif // NET_2_0
                         
                         DataRow r = table.Rows.Find (1);
                         AssertEquals ("#1 should have cleared", true, r == null);
@@ -1222,6 +1333,13 @@ namespace MonoTests.System.Data
                         table.Rows.Add (new object [] { 2, "mono 2" });
                         AssertEquals ("#2 should add row", 1, table.Rows.Count);
                 }
+#if NET_2_0
+                private bool _tableClearedEventFired = false;
+                private void OnTableCleared (object src, DataTableClearEventArgs args)
+                {
+                        _tableClearedEventFired = true;
+                }
+#endif // NET_2_0
                 
 
                [Test]
@@ -1275,7 +1393,6 @@ namespace MonoTests.System.Data
 
                [Test]
                [ExpectedException (typeof (DataException))]
-               [NUnit.Framework.Category ("NotWorking")]
                public void SetPrimaryKeyAssertsNonNull ()
                {
                        DataTable dt = new DataTable ("table");
@@ -1301,6 +1418,26 @@ namespace MonoTests.System.Data
                        dt.Rows.Add (new object [] {DBNull.Value, 3});
                }
 
+               [Test]
+               public void PrimaryKey_CheckSetsAllowDBNull ()
+               {
+                       DataTable table = new DataTable ();
+                       DataColumn col1 = table.Columns.Add ("col1", typeof (int));
+                       DataColumn col2 = table.Columns.Add ("col2", typeof (int));
+       
+                       AssertEquals ("#1" , true, col1.AllowDBNull);
+                       AssertEquals ("#2" , true, col2.AllowDBNull);
+                       AssertEquals ("#3" , false, col2.Unique);
+                       AssertEquals ("#4" , false, col2.Unique);
+
+                       table.PrimaryKey = new DataColumn[] {col1,col2};
+                       AssertEquals ("#5" , false, col1.AllowDBNull);
+                       AssertEquals ("#6" , false, col2.AllowDBNull);
+                       // LAMESPEC or bug ?? 
+                       AssertEquals ("#7" , false, col1.Unique);
+                       AssertEquals ("#8" , false, col2.Unique);
+               }
+
                void RowChanging (object o, DataRowChangeEventArgs e)
                {
                        AssertEquals ("changing.Action", rowChangingExpectedAction, e.Action);
@@ -1372,6 +1509,14 @@ namespace MonoTests.System.Data
                         }
                 }
 
+                               [Test]
+                               public void ColumnObjectTypeTest() {
+                                       DataTable dt = new DataTable();\r
+                                       dt.Columns.Add("Series Label", typeof(SqlInt32));\r
+                                       dt.Rows.Add(new object[] {"sss"});
+                                       AssertEquals(1, dt.Rows.Count);
+                               }
+
                 public void OnRowChanging (object src, DataRowChangeEventArgs args)
                 {
                         rowActionChanging = args.Action;
@@ -1397,5 +1542,46 @@ namespace MonoTests.System.Data
                                                                                                     
          }
 
-       
+       [Serializable]
+       [TestFixture]
+       public class AppDomainsAndFormatInfo
+       {
+               public void Remote ()
+               {
+                       int n = (int) Convert.ChangeType ("5", typeof (int));
+                       Assertion.AssertEquals ("n", 5, n);
+               }
+#if !TARGET_JVM
+               [Test]
+               public void NFIFromBug55978 ()
+               {
+                       AppDomain domain = AppDomain.CreateDomain ("testdomain");
+                       AppDomainsAndFormatInfo test = new AppDomainsAndFormatInfo ();
+                       test.Remote ();
+                       domain.DoCallBack (new CrossAppDomainDelegate (test.Remote));
+                       AppDomain.Unload (domain);
+               }
+#endif
+
+               [Test]
+               public void Bug55978 ()
+               {
+                       DataTable dt = new DataTable ();
+                       dt.Columns.Add ("StartDate", typeof (DateTime));
+        
+                       DataRow dr;
+                       DateTime date = DateTime.Now;
+        
+                       for (int i = 0; i < 10; i++) {
+                               dr = dt.NewRow ();
+                               dr ["StartDate"] = date.AddDays (i);
+                               dt.Rows.Add (dr);
+                       }
+        
+                       DataView dv = dt.DefaultView;
+                       dv.RowFilter = "StartDate >= '" + DateTime.Now.AddDays (2) + "' and StartDate <= '" + DateTime.Now.AddDays (4) + "'";
+                       Assertion.AssertEquals ("Table", 10, dt.Rows.Count);
+                       Assertion.AssertEquals ("View", 2, dv.Count);
+               }
+       }
 }