Merge remote-tracking branch 'public/master'
[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 using Mono.Data.Sqlite;
36 /*--For Bug 853 Test End--*/
37
38 using NUnit.Framework;
39
40 namespace MonoTests.System.Data.Common
41 {
42         [TestFixture]
43         public class DbDataAdapterTest
44         {
45 #if NET_2_0
46                 [Test]
47                 public void UpdateBatchSize ()
48                 {
49                         MyAdapter da = new MyAdapter ();
50                         try {
51                                 da.UpdateBatchSize = 0;
52                                 Assert.Fail ("#A1");
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");
58                         }
59                         Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
60
61                         try {
62                                 da.UpdateBatchSize = int.MaxValue;
63                                 Assert.Fail ("#B1");
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");
69                         }
70                         Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
71
72                         da.UpdateBatchSize = 1;
73                         Assert.AreEqual (1, da.UpdateBatchSize, "#C");
74                 }
75
76                 [Test]
77                 public void UpdateBatchSize_Negative ()
78                 {
79                         MyAdapter da = new MyAdapter ();
80                         try {
81                                 da.UpdateBatchSize = -1;
82                                 Assert.Fail ("#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");
88                         }
89                 }
90
91                 [Test]
92                 public void AddToBatch ()
93                 {
94                         MyAdapter da = new MyAdapter ();
95                         try {
96                                 da.AddToBatch (new SqlCommand ());
97                                 Assert.Fail ("#1");
98                         } catch (NotSupportedException ex) {
99                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
100                                 Assert.IsNull (ex.InnerException, "#3");
101                                 Assert.IsNotNull (ex.Message, "#4");
102                         }
103                 }
104
105                 [Test]
106                 public void ClearBatch ()
107                 {
108                         MyAdapter da = new MyAdapter ();
109                         try {
110                                 da.ClearBatch ();
111                                 Assert.Fail ("#1");
112                         } catch (NotSupportedException ex) {
113                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
114                                 Assert.IsNull (ex.InnerException, "#3");
115                                 Assert.IsNotNull (ex.Message, "#4");
116                         }
117                 }
118
119                 [Test]
120                 public void ExecuteBatch ()
121                 {
122                         MyAdapter da = new MyAdapter ();
123                         try {
124                                 da.ExecuteBatch ();
125                                 Assert.Fail ("#1");
126                         } catch (NotSupportedException ex) {
127                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
128                                 Assert.IsNull (ex.InnerException, "#3");
129                                 Assert.IsNotNull (ex.Message, "#4");
130                         }
131                 }
132
133                 [Test]
134                 public void GetBatchedParameter ()
135                 {
136                         MyAdapter da = new MyAdapter ();
137                         try {
138                                 da.GetBatchedParameter (1, 1);
139                                 Assert.Fail ("#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");
144                         }
145                 }
146
147                 [Test]
148                 public void GetBatchedRecordsAffected ()
149                 {
150                         MyAdapter da = new MyAdapter ();
151                         int recordsAffected = 0;
152                         Exception error = null;
153
154                         Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
155                                 out recordsAffected, out error), "#1");
156                         Assert.AreEqual (1, recordsAffected, "#2");
157                         Assert.IsNull (error, "#3");
158                 }
159
160                 [Test]
161                 public void InitializeBatching ()
162                 {
163                         MyAdapter da = new MyAdapter ();
164                         try {
165                                 da.InitializeBatching ();
166                                 Assert.Fail ("#1");
167                         } catch (NotSupportedException ex) {
168                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
169                                 Assert.IsNull (ex.InnerException, "#3");
170                                 Assert.IsNotNull (ex.Message, "#4");
171                         }
172                 }
173
174                 [Test]
175                 public void TerminateBatching ()
176                 {
177                         MyAdapter da = new MyAdapter ();
178                         try {
179                                 da.TerminateBatching ();
180                                 Assert.Fail ("#1");
181                         } catch (NotSupportedException ex) {
182                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
183                                 Assert.IsNull (ex.InnerException, "#3");
184                                 Assert.IsNotNull (ex.Message, "#4");
185                         }
186                 }
187                 
188                 [Test]
189                 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
190                 public void XimarinBugzillaBug853Test()
191                 {
192                         const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
193                         SqliteConnection dbConnection = new SqliteConnection(connectionString);
194                         dbConnection.Open();
195                         SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
196                         ClearTableEntry.ExecuteNonQuery();
197
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();
202                         
203                         DataSet dataSet = new DataSet();
204
205                         sqliteDataAdapter.Fill(dataSet, "Primus");//reset
206
207                         DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
208                         rowToBeAdded["id"] = 123;
209                         rowToBeAdded["name"] = "Name";//not null primary key
210                         rowToBeAdded["value"] = 777;
211
212                         dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
213 sqliteDataAdapter.Update (dataSet, "Primus");
214
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");
220
221                         dbConnection.Close();
222                         dbConnection = null;
223                 }
224
225 #endif
226
227                 class MyAdapter : DbDataAdapter
228                 {
229 #if ONLY_1_1
230                         protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
231                                                                                      StatementType statementType,
232                                                                                      DataTableMapping tableMapping)
233                         {
234                                 throw new NotImplementedException ();
235                         }
236
237                         protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
238                                                                                        StatementType statementType,
239                                                                                        DataTableMapping tableMapping)
240                         {
241                                 throw new NotImplementedException ();
242                         }
243
244                         protected override void OnRowUpdated (RowUpdatedEventArgs value)
245                         {
246                                 throw new NotImplementedException ();
247                         }
248
249                         protected override void OnRowUpdating (RowUpdatingEventArgs value)
250                         {
251                                 throw new NotImplementedException ();
252                         }
253 #endif
254
255 #if NET_2_0
256                         public new int AddToBatch (IDbCommand command)
257                         {
258                                 return base.AddToBatch (command);
259                         }
260
261                         public new void ClearBatch ()
262                         {
263                                 base.ClearBatch ();
264                         }
265
266                         public new void ExecuteBatch ()
267                         {
268                                 base.ClearBatch ();
269                         }
270
271                         public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
272                         {
273                                 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
274                         }
275
276                         public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
277                         {
278                                 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
279                         }
280
281                         public new void InitializeBatching ()
282                         {
283                                 base.InitializeBatching ();
284                         }
285
286                         public new void TerminateBatching ()
287                         {
288                                 base.TerminateBatching ();
289                         }
290 #endif
291                 }
292         }
293 }