Merge pull request #1496 from echampet/serializers
[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                 [Test]
48                 public void UpdateBatchSize ()
49                 {
50                         MyAdapter da = new MyAdapter ();
51                         try {
52                                 da.UpdateBatchSize = 0;
53                                 Assert.Fail ("#A1");
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");
59                         }
60                         Assert.AreEqual (1, da.UpdateBatchSize, "#A5");
61
62                         try {
63                                 da.UpdateBatchSize = int.MaxValue;
64                                 Assert.Fail ("#B1");
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");
70                         }
71                         Assert.AreEqual (1, da.UpdateBatchSize, "#B5");
72
73                         da.UpdateBatchSize = 1;
74                         Assert.AreEqual (1, da.UpdateBatchSize, "#C");
75                 }
76
77                 [Test]
78                 public void UpdateBatchSize_Negative ()
79                 {
80                         MyAdapter da = new MyAdapter ();
81                         try {
82                                 da.UpdateBatchSize = -1;
83                                 Assert.Fail ("#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");
89                         }
90                 }
91
92                 [Test]
93                 public void AddToBatch ()
94                 {
95                         MyAdapter da = new MyAdapter ();
96                         try {
97                                 da.AddToBatch (new SqlCommand ());
98                                 Assert.Fail ("#1");
99                         } catch (NotSupportedException ex) {
100                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
101                                 Assert.IsNull (ex.InnerException, "#3");
102                                 Assert.IsNotNull (ex.Message, "#4");
103                         }
104                 }
105
106                 [Test]
107                 public void ClearBatch ()
108                 {
109                         MyAdapter da = new MyAdapter ();
110                         try {
111                                 da.ClearBatch ();
112                                 Assert.Fail ("#1");
113                         } catch (NotSupportedException ex) {
114                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
115                                 Assert.IsNull (ex.InnerException, "#3");
116                                 Assert.IsNotNull (ex.Message, "#4");
117                         }
118                 }
119
120                 [Test]
121                 public void ExecuteBatch ()
122                 {
123                         MyAdapter da = new MyAdapter ();
124                         try {
125                                 da.ExecuteBatch ();
126                                 Assert.Fail ("#1");
127                         } catch (NotSupportedException ex) {
128                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
129                                 Assert.IsNull (ex.InnerException, "#3");
130                                 Assert.IsNotNull (ex.Message, "#4");
131                         }
132                 }
133
134                 [Test]
135                 public void GetBatchedParameter ()
136                 {
137                         MyAdapter da = new MyAdapter ();
138                         try {
139                                 da.GetBatchedParameter (1, 1);
140                                 Assert.Fail ("#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");
145                         }
146                 }
147
148                 [Test]
149                 public void GetBatchedRecordsAffected ()
150                 {
151                         MyAdapter da = new MyAdapter ();
152                         int recordsAffected = 0;
153                         Exception error = null;
154
155                         Assert.IsTrue (da. GetBatchedRecordsAffected (int.MinValue,
156                                 out recordsAffected, out error), "#1");
157                         Assert.AreEqual (1, recordsAffected, "#2");
158                         Assert.IsNull (error, "#3");
159                 }
160
161                 [Test]
162                 public void InitializeBatching ()
163                 {
164                         MyAdapter da = new MyAdapter ();
165                         try {
166                                 da.InitializeBatching ();
167                                 Assert.Fail ("#1");
168                         } catch (NotSupportedException ex) {
169                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
170                                 Assert.IsNull (ex.InnerException, "#3");
171                                 Assert.IsNotNull (ex.Message, "#4");
172                         }
173                 }
174
175                 [Test]
176                 public void TerminateBatching ()
177                 {
178                         MyAdapter da = new MyAdapter ();
179                         try {
180                                 da.TerminateBatching ();
181                                 Assert.Fail ("#1");
182                         } catch (NotSupportedException ex) {
183                                 Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
184                                 Assert.IsNull (ex.InnerException, "#3");
185                                 Assert.IsNotNull (ex.Message, "#4");
186                         }
187                 }
188 #if !MOBILE     
189                 [Test]
190                 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
191                 public void XimarinBugzillaBug853Test()
192                 {
193                         const string connectionString = "URI = file:./SqliteTest.db; Version = 3";//will be in System.Data directory
194                         SqliteConnection dbConnection = new SqliteConnection(connectionString);
195                         dbConnection.Open();
196                         SqliteCommand ClearTableEntry=new SqliteCommand("DELETE FROM Primus;",dbConnection);
197                         ClearTableEntry.ExecuteNonQuery();
198
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();
203                         
204                         DataSet dataSet = new DataSet();
205
206                         sqliteDataAdapter.Fill(dataSet, "Primus");//reset
207
208                         DataRow rowToBeAdded = dataSet.Tables["Primus"].NewRow();
209                         rowToBeAdded["id"] = 123;
210                         rowToBeAdded["name"] = "Name";//not null primary key
211                         rowToBeAdded["value"] = 777;
212
213                         dataSet.Tables["Primus"].Rows.Add(rowToBeAdded);
214 sqliteDataAdapter.Update (dataSet, "Primus");
215
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");
221
222                         dbConnection.Close();
223                         dbConnection = null;
224                 }
225
226                 [Test]
227                 [Category ("NotWorking")] // Requires newer sqlite than is on wrench
228                 public void UpdateResetRowErrorCorrectly ()
229                 {
230                         const string connectionString = "URI = file::memory:; Version = 3";
231                         using (var dbConnection = new SqliteConnection (connectionString)) {
232                                 dbConnection.Open ();
233
234                                 using (var cmd = dbConnection.CreateCommand ()) {
235                                         cmd.CommandText = "CREATE TABLE data (id PRIMARY KEY, name TEXT)";
236                                         cmd.ExecuteNonQuery ();
237                                 }
238
239
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;
245
246                                 var ds1 = new DataSet ();
247                                 da.Fill (ds1, "data");
248
249                                 var table = ds1.Tables [0];
250                                 var row = table.NewRow ();
251                                 row ["id"] = 10;
252                                 row ["name"] = "Bart";
253                                 table.Rows.Add (row);
254
255                                 var ds2 = ds1.GetChanges ();
256                                 da.Update (ds2, "data");
257                                 Assert.IsFalse (ds2.HasErrors);
258                         }
259                 }
260 #endif
261
262
263                 class MyAdapter : DbDataAdapter
264                 {
265
266                         public new int AddToBatch (IDbCommand command)
267                         {
268                                 return base.AddToBatch (command);
269                         }
270
271                         public new void ClearBatch ()
272                         {
273                                 base.ClearBatch ();
274                         }
275
276                         public new void ExecuteBatch ()
277                         {
278                                 base.ClearBatch ();
279                         }
280
281                         public new IDataParameter GetBatchedParameter (int commandIdentifier, int parameterIndex)
282                         {
283                                 return base.GetBatchedParameter (commandIdentifier, parameterIndex);
284                         }
285
286                         public new bool GetBatchedRecordsAffected (int commandIdentifier, out int recordsAffected, out Exception error)
287                         {
288                                 return base.GetBatchedRecordsAffected (commandIdentifier, out recordsAffected, out error);
289                         }
290
291                         public new void InitializeBatching ()
292                         {
293                                 base.InitializeBatching ();
294                         }
295
296                         public new void TerminateBatching ()
297                         {
298                                 base.TerminateBatching ();
299                         }
300                 }
301         }
302 }