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--*/
36 using Mono.Data.Sqlite;
38 /*--For Bug 853 Test End--*/
40 using NUnit.Framework;
42 namespace MonoTests.System.Data.Common
45 public class DbDataAdapterTest
49 public void UpdateBatchSize ()
51 MyAdapter da = new MyAdapter ();
53 da.UpdateBatchSize = 0;
55 } catch (NotSupportedException ex) {
56 // Specified method is not supported
57 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
58 Assert.IsNull (ex.InnerException, "#A3");
59 Assert.IsNotNull (ex.Message, "#A4");
61 Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
64 da.UpdateBatchSize = int.MaxValue;
66 } catch (NotSupportedException ex) {
67 // Specified method is not supported
68 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
69 Assert.IsNull (ex.InnerException, "#B3");
70 Assert.IsNotNull (ex.Message, "#B4");
72 Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
74 da.UpdateBatchSize = 1;
75 Assert.AreEqual (1, da.UpdateBatchSize, "#C");
79 public void UpdateBatchSize_Negative ()
81 MyAdapter da = new MyAdapter ();
83 da.UpdateBatchSize = -1;
85 } catch (NotSupportedException ex) {
86 // Specified method is not supported
87 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
88 Assert.IsNull (ex.InnerException, "#3");
89 Assert.IsNotNull (ex.Message, "#4");
94 public void AddToBatch ()
96 MyAdapter da = new MyAdapter ();
98 da.AddToBatch (new SqlCommand ());
100 } catch (NotSupportedException ex) {
101 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
102 Assert.IsNull (ex.InnerException, "#3");
103 Assert.IsNotNull (ex.Message, "#4");
108 public void ClearBatch ()
110 MyAdapter da = new MyAdapter ();
114 } catch (NotSupportedException ex) {
115 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
116 Assert.IsNull (ex.InnerException, "#3");
117 Assert.IsNotNull (ex.Message, "#4");
122 public void ExecuteBatch ()
124 MyAdapter da = new MyAdapter ();
128 } catch (NotSupportedException ex) {
129 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
130 Assert.IsNull (ex.InnerException, "#3");
131 Assert.IsNotNull (ex.Message, "#4");
136 public void GetBatchedParameter ()
138 MyAdapter da = new MyAdapter ();
140 da.GetBatchedParameter (1, 1);
142 } catch (NotSupportedException ex) {
143 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
144 Assert.IsNull (ex.InnerException, "#3");
145 Assert.IsNotNull (ex.Message, "#4");
150 public void GetBatchedRecordsAffected ()
152 MyAdapter da = new MyAdapter ();
153 int recordsAffected = 0;
154 Exception error = null;
156 Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
157 out recordsAffected, out error), "#1");
158 Assert.AreEqual (1, recordsAffected, "#2");
159 Assert.IsNull (error, "#3");
163 public void InitializeBatching ()
165 MyAdapter da = new MyAdapter ();
167 da.InitializeBatching ();
169 } catch (NotSupportedException ex) {
170 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
171 Assert.IsNull (ex.InnerException, "#3");
172 Assert.IsNotNull (ex.Message, "#4");
177 public void TerminateBatching ()
179 MyAdapter da = new MyAdapter ();
181 da.TerminateBatching ();
183 } catch (NotSupportedException ex) {
184 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
185 Assert.IsNull (ex.InnerException, "#3");
186 Assert.IsNotNull (ex.Message, "#4");
191 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
192 public void XimarinBugzillaBug853Test()
194 const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
195 SqliteConnection dbConnection = new SqliteConnection(connectionString);
197 SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
198 ClearTableEntry.ExecuteNonQuery();
200 SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
201 SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
202 sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
203 sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
205 DataSet dataSet = new DataSet();
207 sqliteDataAdapter.Fill(dataSet, "Primus");//reset
209 DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
210 rowToBeAdded["id"] = 123;
211 rowToBeAdded["name"] = "Name";//not null primary key
212 rowToBeAdded["value"] = 777;
214 dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
215 sqliteDataAdapter.Update (dataSet, "Primus");
217 //This would fail with NULL constraint violation in bug
218 //report. Because before the patch, it would create
219 //a new record with all fields being null-- if the
220 //exception rises, test fails
221 sqliteDataAdapter.Update (dataSet, "Primus");
223 dbConnection.Close();
228 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
229 public void UpdateResetRowErrorCorrectly ()
231 const string connectionString = "URI = file::memory:; Version = 3";
232 using (var dbConnection = new SqliteConnection (connectionString)) {
233 dbConnection.Open ();
235 using (var cmd = dbConnection.CreateCommand ()) {
236 cmd.CommandText = "CREATE TABLE data (id PRIMARY KEY, name TEXT)";
237 cmd.ExecuteNonQuery ();
241 var ts = dbConnection.BeginTransaction ();
242 var da = new SqliteDataAdapter ("SELECT * FROM data", dbConnection);
243 var builder = new SqliteCommandBuilder (da);
244 da.UpdateCommand = builder.GetUpdateCommand ();
245 da.UpdateCommand.Transaction = ts;
247 var ds1 = new DataSet ();
248 da.Fill (ds1, "data");
250 var table = ds1.Tables [0];
251 var row = table.NewRow ();
253 row ["name"] = "Bart";
254 table.Rows.Add (row);
256 var ds2 = ds1.GetChanges ();
257 da.Update (ds2, "data");
258 Assert.IsFalse (ds2.HasErrors);
265 class MyAdapter : DbDataAdapter
268 protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
269 StatementType statementType,
270 DataTableMapping tableMapping)
272 throw new NotImplementedException ();
275 protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
276 StatementType statementType,
277 DataTableMapping tableMapping)
279 throw new NotImplementedException ();
282 protected override void OnRowUpdated (RowUpdatedEventArgs value)
284 throw new NotImplementedException ();
287 protected override void OnRowUpdating (RowUpdatingEventArgs value)
289 throw new NotImplementedException ();
294 public new int AddToBatch (IDbCommand command)
296 return base.AddToBatch (command);
299 public new void ClearBatch ()
304 public new void ExecuteBatch ()
309 public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
311 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
314 public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
316 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
319 public new void InitializeBatching ()
321 base.InitializeBatching ();
324 public new void TerminateBatching ()
326 base.TerminateBatching ();