SqlCommandBuilderTest.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. try {
  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. } finally {
  96. ConnectionManager.Singleton.CloseConnection ();
  97. }
  98. }
  99. [Test]
  100. public void GetUpdateCommandTest_CheckNonUpdatableColumns ()
  101. {
  102. IDbConnection conn = ConnectionManager.Singleton.Connection;
  103. try {
  104. ConnectionManager.Singleton.OpenConnection ();
  105. IDbCommand cmd = conn.CreateCommand ();
  106. cmd.CommandText = "create table #tmp_table (id int primary key , counter int identity(1,1), value varchar(10))";
  107. cmd.ExecuteNonQuery ();
  108. string selectQuery = "select id, counter, value, id+1 as next_id from #tmp_table";
  109. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  110. DataSet ds = new DataSet ();
  111. da.Fill (ds);
  112. Assert.AreEqual (1, ds.Tables.Count, "#1");
  113. Assert.AreEqual (4, ds.Tables [0].Columns.Count, "#2");
  114. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  115. SqlCommand updateCmd = cb.GetUpdateCommand ();
  116. Assert.AreEqual ("UPDATE #tmp_table SET id = @p1, value = @p2 WHERE ((id = @p3) AND ((@p4 = 1 AND " +
  117. "counter IS NULL) OR (counter = @p5)) AND ((@p6 = 1 AND value IS NULL) OR (value = @p7)))",
  118. updateCmd.CommandText, "#3");
  119. Assert.AreEqual (7, updateCmd.Parameters.Count, "#3");
  120. } finally {
  121. ConnectionManager.Singleton.CloseConnection ();
  122. }
  123. }
  124. [Test]
  125. [ExpectedException (typeof (DBConcurrencyException))]
  126. public void GetUpdateCommandDBConcurrencyExceptionTest ()
  127. {
  128. IDbConnection conn = ConnectionManager.Singleton.Connection;
  129. try {
  130. ConnectionManager.Singleton.OpenConnection ();
  131. string selectQuery = "select id, fname from employee where id = 1";
  132. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  133. DataSet ds = new DataSet ();
  134. da.Fill (ds, "IntTest");
  135. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  136. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  137. DataRow [] rows = ds.Tables [0].Select ("id=1");
  138. rows [0] [0] = 6660; // non existent
  139. ds.Tables [0].AcceptChanges (); // moves 6660 to original value
  140. rows [0] [0] = 1; // moves 6660 as search key into db table
  141. da.Update (rows);
  142. } finally {
  143. ConnectionManager.Singleton.CloseConnection ();
  144. }
  145. }
  146. [Test]
  147. public void GetDeleteCommandTest ()
  148. {
  149. IDbConnection conn = ConnectionManager.Singleton.Connection;
  150. try {
  151. ConnectionManager.Singleton.OpenConnection ();
  152. string selectQuery = "select id, fname, id+1 as next_id from employee where id = 1";
  153. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  154. DataSet ds = new DataSet ();
  155. da.Fill (ds, "IntTest");
  156. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  157. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  158. SqlCommand cmd = cb.GetDeleteCommand ();
  159. Assert.AreEqual ("DELETE FROM employee WHERE ((id = @p1) AND ((@p2 = 1 AND fname IS NULL) OR (fname = @p3)))",
  160. cmd.CommandText, "#2");
  161. } finally {
  162. ConnectionManager.Singleton.CloseConnection ();
  163. }
  164. }
  165. [Test]
  166. public void DefaultPropertiesTest ()
  167. {
  168. SqlCommandBuilder cb = new SqlCommandBuilder ();
  169. #if NET_1_1 || NET_1_0 || ONLY_1_1
  170. Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictDetection);
  171. #endif // NET_1_1 || NET_1_0 || ONLY_1_1
  172. Assert.AreEqual ("", cb.QuotePrefix, "#5");
  173. Assert.AreEqual ("", cb.QuoteSuffix, "#6");
  174. #if NET_2_0
  175. Assert.AreEqual (".", cb.CatalogSeparator, "#2");
  176. Assert.AreEqual ("", cb.DecimalSeparator, "#3");
  177. Assert.AreEqual (".", cb.SchemaSeparator, "#4");
  178. Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
  179. IDbConnection conn = ConnectionManager.Singleton.Connection;
  180. try {
  181. conn.Open ();
  182. cb = new SqlCommandBuilder ();
  183. Assert.AreEqual ("\"monotest\"", cb.QuoteIdentifier ("monotest", (SqlConnection) conn), "#7");
  184. Assert.AreEqual ("monotest", cb.UnquoteIdentifier ("\"monotest\"", (SqlConnection) conn), "#8");
  185. conn.Close ();
  186. } finally {
  187. ConnectionManager.Singleton.CloseConnection ();
  188. }
  189. // FIXME: test SetAllValues
  190. #endif // NET_2_0
  191. }
  192. // FIXME: Add tests for examining RowError
  193. // FIXME: Add test for ContinueUpdateOnError property
  194. [Test]
  195. public void CheckParameters_BuiltCommand ()
  196. {
  197. using (IDbConnection conn = ConnectionManager.Singleton.Connection) {
  198. SqlDataAdapter adapter = new SqlDataAdapter ("select id,type_varchar from string_family", (SqlConnection)conn);
  199. SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
  200. DataSet ds = new DataSet ();
  201. adapter.Fill(ds);
  202. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#1");
  203. DataRow row_rsInput = ds.Tables[0].NewRow();
  204. row_rsInput["id"] = 100;
  205. row_rsInput["type_varchar"] = "ttt";
  206. ds.Tables[0].Rows.Add(row_rsInput);
  207. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#2");
  208. row_rsInput = ds.Tables[0].NewRow();
  209. row_rsInput["id"] = 101;
  210. row_rsInput["type_varchar"] = "ttt";
  211. ds.Tables[0].Rows.Add(row_rsInput);
  212. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#3");
  213. }
  214. }
  215. }
  216. }