SqlCommandBuilderTest.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // SqlCommandBuilderTest.cs - NUnit Test Cases for testing the
  2. // SqlCommandBuilder class
  3. //
  4. // Authors:
  5. // Sureshkumar T ([email protected])
  6. //
  7. // Copyright (c) 2004 Novell Inc., and the individuals listed on the
  8. // ChangeLog entries.
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person
  12. // obtaining a copy of this software and associated documentation
  13. // files (the "Software"), to deal in the Software without
  14. // restriction, including without limitation the rights to use, copy,
  15. // modify, merge, publish, distribute, sublicense, and/or sell copies
  16. // of the Software, and to permit persons to whom the Software is
  17. // furnished to do so, subject to 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
  26. // BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  27. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  28. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. // SOFTWARE.
  30. using System;
  31. using System.Data;
  32. using System.Data.Common;
  33. using System.Data.SqlClient;
  34. using Mono.Data;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Data
  37. {
  38. [TestFixture]
  39. [Category ("sqlserver")]
  40. public class SqlCommandBuilderTest
  41. {
  42. [Test]
  43. public void GetInsertCommandTest ()
  44. {
  45. IDbConnection conn = ConnectionManager.Singleton.Connection;
  46. try {
  47. ConnectionManager.Singleton.OpenConnection ();
  48. string selectQuery = "select id, fname from employee where id = 1";
  49. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  50. DataSet ds = new DataSet ();
  51. da.Fill (ds, "IntTest");
  52. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  53. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  54. SqlCommand cmd = cb.GetInsertCommand ();
  55. Assert.AreEqual ("INSERT INTO employee (id, fname) VALUES (@p1, @p2)",
  56. cmd.CommandText, "#2");
  57. } finally {
  58. ConnectionManager.Singleton.CloseConnection ();
  59. }
  60. }
  61. [Test]
  62. public void GetInsertCommandTestWithExpression ()
  63. {
  64. IDbConnection conn = ConnectionManager.Singleton.Connection;
  65. try {
  66. ConnectionManager.Singleton.OpenConnection ();
  67. string selectQuery = "select id, fname, id+1 as next_id from employee where id = 1";
  68. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  69. DataSet ds = new DataSet ();
  70. da.Fill (ds, "IntTest");
  71. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  72. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  73. SqlCommand cmd = cb.GetInsertCommand ();
  74. Assert.AreEqual ("INSERT INTO employee (id, fname) VALUES (@p1, @p2)",
  75. cmd.CommandText, "#2");
  76. } finally {
  77. ConnectionManager.Singleton.CloseConnection ();
  78. }
  79. }
  80. [Test]
  81. public void GetUpdateCommandTest ()
  82. {
  83. IDbConnection conn = ConnectionManager.Singleton.Connection;
  84. using (conn) {
  85. string selectQuery = "select id, fname, id+1 as next_id from employee where id = 1";
  86. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  87. DataSet ds = new DataSet ();
  88. da.Fill (ds, "IntTest");
  89. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  90. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  91. SqlCommand cmd = cb.GetUpdateCommand ();
  92. Assert.AreEqual ("UPDATE employee SET id = @p1, fname = @p2 WHERE ((id = @p3) AND ((@p4 = 1 AND fname IS NULL) OR (fname = @p5)))",
  93. cmd.CommandText, "#2");
  94. Assert.AreEqual (5, cmd.Parameters.Count, "#3");
  95. }
  96. }
  97. [Test]
  98. [ExpectedException (typeof (DBConcurrencyException))]
  99. public void GetUpdateCommandDBConcurrencyExceptionTest ()
  100. {
  101. IDbConnection conn = ConnectionManager.Singleton.Connection;
  102. try {
  103. ConnectionManager.Singleton.OpenConnection ();
  104. string selectQuery = "select id, fname from employee where id = 1";
  105. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  106. DataSet ds = new DataSet ();
  107. da.Fill (ds, "IntTest");
  108. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  109. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  110. DataRow [] rows = ds.Tables [0].Select ("id=1");
  111. rows [0] [0] = 6660; // non existent
  112. ds.Tables [0].AcceptChanges (); // moves 6660 to original value
  113. rows [0] [0] = 1; // moves 6660 as search key into db table
  114. da.Update (rows);
  115. } finally {
  116. ConnectionManager.Singleton.CloseConnection ();
  117. }
  118. }
  119. [Test]
  120. public void GetDeleteCommandTest ()
  121. {
  122. IDbConnection conn = ConnectionManager.Singleton.Connection;
  123. try {
  124. ConnectionManager.Singleton.OpenConnection ();
  125. string selectQuery = "select id, fname, id+1 as next_id from employee where id = 1";
  126. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  127. DataSet ds = new DataSet ();
  128. da.Fill (ds, "IntTest");
  129. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  130. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  131. SqlCommand cmd = cb.GetDeleteCommand ();
  132. Assert.AreEqual ("DELETE FROM employee WHERE ((id = @p1) AND ((@p2 = 1 AND fname IS NULL) OR (fname = @p3)))",
  133. cmd.CommandText, "#2");
  134. } finally {
  135. ConnectionManager.Singleton.CloseConnection ();
  136. }
  137. }
  138. [Test]
  139. public void DefaultPropertiesTest ()
  140. {
  141. SqlCommandBuilder cb = new SqlCommandBuilder ();
  142. #if NET_1_1 || NET_1_0 || ONLY_1_1
  143. Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictDetection);
  144. #endif // NET_1_1 || NET_1_0 || ONLY_1_1
  145. Assert.AreEqual ("", cb.QuotePrefix, "#5");
  146. Assert.AreEqual ("", cb.QuoteSuffix, "#6");
  147. #if NET_2_0
  148. Assert.AreEqual (".", cb.CatalogSeparator, "#2");
  149. Assert.AreEqual ("", cb.DecimalSeparator, "#3");
  150. Assert.AreEqual (".", cb.SchemaSeparator, "#4");
  151. Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
  152. IDbConnection conn = ConnectionManager.Singleton.Connection;
  153. try {
  154. conn.Open ();
  155. cb = new SqlCommandBuilder ();
  156. Assert.AreEqual ("\"monotest\"", cb.QuoteIdentifier ("monotest", (SqlConnection) conn), "#7");
  157. Assert.AreEqual ("monotest", cb.UnquoteIdentifier ("\"monotest\"", (SqlConnection) conn), "#8");
  158. conn.Close ();
  159. } finally {
  160. ConnectionManager.Singleton.CloseConnection ();
  161. }
  162. // FIXME: test SetAllValues
  163. #endif // NET_2_0
  164. }
  165. // FIXME: Add tests for examining RowError
  166. // FIXME: Add test for ContinueUpdateOnError property
  167. [Test]
  168. public void CheckParameters_BuiltCommand ()
  169. {
  170. using (IDbConnection conn = ConnectionManager.Singleton.Connection) {
  171. SqlDataAdapter adapter = new SqlDataAdapter ("select id,type_varchar from string_family", (SqlConnection)conn);
  172. SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
  173. DataSet ds = new DataSet ();
  174. adapter.Fill(ds);
  175. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#1");
  176. DataRow row_rsInput = ds.Tables[0].NewRow();
  177. row_rsInput["id"] = 100;
  178. row_rsInput["type_varchar"] = "ttt";
  179. ds.Tables[0].Rows.Add(row_rsInput);
  180. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#2");
  181. row_rsInput = ds.Tables[0].NewRow();
  182. row_rsInput["id"] = 101;
  183. row_rsInput["type_varchar"] = "ttt";
  184. ds.Tables[0].Rows.Add(row_rsInput);
  185. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#3");
  186. }
  187. }
  188. }
  189. }