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 #if FEATURE_NO_BSD_SOCKETS
100 } catch (PlatformNotSupportedException) {
102 } catch (NotSupportedException ex) {
103 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
104 Assert.IsNull (ex.InnerException, "#3");
105 Assert.IsNotNull (ex.Message, "#4");
111 public void ClearBatch ()
113 MyAdapter da = new MyAdapter ();
117 } catch (NotSupportedException ex) {
118 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
119 Assert.IsNull (ex.InnerException, "#3");
120 Assert.IsNotNull (ex.Message, "#4");
125 public void ExecuteBatch ()
127 MyAdapter da = new MyAdapter ();
131 } catch (NotSupportedException ex) {
132 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
133 Assert.IsNull (ex.InnerException, "#3");
134 Assert.IsNotNull (ex.Message, "#4");
139 public void GetBatchedParameter ()
141 MyAdapter da = new MyAdapter ();
143 da.GetBatchedParameter (1, 1);
145 } catch (NotSupportedException ex) {
146 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
147 Assert.IsNull (ex.InnerException, "#3");
148 Assert.IsNotNull (ex.Message, "#4");
153 public void GetBatchedRecordsAffected ()
155 MyAdapter da = new MyAdapter ();
156 int recordsAffected = 0;
157 Exception error = null;
159 Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
160 out recordsAffected, out error), "#1");
161 Assert.AreEqual (1, recordsAffected, "#2");
162 Assert.IsNull (error, "#3");
166 public void InitializeBatching ()
168 MyAdapter da = new MyAdapter ();
170 da.InitializeBatching ();
172 } catch (NotSupportedException ex) {
173 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
174 Assert.IsNull (ex.InnerException, "#3");
175 Assert.IsNotNull (ex.Message, "#4");
180 public void TerminateBatching ()
182 MyAdapter da = new MyAdapter ();
184 da.TerminateBatching ();
186 } catch (NotSupportedException ex) {
187 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
188 Assert.IsNull (ex.InnerException, "#3");
189 Assert.IsNotNull (ex.Message, "#4");
194 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
195 public void XimarinBugzillaBug853Test()
197 const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
198 SqliteConnection dbConnection = new SqliteConnection(connectionString);
200 SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
201 ClearTableEntry.ExecuteNonQuery();
203 SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
204 SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
205 sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
206 sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
208 DataSet dataSet = new DataSet();
210 sqliteDataAdapter.Fill(dataSet, "Primus");//reset
212 DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
213 rowToBeAdded["id"] = 123;
214 rowToBeAdded["name"] = "Name";//not null primary key
215 rowToBeAdded["value"] = 777;
217 dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
218 sqliteDataAdapter.Update (dataSet, "Primus");
220 //This would fail with NULL constraint violation in bug
221 //report. Because before the patch, it would create
222 //a new record with all fields being null-- if the
223 //exception rises, test fails
224 sqliteDataAdapter.Update (dataSet, "Primus");
226 dbConnection.Close();
231 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
232 public void UpdateResetRowErrorCorrectly ()
234 const string connectionString = "URI = file::memory:; Version = 3";
235 using (var dbConnection = new SqliteConnection (connectionString)) {
236 dbConnection.Open ();
238 using (var cmd = dbConnection.CreateCommand ()) {
239 cmd.CommandText = "CREATE TABLE data (id PRIMARY KEY, name TEXT)";
240 cmd.ExecuteNonQuery ();
244 var ts = dbConnection.BeginTransaction ();
245 var da = new SqliteDataAdapter ("SELECT * FROM data", dbConnection);
246 var builder = new SqliteCommandBuilder (da);
247 da.UpdateCommand = builder.GetUpdateCommand ();
248 da.UpdateCommand.Transaction = ts;
250 var ds1 = new DataSet ();
251 da.Fill (ds1, "data");
253 var table = ds1.Tables [0];
254 var row = table.NewRow ();
256 row ["name"] = "Bart";
257 table.Rows.Add (row);
259 var ds2 = ds1.GetChanges ();
260 da.Update (ds2, "data");
261 Assert.IsFalse (ds2.HasErrors);
267 class MyAdapter : DbDataAdapter
270 public new int AddToBatch (IDbCommand command)
272 return base.AddToBatch (command);
275 public new void ClearBatch ()
280 public new void ExecuteBatch ()
285 public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
287 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
290 public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
292 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
295 public new void InitializeBatching ()
297 base.InitializeBatching ();
300 public new void TerminateBatching ()
302 base.TerminateBatching ();