Merge pull request #1304 from slluis/mac-proxy-autoconfig
[mono.git] / mcs / class / System.Data / Test / System.Data.Common / DbDataAdapterTest.cs
1 //
2 // DbDataAdapterTest.cs - NUnit Test Cases for testing the DbDataAdapter class
3 //
4 // Author:
5 //      Gert Driesen (drieseng@users.sourceforge.net)
6 //
7 // Copyright (c) 2007 Gert Driesen
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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.
27 //
28
29 using System;
30 using System.Data;
31 using System.Data.Common;
32 using System.Data.SqlClient;
33
34 /*--For Bug 853 Test Begin--*/
35 #if !MOBILE
36 using Mono.Data.Sqlite;
37 #endif
38 /*--For Bug 853 Test End--*/
39
40 using NUnit.Framework;
41
42 namespace MonoTests.System.Data.Common
43 {
44         [TestFixture]
45         public class DbDataAdapterTest
46         {
47 #if NET_2_0
48                 [Test]
49                 public void UpdateBatchSize ()
50                 {
51                         MyAdapter da = new MyAdapter ();
52                         try {
53                                 da.UpdateBatchSize = 0;
54                                 Assert.Fail ("#A1");
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");
60                         }
61                         Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
62
63                         try {
64                                 da.UpdateBatchSize = int.MaxValue;
65                                 Assert.Fail ("#B1");
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");
71                         }
72                         Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
73
74                         da.UpdateBatchSize = 1;
75                         Assert.AreEqual (1, da.UpdateBatchSize, "#C");
76                 }
77
78                 [Test]
79                 public void UpdateBatchSize_Negative ()
80                 {
81                         MyAdapter da = new MyAdapter ();
82                         try {
83                                 da.UpdateBatchSize = -1;
84                                 Assert.Fail ("#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");
90                         }
91                 }
92
93                 [Test]
94                 public void AddToBatch ()
95                 {
96                         MyAdapter da = new MyAdapter ();
97                         try {
98                                 da.AddToBatch (new SqlCommand ());
99                                 Assert.Fail ("#1");
100                         } catch (NotSupportedException ex) {
101                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
102                                 Assert.IsNull (ex.InnerException, "#3");
103                                 Assert.IsNotNull (ex.Message, "#4");
104                         }
105                 }
106
107                 [Test]
108                 public void ClearBatch ()
109                 {
110                         MyAdapter da = new MyAdapter ();
111                         try {
112                                 da.ClearBatch ();
113                                 Assert.Fail ("#1");
114                         } catch (NotSupportedException ex) {
115                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
116                                 Assert.IsNull (ex.InnerException, "#3");
117                                 Assert.IsNotNull (ex.Message, "#4");
118                         }
119                 }
120
121                 [Test]
122                 public void ExecuteBatch ()
123                 {
124                         MyAdapter da = new MyAdapter ();
125                         try {
126                                 da.ExecuteBatch ();
127                                 Assert.Fail ("#1");
128                         } catch (NotSupportedException ex) {
129                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
130                                 Assert.IsNull (ex.InnerException, "#3");
131                                 Assert.IsNotNull (ex.Message, "#4");
132                         }
133                 }
134
135                 [Test]
136                 public void GetBatchedParameter ()
137                 {
138                         MyAdapter da = new MyAdapter ();
139                         try {
140                                 da.GetBatchedParameter (1, 1);
141                                 Assert.Fail ("#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");
146                         }
147                 }
148
149                 [Test]
150                 public void GetBatchedRecordsAffected ()
151                 {
152                         MyAdapter da = new MyAdapter ();
153                         int recordsAffected = 0;
154                         Exception error = null;
155
156                         Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
157                                 out recordsAffected, out error), "#1");
158                         Assert.AreEqual (1, recordsAffected, "#2");
159                         Assert.IsNull (error, "#3");
160                 }
161
162                 [Test]
163                 public void InitializeBatching ()
164                 {
165                         MyAdapter da = new MyAdapter ();
166                         try {
167                                 da.InitializeBatching ();
168                                 Assert.Fail ("#1");
169                         } catch (NotSupportedException ex) {
170                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
171                                 Assert.IsNull (ex.InnerException, "#3");
172                                 Assert.IsNotNull (ex.Message, "#4");
173                         }
174                 }
175
176                 [Test]
177                 public void TerminateBatching ()
178                 {
179                         MyAdapter da = new MyAdapter ();
180                         try {
181                                 da.TerminateBatching ();
182                                 Assert.Fail ("#1");
183                         } catch (NotSupportedException ex) {
184                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
185                                 Assert.IsNull (ex.InnerException, "#3");
186                                 Assert.IsNotNull (ex.Message, "#4");
187                         }
188                 }
189 #if !MOBILE     
190                 [Test]
191                 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
192                 public void XimarinBugzillaBug853Test()
193                 {
194                         const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
195                         SqliteConnection dbConnection = new SqliteConnection(connectionString);
196                         dbConnection.Open();
197                         SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
198                         ClearTableEntry.ExecuteNonQuery();
199
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();
204                         
205                         DataSet dataSet = new DataSet();
206
207                         sqliteDataAdapter.Fill(dataSet, "Primus");//reset
208
209                         DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
210                         rowToBeAdded["id"] = 123;
211                         rowToBeAdded["name"] = "Name";//not null primary key
212                         rowToBeAdded["value"] = 777;
213
214                         dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
215 sqliteDataAdapter.Update (dataSet, "Primus");
216
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");
222
223                         dbConnection.Close();
224                         dbConnection = null;
225                 }
226
227                 [Test]
228                 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
229                 public void UpdateResetRowErrorCorrectly ()
230                 {
231                         const string connectionString = "URI = file::memory:; Version = 3";
232                         using (var dbConnection = new SqliteConnection (connectionString)) {
233                                 dbConnection.Open ();
234
235                                 using (var cmd = dbConnection.CreateCommand ()) {
236                                         cmd.CommandText = "CREATE TABLE data (id PRIMARY KEY, name TEXT)";
237                                         cmd.ExecuteNonQuery ();
238                                 }
239
240
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;
246
247                                 var ds1 = new DataSet ();
248                                 da.Fill (ds1, "data");
249
250                                 var table = ds1.Tables [0];
251                                 var row = table.NewRow ();
252                                 row ["id"] = 10;
253                                 row ["name"] = "Bart";
254                                 table.Rows.Add (row);
255
256                                 var ds2 = ds1.GetChanges ();
257                                 da.Update (ds2, "data");
258                                 Assert.IsFalse (ds2.HasErrors);
259                         }
260                 }
261 #endif
262
263 #endif
264
265                 class MyAdapter : DbDataAdapter
266                 {
267 #if ONLY_1_1
268                         protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
269                                                                                      StatementType statementType,
270                                                                                      DataTableMapping tableMapping)
271                         {
272                                 throw new NotImplementedException ();
273                         }
274
275                         protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
276                                                                                        StatementType statementType,
277                                                                                        DataTableMapping tableMapping)
278                         {
279                                 throw new NotImplementedException ();
280                         }
281
282                         protected override void OnRowUpdated (RowUpdatedEventArgs value)
283                         {
284                                 throw new NotImplementedException ();
285                         }
286
287                         protected override void OnRowUpdating (RowUpdatingEventArgs value)
288                         {
289                                 throw new NotImplementedException ();
290                         }
291 #endif
292
293 #if NET_2_0
294                         public new int AddToBatch (IDbCommand command)
295                         {
296                                 return base.AddToBatch (command);
297                         }
298
299                         public new void ClearBatch ()
300                         {
301                                 base.ClearBatch ();
302                         }
303
304                         public new void ExecuteBatch ()
305                         {
306                                 base.ClearBatch ();
307                         }
308
309                         public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
310                         {
311                                 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
312                         }
313
314                         public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
315                         {
316                                 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
317                         }
318
319                         public new void InitializeBatching ()
320                         {
321                                 base.InitializeBatching ();
322                         }
323
324                         public new void TerminateBatching ()
325                         {
326                                 base.TerminateBatching ();
327                         }
328 #endif
329                 }
330         }
331 }