SqlCommandTest.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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. cmd.CommandText = "Delete from numeric_family where id=100";
  203. result = cmd.ExecuteNonQuery ();
  204. Assert.AreEqual (1, result, "#4 One row shud be deleted");
  205. }finally {
  206. trans.Rollback ();
  207. }
  208. }
  209. [Test]
  210. public void ExecuteReaderTest ()
  211. {
  212. SqlDataReader reader = null;
  213. conn = new SqlConnection (connectionString);
  214. // Test exception is thrown if conn is closed
  215. cmd = new SqlCommand ("Select count(*) from numeric_family");
  216. try {
  217. reader = cmd.ExecuteReader ();
  218. }catch (AssertionException e) {
  219. throw e;
  220. }catch (Exception e) {
  221. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  222. "#1 Incorrect Exception");
  223. }
  224. conn.Open ();
  225. // Test exception is thrown for Invalid Query
  226. cmd = new SqlCommand ("InvalidQuery", conn);
  227. try {
  228. reader = cmd.ExecuteReader ();
  229. Assert.Fail ("#1 Exception shud be thrown");
  230. }catch (AssertionException e) {
  231. throw e;
  232. }catch (Exception e) {
  233. Assert.AreEqual (typeof(SqlException), e.GetType (),
  234. "#2 Incorrect Exception : " + e);
  235. }
  236. // NOTE
  237. // Test SqlException is thrown if a row is locked
  238. // should lock a particular row and then modify it
  239. /*
  240. */
  241. // Test Connection cannot be modified when reader is in use
  242. // NOTE : msdotnet contradicts documented behavior
  243. cmd.CommandText = "select * from numeric_family where id=1";
  244. reader = cmd.ExecuteReader ();
  245. reader.Read ();
  246. conn.Close (); // valid operation
  247. conn = new SqlConnection (connectionString);
  248. /*
  249. // NOTE msdotnet contradcits documented behavior
  250. // If the above testcase fails, then this shud be tested
  251. // Test connection can be modified once reader is closed
  252. conn.Close ();
  253. reader.Close ();
  254. conn = new SqlConnection (connectionString); // valid operation
  255. */
  256. }
  257. [Test]
  258. public void ExecuteReaderCommandBehaviorTest ()
  259. {
  260. // Test for command behaviors
  261. DataTable schemaTable = null;
  262. SqlDataReader reader = null;
  263. conn = new SqlConnection (connectionString);
  264. conn.Open ();
  265. cmd = new SqlCommand ("", conn);
  266. cmd.CommandText = "Select id from numeric_family where id <=4 order by id asc;";
  267. cmd.CommandText += "Select type_bit from numeric_family where id <=4 order by id asc";
  268. // Test for default command behavior
  269. reader = cmd.ExecuteReader ();
  270. int rows = 0;
  271. int results = 0;
  272. do {
  273. while (reader.Read ())
  274. rows++ ;
  275. Assert.AreEqual (4, rows, "#1 Multiple rows shud be returned");
  276. results++;
  277. rows = 0;
  278. }while (reader.NextResult());
  279. Assert.AreEqual (2, results, "#2 Multiple result sets shud be returned");
  280. reader.Close ();
  281. // Test if closing reader, closes the connection
  282. reader = cmd.ExecuteReader (CommandBehavior.CloseConnection);
  283. reader.Close ();
  284. Assert.AreEqual (ConnectionState.Closed, conn.State,
  285. "#3 Command Behavior is not followed");
  286. conn.Open();
  287. // Test if row info and primary Key info is returned
  288. reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
  289. schemaTable = reader.GetSchemaTable ();
  290. Assert.IsTrue(reader.HasRows, "#4 Data Rows shud also be returned");
  291. Assert.IsTrue ((bool)schemaTable.Rows[0]["IsKey"],
  292. "#5 Primary Key info shud be returned");
  293. reader.Close ();
  294. // Test only column information is returned
  295. reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly);
  296. schemaTable = reader.GetSchemaTable ();
  297. Assert.IsFalse (reader.HasRows, "#6 row data shud not be returned");
  298. Assert.AreEqual(DBNull.Value, schemaTable.Rows[0]["IsKey"],
  299. "#7 Primary Key info shud not be returned");
  300. Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
  301. "#8 Schema Data is Incorrect");
  302. reader.Close ();
  303. // Test only one result set (first) is returned
  304. reader = cmd.ExecuteReader (CommandBehavior.SingleResult);
  305. schemaTable = reader.GetSchemaTable ();
  306. Assert.IsFalse (reader.NextResult(),
  307. "#9 Only one result set shud be returned");
  308. Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
  309. "#10 The result set returned shud be the first result set");
  310. reader.Close ();
  311. // Test only one row is returned for all result sets
  312. // msdotnet doesnt work correctly.. returns only one result set
  313. reader = cmd.ExecuteReader (CommandBehavior.SingleRow);
  314. rows=0;
  315. results=0;
  316. do {
  317. while (reader.Read ())
  318. rows++ ;
  319. Assert.AreEqual (1, rows, "#11 Only one row shud be returned");
  320. results++;
  321. rows = 0;
  322. }while (reader.NextResult());
  323. // NOTE msdotnet contradicts documented behavior.
  324. // Multiple result sets shud be returned , and in this case : 2
  325. //Assert.AreEqual (2, results, "# Multiple result sets shud be returned");
  326. Assert.AreEqual (2, results, "#12 Multiple result sets shud be returned");
  327. reader.Close ();
  328. }
  329. [Test]
  330. public void PrepareTest_CheckValidStatement ()
  331. {
  332. cmd = new SqlCommand ();
  333. conn = new SqlConnection (connectionString);
  334. conn.Open ();
  335. cmd.CommandText = "Select id from numeric_family where id=@ID" ;
  336. cmd.Connection = conn ;
  337. // Test if Parameters are correctly populated
  338. cmd.Parameters.Clear ();
  339. cmd.Parameters.Add ("@ID", SqlDbType.TinyInt);
  340. cmd.Parameters["@ID"].Value = 2 ;
  341. cmd.Prepare ();
  342. Assert.AreEqual (2, cmd.ExecuteScalar (), "#3 Prepared Stmt not working");
  343. cmd.Parameters[0].Value = 3;
  344. Assert.AreEqual (3, cmd.ExecuteScalar (), "#4 Prep Stmt not working");
  345. conn.Close ();
  346. }
  347. [Test]
  348. public void PrepareTest ()
  349. {
  350. cmd = new SqlCommand ();
  351. conn = new SqlConnection (connectionString);
  352. conn.Open ();
  353. cmd.CommandText = "Select id from numeric_family where id=@ID" ;
  354. cmd.Connection = conn ;
  355. // Test InvalidOperation Exception is thrown if Parameter Type
  356. // is not explicitly set
  357. cmd.Parameters.Add ("@ID", 2);
  358. try {
  359. cmd.Prepare ();
  360. Assert.Fail ("#1 Parameter Type shud be explicitly Set");
  361. }catch (AssertionException e) {
  362. throw e;
  363. }catch (Exception e) {
  364. Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
  365. "#2 Incorrect Exception : " + e.StackTrace);
  366. }
  367. // Test Exception is thrown for variable size data if precision/scale
  368. // is not set
  369. cmd.CommandText = "select type_varchar from string_family where type_varchar=@p1";
  370. cmd.Parameters.Clear ();
  371. cmd.Parameters.Add ("@p1", SqlDbType.VarChar);
  372. cmd.Parameters["@p1"].Value = "afasasadadada";
  373. try {
  374. cmd.Prepare ();
  375. Assert.Fail ("#5 Exception shud be thrown");
  376. }catch (AssertionException e) {
  377. throw e;
  378. }catch (Exception e) {
  379. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  380. "#6 Incorrect Exception " + e.StackTrace);
  381. }
  382. // Test Exception is not thrown for Stored Procs
  383. try {
  384. cmd.CommandType = CommandType.StoredProcedure;
  385. cmd.CommandText = "ABFSDSFSF" ;
  386. cmd.Prepare ();
  387. }catch (Exception e) {
  388. Assert.Fail ("#7 Exception shud not be thrown for Stored Procs");
  389. }
  390. cmd.CommandType = CommandType.Text;
  391. conn.Close ();
  392. //Test InvalidOperation Exception is thrown if connection is not set
  393. cmd.Connection = null;
  394. try {
  395. cmd.Prepare ();
  396. Assert.Fail ("#8 InvalidOperation Exception shud be thrown");
  397. }catch (AssertionException e) {
  398. throw e;
  399. }catch (Exception e) {
  400. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  401. "#9 Incorrect Exception : " + e.StackTrace);
  402. }
  403. //Test InvalidOperation Exception is thrown if connection is closed
  404. cmd.Connection = conn ;
  405. try{
  406. cmd.Prepare ();
  407. Assert.Fail ("#4 InvalidOperation Exception shud be thrown");
  408. }catch (AssertionException e) {
  409. throw e;
  410. }catch (Exception e) {
  411. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  412. "Incorrect Exception : " + e.StackTrace);
  413. }
  414. }
  415. [Test]
  416. public void ResetTimeOut ()
  417. {
  418. SqlCommand cmd = new SqlCommand ();
  419. cmd.CommandTimeout = 50 ;
  420. Assert.AreEqual ( cmd.CommandTimeout, 50,
  421. "#1 CommandTimeout should be modfiable");
  422. cmd.ResetCommandTimeout ();
  423. Assert.AreEqual (cmd.CommandTimeout, 30,
  424. "#2 Reset Should set the Timeout to default value");
  425. }
  426. [Test]
  427. [ExpectedException (typeof(ArgumentException))]
  428. public void CommandTimeout ()
  429. {
  430. cmd = new SqlCommand ();
  431. cmd.CommandTimeout = 10;
  432. Assert.AreEqual (10, cmd.CommandTimeout, "#1");
  433. cmd.CommandTimeout = -1;
  434. }
  435. [Test]
  436. [ExpectedException (typeof(ArgumentException))]
  437. public void CommandTypeTest ()
  438. {
  439. cmd = new SqlCommand ();
  440. Assert.AreEqual (CommandType.Text ,cmd.CommandType,
  441. "Default CommandType is text");
  442. cmd.CommandType = (CommandType)(-1);
  443. }
  444. [Test]
  445. [Ignore ("msdotnet contradicts documented behavior")]
  446. [ExpectedException (typeof(InvalidOperationException))]
  447. public void ConnectionTest ()
  448. {
  449. SqlTransaction trans = null;
  450. try {
  451. conn = new SqlConnection (connectionString);
  452. conn.Open ();
  453. trans = conn.BeginTransaction ();
  454. cmd = new SqlCommand ("", conn,trans);
  455. cmd.CommandText = "Select id from numeric_family where id=1";
  456. cmd.Connection = new SqlConnection ();
  457. }finally {
  458. trans.Rollback();
  459. conn.Close ();
  460. }
  461. }
  462. [Test]
  463. public void TransactionTest ()
  464. {
  465. conn = new SqlConnection (connectionString);
  466. cmd = new SqlCommand ("", conn);
  467. Assert.IsNull (cmd.Transaction, "#1 Default value is null");
  468. SqlConnection conn1 = new SqlConnection (connectionString);
  469. conn1.Open ();
  470. SqlTransaction trans1 = conn1.BeginTransaction ();
  471. cmd.Transaction = trans1 ;
  472. try {
  473. cmd.ExecuteNonQuery ();
  474. Assert.Fail ("#2 Connection cannot be different");
  475. }catch (Exception e) {
  476. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  477. "#3 Incorrect Exception : " + e);
  478. }finally {
  479. conn1.Close ();
  480. conn.Close ();
  481. }
  482. }
  483. // Need to add more tests
  484. [Test]
  485. [ExpectedException (typeof(ArgumentException))]
  486. public void UpdatedRowSourceTest ()
  487. {
  488. cmd = new SqlCommand ();
  489. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource,
  490. "#1 Default value is both");
  491. cmd.UpdatedRowSource = UpdateRowSource.None;
  492. Assert.AreEqual (UpdateRowSource.None, cmd.UpdatedRowSource,
  493. "#2");
  494. cmd.UpdatedRowSource = (UpdateRowSource) (-1);
  495. }
  496. [Test]
  497. public void ExecuteNonQueryTempProcedureTest () {
  498. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  499. try {
  500. ConnectionManager.Singleton.OpenConnection ();
  501. // create temp sp here, should normally be created in Setup of test
  502. // case, but cannot be done right now because of ug #68978
  503. DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
  504. SqlCommand cmd = new SqlCommand();
  505. cmd.Connection = conn;
  506. cmd.CommandText = "#sp_temp_insert_employee";
  507. cmd.CommandType = CommandType.StoredProcedure;
  508. Object TestPar = "test";
  509. cmd.Parameters.Add("@fname", SqlDbType.VarChar);
  510. cmd.Parameters ["@fname"].Value = TestPar;
  511. Assert.AreEqual(1,cmd.ExecuteNonQuery());
  512. } finally {
  513. DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
  514. DBHelper.ExecuteSimpleSP (conn, "sp_clean_person_table");
  515. ConnectionManager.Singleton.CloseConnection ();
  516. }
  517. }
  518. /**
  519. * Verifies whether an enum value is converted to a numeric value when
  520. * used as value for a numeric parameter (bug #66630)
  521. */
  522. [Test]
  523. public void EnumParameterTest() {
  524. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  525. try {
  526. ConnectionManager.Singleton.OpenConnection ();
  527. // create temp sp here, should normally be created in Setup of test
  528. // case, but cannot be done right now because of ug #68978
  529. DBHelper.ExecuteNonQuery (conn, "CREATE PROCEDURE #Bug66630 ("
  530. + "@Status smallint = 7"
  531. + ")"
  532. + "AS" + Environment.NewLine
  533. + "BEGIN" + Environment.NewLine
  534. + "SELECT CAST(5 AS int), @Status" + Environment.NewLine
  535. + "END");
  536. SqlCommand cmd = new SqlCommand("#Bug66630", conn);
  537. cmd.CommandType = CommandType.StoredProcedure;
  538. cmd.Parameters.Add("@Status", SqlDbType.Int).Value = Status.Error;
  539. using (SqlDataReader dr = cmd.ExecuteReader()) {
  540. // one record should be returned
  541. Assert.IsTrue(dr.Read(), "EnumParameterTest#1");
  542. // we should get two field in the result
  543. Assert.AreEqual(2, dr.FieldCount, "EnumParameterTest#2");
  544. // field 1
  545. Assert.AreEqual("int", dr.GetDataTypeName(0), "EnumParameterTest#3");
  546. Assert.AreEqual(5, dr.GetInt32(0), "EnumParameterTest#4");
  547. // field 2
  548. Assert.AreEqual("smallint", dr.GetDataTypeName(1), "EnumParameterTest#5");
  549. Assert.AreEqual((short) Status.Error, dr.GetInt16(1), "EnumParameterTest#6");
  550. // only one record should be returned
  551. Assert.IsFalse(dr.Read(), "EnumParameterTest#7");
  552. }
  553. } finally {
  554. DBHelper.ExecuteNonQuery (conn, "if exists (select name from sysobjects " +
  555. " where name like '#temp_Bug66630' and type like 'P') " +
  556. " drop procedure #temp_Bug66630; ");
  557. ConnectionManager.Singleton.CloseConnection ();
  558. }
  559. }
  560. /**
  561. * The below test does not need a connection but since the setup opens
  562. * the connection i will need to close it
  563. */
  564. [Test]
  565. public void CloneTest() {
  566. ConnectionManager.Singleton.OpenConnection ();
  567. SqlCommand cmd = new SqlCommand();
  568. cmd.Connection = null;
  569. cmd.CommandText = "sp_insert";
  570. cmd.CommandType = CommandType.StoredProcedure;
  571. Object TestPar = DBNull.Value;
  572. cmd.Parameters.Add("@TestPar1", SqlDbType.Int);
  573. cmd.Parameters["@TestPar1"].Value = TestPar;
  574. cmd.Parameters.Add("@BirthDate", DateTime.Now);
  575. cmd.DesignTimeVisible = true;
  576. cmd.CommandTimeout = 100;
  577. Object clone1 = ((ICloneable)(cmd)).Clone();
  578. SqlCommand cmd1 = (SqlCommand) clone1;
  579. Assert.AreEqual(2, cmd1.Parameters.Count);
  580. Assert.AreEqual(100, cmd1.CommandTimeout);
  581. cmd1.Parameters.Add("@test", DateTime.Now);
  582. // to check that it is deep copy and not a shallow copy of the
  583. // parameter collection
  584. Assert.AreEqual(3, cmd1.Parameters.Count);
  585. Assert.AreEqual(2, cmd.Parameters.Count);
  586. }
  587. private enum Status {
  588. OK = 0,
  589. Error = 3
  590. }
  591. private readonly string CREATE_TMP_SP_TEMP_INSERT_PERSON = ("create procedure #sp_temp_insert_employee ( " + Environment.NewLine +
  592. "@fname varchar (20)) " + Environment.NewLine +
  593. "as " + Environment.NewLine +
  594. "begin" + Environment.NewLine +
  595. "declare @id int;" + Environment.NewLine +
  596. "select @id = max (id) from employee;" + Environment.NewLine +
  597. "set @id = @id + 6000 + 1;" + Environment.NewLine +
  598. "insert into employee (id, fname, dob, doj) values (@id, @fname, '1980-02-11', getdate ());" + Environment.NewLine +
  599. "return @id;" + Environment.NewLine +
  600. "end");
  601. private readonly string DROP_TMP_SP_TEMP_INSERT_PERSON = ("if exists (select name from sysobjects where " + Environment.NewLine +
  602. "name = '#sp_temp_insert_employee' and type = 'P') " + Environment.NewLine +
  603. "drop procedure #sp_temp_insert_employee; ");
  604. }
  605. }