2 // SqlDataAdapterTest.cs - NUnit Test Cases for testing the
3 // SqlDataAdapter class
5 // Umadevi S (sumadevi@novell.com)
6 // Sureshkumar T (tsureshkumar@novell.com)
7 // Senganal T (tsenganal@novell.com)
9 // Copyright (c) 2004 Novell Inc., and the individuals listed
10 // on the ChangeLog entries.
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Data.Common;
35 using System.Data.SqlClient;
36 using System.Configuration;
38 using NUnit.Framework;
40 namespace MonoTests.System.Data.Connected.SqlClient
43 [Category ("sqlserver")]
44 public class SqlDataAdapterTest
46 SqlDataAdapter adapter;
49 string connectionString = ConnectionManager.Instance.Sql.ConnectionString;
56 engine = ConnectionManager.Instance.Sql.EngineConfig;
60 public void TearDown ()
62 if (adapter != null) {
79 [Category("NotWorking")]
80 public void Update_DeleteRow ()
82 conn = new SqlConnection (ConnectionManager.Instance.Sql.ConnectionString);
85 DataTable dt = new DataTable ();
86 adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
87 SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
88 adapter.DeleteCommand = builder.GetDeleteCommand ();
91 DateTime now = DateTime.Now;
93 DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
94 now.Minute, now.Second);
96 DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
97 now.Minute, now.Second);
98 dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
101 DataRow newRow = dt.NewRow ();
102 newRow ["id"] = 6002;
103 newRow ["fname"] = "boston";
104 newRow ["dob"] = dob;
105 newRow ["doj"] = doj;
106 newRow ["email"] = "mono@novell.com";
107 dt.Rows.Add (newRow);
110 foreach (DataRow row in dt.Rows)
111 if (((int) row ["id"]) == 6002)
115 SqlCommand cmd = conn.CreateCommand ();
116 cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
117 dr = cmd.ExecuteReader ();
118 Assert.IsFalse (dr.Read ());
121 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
126 [Category("NotWorking")]
127 public void Update_InsertRow ()
129 conn = new SqlConnection (ConnectionManager.Instance.Sql.ConnectionString);
132 DataTable dt = new DataTable ();
133 adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
135 SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
136 adapter.InsertCommand = builder.GetInsertCommand ();
139 DateTime now = DateTime.Now;
141 DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
142 now.Minute, now.Second);
144 DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
145 now.Minute, now.Second);
146 dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
149 DataRow newRow = dt.NewRow ();
150 newRow ["id"] = 6002;
151 newRow ["fname"] = "boston";
152 newRow ["dob"] = dob;
153 newRow ["doj"] = doj;
154 newRow ["email"] = "mono@novell.com";
155 dt.Rows.Add (newRow);
158 SqlCommand cmd = conn.CreateCommand ();
159 cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
160 dr = cmd.ExecuteReader ();
161 Assert.IsTrue (dr.Read (), "#A1");
162 Assert.AreEqual (6002, dr.GetValue (0), "#A2");
163 Assert.AreEqual ("boston", dr.GetValue (1), "#A3");
164 Assert.AreEqual (DBNull.Value, dr.GetValue (2), "#A4");
165 Assert.AreEqual (dob, dr.GetValue (3), "#A5");
166 Assert.AreEqual (doj, dr.GetValue (4), "#A6");
167 Assert.AreEqual ("mono@novell.com", dr.GetValue (5), "#A7");
168 Assert.IsFalse (dr.Read (), "#A8");
171 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
176 [Category("NotWorking")]
177 public void Update_UpdateRow ()
179 conn = new SqlConnection (ConnectionManager.Instance.Sql.ConnectionString);
182 DataTable dt = new DataTable ();
183 adapter = new SqlDataAdapter ("SELECT * FROM employee", conn);
184 SqlCommandBuilder builder = new SqlCommandBuilder (adapter);
185 adapter.UpdateCommand = builder.GetUpdateCommand ();
188 DateTime now = DateTime.Now;
190 DateTime doj = new DateTime (now.Year, now.Month, now.Day, now.Hour,
191 now.Minute, now.Second);
193 DateTime dob = new DateTime (now.Year, now.Month, now.Day, now.Hour,
194 now.Minute, now.Second);
195 dob.Subtract (new TimeSpan (20 * 365, 0, 0, 0));
198 DataRow newRow = dt.NewRow ();
199 newRow ["id"] = 6002;
200 newRow ["fname"] = "boston";
201 newRow ["dob"] = dob;
202 newRow ["doj"] = doj;
203 newRow ["email"] = "mono@novell.com";
204 dt.Rows.Add (newRow);
207 foreach (DataRow row in dt.Rows)
208 if (((int) row ["id"]) == 6002)
209 row ["lname"] = "de Icaza";
212 SqlCommand cmd = conn.CreateCommand ();
213 cmd.CommandText = "SELECT id, fname, lname, dob, doj, email FROM employee WHERE id = 6002";
214 dr = cmd.ExecuteReader ();
215 Assert.IsTrue (dr.Read (), "#A1");
216 Assert.AreEqual (6002, dr.GetValue (0), "#A2");
217 Assert.AreEqual ("boston", dr.GetValue (1), "#A3");
218 Assert.AreEqual ("de Icaza", dr.GetValue (2), "#A4");
219 Assert.AreEqual (dob, dr.GetValue (3), "#A5");
220 Assert.AreEqual (doj, dr.GetValue (4), "#A6");
221 Assert.AreEqual ("mono@novell.com", dr.GetValue (5), "#A7");
222 Assert.IsFalse (dr.Read (), "#A8");
225 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
230 The below test will not run everytime, since the region id column is unique
231 so change the regionid if you want the test to pass.
235 public void UpdateTest () {
236 conn = (SqlConnection) ConnectionManager.Singleton.Connection;
238 ConnectionManager.Singleton.OpenConnection ();
239 DataTable dt = new DataTable();
240 SqlDataAdapter da = null;
241 da = new SqlDataAdapter("Select * from employee", conn);
242 //SqlCommandBuilder cb = new SqlCommandBuilder (da);
244 DataRow dr = dt.NewRow();
246 dr ["fname"] = "boston";
247 dr ["dob"] = DateTime.Now.Subtract (new TimeSpan (20*365, 0, 0, 0));
248 dr ["doj"] = DateTime.Now;
253 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
254 ConnectionManager.Singleton.Sql.CloseConnection ();
258 private static void OnRowUpdatedTest (object sender, SqlRowUpdatedEventArgs e)
263 private static void OnRowUpdatingTest (object sender, SqlRowUpdatingEventArgs e)
268 private static bool rowUpdated = false;
269 private static bool rowUpdating = false;
271 public void RowUpdatedTest () {
272 conn = (SqlConnection) ConnectionManager.Singleton.Connection;
274 ConnectionManager.Singleton.OpenConnection ();
276 DataSet ds = new DataSet ();
277 SqlDataAdapter da = null;
278 da = new SqlDataAdapter("Select * from employee", conn);
279 //SqlCommandBuilder cb = new SqlCommandBuilder (da);
282 da.RowUpdated += new SqlRowUpdatedEventHandler (OnRowUpdatedTest);
283 da.RowUpdating += new SqlRowUpdatingEventHandler (OnRowUpdatingTest);
287 da.UpdateCommand = new SqlCommand ("Update employee set id = @id");
291 da.RowUpdated -= new SqlRowUpdatedEventHandler (OnRowUpdatedTest);
292 da.RowUpdating -= new SqlRowUpdatingEventHandler (OnRowUpdatingTest);
293 Assert.AreEqual (true, rowUpdated, "RowUpdated");
294 Assert.AreEqual (true, rowUpdating, "RowUpdating");
296 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
297 ConnectionManager.Singleton.Sql.CloseConnection ();
303 This needs a errortable created as follows
304 id uniqueidentifier,name char(10) , with values
310 public void NullGuidTest()
312 conn = ConnectionManager.Instance.Sql.Connection;
314 DBHelper.ExecuteNonQuery (conn, "create table #tmp_guid_table ( " +
315 " id uniqueidentifier default newid (), " +
317 DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (name) values (null)");
318 DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (id, name) values (null, 'bbbb')");
319 SqlDataAdapter da = new SqlDataAdapter("select * from #tmp_guid_table", conn);
320 DataSet ds = new DataSet();
322 Assert.AreEqual (1, ds.Tables.Count, "#1");
323 Assert.AreEqual (DBNull.Value, ds.Tables [0].Rows [1] ["id"], "#2");
325 ConnectionManager.Instance.Sql.CloseConnection ();
327 // the bug 68804 - is that the fill hangs!
328 Assert.AreEqual("Done","Done");
332 public void DefaultConstructorTest ()
334 adapter = new SqlDataAdapter ();
335 Assert.AreEqual (MissingMappingAction.Passthrough,
336 adapter.MissingMappingAction,
337 "#1 Missing Mapping acttion default to Passthrough");
338 Assert.AreEqual (MissingSchemaAction.Add,
339 adapter.MissingSchemaAction,
340 "#2 Missing Schme action default to Add");
344 public void OverloadedConstructorsTest ()
346 SqlCommand selCmd = new SqlCommand ("Select * from numeric_family");
347 adapter = new SqlDataAdapter (selCmd);
348 Assert.AreEqual (MissingMappingAction.Passthrough,
349 adapter.MissingMappingAction,
350 "#1 Missing Mapping acttion default to Passthrough");
351 Assert.AreEqual (MissingSchemaAction.Add,
352 adapter.MissingSchemaAction,
353 "#2 Missing Schme action default to Add");
354 Assert.AreSame (selCmd, adapter.SelectCommand,
355 "#3 Select Command shud be a ref to the arg passed");
357 conn = new SqlConnection (connectionString);
358 String selStr = "Select * from numeric_family";
359 adapter = new SqlDataAdapter (selStr, conn);
360 Assert.AreEqual (MissingMappingAction.Passthrough,
361 adapter.MissingMappingAction,
362 "#4 Missing Mapping acttion default to Passthrough");
363 Assert.AreEqual (MissingSchemaAction.Add,
364 adapter.MissingSchemaAction,
365 "#5 Missing Schme action default to Add");
366 Assert.AreSame (selStr, adapter.SelectCommand.CommandText,
367 "#6 Select Command shud be a ref to the arg passed");
368 Assert.AreSame (conn, adapter.SelectCommand.Connection,
369 "#7 cmd.connection shud be t ref to connection obj");
371 selStr = "Select * from numeric_family";
372 adapter = new SqlDataAdapter (selStr, connectionString);
373 Assert.AreEqual (MissingMappingAction.Passthrough,
374 adapter.MissingMappingAction,
375 "#8 Missing Mapping action shud default to Passthrough");
376 Assert.AreEqual (MissingSchemaAction.Add,
377 adapter.MissingSchemaAction,
378 "#9 Missing Schema action shud default to Add");
379 Assert.AreSame (selStr,
380 adapter.SelectCommand.CommandText,
382 Assert.AreEqual (connectionString,
383 adapter.SelectCommand.Connection.ConnectionString,
388 public void Fill_Test_ConnState ()
390 //Check if Connection State is maintained correctly ..
391 data = new DataSet ("test1");
392 adapter = new SqlDataAdapter ("select id from numeric_family where id=1",
394 SqlCommand cmd = adapter.SelectCommand ;
396 Assert.AreEqual (ConnectionState.Closed,
397 cmd.Connection.State, "#1 Connection shud be in closed state");
399 Assert.AreEqual (1, data.Tables.Count, "#2 One table shud be populated");
400 Assert.AreEqual (ConnectionState.Closed, cmd.Connection.State,
401 "#3 Connection shud be closed state");
403 data = new DataSet ("test2");
404 cmd.Connection.Open ();
405 Assert.AreEqual (ConnectionState.Open, cmd.Connection.State,
406 "#3 Connection shud be open");
408 Assert.AreEqual (1, data.Tables.Count, "#4 One table shud be populated");
409 Assert.AreEqual (ConnectionState.Open, cmd.Connection.State,
410 "#5 Connection shud be open");
411 cmd.Connection.Close ();
413 // Test if connection is closed when exception occurs
414 cmd.CommandText = "select id1 from numeric_family";
418 if (cmd.Connection.State == ConnectionState.Open) {
419 cmd.Connection.Close ();
420 Assert.Fail ("# Connection Shud be Closed");
426 [Category("NotWorking")]
427 public void Fill_Test_Data ()
429 //Check if a table is created for each resultset
430 String batchQuery = "Select id,type_bit,type_int from numeric_family;";
431 batchQuery += "Select type_bit from numeric_family";
432 adapter = new SqlDataAdapter (batchQuery, connectionString);
433 data = new DataSet ("test1");
435 Assert.AreEqual (2, data.Tables.Count,"#1 2 Table shud be created");
437 //Check if Table and Col are named correctly for unnamed columns
438 string query = "Select 10,20 from numeric_family;" ;
439 query += "Select 10,20 from numeric_family";
440 adapter = new SqlDataAdapter (query, connectionString);
441 data = new DataSet ("test2");
443 Assert.AreEqual (2, data.Tables.Count,
444 "#2 2 Tables shud be created");
445 Assert.AreEqual ("Table", data.Tables[0].TableName, "#3");
446 Assert.AreEqual ("Table1", data.Tables[1].TableName, "#4");
447 Assert.AreEqual ("Column1", data.Tables[0].Columns[0].ColumnName, "#5");
448 Assert.AreEqual ("Column2", data.Tables[0].Columns[1].ColumnName, "#6");
449 Assert.AreEqual ("Column1", data.Tables[1].Columns[0].ColumnName, "#7");
450 Assert.AreEqual ("Column2", data.Tables[1].Columns[1].ColumnName, "#8");
452 //Check if dup columns are named correctly
453 query = "select A.id ,B.id , C.id from numeric_family A, ";
454 query += "numeric_family B , numeric_family C";
455 adapter = new SqlDataAdapter (query, connectionString);
456 data = new DataSet ("test3");
459 // NOTE msdotnet contradicts documented behavior
460 // as per documentation the column names should be
461 // id1,id2,id3 .. but msdotnet returns id,id1,id2
462 Assert.AreEqual ("id", data.Tables[0].Columns[0].ColumnName,
463 "#9 if colname is duplicated ,shud be col,col1,col2 etc");
464 Assert.AreEqual ("id1", data.Tables[0].Columns[1].ColumnName,
465 "#10 if colname is duplicated ,shud be col,col1,col2 etc");
466 Assert.AreEqual ("id2", data.Tables[0].Columns[2].ColumnName,
467 "#11 if colname is duplicated ,shud be col,col1,col2 etc");
469 // Test if tables are created and named accordingly ,
470 // but only for those queries returning result sets
471 query = "update numeric_family set id=100 where id=50;";
472 query += "select * from numeric_family";
473 adapter = new SqlDataAdapter (query, connectionString);
474 data = new DataSet ("test4");
476 Assert.AreEqual (1 ,data.Tables.Count,
477 "#12 Tables shud be named only for queries returning a resultset");
478 Assert.AreEqual ("Table", data.Tables[0].TableName,
479 "#13 The first resutlset shud have 'Table' as its name");
481 // Test behavior with an outerjoin
482 query = "select A.id,B.type_bit from numeric_family A LEFT OUTER JOIN ";
483 query += "numeric_family B on A.id = B.type_bit";
484 adapter = new SqlDataAdapter (query, connectionString);
485 data = new DataSet ("test5");
487 Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
488 "#14 Primary Key shudnt be set if an outer join is performed");
489 Assert.AreEqual (0, data.Tables[0].Constraints.Count,
490 "#15 Constraints shudnt be set if an outer join is performed");
491 adapter = new SqlDataAdapter ("select id from numeric_family",
493 data = new DataSet ("test6");
494 adapter.Fill (data, 1, 1, "numeric_family");
495 Assert.AreEqual (1, data.Tables[0].Rows.Count, "#16");
496 Assert.AreEqual (2, data.Tables[0].Rows[0][0], "#17");
498 // only one test for DataTable.. DataSet tests covers others
499 adapter = new SqlDataAdapter ("select id from numeric_family",
501 DataTable table = new DataTable ("table1");
502 adapter.Fill (table);
503 Assert.AreEqual (4, table.Rows.Count , "#18");
507 public void Fill_Test_PriKey ()
509 // Test if Primary Key & Constraints Collection is correct
510 adapter = new SqlDataAdapter ("select id,type_bit from numeric_family",
512 adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
513 data = new DataSet ("test1");
515 Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
516 "#1 Primary Key shud be set");
517 Assert.AreEqual (1, data.Tables[0].Constraints.Count,
518 "#2 Constraints shud be set");
519 Assert.AreEqual (4, data.Tables[0].Rows.Count,
520 "#3 No Of Rows shud be 4");
522 // Test if data is correctly merged
524 Assert.AreEqual (4, data.Tables[0].Rows.Count,
525 "#4 No of Row shud still be 4");
527 // Test if rows are appended and not merged
528 // when primary key is not returned in the result-set
529 string query = "Select type_int from numeric_family";
530 adapter.SelectCommand.CommandText = query;
531 data = new DataSet ("test2");
533 Assert.AreEqual (4, data.Tables[0].Rows.Count,
534 "#5 No of Rows shud be 4");
536 Assert.AreEqual (8, data.Tables[0].Rows.Count,
537 "#6 No of Rows shud double now");
541 public void Fill_Test_Exceptions ()
543 adapter = new SqlDataAdapter ("select * from numeric_family",
545 data = new DataSet ("test1");
547 adapter.Fill (data, -1, 0, "numeric_family");
548 Assert.Fail ("#1 Exception shud be thrown:Incorrect Arguments");
549 }catch (AssertionException e){
551 }catch (Exception e){
552 Assert.AreEqual (typeof(ArgumentException), e.GetType(),
553 "#2 Incorrect Exception : " + e);
556 // conn is not closed due to a bug..
557 // can be removed later
558 adapter.SelectCommand.Connection.Close ();
561 adapter.Fill (data , 0 , -1 , "numeric_family");
562 Assert.Fail ("#3 Exception shud be thrown:Incorrect Arguments");
563 }catch (AssertionException e){
565 }catch (Exception e){
566 Assert.AreEqual (typeof(ArgumentException), e.GetType(),
567 "#4 Incorrect Exception : " + e);
569 // conn is curr not closed.. can be removed later
570 adapter.SelectCommand.Connection.Close ();
573 // NOTE msdotnet contradicts documented behavior
574 // InvalidOperationException is expected if table is not valid
576 adapter.Fill (data , 0 , 0 , "invalid_talbe_name");
577 }catch (InvalidOperationException e) {
579 }catch (Exception e){
580 Assert.Fail ("#5 Exception shud be thrown : incorrect arugments ");
582 Assert.IsNotNull (ex , "#6 Exception shud be thrown : incorrect args ");
583 adapter.SelectCommand.Connection.Close (); // tmp .. can be removed once the bug if fixed
588 adapter.Fill ( null , 0 , 0 , "numeric_family");
589 Assert.Fail ( "#7 Exception shud be thrown : Invalid Dataset");
590 }catch (AssertionException e){
592 }catch (ArgumentNullException) {
594 }catch (Exception e) {
595 Assert.AreEqual (typeof(SystemException), e.GetType(),
596 "#8 Incorrect Exception : " + e);
598 // conn is currently not being closed..
599 //need to be removed once behavior is fixed
600 adapter.SelectCommand.Connection.Close ();
602 adapter.SelectCommand.Connection = null;
605 Assert.Fail ("#9 Exception shud be thrown : Invalid Connection");
606 }catch (AssertionException e){
608 }catch (Exception e){
609 Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
610 "#10 Incorrect Exception : " + e);
614 bool FillErrorContinue = false;
616 public void Fill_Test_FillErrorTest ()
618 string query = "select type_int from numeric_family where id=1 or id=4 ";
620 DataSet ds = new DataSet ();
621 DataTable table = ds.Tables.Add ("test");
622 table.Columns.Add ("col", typeof (short));
624 adapter = new SqlDataAdapter (query, connectionString);
625 DataTableMapping mapping = adapter.TableMappings.Add ("numeric_family", "test");
626 mapping.ColumnMappings.Add ("type_int", "col");
629 adapter.Fill (ds, "numeric_family");
631 } catch (OverflowException) {
632 } catch (ArgumentException ex) {
633 // System.OverflowException: Value was either too large or too
634 // small for an Int16
635 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#A2");
636 Assert.IsNotNull (ex.InnerException, "#A3");
637 Assert.IsNotNull (ex.Message, "#A4");
638 Assert.IsNull (ex.ParamName, "#A5");
640 OverflowException inner = ex.InnerException as OverflowException;
641 Assert.IsNotNull (inner, "#A6");
642 Assert.AreEqual (typeof (OverflowException), inner.GetType (), "#A7");
643 Assert.IsNull (inner.InnerException, "#A8");
644 Assert.IsNotNull (inner.Message, "#A9");
646 Assert.AreEqual (0, ds.Tables [0].Rows.Count, "#A10");
648 adapter.FillError += new FillErrorEventHandler (ErrorHandler);
649 FillErrorContinue = false;
651 adapter.Fill (ds, "numeric_family");
653 } catch (OverflowException) {
654 } catch (ArgumentException ex) {
655 // System.OverflowException: Value was either too large or too
656 // small for an Int16
657 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
658 Assert.IsNotNull (ex.InnerException, "#B3");
659 Assert.IsNotNull (ex.Message, "#B4");
660 Assert.IsNull (ex.ParamName, "#B5");
662 OverflowException inner = ex.InnerException as OverflowException;
663 Assert.IsNotNull (inner, "#B6");
664 Assert.AreEqual (typeof (OverflowException), inner.GetType (), "#B7");
665 Assert.IsNull (inner.InnerException, "#B8");
666 Assert.IsNotNull (inner.Message, "#B9");
668 Assert.AreEqual (0, ds.Tables [0].Rows.Count, "#B10");
670 FillErrorContinue = true;
671 int count = adapter.Fill (ds, "numeric_family");
672 Assert.AreEqual (1, ds.Tables [0].Rows.Count, "#C1");
673 Assert.AreEqual (1, count, "#C2");
676 void ErrorHandler (object sender, FillErrorEventArgs args)
678 args.Continue = FillErrorContinue;
682 public void GetFillParametersTest ()
684 string query = "select id, type_bit from numeric_family where id > @param1";
685 adapter = new SqlDataAdapter (query, connectionString);
686 IDataParameter[] param = adapter.GetFillParameters ();
687 Assert.AreEqual (0, param.Length, "#1 size shud be 0");
689 SqlParameter param1 = new SqlParameter ();
690 param1.ParameterName = "@param1";
692 adapter.SelectCommand.Parameters.Add (param1);
694 param = adapter.GetFillParameters ();
695 Assert.AreEqual (1, param.Length, "#2 count shud be 1");
696 Assert.AreEqual (param1, param[0], "#3 Params shud be equal");
700 public void FillSchemaTest ()
704 // Test if connection is closed if excepton occurs during fill schema
705 query = "select * from invalid_table";
706 adapter = new SqlDataAdapter (query, connectionString);
707 data = new DataSet ("test");
709 adapter.FillSchema (data , SchemaType.Source);
711 if (adapter.SelectCommand.Connection.State != ConnectionState.Closed) {
712 Assert.Fail ("#0 Conn shud be closed if exception occurs");
713 adapter.SelectCommand.Connection.Close();
717 // Test Primary Key is set (since primary key column returned)
718 query = "select id, type_int from numeric_family where id=1";
719 adapter = new SqlDataAdapter (query, connectionString);
720 data = new DataSet ("test1");
721 adapter.FillSchema (data , SchemaType.Source);
723 Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length,
724 "#1 Primary Key property must be set");
726 // Test Primary Key is not set (since primary key column is returned)
727 query = "select type_bit, type_int from numeric_family where id=1";
728 adapter = new SqlDataAdapter (query, connectionString);
729 data = new DataSet ("test2");
730 adapter.FillSchema (data, SchemaType.Source);
731 Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length,
732 "#2 Primary Key property should not be set");
734 // Test multiple tables are created for a batch query
735 query = "Select id ,type_bit from numeric_family;" ;
736 query += "Select id,type_bit,type_int from numeric_family;";
737 data = new DataSet ("test3");
738 adapter = new SqlDataAdapter (query, connectionString);
739 adapter.FillSchema (data , SchemaType.Source);
740 Assert.AreEqual (2 , data.Tables.Count , "#3 A table shud be created for each Result Set");
741 Assert.AreEqual (2 , data.Tables[0].Columns.Count , "#4 should have 2 columns");
742 Assert.AreEqual (3 , data.Tables[1].Columns.Count , "#5 Should have 3 columns");
744 // Test if table names and column names are filled correctly
745 query = "select 10,20 from numeric_family;" ;
746 query += "select 10,20 from numeric_family;";
747 adapter = new SqlDataAdapter (query, connectionString);
748 data = new DataSet ("test4");
750 adapter.FillSchema (data , SchemaType.Source);
751 }catch (Exception e){
752 Assert.Fail ("#3 Unexpected Exception : " + e);
754 Assert.AreEqual ( "Table", data.Tables[0].TableName);
755 Assert.AreEqual ( "Table1", data.Tables[1].TableName);
756 Assert.AreEqual ( "Column1", data.Tables[0].Columns[0].ColumnName,
757 "#6 Unnamed col shud be named as 'ColumnN'");
758 Assert.AreEqual ( "Column2", data.Tables[0].Columns[1].ColumnName,
759 "#7 Unnamed col shud be named as 'ColumnN'");
760 Assert.AreEqual ( "Column1", data.Tables[1].Columns[0].ColumnName,
761 "#8 Unnamed col shud be named as 'ColumnN'");
762 Assert.AreEqual ( "Column2", data.Tables[1].Columns[1].ColumnName,
763 "#9 Unnamed col shud be named as 'ColumnN'");
764 Assert.AreEqual (ConnectionState.Closed, adapter.SelectCommand.Connection.State,
765 "#10 Connection shud be closed");
767 // Test if mapping works correctly
768 // doesent work in both mono and msdotnet
769 // gotto check if something is wrong
771 query = "select id,type_bit from numeric_family";
772 adapter = new SqlDataAdapter (query, connectionString);
773 data = new DataSet ("test");
774 DataTable table = data.Tables.Add ("numeric_family_1");
775 table.Columns.Add ("id");
776 table.Columns.Add ("type_bit");
777 DataTableMapping map = adapter.TableMappings.Add("numeric_family_1",
779 map.ColumnMappings.Add ("id", "id_1");
780 map.ColumnMappings.Add ("type_bit", "type_bit_1");
781 adapter.FillSchema (data, SchemaType.Source, "numeric_family");
782 foreach (DataTable tab in data.Tables){
783 Console.WriteLine ("Table == {0}",tab.TableName);
784 foreach (DataColumn col in tab.Columns)
785 Console.WriteLine (" Col = {0} " , col.ColumnName);
791 [Category("NotWorking")]
792 public void MissingSchemaActionTest ()
794 adapter = new SqlDataAdapter (
795 "select id,type_bit,type_int from numeric_family where id<=4",
797 data = new DataSet ();
798 Assert.AreEqual (MissingSchemaAction.Add, adapter.MissingSchemaAction,
802 Assert.AreEqual (1, data.Tables.Count , "#1 One table shud be populated");
803 Assert.AreEqual (3, data.Tables[0].Columns.Count, "#2 Missing cols are added");
804 Assert.AreEqual (0, data.Tables[0].PrimaryKey.Length, "#3 Default Value");
806 adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
809 Assert.AreEqual (3, data.Tables[0].Columns.Count,
810 "#4 Missing cols are added");
811 Assert.AreEqual (1, data.Tables[0].PrimaryKey.Length, "#5 Default Value");
813 adapter.MissingSchemaAction = MissingSchemaAction.Ignore ;
816 Assert.AreEqual (0, data.Tables.Count, "#6 Data shud be ignored");
818 adapter.MissingSchemaAction = MissingSchemaAction.Error ;
822 Assert.Fail ("#8 Exception shud be thrown: Schema Mismatch");
823 } catch (InvalidOperationException ex) {
824 Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(),
828 // Test for invalid MissingSchema Value
830 adapter.MissingSchemaAction = (MissingSchemaAction)(-5000);
831 Assert.Fail ("#10 Exception shud be thrown: Invalid Value");
832 } catch (ArgumentOutOfRangeException ex) {
833 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#11");
836 // Tests if Data is filled correctly if schema is defined
837 // manually and MissingSchemaAction.Error is set
838 adapter.MissingSchemaAction = MissingSchemaAction.Error;
840 DataTable table = data.Tables.Add ("Table");
841 table.Columns.Add ("id");
842 table.Columns.Add ("type_bit");
843 table.Columns.Add ("type_int");
845 Assert.AreEqual (1, data.Tables.Count, "#12");
846 Assert.AreEqual (4, data.Tables[0].Rows.Count, "#13");
850 [Category("NotWorking")]
851 public void MissingMappingActionTest ()
853 adapter = new SqlDataAdapter ("select id,type_bit from numeric_family where id=1",
855 data = new DataSet ();
856 Assert.AreEqual (adapter.MissingMappingAction,
857 MissingMappingAction.Passthrough,
860 Assert.AreEqual (1, data.Tables.Count,
861 "#2 One Table shud be created");
862 Assert.AreEqual (2, data.Tables[0].Columns.Count,
863 "#3 Two Cols shud be created");
865 adapter.MissingMappingAction = MissingMappingAction.Ignore;
868 Assert.AreEqual (0, data.Tables.Count, "#4 No table shud be created");
870 adapter.MissingMappingAction = MissingMappingAction.Error;
874 Assert.Fail ("#5 Exception shud be thrown : Mapping is missing");
875 } catch (InvalidOperationException ex) {
876 Assert.AreEqual (typeof(InvalidOperationException), ex.GetType(),
881 adapter.MissingMappingAction = (MissingMappingAction)(-5000);
882 Assert.Fail ("#7 Exception shud be thrown : Invalid Value");
883 } catch (ArgumentOutOfRangeException ex) {
884 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (),
888 // Test if mapping the column and table names works correctly
889 adapter.MissingMappingAction = MissingMappingAction.Error;
891 DataTable table = data.Tables.Add ("numeric_family_1");
892 table.Columns.Add ("id_1");
893 table.Columns.Add ("type_bit_1");
894 table.Columns.Add ("type_int_1");
895 DataTableMapping tableMap = adapter.TableMappings.Add ("numeric_family",
897 tableMap.ColumnMappings.Add ("id", "id_1");
898 tableMap.ColumnMappings.Add ("type_bit", "type_bit_1");
899 tableMap.ColumnMappings.Add ("type_int", "type_int_1");
900 adapter.Fill (data,"numeric_family");
901 Assert.AreEqual (1, data.Tables.Count ,
902 "#8 The DataTable shud be correctly mapped");
903 Assert.AreEqual (3, data.Tables[0].Columns.Count,
904 "#9 The DataColumns shud be corectly mapped");
905 Assert.AreEqual (1, data.Tables[0].Rows.Count,
906 "#10 Data shud be populated if mapping is correct");
910 public void FillSchema_ValuesTest()
912 using (SqlConnection conn = new SqlConnection(connectionString)) {
914 IDbCommand command = conn.CreateCommand();
917 String cmd = "Create Table #tmp_TestTable (" ;
918 cmd += "Field1 DECIMAL (10) NOT NULL,";
919 cmd += "Field2 DECIMAL(19))";
920 command.CommandText = cmd;
921 command.ExecuteNonQuery();
923 DataSet dataSet = new DataSet();
924 string selectString = "SELECT * FROM #tmp_TestTable";
925 IDbDataAdapter dataAdapter = new SqlDataAdapter (
927 dataAdapter.FillSchema(dataSet, SchemaType.Mapped);
929 Assert.AreEqual (1, dataSet.Tables.Count, "#1");
930 Assert.IsFalse (dataSet.Tables[0].Columns[0].AllowDBNull,"#2");
931 Assert.IsTrue (dataSet.Tables[0].Columns[1].AllowDBNull,"#3");
936 public void Fill_CheckSchema ()
938 using (SqlConnection conn = new SqlConnection(connectionString)) {
941 IDbCommand command = conn.CreateCommand();
944 String cmd = "Create Table #tmp_TestTable (" ;
945 cmd += "id int primary key,";
946 cmd += "field int not null)";
947 command.CommandText = cmd;
948 command.ExecuteNonQuery();
950 DataSet dataSet = new DataSet();
951 string selectString = "SELECT * from #tmp_TestTable";
952 IDbDataAdapter dataAdapter = new SqlDataAdapter (
954 dataAdapter.Fill (dataSet);
955 Assert.AreEqual (1, dataSet.Tables.Count, "#A1");
956 Assert.AreEqual (2, dataSet.Tables [0].Columns.Count, "#A2");
957 Assert.IsTrue (dataSet.Tables [0].Columns [1].AllowDBNull, "#A3");
958 Assert.AreEqual (0, dataSet.Tables [0].PrimaryKey.Length, "#A4");
961 dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
962 dataAdapter.Fill (dataSet);
963 Assert.AreEqual (1, dataSet.Tables.Count, "#B1");
964 Assert.AreEqual (2, dataSet.Tables [0].Columns.Count, "#B2");
965 Assert.IsFalse (dataSet.Tables [0].Columns [1].AllowDBNull, "#B3");
966 if (ClientVersion == 7)
967 Assert.AreEqual (0, dataSet.Tables [0].PrimaryKey.Length, "#B4");
969 Assert.AreEqual (1, dataSet.Tables [0].PrimaryKey.Length, "#B4");
974 public void FillSchema_CheckSchema ()
976 using (SqlConnection conn = new SqlConnection(connectionString)) {
979 IDbCommand command = conn.CreateCommand();
982 String cmd = "Create Table #tmp_TestTable (" ;
983 cmd += "id int primary key,";
984 cmd += "field int not null)";
985 command.CommandText = cmd;
986 command.ExecuteNonQuery();
988 DataSet dataSet = new DataSet();
989 string selectString = "SELECT * from #tmp_TestTable";
990 IDbDataAdapter dataAdapter = new SqlDataAdapter (
993 dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
994 Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#1");
997 dataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
998 dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
999 Assert.IsFalse (dataSet.Tables[0].Columns[1].AllowDBNull, "#2");
1002 dataAdapter.MissingSchemaAction = MissingSchemaAction.Ignore;
1003 dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
1004 Assert.AreEqual (0, dataSet.Tables.Count, "#3");
1007 dataAdapter.MissingSchemaAction = MissingSchemaAction.Error;
1009 dataAdapter.FillSchema (dataSet, SchemaType.Mapped);
1010 Assert.Fail ("#4 Error should be thrown");
1011 } catch (InvalidOperationException ex) {
1012 Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#4");
1018 [Ignore("TODO: Set SSPI Connection String")]
1019 public void CreateViewSSPITest ()
1021 var conn = ConnectionManager.Instance.Sql.Connection;
1023 string sql = "create view MONO_TEST_VIEW as select * from Numeric_family";
1025 SqlCommand dbcmd = new SqlCommand( sql, conn );
1026 dbcmd.ExecuteNonQuery();
1028 sql = "drop view MONO_TEST_VIEW";
1030 dbcmd = new SqlCommand( sql, conn );
1031 dbcmd.ExecuteNonQuery();
1037 public void Fill_RelatedTables ()
1039 SqlConnection conn = new SqlConnection(connectionString);
1042 IDbCommand command = conn.CreateCommand();
1044 DataSet dataSet = new DataSet();
1045 string selectString = "SELECT id, type_int from numeric_family where id < 3";
1046 DbDataAdapter dataAdapter = new SqlDataAdapter (selectString,conn);
1048 DataTable table2 = dataSet.Tables.Add ("table2");
1049 DataColumn ccol1 = table2.Columns.Add ("id", typeof (int));
1050 DataColumn ccol2 = table2.Columns.Add ("type_int", typeof (int));
1052 DataTable table1 = dataSet.Tables.Add ("table1");
1053 DataColumn pcol1 = table1.Columns.Add ("id", typeof (int));
1054 DataColumn pcol2 = table1.Columns.Add ("type_int", typeof (int));
1056 table2.Constraints.Add ("fk", pcol1, ccol1);
1057 //table1.Constraints.Add ("fk1", pcol2, ccol2);
1059 dataSet.EnforceConstraints = false;
1060 dataAdapter.Fill (dataSet, "table1");
1061 dataAdapter.Fill (dataSet, "table2");
1063 //Should not throw an exception
1064 dataSet.EnforceConstraints = true;
1066 Assert.AreEqual (2, table1.Rows.Count, "#1");
1067 Assert.AreEqual (2, table2.Rows.Count, "#2");
1072 public void UpdateBatchSizeTest ()
1074 adapter = new SqlDataAdapter();
1075 Assert.AreEqual (1, adapter.UpdateBatchSize, "#1 The default value should be 1");
1076 adapter.UpdateBatchSize = 3;
1077 Assert.AreEqual (3, adapter.UpdateBatchSize, "#2 The value should be 3 after setting the property UpdateBatchSize to 3");
1081 [ExpectedException (typeof (ArgumentOutOfRangeException))]
1082 public void UpdateBatchSizeArgumentOutOfRangeTest ()
1084 adapter = new SqlDataAdapter();
1085 adapter.UpdateBatchSize = -2;
1090 return (engine.ClientVersion);
1096 [Category ("sqlserver")]
1097 public class SqlDataAdapterInheritTest : DbDataAdapter
1099 SqlConnection conn = null;
1102 public void FillDataAdapterTest ()
1104 conn = ConnectionManager.Instance.Sql.Connection;
1107 DataTable dt = new DataTable();
1108 SqlCommand command = new SqlCommand ();
1109 command.CommandText = "Select * from employee;";
1110 command.Connection = conn;
1111 SelectCommand = command;
1112 Fill (dt, command.ExecuteReader ());
1113 Assert.AreEqual (4, dt.Rows.Count, "#1");
1114 Assert.AreEqual (6, dt.Columns.Count, "#2");
1116 DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
1117 ConnectionManager.Instance.Sql.CloseConnection ();