SqlCommandTest.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. //
  2. // SqlCommandTest.cs - NUnit Test Cases for testing the
  3. // SqlCommand class
  4. // Author:
  5. // Umadevi S ([email protected])
  6. // Sureshkumar T ([email protected])
  7. // Senganal T ([email protected])
  8. //
  9. // Copyright (c) 2004 Novell Inc., and the individuals listed
  10. // on the ChangeLog entries.
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.Data.SqlClient;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Data.SqlClient
  37. {
  38. [TestFixture]
  39. [Category ("sqlserver")]
  40. public class SqlCommandTest
  41. {
  42. public SqlConnection conn = null ;
  43. SqlCommand cmd = null;
  44. string connectionString = ConnectionManager.Singleton.ConnectionString;
  45. [SetUp]
  46. public void Setup ()
  47. {
  48. }
  49. [TearDown]
  50. public void TearDown ()
  51. {
  52. if (conn != null)
  53. conn.Close ();
  54. }
  55. [Test]
  56. public void ConstructorTest ()
  57. {
  58. // Test Default Constructor
  59. cmd = new SqlCommand ();
  60. Assert.AreEqual (String.Empty, cmd.CommandText,
  61. "#1 Command Test should be empty");
  62. Assert.AreEqual (30, cmd.CommandTimeout,
  63. "#2 CommandTimeout should be 30");
  64. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  65. "#3 CommandType should be text");
  66. Assert.IsNull (cmd.Connection, "#4 Connection Should be null");
  67. Assert.AreEqual (0, cmd.Parameters.Count,
  68. "#5 Parameter shud be empty");
  69. // Test Overloaded Constructor
  70. String cmdText = "select * from tbl1" ;
  71. cmd = new SqlCommand (cmdText);
  72. Assert.AreEqual (cmdText, cmd.CommandText,
  73. "#5 CommandText should be the same as passed");
  74. Assert.AreEqual (30, cmd.CommandTimeout,
  75. "#6 CommandTimeout should be 30");
  76. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  77. "#7 CommandType should be text");
  78. Assert.IsNull (cmd.Connection , "#8 Connection Should be null");
  79. // Test Overloaded Constructor
  80. SqlConnection conn = new SqlConnection ();
  81. cmd = new SqlCommand (cmdText , conn);
  82. Assert.AreEqual (cmdText, cmd.CommandText,
  83. "#9 CommandText should be the same as passed");
  84. Assert.AreEqual (30, cmd.CommandTimeout,
  85. "#10 CommandTimeout should be 30");
  86. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  87. "#11 CommandType should be text");
  88. Assert.AreSame (cmd.Connection, conn, "#12 Connection Should be same");
  89. // Test Overloaded Constructor
  90. SqlTransaction trans = null ;
  91. try {
  92. conn = new SqlConnection (connectionString);
  93. conn.Open ();
  94. trans = conn.BeginTransaction ();
  95. cmd = new SqlCommand (cmdText, conn, trans);
  96. Assert.AreEqual (cmdText, cmd.CommandText,
  97. "#9 CommandText should be the same as passed");
  98. Assert.AreEqual (30, cmd.CommandTimeout,
  99. "#10 CommandTimeout should be 30");
  100. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  101. "#11 CommandType should be text");
  102. Assert.AreEqual (cmd.Connection, conn,
  103. "#12 Connection Should be null");
  104. Assert.AreEqual (cmd.Transaction, trans,
  105. "#13 Transaction Property should be set");
  106. // Test if parameters are reset to Default Values
  107. cmd = new SqlCommand ();
  108. Assert.AreEqual (String.Empty, cmd.CommandText,
  109. "#1 Command Test should be empty");
  110. Assert.AreEqual (30, cmd.CommandTimeout,
  111. "#2 CommandTimeout should be 30");
  112. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  113. "#3 CommandType should be text");
  114. Assert.IsNull (cmd.Connection, "#4 Connection Should be null");
  115. }finally {
  116. trans.Rollback ();
  117. }
  118. }
  119. [Test]
  120. public void ExecuteScalarTest ()
  121. {
  122. conn = new SqlConnection (connectionString);
  123. cmd = new SqlCommand ("" , conn);
  124. cmd.CommandText = "Select count(*) from numeric_family where id<=4";
  125. //Check Exception is thrown when executed on a closed connection
  126. try {
  127. cmd.ExecuteScalar ();
  128. Assert.Fail ("#1 InvalidOperation Exception must be thrown");
  129. }catch (AssertionException e) {
  130. throw e;
  131. }catch (Exception e) {
  132. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  133. "#2 Incorrect Exception : " + e.StackTrace);
  134. }
  135. // Check the Return value for a Correct Query
  136. object result = 0;
  137. conn.Open ();
  138. result = cmd.ExecuteScalar ();
  139. Assert.AreEqual (4, (int)result, "#3 Query Result returned is incorrect");
  140. cmd.CommandText = "select id , type_bit from numeric_family order by id asc" ;
  141. result = Convert.ToInt32 (cmd.ExecuteScalar ());
  142. Assert.AreEqual (1, result,
  143. "#4 ExecuteScalar Should return (1,1) the result set" );
  144. cmd.CommandText = "select id from numeric_family where id=-1";
  145. result = cmd.ExecuteScalar ();
  146. Assert.IsNull (result, "#5 Null shud be returned if result set is empty");
  147. // Check SqlException is thrown for Invalid Query
  148. cmd.CommandText = "select count* from numeric_family";
  149. try {
  150. result = cmd.ExecuteScalar ();
  151. Assert.Fail ("#6 InCorrect Query should cause an SqlException");
  152. }catch (AssertionException e) {
  153. throw e;
  154. }catch (Exception e) {
  155. Assert.AreEqual (typeof(SqlException), e.GetType(),
  156. "#7 Incorrect Exception : " + e.StackTrace);
  157. }
  158. }
  159. [Test]
  160. public void ExecuteNonQuery ()
  161. {
  162. conn = new SqlConnection (connectionString);
  163. cmd = new SqlCommand ("", conn);
  164. int result = 0;
  165. // Test for exceptions
  166. // Test exception is thrown if connection is closed
  167. cmd.CommandText = "Select id from numeric_family where id=1";
  168. try {
  169. cmd.ExecuteNonQuery ();
  170. Assert.Fail ("#1 Connextion shud be open");
  171. }catch (AssertionException e) {
  172. throw e;
  173. }catch (Exception e) {
  174. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  175. "#2 Incorrect Exception : " + e);
  176. }
  177. // Test Exception is thrown if Query is incorrect
  178. conn.Open ();
  179. cmd.CommandText = "Select id1 from numeric_family";
  180. try {
  181. cmd.ExecuteNonQuery ();
  182. Assert.Fail ("#1 invalid Query");
  183. }catch (AssertionException e) {
  184. throw e;
  185. }catch (Exception e) {
  186. Assert.AreEqual (typeof(SqlException), e.GetType(),
  187. "#2 Incorrect Exception : " + e);
  188. }
  189. // Test Select/Insert/Update/Delete Statements
  190. SqlTransaction trans = conn.BeginTransaction ();
  191. cmd.Transaction = trans;
  192. try {
  193. cmd.CommandText = "Select id from numeric_family where id=1";
  194. result = cmd.ExecuteNonQuery ();
  195. Assert.AreEqual (-1, result, "#1");
  196. cmd.CommandText = "Insert into numeric_family (id,type_int) values (100,200)";
  197. result = cmd.ExecuteNonQuery ();
  198. Assert.AreEqual (1, result, "#2 One row shud be inserted");
  199. cmd.CommandText = "Update numeric_family set type_int=300 where id=100";
  200. result = cmd.ExecuteNonQuery ();
  201. Assert.AreEqual (1, result, "#3 One row shud be updated");
  202. // Test Batch Commands
  203. cmd.CommandText = "Select id from numeric_family where id=1;";
  204. cmd.CommandText += "update numeric_family set type_int=10 where id=1000";
  205. cmd.CommandText += "update numeric_family set type_int=10 where id=100";
  206. result = cmd.ExecuteNonQuery ();
  207. Assert.AreEqual (1, result, "#4 One row shud be updated");
  208. cmd.CommandText = "Delete from numeric_family where id=100";
  209. result = cmd.ExecuteNonQuery ();
  210. Assert.AreEqual (1, result, "#5 One row shud be deleted");
  211. }finally {
  212. trans.Rollback ();
  213. }
  214. }
  215. [Test]
  216. public void ExecuteReaderTest ()
  217. {
  218. SqlDataReader reader = null;
  219. conn = new SqlConnection (connectionString);
  220. // Test exception is thrown if conn is closed
  221. cmd = new SqlCommand ("Select count(*) from numeric_family");
  222. try {
  223. reader = cmd.ExecuteReader ();
  224. }catch (AssertionException e) {
  225. throw e;
  226. }catch (Exception e) {
  227. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  228. "#1 Incorrect Exception");
  229. }
  230. conn.Open ();
  231. // Test exception is thrown for Invalid Query
  232. cmd = new SqlCommand ("InvalidQuery", conn);
  233. try {
  234. reader = cmd.ExecuteReader ();
  235. Assert.Fail ("#1 Exception shud be thrown");
  236. }catch (AssertionException e) {
  237. throw e;
  238. }catch (Exception e) {
  239. Assert.AreEqual (typeof(SqlException), e.GetType (),
  240. "#2 Incorrect Exception : " + e);
  241. }
  242. // NOTE
  243. // Test SqlException is thrown if a row is locked
  244. // should lock a particular row and then modify it
  245. /*
  246. */
  247. // Test Connection cannot be modified when reader is in use
  248. // NOTE : msdotnet contradicts documented behavior
  249. cmd.CommandText = "select * from numeric_family where id=1";
  250. reader = cmd.ExecuteReader ();
  251. reader.Read ();
  252. conn.Close (); // valid operation
  253. conn = new SqlConnection (connectionString);
  254. /*
  255. // NOTE msdotnet contradcits documented behavior
  256. // If the above testcase fails, then this shud be tested
  257. // Test connection can be modified once reader is closed
  258. conn.Close ();
  259. reader.Close ();
  260. conn = new SqlConnection (connectionString); // valid operation
  261. */
  262. }
  263. [Test]
  264. public void ExecuteReaderCommandBehaviorTest ()
  265. {
  266. // Test for command behaviors
  267. DataTable schemaTable = null;
  268. SqlDataReader reader = null;
  269. conn = new SqlConnection (connectionString);
  270. conn.Open ();
  271. cmd = new SqlCommand ("", conn);
  272. cmd.CommandText = "Select id from numeric_family where id <=4 order by id asc;";
  273. cmd.CommandText += "Select type_bit from numeric_family where id <=4 order by id asc";
  274. // Test for default command behavior
  275. reader = cmd.ExecuteReader ();
  276. int rows = 0;
  277. int results = 0;
  278. do {
  279. while (reader.Read ())
  280. rows++ ;
  281. Assert.AreEqual (4, rows, "#1 Multiple rows shud be returned");
  282. results++;
  283. rows = 0;
  284. }while (reader.NextResult());
  285. Assert.AreEqual (2, results, "#2 Multiple result sets shud be returned");
  286. reader.Close ();
  287. // Test if closing reader, closes the connection
  288. reader = cmd.ExecuteReader (CommandBehavior.CloseConnection);
  289. reader.Close ();
  290. Assert.AreEqual (ConnectionState.Closed, conn.State,
  291. "#3 Command Behavior is not followed");
  292. conn.Open();
  293. // Test if row info and primary Key info is returned
  294. reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
  295. schemaTable = reader.GetSchemaTable ();
  296. Assert.IsTrue(reader.HasRows, "#4 Data Rows shud also be returned");
  297. Assert.IsTrue ((bool)schemaTable.Rows[0]["IsKey"],
  298. "#5 Primary Key info shud be returned");
  299. reader.Close ();
  300. // Test only column information is returned
  301. reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly);
  302. schemaTable = reader.GetSchemaTable ();
  303. Assert.IsFalse (reader.HasRows, "#6 row data shud not be returned");
  304. Assert.AreEqual(DBNull.Value, schemaTable.Rows[0]["IsKey"],
  305. "#7 Primary Key info shud not be returned");
  306. Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
  307. "#8 Schema Data is Incorrect");
  308. reader.Close ();
  309. // Test only one result set (first) is returned
  310. reader = cmd.ExecuteReader (CommandBehavior.SingleResult);
  311. schemaTable = reader.GetSchemaTable ();
  312. Assert.IsFalse (reader.NextResult(),
  313. "#9 Only one result set shud be returned");
  314. Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
  315. "#10 The result set returned shud be the first result set");
  316. reader.Close ();
  317. // Test only one row is returned for all result sets
  318. // msdotnet doesnt work correctly.. returns only one result set
  319. reader = cmd.ExecuteReader (CommandBehavior.SingleRow);
  320. rows=0;
  321. results=0;
  322. do {
  323. while (reader.Read ())
  324. rows++ ;
  325. Assert.AreEqual (1, rows, "#11 Only one row shud be returned");
  326. results++;
  327. rows = 0;
  328. }while (reader.NextResult());
  329. // NOTE msdotnet contradicts documented behavior.
  330. // Multiple result sets shud be returned , and in this case : 2
  331. //Assert.AreEqual (2, results, "# Multiple result sets shud be returned");
  332. Assert.AreEqual (2, results, "#12 Multiple result sets shud be returned");
  333. reader.Close ();
  334. }
  335. [Test]
  336. public void PrepareTest_CheckValidStatement ()
  337. {
  338. cmd = new SqlCommand ();
  339. conn = new SqlConnection (connectionString);
  340. conn.Open ();
  341. cmd.CommandText = "Select id from numeric_family where id=@ID" ;
  342. cmd.Connection = conn ;
  343. // Test if Parameters are correctly populated
  344. cmd.Parameters.Clear ();
  345. cmd.Parameters.Add ("@ID", SqlDbType.TinyInt);
  346. cmd.Parameters["@ID"].Value = 2 ;
  347. cmd.Prepare ();
  348. Assert.AreEqual (2, cmd.ExecuteScalar (), "#3 Prepared Stmt not working");
  349. cmd.Parameters[0].Value = 3;
  350. Assert.AreEqual (3, cmd.ExecuteScalar (), "#4 Prep Stmt not working");
  351. conn.Close ();
  352. }
  353. [Test]
  354. public void PrepareTest ()
  355. {
  356. cmd = new SqlCommand ();
  357. conn = new SqlConnection (connectionString);
  358. conn.Open ();
  359. cmd.CommandText = "Select id from numeric_family where id=@ID" ;
  360. cmd.Connection = conn ;
  361. // Test InvalidOperation Exception is thrown if Parameter Type
  362. // is not explicitly set
  363. cmd.Parameters.Add ("@ID", 2);
  364. try {
  365. cmd.Prepare ();
  366. Assert.Fail ("#1 Parameter Type shud be explicitly Set");
  367. }catch (AssertionException e) {
  368. throw e;
  369. }catch (Exception e) {
  370. Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
  371. "#2 Incorrect Exception : " + e.StackTrace);
  372. }
  373. // Test Exception is thrown for variable size data if precision/scale
  374. // is not set
  375. cmd.CommandText = "select type_varchar from string_family where type_varchar=@p1";
  376. cmd.Parameters.Clear ();
  377. cmd.Parameters.Add ("@p1", SqlDbType.VarChar);
  378. cmd.Parameters["@p1"].Value = "afasasadadada";
  379. try {
  380. cmd.Prepare ();
  381. Assert.Fail ("#5 Exception shud be thrown");
  382. }catch (AssertionException e) {
  383. throw e;
  384. }catch (Exception e) {
  385. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  386. "#6 Incorrect Exception " + e.StackTrace);
  387. }
  388. // Test Exception is not thrown for Stored Procs
  389. try {
  390. cmd.CommandType = CommandType.StoredProcedure;
  391. cmd.CommandText = "ABFSDSFSF" ;
  392. cmd.Prepare ();
  393. }catch (Exception e) {
  394. Assert.Fail ("#7 Exception shud not be thrown for Stored Procs");
  395. }
  396. cmd.CommandType = CommandType.Text;
  397. conn.Close ();
  398. //Test InvalidOperation Exception is thrown if connection is not set
  399. cmd.Connection = null;
  400. try {
  401. cmd.Prepare ();
  402. Assert.Fail ("#8 InvalidOperation Exception shud be thrown");
  403. }catch (AssertionException e) {
  404. throw e;
  405. }catch (Exception e) {
  406. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  407. "#9 Incorrect Exception : " + e.StackTrace);
  408. }
  409. //Test InvalidOperation Exception is thrown if connection is closed
  410. cmd.Connection = conn ;
  411. try{
  412. cmd.Prepare ();
  413. Assert.Fail ("#4 InvalidOperation Exception shud be thrown");
  414. }catch (AssertionException e) {
  415. throw e;
  416. }catch (Exception e) {
  417. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  418. "Incorrect Exception : " + e.StackTrace);
  419. }
  420. }
  421. [Test]
  422. public void ResetTimeOut ()
  423. {
  424. SqlCommand cmd = new SqlCommand ();
  425. cmd.CommandTimeout = 50 ;
  426. Assert.AreEqual ( cmd.CommandTimeout, 50,
  427. "#1 CommandTimeout should be modfiable");
  428. cmd.ResetCommandTimeout ();
  429. Assert.AreEqual (cmd.CommandTimeout, 30,
  430. "#2 Reset Should set the Timeout to default value");
  431. }
  432. [Test]
  433. [ExpectedException (typeof(ArgumentException))]
  434. public void CommandTimeout ()
  435. {
  436. cmd = new SqlCommand ();
  437. cmd.CommandTimeout = 10;
  438. Assert.AreEqual (10, cmd.CommandTimeout, "#1");
  439. cmd.CommandTimeout = -1;
  440. }
  441. [Test]
  442. [ExpectedException (typeof(ArgumentException))]
  443. public void CommandTypeTest ()
  444. {
  445. cmd = new SqlCommand ();
  446. Assert.AreEqual (CommandType.Text ,cmd.CommandType,
  447. "Default CommandType is text");
  448. cmd.CommandType = (CommandType)(-1);
  449. }
  450. [Test]
  451. [Ignore ("msdotnet contradicts documented behavior")]
  452. [ExpectedException (typeof(InvalidOperationException))]
  453. public void ConnectionTest ()
  454. {
  455. SqlTransaction trans = null;
  456. try {
  457. conn = new SqlConnection (connectionString);
  458. conn.Open ();
  459. trans = conn.BeginTransaction ();
  460. cmd = new SqlCommand ("", conn,trans);
  461. cmd.CommandText = "Select id from numeric_family where id=1";
  462. cmd.Connection = new SqlConnection ();
  463. }finally {
  464. trans.Rollback();
  465. conn.Close ();
  466. }
  467. }
  468. [Test]
  469. public void TransactionTest ()
  470. {
  471. conn = new SqlConnection (connectionString);
  472. cmd = new SqlCommand ("", conn);
  473. Assert.IsNull (cmd.Transaction, "#1 Default value is null");
  474. SqlConnection conn1 = new SqlConnection (connectionString);
  475. conn1.Open ();
  476. SqlTransaction trans1 = conn1.BeginTransaction ();
  477. cmd.Transaction = trans1 ;
  478. try {
  479. cmd.ExecuteNonQuery ();
  480. Assert.Fail ("#2 Connection cannot be different");
  481. }catch (Exception e) {
  482. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  483. "#3 Incorrect Exception : " + e);
  484. }finally {
  485. conn1.Close ();
  486. conn.Close ();
  487. }
  488. }
  489. // Need to add more tests
  490. [Test]
  491. [ExpectedException (typeof(ArgumentException))]
  492. public void UpdatedRowSourceTest ()
  493. {
  494. cmd = new SqlCommand ();
  495. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource,
  496. "#1 Default value is both");
  497. cmd.UpdatedRowSource = UpdateRowSource.None;
  498. Assert.AreEqual (UpdateRowSource.None, cmd.UpdatedRowSource,
  499. "#2");
  500. cmd.UpdatedRowSource = (UpdateRowSource) (-1);
  501. }
  502. [Test]
  503. public void ExecuteNonQueryTempProcedureTest () {
  504. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  505. try {
  506. ConnectionManager.Singleton.OpenConnection ();
  507. // create temp sp here, should normally be created in Setup of test
  508. // case, but cannot be done right now because of ug #68978
  509. DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
  510. SqlCommand cmd = new SqlCommand();
  511. cmd.Connection = conn;
  512. cmd.CommandText = "#sp_temp_insert_employee";
  513. cmd.CommandType = CommandType.StoredProcedure;
  514. Object TestPar = "test";
  515. cmd.Parameters.Add("@fname", SqlDbType.VarChar);
  516. cmd.Parameters ["@fname"].Value = TestPar;
  517. Assert.AreEqual(1,cmd.ExecuteNonQuery());
  518. } finally {
  519. DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
  520. DBHelper.ExecuteSimpleSP (conn, "sp_clean_person_table");
  521. ConnectionManager.Singleton.CloseConnection ();
  522. }
  523. }
  524. // Test for bug #76778
  525. // Test for a case, when query size is greater than the block size
  526. [Test]
  527. public void LongQueryTest ()
  528. {
  529. SqlConnection conn = new SqlConnection (
  530. connectionString + ";Pooling=false");
  531. using (conn) {
  532. conn.Open ();
  533. SqlCommand cmd = conn.CreateCommand ();
  534. String value = new String ('a', 10000);
  535. cmd.CommandText = String.Format ("Select '{0}'", value);
  536. cmd.ExecuteNonQuery ();
  537. }
  538. }
  539. // Test for bug #76778
  540. // To make sure RPC (when implemented) works ok..
  541. [Test]
  542. public void LongStoredProcTest()
  543. {
  544. SqlConnection conn = new SqlConnection (
  545. connectionString + ";Pooling=false");
  546. using (conn) {
  547. conn.Open ();
  548. int size = conn.PacketSize ;
  549. SqlCommand cmd = conn.CreateCommand ();
  550. // create a temp stored proc ..
  551. cmd.CommandText = "Create Procedure #sp_tmp_long_params ";
  552. cmd.CommandText += "@p1 nvarchar (4000), ";
  553. cmd.CommandText += "@p2 nvarchar (4000), ";
  554. cmd.CommandText += "@p3 nvarchar (4000), ";
  555. cmd.CommandText += "@p4 nvarchar (4000) out ";
  556. cmd.CommandText += "As ";
  557. cmd.CommandText += "Begin ";
  558. cmd.CommandText += "Set @p4 = N'Hello' ";
  559. cmd.CommandText += "Return 2 ";
  560. cmd.CommandText += "End";
  561. cmd.ExecuteNonQuery ();
  562. //execute the proc
  563. cmd.CommandType = CommandType.StoredProcedure;
  564. cmd.CommandText = "#sp_tmp_long_params";
  565. String value = new String ('a', 4000);
  566. SqlParameter p1 = new SqlParameter ("@p1",
  567. SqlDbType.NVarChar,4000);
  568. p1.Value = value;
  569. SqlParameter p2 = new SqlParameter ("@p2",
  570. SqlDbType.NVarChar,4000);
  571. p2.Value = value;
  572. SqlParameter p3 = new SqlParameter ("@p3",
  573. SqlDbType.NVarChar,4000);
  574. p3.Value = value;
  575. SqlParameter p4 = new SqlParameter ("@p4",
  576. SqlDbType.NVarChar,4000);
  577. p4.Direction = ParameterDirection.Output;
  578. // for now, name shud be @RETURN_VALUE
  579. // can be changed once RPC is implemented
  580. SqlParameter p5 = new SqlParameter ("@RETURN_VALUE", SqlDbType.Int);
  581. p5.Direction = ParameterDirection.ReturnValue ;
  582. cmd.Parameters.Add (p1);
  583. cmd.Parameters.Add (p2);
  584. cmd.Parameters.Add (p3);
  585. cmd.Parameters.Add (p4);
  586. cmd.Parameters.Add (p5);
  587. cmd.ExecuteNonQuery ();
  588. Assert.AreEqual ("Hello", p4.Value, "#1");
  589. Assert.AreEqual (2, p5.Value, "#2");
  590. }
  591. }
  592. // Test for bug #76880
  593. [Test]
  594. public void DateTimeParameterTest ()
  595. {
  596. SqlConnection conn = new SqlConnection (connectionString);
  597. using (conn) {
  598. conn.Open ();
  599. SqlCommand cmd = conn.CreateCommand ();
  600. cmd.CommandText = "select * from datetime_family where type_datetime=@p1";
  601. cmd.Parameters.Add ("@p1", SqlDbType.DateTime).Value= "10-10-2005";
  602. // shudnt cause and exception
  603. SqlDataReader rdr = cmd.ExecuteReader ();
  604. rdr.Close ();
  605. }
  606. }
  607. /**
  608. * Verifies whether an enum value is converted to a numeric value when
  609. * used as value for a numeric parameter (bug #66630)
  610. */
  611. [Test]
  612. public void EnumParameterTest() {
  613. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  614. try {
  615. ConnectionManager.Singleton.OpenConnection ();
  616. // create temp sp here, should normally be created in Setup of test
  617. // case, but cannot be done right now because of ug #68978
  618. DBHelper.ExecuteNonQuery (conn, "CREATE PROCEDURE #Bug66630 ("
  619. + "@Status smallint = 7"
  620. + ")"
  621. + "AS" + Environment.NewLine
  622. + "BEGIN" + Environment.NewLine
  623. + "SELECT CAST(5 AS int), @Status" + Environment.NewLine
  624. + "END");
  625. SqlCommand cmd = new SqlCommand("#Bug66630", conn);
  626. cmd.CommandType = CommandType.StoredProcedure;
  627. cmd.Parameters.Add("@Status", SqlDbType.Int).Value = Status.Error;
  628. using (SqlDataReader dr = cmd.ExecuteReader()) {
  629. // one record should be returned
  630. Assert.IsTrue(dr.Read(), "EnumParameterTest#1");
  631. // we should get two field in the result
  632. Assert.AreEqual(2, dr.FieldCount, "EnumParameterTest#2");
  633. // field 1
  634. Assert.AreEqual("int", dr.GetDataTypeName(0), "EnumParameterTest#3");
  635. Assert.AreEqual(5, dr.GetInt32(0), "EnumParameterTest#4");
  636. // field 2
  637. Assert.AreEqual("smallint", dr.GetDataTypeName(1), "EnumParameterTest#5");
  638. Assert.AreEqual((short) Status.Error, dr.GetInt16(1), "EnumParameterTest#6");
  639. // only one record should be returned
  640. Assert.IsFalse(dr.Read(), "EnumParameterTest#7");
  641. }
  642. } finally {
  643. DBHelper.ExecuteNonQuery (conn, "if exists (select name from sysobjects " +
  644. " where name like '#temp_Bug66630' and type like 'P') " +
  645. " drop procedure #temp_Bug66630; ");
  646. ConnectionManager.Singleton.CloseConnection ();
  647. }
  648. }
  649. /**
  650. * The below test does not need a connection but since the setup opens
  651. * the connection i will need to close it
  652. */
  653. [Test]
  654. public void CloneTest() {
  655. ConnectionManager.Singleton.OpenConnection ();
  656. SqlCommand cmd = new SqlCommand();
  657. cmd.Connection = null;
  658. cmd.CommandText = "sp_insert";
  659. cmd.CommandType = CommandType.StoredProcedure;
  660. Object TestPar = DBNull.Value;
  661. cmd.Parameters.Add("@TestPar1", SqlDbType.Int);
  662. cmd.Parameters["@TestPar1"].Value = TestPar;
  663. cmd.Parameters.Add("@BirthDate", DateTime.Now);
  664. cmd.DesignTimeVisible = true;
  665. cmd.CommandTimeout = 100;
  666. Object clone1 = ((ICloneable)(cmd)).Clone();
  667. SqlCommand cmd1 = (SqlCommand) clone1;
  668. Assert.AreEqual(2, cmd1.Parameters.Count);
  669. Assert.AreEqual(100, cmd1.CommandTimeout);
  670. cmd1.Parameters.Add("@test", DateTime.Now);
  671. // to check that it is deep copy and not a shallow copy of the
  672. // parameter collection
  673. Assert.AreEqual(3, cmd1.Parameters.Count);
  674. Assert.AreEqual(2, cmd.Parameters.Count);
  675. }
  676. private enum Status {
  677. OK = 0,
  678. Error = 3
  679. }
  680. private readonly string CREATE_TMP_SP_TEMP_INSERT_PERSON = ("create procedure #sp_temp_insert_employee ( " + Environment.NewLine +
  681. "@fname varchar (20)) " + Environment.NewLine +
  682. "as " + Environment.NewLine +
  683. "begin" + Environment.NewLine +
  684. "declare @id int;" + Environment.NewLine +
  685. "select @id = max (id) from employee;" + Environment.NewLine +
  686. "set @id = @id + 6000 + 1;" + Environment.NewLine +
  687. "insert into employee (id, fname, dob, doj) values (@id, @fname, '1980-02-11', getdate ());" + Environment.NewLine +
  688. "return @id;" + Environment.NewLine +
  689. "end");
  690. private readonly string DROP_TMP_SP_TEMP_INSERT_PERSON = ("if exists (select name from sysobjects where " + Environment.NewLine +
  691. "name = '#sp_temp_insert_employee' and type = 'P') " + Environment.NewLine +
  692. "drop procedure #sp_temp_insert_employee; ");
  693. }
  694. }