2
0

SqlDataAdapterTest.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // SqlDataAdapterTest.cs - NUnit Test Cases for testing the
  3. // SqlDataAdapter class
  4. // Author:
  5. // Umadevi S ([email protected])
  6. // Sureshkumar T ([email protected])
  7. //
  8. // Copyright (c) 2004 Novell Inc., and the individuals listed
  9. // on the ChangeLog entries.
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using System.Data.SqlClient;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Data.SqlClient
  36. {
  37. [TestFixture]
  38. [Category ("sqlserver")]
  39. public class SqlDataAdapterTest
  40. {
  41. SqlConnection conn;
  42. [Test]
  43. /**
  44. The below test will not run everytime, since the region id column is unique
  45. so change the regionid if you want the test to pass.
  46. **/
  47. public void UpdateTest () {
  48. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  49. try {
  50. ConnectionManager.Singleton.OpenConnection ();
  51. DataTable dt = new DataTable();
  52. SqlDataAdapter da = null;
  53. da = new SqlDataAdapter("Select * from employee;", conn);
  54. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  55. da.Fill(dt);
  56. DataRow dr = dt.NewRow();
  57. dr ["id"] = 6002;
  58. dr ["fname"] = "boston";
  59. dr ["dob"] = DateTime.Now.Subtract (new TimeSpan (20*365, 0, 0, 0));
  60. dr ["doj"] = DateTime.Now;
  61. dt.Rows.Add(dr);
  62. da.Update(dt);
  63. } finally {
  64. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  65. ConnectionManager.Singleton.CloseConnection ();
  66. }
  67. }
  68. [Test]
  69. public void FillSchemaTest()
  70. {
  71. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  72. try {
  73. ConnectionManager.Singleton.OpenConnection ();
  74. string sql = "select * from employee;";
  75. SqlCommand c = conn.CreateCommand();
  76. c.CommandText = sql;
  77. SqlDataReader dr = c.ExecuteReader(CommandBehavior.KeyInfo|CommandBehavior.SchemaOnly);
  78. DataTable schema = dr.GetSchemaTable();
  79. DataRowCollection drc = schema.Rows;
  80. DataRow r = drc[0];
  81. Assert.AreEqual("id",r["ColumnName"].ToString());
  82. } finally {
  83. ConnectionManager.Singleton.CloseConnection ();
  84. }
  85. }
  86. /**
  87. This needs a errortable created as follows
  88. id uniqueidentifier,name char(10) , with values
  89. Guid name
  90. {A12...} NULL
  91. NULL bbbbbb
  92. **/
  93. [Test]
  94. public void NullGuidTest()
  95. {
  96. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  97. try {
  98. ConnectionManager.Singleton.OpenConnection ();
  99. DBHelper.ExecuteNonQuery (conn, "create table #tmp_guid_table ( " +
  100. " id uniqueidentifier default newid (), " +
  101. " name char (10))");
  102. DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (name) values (null)");
  103. DBHelper.ExecuteNonQuery (conn, "insert into #tmp_guid_table (id, name) values (null, 'bbbb')");
  104. SqlDataAdapter da = new SqlDataAdapter("select * from #tmp_guid_table", conn);
  105. DataSet ds = new DataSet();
  106. da.Fill(ds);
  107. Assert.AreEqual (1, ds.Tables.Count, "#1");
  108. Assert.AreEqual (DBNull.Value, ds.Tables [0].Rows [1] ["id"], "#2");
  109. } finally {
  110. ConnectionManager.Singleton.CloseConnection ();
  111. }
  112. // the bug 68804 - is that the fill hangs!
  113. Assert.AreEqual("Done","Done");
  114. }
  115. }
  116. }