SqlCommandBuilderTest.cs 11 KB

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