This is the associated unit test for the Ximarin Bugzilla #853 bug...
[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                 public void XimarinBugzillaBug853Test()
190                 {
191                         const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
192                         SqliteConnection dbConnection = new SqliteConnection(connectionString);
193                         dbConnection.Open();
194                         SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
195                         ClearTableEntry.ExecuteNonQuery();
196
197                         SqliteDataAdapter sqliteDataAdapter = new SqliteDataAdapter("SELECT * FROM primus", dbConnection);
198                         SqliteCommandBuilder builder = new SqliteCommandBuilder(sqliteDataAdapter);
199                         sqliteDataAdapter.InsertCommand = builder.GetInsertCommand();
200                         sqliteDataAdapter.DeleteCommand = builder.GetDeleteCommand();
201                         
202                         DataSet dataSet = new DataSet();
203
204                         sqliteDataAdapter.Fill(dataSet, "Primus");//reset
205
206                         DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
207                         rowToBeAdded["id"] = 123;
208                         rowToBeAdded["name"] = "Name";//not null primary key
209                         rowToBeAdded["value"] = 777;
210
211                         dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
212 sqliteDataAdapter.Update (dataSet, "Primus");
213
214                         //This would fail with NULL constraint violation in bug
215                         //report.  Because before the patch, it would create
216                         //a new record with all fields being null-- if the
217                         //exception rises, test fails
218                         sqliteDataAdapter.Update (dataSet, "Primus");
219
220                         dbConnection.Close();
221                         dbConnection = null;
222                 }
223
224 #endif
225
226                 class MyAdapter : DbDataAdapter
227                 {
228 #if ONLY_1_1
229                         protected override RowUpdatedEventArgs CreateRowUpdatedEvent (DataRow dataRow, IDbCommand command,
230                                                                                      StatementType statementType,
231                                                                                      DataTableMapping tableMapping)
232                         {
233                                 throw new NotImplementedException ();
234                         }
235
236                         protected override RowUpdatingEventArgs CreateRowUpdatingEvent (DataRow dataRow, IDbCommand command,
237                                                                                        StatementType statementType,
238                                                                                        DataTableMapping tableMapping)
239                         {
240                                 throw new NotImplementedException ();
241                         }
242
243                         protected override void OnRowUpdated (RowUpdatedEventArgs value)
244                         {
245                                 throw new NotImplementedException ();
246                         }
247
248                         protected override void OnRowUpdating (RowUpdatingEventArgs value)
249                         {
250                                 throw new NotImplementedException ();
251                         }
252 #endif
253
254 #if NET_2_0
255                         public new int AddToBatch (IDbCommand command)
256                         {
257                                 return base.AddToBatch (command);
258                         }
259
260                         public new void ClearBatch ()
261                         {
262                                 base.ClearBatch ();
263                         }
264
265                         public new void ExecuteBatch ()
266                         {
267                                 base.ClearBatch ();
268                         }
269
270                         public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
271                         {
272                                 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
273                         }
274
275                         public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
276                         {
277                                 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
278                         }
279
280                         public new void InitializeBatching ()
281                         {
282                                 base.InitializeBatching ();
283                         }
284
285                         public new void TerminateBatching ()
286                         {
287                                 base.TerminateBatching ();
288                         }
289 #endif
290                 }
291         }
292 }