2 // SqlDataAdapterTest.cs - NUnit Test Cases for testing the
3 // SqlDataAdapter 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.SqlClient;
33 using System.Data.Odbc;
36 using NUnit.Framework;
38 namespace MonoTests.System.Data.SqlClient
41 public class SqlDataAdapterTest
43 [Test] // SqlDataAdapter ()
44 public void Constructor1 ()
46 SqlDataAdapter da = new SqlDataAdapter ();
47 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
48 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
49 Assert.IsNull (da.Container, "#3");
50 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
51 Assert.IsNull (da.DeleteCommand, "#5");
52 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
53 Assert.IsNull (da.InsertCommand, "#7");
54 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
55 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
56 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
57 Assert.IsNull (da.SelectCommand, "#11");
58 Assert.IsNull (da.Site, "#12");
59 Assert.IsNotNull (da.TableMappings, "#13");
60 Assert.AreEqual (0, da.TableMappings.Count, "#14");
61 Assert.AreEqual (1, da.UpdateBatchSize, "#15");
62 Assert.IsNull (da.UpdateCommand, "#16");
65 [Test] // SqlDataAdapter (SqlCommand)
66 public void Constructor2 ()
68 SqlCommand cmd = new SqlCommand ();
69 SqlDataAdapter da = new SqlDataAdapter (cmd);
70 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
71 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
72 Assert.IsNull (da.Container, "#3");
73 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
74 Assert.IsNull (da.DeleteCommand, "#5");
75 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
76 Assert.IsNull (da.InsertCommand, "#7");
77 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
78 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
79 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
80 Assert.IsNotNull (da.SelectCommand, "#11");
81 Assert.AreSame (cmd, da.SelectCommand, "#12");
82 Assert.IsNull (da.Site, "#13");
83 Assert.IsNotNull (da.TableMappings, "#14");
84 Assert.AreEqual (0, da.TableMappings.Count, "#15");
85 Assert.AreEqual (1, da.UpdateBatchSize, "#16");
86 Assert.IsNull (da.UpdateCommand, "#17");
89 [Test] // SqlDataAdapter (SqlCommand)
90 public void Constructor2_SelectCommand_Null ()
92 SqlDataAdapter da = new SqlDataAdapter ((SqlCommand) null);
93 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
94 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
95 Assert.IsNull (da.Container, "#3");
96 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
97 Assert.IsNull (da.DeleteCommand, "#5");
98 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
99 Assert.IsNull (da.InsertCommand, "#7");
100 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
101 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
102 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
103 Assert.IsNull (da.SelectCommand, "#11");
104 Assert.IsNull (da.Site, "#12");
105 Assert.IsNotNull (da.TableMappings, "#13");
106 Assert.AreEqual (0, da.TableMappings.Count, "#14");
107 Assert.AreEqual (1, da.UpdateBatchSize, "#15");
108 Assert.IsNull (da.UpdateCommand, "#16");
111 [Test] // SqlDataAdapter (string, SqlConnection)
112 public void Constructor3 ()
114 string selectCommandText = "SELECT * FROM Authors";
115 SqlConnection selectConnection = new SqlConnection ();
117 SqlDataAdapter da = new SqlDataAdapter (selectCommandText,
119 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
120 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
121 Assert.IsNull (da.Container, "#3");
122 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
123 Assert.IsNull (da.DeleteCommand, "#5");
124 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
125 Assert.IsNull (da.InsertCommand, "#7");
126 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
127 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
128 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
129 Assert.IsNotNull (da.SelectCommand, "#11");
130 Assert.AreSame (selectCommandText, da.SelectCommand.CommandText, "#12");
131 Assert.AreSame (selectConnection, da.SelectCommand.Connection, "#13");
132 Assert.IsNull (da.Site, "#14");
133 Assert.IsNotNull (da.TableMappings, "#15");
134 Assert.AreEqual (0, da.TableMappings.Count, "#16");
135 Assert.AreEqual (1, da.UpdateBatchSize, "#17");
136 Assert.IsNull (da.UpdateCommand, "#18");
139 [Test] // SqlDataAdapter (string, SqlConnection)
140 public void Constructor3_SelectCommandText_Null ()
142 SqlConnection selectConnection = new SqlConnection ();
144 SqlDataAdapter da = new SqlDataAdapter ((string) null,
146 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
147 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
148 Assert.IsNull (da.Container, "#3");
149 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
150 Assert.IsNull (da.DeleteCommand, "#5");
151 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
152 Assert.IsNull (da.InsertCommand, "#7");
153 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
154 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
155 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
156 Assert.IsNotNull (da.SelectCommand, "#11");
157 Assert.IsNotNull (da.SelectCommand.CommandText, "#12");
158 Assert.AreEqual (string.Empty, da.SelectCommand.CommandText, "#13");
159 Assert.AreSame (selectConnection, da.SelectCommand.Connection, "#14");
160 Assert.IsNull (da.Site, "#15");
161 Assert.IsNotNull (da.TableMappings, "#16");
162 Assert.AreEqual (0, da.TableMappings.Count, "#17");
163 Assert.AreEqual (1, da.UpdateBatchSize, "#18");
164 Assert.IsNull (da.UpdateCommand, "#19");
167 [Test] // SqlDataAdapter (string, SqlConnection)
168 public void Constructor3_SelectConnection_Null ()
170 string selectCommandText = "SELECT * FROM Authors";
172 SqlDataAdapter da = new SqlDataAdapter (selectCommandText,
173 (SqlConnection) null);
174 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
175 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
176 Assert.IsNull (da.Container, "#3");
177 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
178 Assert.IsNull (da.DeleteCommand, "#5");
179 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
180 Assert.IsNull (da.InsertCommand, "#7");
181 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
182 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
183 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
184 Assert.IsNotNull (da.SelectCommand, "#11");
185 Assert.AreSame (selectCommandText, da.SelectCommand.CommandText, "#12");
186 Assert.IsNull (da.SelectCommand.Connection, "#13");
187 Assert.IsNull (da.Site, "#14");
188 Assert.IsNotNull (da.TableMappings, "#15");
189 Assert.AreEqual (0, da.TableMappings.Count, "#16");
190 Assert.AreEqual (1, da.UpdateBatchSize, "#17");
191 Assert.IsNull (da.UpdateCommand, "#18");
194 [Test] // SqlDataAdapter (string, string)]
195 public void Constructor4 ()
197 string selectCommandText = "SELECT * FROM Authors";
198 string selectConnectionString = "server=SQLSRV;database=Mono";
200 SqlDataAdapter da = new SqlDataAdapter (selectCommandText,
201 selectConnectionString);
202 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
203 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
204 Assert.IsNull (da.Container, "#3");
205 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
206 Assert.IsNull (da.DeleteCommand, "#5");
207 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
208 Assert.IsNull (da.InsertCommand, "#7");
209 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
210 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
211 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
212 Assert.IsNotNull (da.SelectCommand, "#11");
213 Assert.AreSame (selectCommandText, da.SelectCommand.CommandText, "#12");
214 Assert.IsNotNull (da.SelectCommand.Connection, "#13");
215 Assert.AreEqual (selectConnectionString, da.SelectCommand.Connection.ConnectionString, "#14");
216 Assert.IsNull (da.Site, "#15");
217 Assert.IsNotNull (da.TableMappings, "#16");
218 Assert.AreEqual (0, da.TableMappings.Count, "#17");
219 Assert.AreEqual (1, da.UpdateBatchSize, "#18");
220 Assert.IsNull (da.UpdateCommand, "#19");
223 [Test] // SqlDataAdapter (string, string)]
224 public void Constructor4_SelectCommandText_Null ()
226 string selectConnectionString = "server=SQLSRV;database=Mono";
228 SqlDataAdapter da = new SqlDataAdapter ((string) null,
229 selectConnectionString);
230 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
231 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
232 Assert.IsNull (da.Container, "#3");
233 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
234 Assert.IsNull (da.DeleteCommand, "#5");
235 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
236 Assert.IsNull (da.InsertCommand, "#7");
237 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
238 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
239 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
240 Assert.IsNotNull (da.SelectCommand, "#11");
241 Assert.IsNotNull (da.SelectCommand.CommandText, "#12");
242 Assert.AreEqual (string.Empty, da.SelectCommand.CommandText, "#13");
243 Assert.IsNotNull (da.SelectCommand.Connection, "#14");
244 Assert.AreEqual (selectConnectionString, da.SelectCommand.Connection.ConnectionString, "#15");
245 Assert.IsNull (da.Site, "#16");
246 Assert.IsNotNull (da.TableMappings, "#17");
247 Assert.AreEqual (0, da.TableMappings.Count, "#18");
248 Assert.AreEqual (1, da.UpdateBatchSize, "#19");
249 Assert.IsNull (da.UpdateCommand, "#20");
252 [Test] // SqlDataAdapter (string, string)]
253 public void Constructor4_SelectConnectionString_Null ()
255 string selectCommandText = "SELECT * FROM Authors";
257 SqlDataAdapter da = new SqlDataAdapter (selectCommandText,
259 Assert.IsTrue (da.AcceptChangesDuringFill, "#1");
260 Assert.IsTrue (da.AcceptChangesDuringUpdate, "#2");
261 Assert.IsNull (da.Container, "#3");
262 Assert.IsFalse (da.ContinueUpdateOnError, "#4");
263 Assert.IsNull (da.DeleteCommand, "#5");
264 Assert.AreEqual (LoadOption.OverwriteChanges, da.FillLoadOption, "#6");
265 Assert.IsNull (da.InsertCommand, "#7");
266 Assert.AreEqual (MissingMappingAction.Passthrough, da.MissingMappingAction, "#8");
267 Assert.AreEqual (MissingSchemaAction.Add, da.MissingSchemaAction, "#9");
268 Assert.IsFalse (da.ReturnProviderSpecificTypes, "#10");
269 Assert.IsNotNull (da.SelectCommand, "#11");
270 Assert.AreSame (selectCommandText, da.SelectCommand.CommandText, "#12");
271 Assert.IsNotNull (da.SelectCommand.Connection, "#14");
272 Assert.AreEqual (string.Empty, da.SelectCommand.Connection.ConnectionString, "#15");
273 Assert.IsNull (da.Site, "#16");
274 Assert.IsNotNull (da.TableMappings, "#17");
275 Assert.AreEqual (0, da.TableMappings.Count, "#18");
276 Assert.AreEqual (1, da.UpdateBatchSize, "#19");
277 Assert.IsNull (da.UpdateCommand, "#20");
281 public void DeleteCommand ()
283 SqlDataAdapter da = new SqlDataAdapter ();
284 SqlCommand cmd1 = new SqlCommand ();
285 SqlCommand cmd2 = new SqlCommand ();
287 da.DeleteCommand = cmd1;
288 Assert.AreSame (cmd1, da.DeleteCommand, "#1");
289 da.DeleteCommand = cmd2;
290 Assert.AreSame (cmd2, da.DeleteCommand, "#2");
291 da.DeleteCommand = null;
292 Assert.IsNull (da.DeleteCommand, "#3");
296 public void Dispose ()
298 SqlDataAdapter da = new SqlDataAdapter ();
299 da.DeleteCommand = new SqlCommand ();
300 da.InsertCommand = new SqlCommand ();
301 da.SelectCommand = new SqlCommand ();
302 da.UpdateCommand = new SqlCommand ();
305 Assert.IsNull (da.DeleteCommand, "#1");
306 Assert.IsNull (da.InsertCommand, "#2");
307 Assert.IsNull (da.SelectCommand, "#3");
308 Assert.IsNotNull (da.TableMappings, "#4");
309 Assert.AreEqual (0, da.TableMappings.Count, "#5");
310 Assert.IsNull (da.UpdateCommand, "#6");
314 public void InsertCommand ()
316 SqlDataAdapter da = new SqlDataAdapter ();
317 SqlCommand cmd1 = new SqlCommand ();
318 SqlCommand cmd2 = new SqlCommand ();
320 da.InsertCommand = cmd1;
321 Assert.AreSame (cmd1, da.InsertCommand, "#1");
322 da.InsertCommand = cmd2;
323 Assert.AreSame (cmd2, da.InsertCommand, "#2");
324 da.InsertCommand = null;
325 Assert.IsNull (da.InsertCommand, "#3");
329 public void SelectCommand ()
331 SqlDataAdapter da = new SqlDataAdapter ();
332 SqlCommand cmd1 = new SqlCommand ();
333 SqlCommand cmd2 = new SqlCommand ();
335 da.SelectCommand = cmd1;
336 Assert.AreSame (cmd1, da.SelectCommand, "#1");
337 da.SelectCommand = cmd2;
338 Assert.AreSame (cmd2, da.SelectCommand, "#2");
339 da.SelectCommand = null;
340 Assert.IsNull (da.SelectCommand, "#3");
344 public void UpdateBatchSize ()
346 SqlDataAdapter da = new SqlDataAdapter ();
347 da.UpdateBatchSize = 0;
348 Assert.AreEqual (0, da.UpdateBatchSize, "#1");
349 da.UpdateBatchSize = int.MaxValue;
350 Assert.AreEqual (int.MaxValue, da.UpdateBatchSize, "#2");
351 da.UpdateBatchSize = 1;
352 Assert.AreEqual (1, da.UpdateBatchSize, "#3");
356 public void UpdateBatchSize_Negative ()
358 SqlDataAdapter da = new SqlDataAdapter ();
360 da.UpdateBatchSize = -1;
362 } catch (ArgumentOutOfRangeException ex) {
363 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#2");
364 Assert.IsNull (ex.InnerException, "#3");
365 Assert.IsNotNull (ex.Message, "#4");
366 Assert.IsNotNull (ex.ParamName, "#5");
367 Assert.AreEqual ("UpdateBatchSize", ex.ParamName, "#6");
372 public void UpdateCommand ()
374 SqlDataAdapter da = new SqlDataAdapter ();
375 SqlCommand cmd1 = new SqlCommand ();
376 SqlCommand cmd2 = new SqlCommand ();
378 da.UpdateCommand = cmd1;
379 Assert.AreSame (cmd1, da.UpdateCommand, "#1");
380 da.UpdateCommand = cmd2;
381 Assert.AreSame (cmd2, da.UpdateCommand, "#2");
382 da.UpdateCommand = null;
383 Assert.IsNull (da.UpdateCommand, "#3");
388 public void DeleteCommand_IDbDataAdapter ()
390 IDbDataAdapter da = new SqlDataAdapter ();
391 SqlCommand cmd1 = new SqlCommand ();
392 SqlCommand cmd2 = new SqlCommand ();
394 da.DeleteCommand = cmd1;
395 Assert.AreSame (cmd1, da.DeleteCommand, "#A1");
396 da.DeleteCommand = cmd2;
397 Assert.AreSame (cmd2, da.DeleteCommand, "#A2");
398 da.DeleteCommand = null;
399 Assert.IsNull (da.DeleteCommand, "#A3");
402 da.DeleteCommand = new OdbcCommand ();
404 } catch (InvalidCastException ex) {
405 Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#B2");
406 Assert.IsNull (ex.InnerException, "#B3");
407 Assert.IsNotNull (ex.Message, "#B4");
413 public void InsertCommand_IDbDataAdapter ()
415 IDbDataAdapter da = new SqlDataAdapter ();
416 SqlCommand cmd1 = new SqlCommand ();
417 SqlCommand cmd2 = new SqlCommand ();
419 da.InsertCommand = cmd1;
420 Assert.AreSame (cmd1, da.InsertCommand, "#A1");
421 da.InsertCommand = cmd2;
422 Assert.AreSame (cmd2, da.InsertCommand, "#A2");
423 da.InsertCommand = null;
424 Assert.IsNull (da.InsertCommand, "#A3");
427 da.InsertCommand = new OdbcCommand ();
429 } catch (InvalidCastException ex) {
430 Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#B2");
431 Assert.IsNull (ex.InnerException, "#B3");
432 Assert.IsNotNull (ex.Message, "#B4");
437 public void SelectCommand_IDbDataAdapter ()
439 IDbDataAdapter da = new SqlDataAdapter ();
440 SqlCommand cmd1 = new SqlCommand ();
441 SqlCommand cmd2 = new SqlCommand ();
443 da.SelectCommand = cmd1;
444 Assert.AreSame (cmd1, da.SelectCommand, "#A1");
445 da.SelectCommand = cmd2;
446 Assert.AreSame (cmd2, da.SelectCommand, "#A2");
447 da.SelectCommand = null;
448 Assert.IsNull (da.SelectCommand, "#A3");
451 da.SelectCommand = new OdbcCommand ();
453 } catch (InvalidCastException ex) {
454 Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#B2");
455 Assert.IsNull (ex.InnerException, "#B3");
456 Assert.IsNotNull (ex.Message, "#B4");
461 public void UpdateCommand_IDbDataAdapter ()
463 IDbDataAdapter da = new SqlDataAdapter ();
464 SqlCommand cmd1 = new SqlCommand ();
465 SqlCommand cmd2 = new SqlCommand ();
467 da.UpdateCommand = cmd1;
468 Assert.AreSame (cmd1, da.UpdateCommand, "#A1");
469 da.UpdateCommand = cmd2;
470 Assert.AreSame (cmd2, da.UpdateCommand, "#A2");
471 da.UpdateCommand = null;
472 Assert.IsNull (da.UpdateCommand, "#A3");
475 da.UpdateCommand = new OdbcCommand ();
477 } catch (InvalidCastException ex) {
478 Assert.AreEqual (typeof (InvalidCastException), ex.GetType (), "#B2");
479 Assert.IsNull (ex.InnerException, "#B3");
480 Assert.IsNotNull (ex.Message, "#B4");