2 // DbDataAdapterTest.cs - NUnit Test Cases for testing the DbDataAdapter class
5 // Gert Driesen (drieseng@users.sourceforge.net)
7 // Copyright (c) 2007 Gert Driesen
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Data.Common;
32 using System.Data.SqlClient;
34 /*--For Bug 853 Test Begin--*/
35 using Mono.Data.Sqlite;
36 /*--For Bug 853 Test End--*/
38 using NUnit.Framework;
40 namespace MonoTests.System.Data.Common
43 public class DbDataAdapterTest
47 public void UpdateBatchSize ()
49 MyAdapter da = new MyAdapter ();
51 da.UpdateBatchSize = 0;
53 } catch (NotSupportedException ex) {
54 // Specified method is not supported
55 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
56 Assert.IsNull (ex.InnerException, "#A3");
57 Assert.IsNotNull (ex.Message, "#A4");
59 Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
62 da.UpdateBatchSize = int.MaxValue;
64 } catch (NotSupportedException ex) {
65 // Specified method is not supported
66 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
67 Assert.IsNull (ex.InnerException, "#B3");
68 Assert.IsNotNull (ex.Message, "#B4");
70 Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
72 da.UpdateBatchSize = 1;
73 Assert.AreEqual (1, da.UpdateBatchSize, "#C");
77 public void UpdateBatchSize_Negative ()
79 MyAdapter da = new MyAdapter ();
81 da.UpdateBatchSize = -1;
83 } catch (NotSupportedException ex) {
84 // Specified method is not supported
85 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
86 Assert.IsNull (ex.InnerException, "#3");
87 Assert.IsNotNull (ex.Message, "#4");
92 public void AddToBatch ()
94 MyAdapter da = new MyAdapter ();
96 da.AddToBatch (new SqlCommand ());
98 } catch (NotSupportedException ex) {
99 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
100 Assert.IsNull (ex.InnerException, "#3");
101 Assert.IsNotNull (ex.Message, "#4");
106 public void ClearBatch ()
108 MyAdapter da = new MyAdapter ();
112 } catch (NotSupportedException ex) {
113 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
114 Assert.IsNull (ex.InnerException, "#3");
115 Assert.IsNotNull (ex.Message, "#4");
120 public void ExecuteBatch ()
122 MyAdapter da = new MyAdapter ();
126 } catch (NotSupportedException ex) {
127 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
128 Assert.IsNull (ex.InnerException, "#3");
129 Assert.IsNotNull (ex.Message, "#4");
134 public void GetBatchedParameter ()
136 MyAdapter da = new MyAdapter ();
138 da.GetBatchedParameter (1, 1);
140 } catch (NotSupportedException ex) {
141 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
142 Assert.IsNull (ex.InnerException, "#3");
143 Assert.IsNotNull (ex.Message, "#4");
148 public void GetBatchedRecordsAffected ()
150 MyAdapter da = new MyAdapter ();
151 int recordsAffected = 0;
152 Exception error = null;
154 Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
155 out recordsAffected, out error), "#1");
156 Assert.AreEqual (1, recordsAffected, "#2");
157 Assert.IsNull (error, "#3");
161 public void InitializeBatching ()
163 MyAdapter da = new MyAdapter ();
165 da.InitializeBatching ();
167 } catch (NotSupportedException ex) {
168 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
169 Assert.IsNull (ex.InnerException, "#3");
170 Assert.IsNotNull (ex.Message, "#4");
175 public void TerminateBatching ()
177 MyAdapter da = new MyAdapter ();
179 da.TerminateBatching ();
181 } catch (NotSupportedException ex) {
182 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
183 Assert.IsNull (ex.InnerException, "#3");
184 Assert.IsNotNull (ex.Message, "#4");
189 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
190 public void XimarinBugzillaBug853Test()
192 const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
193 SqliteConnection dbConnection = new SqliteConnection(connectionString);
195 SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
196 ClearTableEntry.ExecuteNonQuery();
198 SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
199 SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
200 sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
201 sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
203 DataSet dataSet = new DataSet();
205 sqliteDataAdapter.Fill(dataSet, "Primus");//reset
207 DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
208 rowToBeAdded["id"] = 123;
209 rowToBeAdded["name"] = "Name";//not null primary key
210 rowToBeAdded["value"] = 777;
212 dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
213 sqliteDataAdapter.Update (dataSet, "Primus");
215 //This would fail with NULL constraint violation in bug
216 //report. Because before the patch, it would create
217 //a new record with all fields being null-- if the
218 //exception rises, test fails
219 sqliteDataAdapter.Update (dataSet, "Primus");
221 dbConnection.Close();
227 class MyAdapter : DbDataAdapter
230 protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
231 StatementType statementType,
232 DataTableMapping tableMapping)
234 throw new NotImplementedException ();
237 protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
238 StatementType statementType,
239 DataTableMapping tableMapping)
241 throw new NotImplementedException ();
244 protected override void OnRowUpdated (RowUpdatedEventArgs value)
246 throw new NotImplementedException ();
249 protected override void OnRowUpdating (RowUpdatingEventArgs value)
251 throw new NotImplementedException ();
256 public new int AddToBatch (IDbCommand command)
258 return base.AddToBatch (command);
261 public new void ClearBatch ()
266 public new void ExecuteBatch ()
271 public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
273 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
276 public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
278 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
281 public new void InitializeBatching ()
283 base.InitializeBatching ();
286 public new void TerminateBatching ()
288 base.TerminateBatching ();