SqlCommandBuilderTest.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #if NET_2_0
  56. Assert.AreEqual ("INSERT INTO [employee] ([id], [fname]) VALUES (@p1, @p2)",
  57. cmd.CommandText, "#2");
  58. #else
  59. Assert.AreEqual ("INSERT INTO employee (id, fname) VALUES (@p1, @p2)",
  60. cmd.CommandText, "#2");
  61. #endif
  62. } finally {
  63. ConnectionManager.Singleton.CloseConnection ();
  64. }
  65. }
  66. [Test]
  67. public void GetInsertCommandTestWithExpression ()
  68. {
  69. IDbConnection conn = ConnectionManager.Singleton.Connection;
  70. try {
  71. ConnectionManager.Singleton.OpenConnection ();
  72. string selectQuery = "select id, fname, id+1 as next_id from employee where id = 1";
  73. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  74. DataSet ds = new DataSet ();
  75. da.Fill (ds, "IntTest");
  76. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  77. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  78. SqlCommand cmd = cb.GetInsertCommand ();
  79. #if NET_2_0
  80. Assert.AreEqual ("INSERT INTO [employee] ([id], [fname]) VALUES (@p1, @p2)",
  81. cmd.CommandText, "#2");
  82. #else
  83. Assert.AreEqual ("INSERT INTO employee (id, fname) VALUES (@p1, @p2)",
  84. cmd.CommandText, "#2");
  85. #endif
  86. } finally {
  87. ConnectionManager.Singleton.CloseConnection ();
  88. }
  89. }
  90. [Test]
  91. public void GetUpdateCommandTest ()
  92. {
  93. IDbConnection conn = ConnectionManager.Singleton.Connection;
  94. try {
  95. string selectQuery = "select id, fname, lname, id+1 as next_id from employee where id = 1";
  96. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  97. DataSet ds = new DataSet ();
  98. da.Fill (ds, "IntTest");
  99. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  100. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  101. SqlCommand cmd = cb.GetUpdateCommand ();
  102. #if NET_2_0
  103. Assert.AreEqual ("UPDATE [employee] SET [id] = @p1, [fname] = @p2, [lname] = @p3 WHERE (([id] = @p4)" +
  104. " AND ([fname] = @p5) AND ((@p6 = 1 AND [lname] IS NULL) OR ([lname] = @p7)))",
  105. cmd.CommandText, "#2");
  106. #else
  107. Assert.AreEqual ("UPDATE employee SET id = @p1, fname = @p2, lname = @p3 WHERE ((id = @p4)" +
  108. " AND (fname = @p5) AND ((@p6 = 1 AND lname IS NULL) OR (lname = @p7)))",
  109. cmd.CommandText, "#2");
  110. #endif
  111. Assert.AreEqual (7, cmd.Parameters.Count, "#3");
  112. } finally {
  113. ConnectionManager.Singleton.CloseConnection ();
  114. }
  115. }
  116. #if NET_2_0
  117. [Test]
  118. public void GetUpdateCommandBoolTest ()
  119. {
  120. IDbConnection conn = ConnectionManager.Singleton.Connection;
  121. try {
  122. string selectQuery = "select id, fname, lname, id+1 as next_id from employee where id = 1";
  123. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  124. DataSet ds = new DataSet ();
  125. da.Fill (ds, "IntTest");
  126. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  127. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  128. SqlCommand cmd = cb.GetUpdateCommand (true);
  129. Assert.AreEqual ("UPDATE [employee] SET [id] = @id, [fname] = @fname, [lname] = @lname WHERE (([id] = @id)" +
  130. " AND ([fname] = @fname) AND ((@lname = 1 AND [lname] IS NULL) OR ([lname] = @lname)))",
  131. cmd.CommandText, "#2");
  132. Assert.AreEqual (7, cmd.Parameters.Count, "#3");
  133. } finally {
  134. ConnectionManager.Singleton.CloseConnection ();
  135. }
  136. }
  137. #endif
  138. [Test]
  139. public void GetUpdateCommandTest_CheckNonUpdatableColumns ()
  140. {
  141. IDbConnection conn = ConnectionManager.Singleton.Connection;
  142. try {
  143. ConnectionManager.Singleton.OpenConnection ();
  144. IDbCommand cmd = conn.CreateCommand ();
  145. cmd.CommandText = "create table #tmp_table (id int primary key , counter int identity(1,1), value varchar(10))";
  146. cmd.ExecuteNonQuery ();
  147. string selectQuery = "select id, counter, value, id+1 as next_id from #tmp_table";
  148. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  149. DataSet ds = new DataSet ();
  150. da.Fill (ds);
  151. Assert.AreEqual (1, ds.Tables.Count, "#1");
  152. Assert.AreEqual (4, ds.Tables [0].Columns.Count, "#2");
  153. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  154. SqlCommand updateCmd = cb.GetUpdateCommand ();
  155. #if NET_2_0
  156. Assert.AreEqual ("UPDATE [#tmp_table] SET [id] = @p1, [value] = @p2 WHERE (([id] = @p3) AND (" +
  157. "[counter] = @p4) AND ((@p5 = 1 AND [value] IS NULL) OR ([value] = @p6)))",
  158. updateCmd.CommandText, "#3");
  159. #else
  160. Assert.AreEqual ("UPDATE #tmp_table SET id = @p1, value = @p2 WHERE ((id = @p3) AND (" +
  161. "counter = @p4) AND ((@p5 = 1 AND value IS NULL) OR (value = @p6)))",
  162. updateCmd.CommandText, "#3");
  163. #endif
  164. Assert.AreEqual (6, updateCmd.Parameters.Count, "#4");
  165. SqlCommand delCmd = cb.GetDeleteCommand ();
  166. #if NET_2_0
  167. Assert.AreEqual ("DELETE FROM [#tmp_table] WHERE (([id] = @p1) AND ([counter] = @p2) AND " +
  168. "((@p3 = 1 AND [value] IS NULL) OR ([value] = @p4)))", delCmd.CommandText, "#5");
  169. #else
  170. Assert.AreEqual ("DELETE FROM #tmp_table WHERE ((id = @p1) AND (counter = @p2) AND " +
  171. "((@p3 = 1 AND value IS NULL) OR (value = @p4)))", delCmd.CommandText, "#5");
  172. #endif
  173. Assert.AreEqual (4, delCmd.Parameters.Count, "#6");
  174. } finally {
  175. ConnectionManager.Singleton.CloseConnection ();
  176. }
  177. }
  178. [Test]
  179. public void GetUpdateDeleteCommand_CheckParameters ()
  180. {
  181. IDbConnection conn = ConnectionManager.Singleton.Connection;
  182. try {
  183. ConnectionManager.Singleton.OpenConnection ();
  184. SqlDataAdapter adapter = new SqlDataAdapter ("select id, type_varchar from string_family",
  185. (SqlConnection)conn);
  186. SqlCommandBuilder cb = new SqlCommandBuilder (adapter);
  187. SqlCommand updateCommand = cb.GetUpdateCommand ();
  188. Assert.AreEqual (5, updateCommand.Parameters.Count, "#1");
  189. Assert.AreEqual (SqlDbType.Int, updateCommand.Parameters ["@p4"].SqlDbType, "#2");
  190. Assert.AreEqual (1, updateCommand.Parameters ["@p4"].Value, "#3");
  191. SqlCommand delCommand = cb.GetDeleteCommand ();
  192. Assert.AreEqual (3, delCommand.Parameters.Count, "#4");
  193. Assert.AreEqual (SqlDbType.Int, delCommand.Parameters ["@p2"].SqlDbType, "#5");
  194. Assert.AreEqual (1, delCommand.Parameters ["@p2"].Value, "#6");
  195. } finally {
  196. ConnectionManager.Singleton.CloseConnection ();
  197. }
  198. }
  199. #if NET_2_0
  200. [Test]
  201. public void GetUpdateCommandBoolTest_CheckNonUpdatableColumns ()
  202. {
  203. IDbConnection conn = ConnectionManager.Singleton.Connection;
  204. try {
  205. ConnectionManager.Singleton.OpenConnection ();
  206. IDbCommand cmd = conn.CreateCommand ();
  207. cmd.CommandText = "create table #tmp_table (id int primary key , counter int identity(1,1), value varchar(10))";
  208. cmd.ExecuteNonQuery ();
  209. string selectQuery = "select id, counter, value, id+1 as next_id from #tmp_table";
  210. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  211. DataSet ds = new DataSet ();
  212. da.Fill (ds);
  213. Assert.AreEqual (1, ds.Tables.Count, "#1");
  214. Assert.AreEqual (4, ds.Tables [0].Columns.Count, "#2");
  215. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  216. SqlCommand updateCmd = cb.GetUpdateCommand (true);
  217. Assert.AreEqual ("UPDATE [#tmp_table] SET [id] = @id, [value] = @value WHERE (([id] = @id) AND (" +
  218. "[counter] = @counter) AND ((@value = 1 AND [value] IS NULL) OR ([value] = @value)))",
  219. updateCmd.CommandText, "#3");
  220. Assert.AreEqual (6, updateCmd.Parameters.Count, "#4");
  221. SqlCommand delCmd = cb.GetDeleteCommand (true);
  222. Assert.AreEqual ("DELETE FROM [#tmp_table] WHERE (([id] = @id) AND ([counter] = @counter) AND " +
  223. "((@value = 1 AND [value] IS NULL) OR ([value] = @value)))", delCmd.CommandText, "#5");
  224. Assert.AreEqual (4, delCmd.Parameters.Count, "#6");
  225. } finally {
  226. ConnectionManager.Singleton.CloseConnection ();
  227. }
  228. }
  229. [Test]
  230. public void GetUpdateDeleteCommandBoolTest_CheckParameters ()
  231. {
  232. IDbConnection conn = ConnectionManager.Singleton.Connection;
  233. try {
  234. ConnectionManager.Singleton.OpenConnection ();
  235. SqlDataAdapter adapter = new SqlDataAdapter ("select id, type_varchar from string_family",
  236. (SqlConnection)conn);
  237. SqlCommandBuilder cb = new SqlCommandBuilder (adapter);
  238. SqlCommand updateCommand = cb.GetUpdateCommand (true);
  239. Assert.AreEqual (5, updateCommand.Parameters.Count, "#1");
  240. Assert.AreEqual (SqlDbType.VarChar, updateCommand.Parameters ["@type_varchar"].SqlDbType, "#2");
  241. // FIXME: NotWorking
  242. //Assert.AreEqual (1, updateCommand.Parameters ["@type_char"].Value, "#3");
  243. SqlCommand delCommand = cb.GetDeleteCommand (true);
  244. Assert.AreEqual (3, delCommand.Parameters.Count, "#4");
  245. Assert.AreEqual (SqlDbType.Int, delCommand.Parameters ["@type_varchar"].SqlDbType, "#5");
  246. Assert.AreEqual (1, delCommand.Parameters ["@type_varchar"].Value, "#6");
  247. } finally {
  248. ConnectionManager.Singleton.CloseConnection ();
  249. }
  250. }
  251. #endif
  252. [Test]
  253. [ExpectedException (typeof (DBConcurrencyException))]
  254. public void GetUpdateCommandDBConcurrencyExceptionTest ()
  255. {
  256. IDbConnection conn = ConnectionManager.Singleton.Connection;
  257. try {
  258. ConnectionManager.Singleton.OpenConnection ();
  259. string selectQuery = "select id, fname from employee where id = 1";
  260. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  261. DataSet ds = new DataSet ();
  262. da.Fill (ds, "IntTest");
  263. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  264. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  265. DataRow [] rows = ds.Tables [0].Select ("id=1");
  266. rows [0] [0] = 6660; // non existent
  267. ds.Tables [0].AcceptChanges (); // moves 6660 to original value
  268. rows [0] [0] = 1; // moves 6660 as search key into db table
  269. da.Update (rows);
  270. } finally {
  271. ConnectionManager.Singleton.CloseConnection ();
  272. }
  273. }
  274. [Test]
  275. [ExpectedException (typeof (DBConcurrencyException))]
  276. public void GetDeleteCommandDBConcurrencyExceptionTest ()
  277. {
  278. IDbConnection conn = ConnectionManager.Singleton.Connection;
  279. try {
  280. ConnectionManager.Singleton.OpenConnection ();
  281. string selectQuery = "select id, fname from employee where id = 1";
  282. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  283. DataSet ds = new DataSet ();
  284. da.Fill (ds, "IntTest");
  285. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  286. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  287. DataRow [] rows = ds.Tables [0].Select ("id=1");
  288. rows [0] [0] = 6660; // non existent
  289. ds.Tables [0].AcceptChanges (); // moves 6660 to original value
  290. rows [0].Delete (); // moves 6660 as search key into db table
  291. da.Update (rows);
  292. } finally {
  293. ConnectionManager.Singleton.CloseConnection ();
  294. }
  295. }
  296. [Test]
  297. public void GetDeleteCommandTest ()
  298. {
  299. IDbConnection conn = ConnectionManager.Singleton.Connection;
  300. try {
  301. ConnectionManager.Singleton.OpenConnection ();
  302. string selectQuery = "select id, fname, lname, id+1 as next_id from employee where id = 1";
  303. SqlDataAdapter da = new SqlDataAdapter (selectQuery, (SqlConnection) conn);
  304. DataSet ds = new DataSet ();
  305. da.Fill (ds, "IntTest");
  306. Assert.AreEqual (1, ds.Tables.Count, "#1 atleast one table should be filled");
  307. SqlCommandBuilder cb = new SqlCommandBuilder (da);
  308. SqlCommand cmd = cb.GetDeleteCommand ();
  309. #if NET_2_0
  310. Assert.AreEqual ("DELETE FROM [employee] WHERE (([id] = @p1) AND ([fname] = @p2) AND " +
  311. "((@p3 = 1 AND [lname] IS NULL) OR ([lname] = @p4)))", cmd.CommandText, "#2");
  312. #else
  313. Assert.AreEqual ("DELETE FROM employee WHERE ((id = @p1) AND (fname = @p2) AND " +
  314. "((@p3 = 1 AND lname IS NULL) OR (lname = @p4)))", cmd.CommandText, "#2");
  315. #endif
  316. } finally {
  317. ConnectionManager.Singleton.CloseConnection ();
  318. }
  319. }
  320. [Test]
  321. public void DefaultPropertiesTest ()
  322. {
  323. SqlCommandBuilder cb = new SqlCommandBuilder ();
  324. #if NET_1_0
  325. Assert.AreEqual (ConflictOption.CompareAllSearchableValues, cb.ConflictDetection);
  326. Assert.AreEqual ("", cb.QuotePrefix, "#5");
  327. Assert.AreEqual ("", cb.QuoteSuffix, "#6");
  328. #endif
  329. #if NET_2_0
  330. Assert.AreEqual ("[", cb.QuotePrefix, "#5");
  331. Assert.AreEqual ("]", cb.QuoteSuffix, "#6");
  332. Assert.AreEqual (".", cb.CatalogSeparator, "#2");
  333. //Assert.AreEqual ("", cb.DecimalSeparator, "#3");
  334. Assert.AreEqual (".", cb.SchemaSeparator, "#4");
  335. Assert.AreEqual (CatalogLocation.Start, cb.CatalogLocation, "#1");
  336. IDbConnection conn = ConnectionManager.Singleton.Connection;
  337. try {
  338. conn.Open ();
  339. cb = new SqlCommandBuilder ();
  340. #if NET_2_0
  341. Assert.AreEqual ("[monotest]", cb.QuoteIdentifier ("monotest"), "#7");
  342. Assert.AreEqual ("\"monotest\"", cb.UnquoteIdentifier ("\"monotest\""), "#8");
  343. #else
  344. Assert.AreEqual ("\"monotest\"", cb.QuoteIdentifier ("monotest"), "#7");
  345. Assert.AreEqual ("monotest", cb.UnquoteIdentifier ("\"monotest\""), "#8");
  346. #endif
  347. conn.Close ();
  348. } finally {
  349. ConnectionManager.Singleton.CloseConnection ();
  350. }
  351. // FIXME: test SetAllValues
  352. #endif // NET_2_0
  353. }
  354. // FIXME: Add tests for examining RowError
  355. // FIXME: Add test for ContinueUpdateOnError property
  356. [Test]
  357. public void CheckParameters_BuiltCommand ()
  358. {
  359. using (IDbConnection conn = ConnectionManager.Singleton.Connection) {
  360. SqlDataAdapter adapter = new SqlDataAdapter ("select id,type_varchar from string_family", (SqlConnection)conn);
  361. SqlCommandBuilder cb = new SqlCommandBuilder(adapter);
  362. DataSet ds = new DataSet ();
  363. adapter.Fill(ds);
  364. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#1");
  365. DataRow row_rsInput = ds.Tables[0].NewRow();
  366. row_rsInput["id"] = 100;
  367. row_rsInput["type_varchar"] = "ttt";
  368. ds.Tables[0].Rows.Add(row_rsInput);
  369. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#2");
  370. row_rsInput = ds.Tables[0].NewRow();
  371. row_rsInput["id"] = 101;
  372. row_rsInput["type_varchar"] = "ttt";
  373. ds.Tables[0].Rows.Add(row_rsInput);
  374. Assert.AreEqual (2, cb.GetInsertCommand().Parameters.Count, "#3");
  375. }
  376. }
  377. }
  378. }