Merge pull request #347 from JamesB7/master
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DbDataAdapterTest.cs
index 04a07c4179b67eccf7b966f0da7f39e7e611235c..96dd9b84aaab7711c6fdff93227a4fdcdcfe9c64 100644 (file)
 using System;
 using System.Data;
 using System.Data.Common;
+using System.Data.SqlClient;
+
+/*--For Bug 853 Test Begin--*/
+using Mono.Data.Sqlite;
+/*--For Bug 853 Test End--*/
 
 using NUnit.Framework;
 
@@ -82,6 +87,141 @@ namespace MonoTests.System.Data.Common
                                Assert.IsNotNull (ex.Message, "#4");
                        }
                }
+
+               [Test]
+               public void AddToBatch ()
+               {
+                       MyAdapter da = new MyAdapter ();
+                       try {
+                               da.AddToBatch (new SqlCommand ());
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException ex) {
+                               Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+               }
+
+               [Test]
+               public void ClearBatch ()
+               {
+                       MyAdapter da = new MyAdapter ();
+                       try {
+                               da.ClearBatch ();
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException ex) {
+                               Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+               }
+
+               [Test]
+               public void ExecuteBatch ()
+               {
+                       MyAdapter da = new MyAdapter ();
+                       try {
+                               da.ExecuteBatch ();
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException ex) {
+                               Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+               }
+
+               [Test]
+               public void GetBatchedParameter ()
+               {
+                       MyAdapter da = new MyAdapter ();
+                       try {
+                               da.GetBatchedParameter (1, 1);
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException ex) {
+                               Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+               }
+
+               [Test]
+               public void GetBatchedRecordsAffected ()
+               {
+                       MyAdapter da = new MyAdapter ();
+                       int recordsAffected = 0;
+                       Exception error = null;
+
+                       Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
+                               out recordsAffected, out error), "#1");
+                       Assert.AreEqual (1, recordsAffected, "#2");
+                       Assert.IsNull (error, "#3");
+               }
+
+               [Test]
+               public void InitializeBatching ()
+               {
+                       MyAdapter da = new MyAdapter ();
+                       try {
+                               da.InitializeBatching ();
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException ex) {
+                               Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+               }
+
+               [Test]
+               public void TerminateBatching ()
+               {
+                       MyAdapter da = new MyAdapter ();
+                       try {
+                               da.TerminateBatching ();
+                               Assert.Fail ("#1");
+                       } catch (NotSupportedException ex) {
+                               Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
+                               Assert.IsNull (ex.InnerException, "#3");
+                               Assert.IsNotNull (ex.Message, "#4");
+                       }
+               }
+               
+               [Test]
+               [Category ("NotWorking")] // Requires newer sqlite than is on wrench
+               public void XimarinBugzillaBug853Test()
+                {
+                        const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
+                        SqliteConnection dbConnection = new SqliteConnection(connectionString);
+                        dbConnection.Open();
+                       SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
+                       ClearTableEntry.ExecuteNonQuery();
+
+                        SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
+                        SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
+                       sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
+                        sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
+                       
+                        DataSet dataSet = new DataSet();
+
+                        sqliteDataAdapter.Fill(dataSet, "Primus");//reset
+
+                        DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
+                        rowToBeAdded["id"] = 123;
+                        rowToBeAdded["name"] = "Name";//not null primary key
+                        rowToBeAdded["value"] = 777;
+
+                        dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
+sqliteDataAdapter.Update (dataSet, "Primus");
+
+                       //This would fail with NULL constraint violation in bug
+                       //report.  Because before the patch, it would create
+                       //a new record with all fields being null-- if the
+                       //exception rises, test fails
+                        sqliteDataAdapter.Update (dataSet, "Primus");
+
+                        dbConnection.Close();
+                        dbConnection = null;
+               }
+
 #endif
 
                class MyAdapter : DbDataAdapter
@@ -111,6 +251,43 @@ namespace MonoTests.System.Data.Common
                                throw new NotImplementedException ();
                        }
 #endif
+
+#if NET_2_0
+                       public new int AddToBatch (IDbCommand command)
+                       {
+                               return base.AddToBatch (command);
+                       }
+
+                       public new void ClearBatch ()
+                       {
+                               base.ClearBatch ();
+                       }
+
+                       public new void ExecuteBatch ()
+                       {
+                               base.ClearBatch ();
+                       }
+
+                       public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
+                       {
+                               return base.GetBatchedParameter (commandIdentifier, parameterIndex);
+                       }
+
+                       public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
+                       {
+                               return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
+                       }
+
+                       public new void InitializeBatching ()
+                       {
+                               base.InitializeBatching ();
+                       }
+
+                       public new void TerminateBatching ()
+                       {
+                               base.TerminateBatching ();
+                       }
+#endif
                }
        }
 }