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
48 public void UpdateBatchSize ()
50 MyAdapter da = new MyAdapter ();
52 da.UpdateBatchSize = 0;
54 } catch (NotSupportedException ex) {
55 // Specified method is not supported
56 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#A2");
57 Assert.IsNull (ex.InnerException, "#A3");
58 Assert.IsNotNull (ex.Message, "#A4");
60 Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
63 da.UpdateBatchSize = int.MaxValue;
65 } catch (NotSupportedException ex) {
66 // Specified method is not supported
67 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#B2");
68 Assert.IsNull (ex.InnerException, "#B3");
69 Assert.IsNotNull (ex.Message, "#B4");
71 Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
73 da.UpdateBatchSize = 1;
74 Assert.AreEqual (1, da.UpdateBatchSize, "#C");
78 public void UpdateBatchSize_Negative ()
80 MyAdapter da = new MyAdapter ();
82 da.UpdateBatchSize = -1;
84 } catch (NotSupportedException ex) {
85 // Specified method is not supported
86 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
87 Assert.IsNull (ex.InnerException, "#3");
88 Assert.IsNotNull (ex.Message, "#4");
93 public void AddToBatch ()
95 MyAdapter da = new MyAdapter ();
97 da.AddToBatch (new SqlCommand ());
99 } catch (NotSupportedException ex) {
100 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
101 Assert.IsNull (ex.InnerException, "#3");
102 Assert.IsNotNull (ex.Message, "#4");
107 public void ClearBatch ()
109 MyAdapter da = new MyAdapter ();
113 } catch (NotSupportedException ex) {
114 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
115 Assert.IsNull (ex.InnerException, "#3");
116 Assert.IsNotNull (ex.Message, "#4");
121 public void ExecuteBatch ()
123 MyAdapter da = new MyAdapter ();
127 } catch (NotSupportedException ex) {
128 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
129 Assert.IsNull (ex.InnerException, "#3");
130 Assert.IsNotNull (ex.Message, "#4");
135 public void GetBatchedParameter ()
137 MyAdapter da = new MyAdapter ();
139 da.GetBatchedParameter (1, 1);
141 } catch (NotSupportedException ex) {
142 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
143 Assert.IsNull (ex.InnerException, "#3");
144 Assert.IsNotNull (ex.Message, "#4");
149 public void GetBatchedRecordsAffected ()
151 MyAdapter da = new MyAdapter ();
152 int recordsAffected = 0;
153 Exception error = null;
155 Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
156 out recordsAffected, out error), "#1");
157 Assert.AreEqual (1, recordsAffected, "#2");
158 Assert.IsNull (error, "#3");
162 public void InitializeBatching ()
164 MyAdapter da = new MyAdapter ();
166 da.InitializeBatching ();
168 } catch (NotSupportedException ex) {
169 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
170 Assert.IsNull (ex.InnerException, "#3");
171 Assert.IsNotNull (ex.Message, "#4");
176 public void TerminateBatching ()
178 MyAdapter da = new MyAdapter ();
180 da.TerminateBatching ();
182 } catch (NotSupportedException ex) {
183 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
184 Assert.IsNull (ex.InnerException, "#3");
185 Assert.IsNotNull (ex.Message, "#4");
190 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
191 public void XimarinBugzillaBug853Test()
193 const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
194 SqliteConnection dbConnection = new SqliteConnection(connectionString);
196 SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
197 ClearTableEntry.ExecuteNonQuery();
199 SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
200 SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
201 sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
202 sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
204 DataSet dataSet = new DataSet();
206 sqliteDataAdapter.Fill(dataSet, "Primus");//reset
208 DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
209 rowToBeAdded["id"] = 123;
210 rowToBeAdded["name"] = "Name";//not null primary key
211 rowToBeAdded["value"] = 777;
213 dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
214 sqliteDataAdapter.Update (dataSet, "Primus");
216 //This would fail with NULL constraint violation in bug
217 //report. Because before the patch, it would create
218 //a new record with all fields being null-- if the
219 //exception rises, test fails
220 sqliteDataAdapter.Update (dataSet, "Primus");
222 dbConnection.Close();
227 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
228 public void UpdateResetRowErrorCorrectly ()
230 const string connectionString = "URI = file::memory:; Version = 3";
231 using (var dbConnection = new SqliteConnection (connectionString)) {
232 dbConnection.Open ();
234 using (var cmd = dbConnection.CreateCommand ()) {
235 cmd.CommandText = "CREATE TABLE data (id PRIMARY KEY, name TEXT)";
236 cmd.ExecuteNonQuery ();
240 var ts = dbConnection.BeginTransaction ();
241 var da = new SqliteDataAdapter ("SELECT * FROM data", dbConnection);
242 var builder = new SqliteCommandBuilder (da);
243 da.UpdateCommand = builder.GetUpdateCommand ();
244 da.UpdateCommand.Transaction = ts;
246 var ds1 = new DataSet ();
247 da.Fill (ds1, "data");
249 var table = ds1.Tables [0];
250 var row = table.NewRow ();
252 row ["name"] = "Bart";
253 table.Rows.Add (row);
255 var ds2 = ds1.GetChanges ();
256 da.Update (ds2, "data");
257 Assert.IsFalse (ds2.HasErrors);
263 class MyAdapter : DbDataAdapter
266 public new int AddToBatch (IDbCommand command)
268 return base.AddToBatch (command);
271 public new void ClearBatch ()
276 public new void ExecuteBatch ()
281 public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
283 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
286 public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
288 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
291 public new void InitializeBatching ()
293 base.InitializeBatching ();
296 public new void TerminateBatching ()
298 base.TerminateBatching ();